From cfac6410f1d2740ab2c47f93592785b3105f88eb Mon Sep 17 00:00:00 2001 From: zYne Date: Fri, 20 Oct 2006 21:46:09 +0000 Subject: [PATCH] Added new datadict drivers + datadict exception classes --- lib/Doctrine/DataDict/Exception.php | 29 +++ lib/Doctrine/DataDict/Mssql/Exception.php | 29 +++ lib/Doctrine/DataDict/Mysql/Exception.php | 29 +++ lib/Doctrine/DataDict/Oracle.php | 138 +++++++++++++++ lib/Doctrine/DataDict/Oracle/Exception.php | 29 +++ lib/Doctrine/DataDict/Pgsql/Exception.php | 29 +++ lib/Doctrine/DataDict/Sqlite.php | 196 +++++++++++++++++++++ lib/Doctrine/DataDict/Sqlite/Sqlite.php | 29 +++ 8 files changed, 508 insertions(+) create mode 100644 lib/Doctrine/DataDict/Exception.php create mode 100644 lib/Doctrine/DataDict/Mssql/Exception.php create mode 100644 lib/Doctrine/DataDict/Mysql/Exception.php create mode 100644 lib/Doctrine/DataDict/Oracle/Exception.php create mode 100644 lib/Doctrine/DataDict/Pgsql/Exception.php create mode 100644 lib/Doctrine/DataDict/Sqlite/Sqlite.php diff --git a/lib/Doctrine/DataDict/Exception.php b/lib/Doctrine/DataDict/Exception.php new file mode 100644 index 000000000..c45faf6b0 --- /dev/null +++ b/lib/Doctrine/DataDict/Exception.php @@ -0,0 +1,29 @@ +. + */ +Doctrine::autoload('Doctrine_Exception'); +/** + * Doctrine_DataDict_Exception + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ +class Doctrine_DataDict_Exception extends Doctrine_Exception { } diff --git a/lib/Doctrine/DataDict/Mssql/Exception.php b/lib/Doctrine/DataDict/Mssql/Exception.php new file mode 100644 index 000000000..29d263234 --- /dev/null +++ b/lib/Doctrine/DataDict/Mssql/Exception.php @@ -0,0 +1,29 @@ +. + */ +Doctrine::autoload('Doctrine_DataDict_Exception'); +/** + * Doctrine_DataDict_Mssql_Exception + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ +class Doctrine_DataDict_Mssql_Exception extends Doctrine_Exception { } diff --git a/lib/Doctrine/DataDict/Mysql/Exception.php b/lib/Doctrine/DataDict/Mysql/Exception.php new file mode 100644 index 000000000..8d6d7223c --- /dev/null +++ b/lib/Doctrine/DataDict/Mysql/Exception.php @@ -0,0 +1,29 @@ +. + */ +Doctrine::autoload('Doctrine_DataDict_Exception'); +/** + * Doctrine_DataDict_Mysql_Exception + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ +class Doctrine_DataDict_Mysql_Exception extends Doctrine_DataDict_Exception { } diff --git a/lib/Doctrine/DataDict/Oracle.php b/lib/Doctrine/DataDict/Oracle.php index 02f011657..1674efa9c 100644 --- a/lib/Doctrine/DataDict/Oracle.php +++ b/lib/Doctrine/DataDict/Oracle.php @@ -28,6 +28,144 @@ */ class Doctrine_DataDict_Mssql extends Doctrine_DataDict { + /** + * Obtain DBMS specific SQL code portion needed to declare an text type + * field to be used in statements like CREATE TABLE. + * + * @param array $field associative array with the name of the properties + * of the field being declared as array indexes. Currently, the types + * of supported field properties are as follows: + * + * length + * Integer value that determines the maximum length of the text + * field. If this argument is missing the field should be + * declared to have the longest length allowed by the DBMS. + * + * default + * Text value to be used as default for this field. + * + * notnull + * Boolean flag that indicates whether this field is constrained + * to not be set to null. + * @return string DBMS specific SQL code portion that should be used to + * declare the specified field. + */ + public function getTypeDeclaration(array $field) { + switch ($field['type']) { + case 'string': + case 'array': + case 'object': + case 'gzip': + $length = !empty($field['length']) + ? $field['length'] : $db->options['default_text_field_length']; + $fixed = !empty($field['fixed']) ? $field['fixed'] : false; + return $fixed ? 'CHAR('.$length.')' : 'VARCHAR2('.$length.')'; + case 'clob': + return 'CLOB'; + case 'blob': + return 'BLOB'; + case 'integer': + if (!empty($field['length'])) { + return 'NUMBER('.$field['length'].')'; + } + return 'INT'; + case 'boolean': + return 'NUMBER(1)'; + case 'date': + case 'time': + case 'timestamp': + return 'DATE'; + case 'float': + return 'NUMBER'; + case 'decimal': + return 'NUMBER(*,'.$db->options['decimal_places'].')'; + } + } + /** + * Maps a native array description of a field to a MDB2 datatype and length + * + * @param array $field native field description + * @return array containing the various possible types, length, sign, fixed + * @throws Doctrine_DataDict_Oracle_Exception + */ + function mapNativeDatatype($field) { + $db_type = strtolower($field['type']); + $type = array(); + $length = $unsigned = $fixed = null; + if (!empty($field['length'])) { + $length = $field['length']; + } + switch ($db_type) { + case 'integer': + case 'pls_integer': + case 'binary_integer': + $type[] = 'integer'; + if ($length == '1') { + $type[] = 'boolean'; + if (preg_match('/^(is|has)/', $field['name'])) { + $type = array_reverse($type); + } + } + break; + case 'varchar': + case 'varchar2': + case 'nvarchar2': + $fixed = false; + case 'char': + case 'nchar': + $type[] = 'text'; + if ($length == '1') { + $type[] = 'boolean'; + if (preg_match('/^(is|has)/', $field['name'])) { + $type = array_reverse($type); + } + } + if ($fixed !== false) { + $fixed = true; + } + break; + case 'date': + case 'timestamp': + $type[] = 'timestamp'; + $length = null; + break; + case 'float': + $type[] = 'float'; + break; + case 'number': + if (!empty($field['scale'])) { + $type[] = 'decimal'; + } else { + $type[] = 'integer'; + if ($length == '1') { + $type[] = 'boolean'; + if (preg_match('/^(is|has)/', $field['name'])) { + $type = array_reverse($type); + } + } + } + break; + case 'long': + $type[] = 'text'; + case 'clob': + case 'nclob': + $type[] = 'clob'; + break; + case 'blob': + case 'raw': + case 'long raw': + case 'bfile': + $type[] = 'blob'; + $length = null; + break; + case 'rowid': + case 'urowid': + default: + throw new Doctrine_DataDict_Oracle_Exception('unknown database attribute type: '.$db_type); + } + + return array($type, $length, $unsigned, $fixed); + } /** * lists all databases * diff --git a/lib/Doctrine/DataDict/Oracle/Exception.php b/lib/Doctrine/DataDict/Oracle/Exception.php new file mode 100644 index 000000000..a37b9fee6 --- /dev/null +++ b/lib/Doctrine/DataDict/Oracle/Exception.php @@ -0,0 +1,29 @@ +. + */ +Doctrine::autoload('Doctrine_DataDict_Exception'); +/** + * Doctrine_DataDict_Oracle_Exception + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ +class Doctrine_DataDict_Oracle_Exception extends Doctrine_DataDict_Exception { } diff --git a/lib/Doctrine/DataDict/Pgsql/Exception.php b/lib/Doctrine/DataDict/Pgsql/Exception.php new file mode 100644 index 000000000..5ffd979e6 --- /dev/null +++ b/lib/Doctrine/DataDict/Pgsql/Exception.php @@ -0,0 +1,29 @@ +. + */ +Doctrine::autoload('Doctrine_DataDict_Exception'); +/** + * Doctrine_DataDict_Pgsql_Exception + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ +class Doctrine_DataDict_Pgsql_Exception extends Doctrine_DataDict_Exception { } diff --git a/lib/Doctrine/DataDict/Sqlite.php b/lib/Doctrine/DataDict/Sqlite.php index 898d6487f..3faf58751 100644 --- a/lib/Doctrine/DataDict/Sqlite.php +++ b/lib/Doctrine/DataDict/Sqlite.php @@ -24,10 +24,206 @@ * @url http://www.phpdoctrine.com * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @author Konsta Vesterinen + * @author Lukas Smith (PEAR MDB2 library) * @version $Id$ */ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict { + /** + * Obtain DBMS specific SQL code portion needed to declare an text type + * field to be used in statements like CREATE TABLE. + * + * @param array $field associative array with the name of the properties + * of the field being declared as array indexes. Currently, the types + * of supported field properties are as follows: + * + * length + * Integer value that determines the maximum length of the text + * field. If this argument is missing the field should be + * declared to have the longest length allowed by the DBMS. + * + * default + * Text value to be used as default for this field. + * + * notnull + * Boolean flag that indicates whether this field is constrained + * to not be set to null. + * @return string DBMS specific SQL code portion that should be used to + * declare the specified field. + * @access public + */ + function getTypeDeclaration($field) { + switch ($field['type']) { + case 'text': + $length = !empty($field['length']) + ? $field['length'] : false; + $fixed = !empty($field['fixed']) ? $field['fixed'] : false; + return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$db->options['default_text_field_length'].')') + : ($length ? 'VARCHAR('.$length.')' : 'TEXT'); + case 'clob': + if (!empty($field['length'])) { + $length = $field['length']; + if ($length <= 255) { + return 'TINYTEXT'; + } elseif ($length <= 65535) { + return 'TEXT'; + } elseif ($length <= 16777215) { + return 'MEDIUMTEXT'; + } + } + return 'LONGTEXT'; + case 'blob': + if (!empty($field['length'])) { + $length = $field['length']; + if ($length <= 255) { + return 'TINYBLOB'; + } elseif ($length <= 65535) { + return 'BLOB'; + } elseif ($length <= 16777215) { + return 'MEDIUMBLOB'; + } + } + return 'LONGBLOB'; + case 'integer': + if (!empty($field['length'])) { + $length = $field['length']; + if ($length <= 2) { + return 'SMALLINT'; + } elseif ($length == 3 || $length == 4) { + return 'INTEGER'; + } elseif ($length > 4) { + return 'BIGINT'; + } + } + return 'INTEGER'; + case 'boolean': + return 'BOOLEAN'; + case 'date': + return 'DATE'; + case 'time': + return 'TIME'; + case 'timestamp': + return 'DATETIME'; + case 'float': + return 'DOUBLE'.($db->options['fixed_float'] ? '('. + ($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' : ''); + case 'decimal': + $length = !empty($field['length']) ? $field['length'] : 18; + return 'DECIMAL('.$length.','.$db->options['decimal_places'].')'; + } + return ''; + } + /** + * Maps a native array description of a field to a MDB2 datatype and length + * + * @param array $field native field description + * @return array containing the various possible types, length, sign, fixed + * @access public + */ + function mapNativeDatatype($field) { + $db_type = strtolower($field['type']); + $length = !empty($field['length']) ? $field['length'] : null; + $unsigned = !empty($field['unsigned']) ? $field['unsigned'] : null; + $fixed = null; + $type = array(); + switch ($db_type) { + case 'boolean': + $type[] = 'boolean'; + break; + case 'tinyint': + $type[] = 'integer'; + $type[] = 'boolean'; + if (preg_match('/^(is|has)/', $field['name'])) { + $type = array_reverse($type); + } + $unsigned = preg_match('/ unsigned/i', $field['type']); + $length = 1; + break; + case 'smallint': + $type[] = 'integer'; + $unsigned = preg_match('/ unsigned/i', $field['type']); + $length = 2; + break; + case 'mediumint': + $type[] = 'integer'; + $unsigned = preg_match('/ unsigned/i', $field['type']); + $length = 3; + break; + case 'int': + case 'integer': + case 'serial': + $type[] = 'integer'; + $unsigned = preg_match('/ unsigned/i', $field['type']); + $length = 4; + break; + case 'bigint': + case 'bigserial': + $type[] = 'integer'; + $unsigned = preg_match('/ unsigned/i', $field['type']); + $length = 8; + break; + case 'clob': + case 'tinytext': + case 'mediumtext': + case 'longtext': + case 'text': + case 'varchar': + case 'varchar2': + $fixed = false; + case 'char': + $type[] = 'text'; + if ($length == '1') { + $type[] = 'boolean'; + if (preg_match('/^(is|has)/', $field['name'])) { + $type = array_reverse($type); + } + } elseif (strstr($db_type, 'text')) { + $type[] = 'clob'; + } + if ($fixed !== false) { + $fixed = true; + } + break; + case 'date': + $type[] = 'date'; + $length = null; + break; + case 'datetime': + case 'timestamp': + $type[] = 'timestamp'; + $length = null; + break; + case 'time': + $type[] = 'time'; + $length = null; + break; + case 'float': + case 'double': + case 'real': + $type[] = 'float'; + break; + case 'decimal': + case 'numeric': + $type[] = 'decimal'; + break; + case 'tinyblob': + case 'mediumblob': + case 'longblob': + case 'blob': + $type[] = 'blob'; + $length = null; + break; + case 'year': + $type[] = 'integer'; + $type[] = 'date'; + $length = null; + break; + default: + throw new Doctrine_DataDict_Sqlite_Exception('unknown database attribute type: '.$db_type); + } + + return array($type, $length, $unsigned, $fixed); + } /** * lists all databases * diff --git a/lib/Doctrine/DataDict/Sqlite/Sqlite.php b/lib/Doctrine/DataDict/Sqlite/Sqlite.php new file mode 100644 index 000000000..e04baae1d --- /dev/null +++ b/lib/Doctrine/DataDict/Sqlite/Sqlite.php @@ -0,0 +1,29 @@ +. + */ +Doctrine::autoload('Doctrine_DataDict_Exception'); +/** + * Doctrine_DataDict_Sqlite_Exception + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ +class Doctrine_DataDict_Sqlite_Exception extends Doctrine_DataDict_Exception { }