From fa5ab7d2b1837c6c2e0f6256fd1e81fcf09608ff Mon Sep 17 00:00:00 2001 From: lsmith Date: Thu, 10 Jan 2008 15:49:25 +0000 Subject: [PATCH] - moved getIntegerDeclaration to export since we already have getDeclaration in there --- lib/Doctrine/DataDict/Mysql.php | 51 ---------------------- lib/Doctrine/DataDict/Pgsql.php | 54 ----------------------- lib/Doctrine/DataDict/Sqlite.php | 58 +------------------------ lib/Doctrine/Export.php | 5 +-- lib/Doctrine/Export/Mysql.php | 51 ++++++++++++++++++++++ lib/Doctrine/Export/Pgsql.php | 69 ++++++++++++++++++++++++----- lib/Doctrine/Export/Sqlite.php | 74 +++++++++++++++++++++++++++----- 7 files changed, 177 insertions(+), 185 deletions(-) diff --git a/lib/Doctrine/DataDict/Mysql.php b/lib/Doctrine/DataDict/Mysql.php index 845de3942..1c655f3db 100644 --- a/lib/Doctrine/DataDict/Mysql.php +++ b/lib/Doctrine/DataDict/Mysql.php @@ -413,55 +413,4 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict { return 'COLLATE ' . $collation; } - - /** - * Obtain DBMS specific SQL code portion needed to declare an integer type - * field to be used in statements like CREATE TABLE. - * - * @param string $name name the field to be declared. - * @param string $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: - * - * unsigned - * Boolean flag that indicates whether the field - * should be declared as unsigned integer if - * possible. - * - * default - * Integer 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 getIntegerDeclaration($name, $field) - { - $default = $autoinc = ''; - if ( ! empty($field['autoincrement'])) { - $autoinc = ' AUTO_INCREMENT'; - } elseif (array_key_exists('default', $field)) { - if ($field['default'] === '') { - $field['default'] = empty($field['notnull']) ? null : 0; - } - if (is_null($field['default'])) { - $default = ' DEFAULT NULL'; - } else { - $default = ' DEFAULT '.$this->conn->quote($field['default']); - } - } elseif (empty($field['notnull'])) { - $default = ' DEFAULT NULL'; - } - - $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; - $unsigned = (isset($field['unsigned']) && $field['unsigned']) ? ' UNSIGNED' : ''; - - $name = $this->conn->quoteIdentifier($name, true); - - return $name . ' ' . $this->getNativeDeclaration($field) . $unsigned . $default . $notnull . $autoinc; - } } diff --git a/lib/Doctrine/DataDict/Pgsql.php b/lib/Doctrine/DataDict/Pgsql.php index 35860d226..5113926e0 100644 --- a/lib/Doctrine/DataDict/Pgsql.php +++ b/lib/Doctrine/DataDict/Pgsql.php @@ -559,60 +559,6 @@ class Doctrine_DataDict_Pgsql extends Doctrine_DataDict 'fixed' => $fixed); } - /** - * Obtain DBMS specific SQL code portion needed to declare an integer type - * field to be used in statements like CREATE TABLE. - * - * @param string $name name the field to be declared. - * @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: - * - * unsigned - * Boolean flag that indicates whether the field should be - * declared as unsigned integer if possible. - * - * default - * Integer 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 getIntegerDeclaration($name, $field) - { - /** - if ( ! empty($field['unsigned'])) { - $this->conn->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer"; - } - */ - - if ( ! empty($field['autoincrement'])) { - $name = $this->conn->quoteIdentifier($name, true); - return $name . ' ' . $this->getNativeDeclaration($field); - } - - $default = ''; - if (array_key_exists('default', $field)) { - if ($field['default'] === '') { - $field['default'] = empty($field['notnull']) ? null : 0; - } - $default = ' DEFAULT '.$this->conn->quote($field['default'], $field['type']); - } - /** - TODO: is this needed ? - elseif (empty($field['notnull'])) { - $default = ' DEFAULT NULL'; - } - */ - - $notnull = empty($field['notnull']) ? '' : ' NOT NULL'; - $name = $this->conn->quoteIdentifier($name, true); - return $name . ' ' . $this->getNativeDeclaration($field) . $default . $notnull; - } - /** * parseBoolean * parses a literal boolean value and returns diff --git a/lib/Doctrine/DataDict/Sqlite.php b/lib/Doctrine/DataDict/Sqlite.php index cbca8f614..47702eeff 100644 --- a/lib/Doctrine/DataDict/Sqlite.php +++ b/lib/Doctrine/DataDict/Sqlite.php @@ -241,60 +241,4 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict 'unsigned' => $unsigned, 'fixed' => $fixed); } - - /** - * Obtain DBMS specific SQL code portion needed to declare an integer type - * field to be used in statements like CREATE TABLE. - * - * @param string $name name the field to be declared. - * @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: - * - * unsigned - * Boolean flag that indicates whether the field - * should be declared as unsigned integer if - * possible. - * - * default - * Integer 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 protected - */ - public function getIntegerDeclaration($name, array $field) - { - $default = $autoinc = ''; - $type = $this->getNativeDeclaration($field); - - $autoincrement = isset($field['autoincrement']) && $field['autoincrement']; - - if ($autoincrement) { - $autoinc = ' PRIMARY KEY AUTOINCREMENT'; - $type = 'INTEGER'; - } elseif (array_key_exists('default', $field)) { - if ($field['default'] === '') { - $field['default'] = empty($field['notnull']) ? null : 0; - } - $default = ' DEFAULT ' . $this->conn->quote($field['default'], $field['type']); - }/** - elseif (empty($field['notnull'])) { - $default = ' DEFAULT NULL'; - } - */ - - $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; - - // sqlite does not support unsigned attribute for autoinremented fields - $unsigned = (isset($field['unsigned']) && $field['unsigned'] && !$autoincrement) ? ' UNSIGNED' : ''; - - $name = $this->conn->quoteIdentifier($name, true); - return $name . ' ' . $type . $unsigned . $default . $notnull . $autoinc; - } -} \ No newline at end of file +} diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index 7c6f0efad..772c415ed 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -693,11 +693,10 @@ class Doctrine_Export extends Doctrine_Connection_Module */ public function getDeclaration($name, array $field) { - $method = 'get' . $field['type'] . 'Declaration'; - if (method_exists($this->conn->dataDict, $method)) { - return $this->conn->dataDict->$method($name, $field); + if (method_exists($this, $method)) { + return $this->$method($name, $field); } $default = $this->getDefaultFieldDeclaration($field); diff --git a/lib/Doctrine/Export/Mysql.php b/lib/Doctrine/Export/Mysql.php index 3c17590ef..8bae2852c 100644 --- a/lib/Doctrine/Export/Mysql.php +++ b/lib/Doctrine/Export/Mysql.php @@ -502,6 +502,57 @@ class Doctrine_Export_Mysql extends Doctrine_Export return $query; } + /** + * Obtain DBMS specific SQL code portion needed to declare an integer type + * field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @param string $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: + * + * unsigned + * Boolean flag that indicates whether the field + * should be declared as unsigned integer if + * possible. + * + * default + * Integer 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 getIntegerDeclaration($name, $field) + { + $default = $autoinc = ''; + if ( ! empty($field['autoincrement'])) { + $autoinc = ' AUTO_INCREMENT'; + } elseif (array_key_exists('default', $field)) { + if ($field['default'] === '') { + $field['default'] = empty($field['notnull']) ? null : 0; + } + if (is_null($field['default'])) { + $default = ' DEFAULT NULL'; + } else { + $default = ' DEFAULT '.$this->conn->quote($field['default']); + } + } elseif (empty($field['notnull'])) { + $default = ' DEFAULT NULL'; + } + + $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; + $unsigned = (isset($field['unsigned']) && $field['unsigned']) ? ' UNSIGNED' : ''; + + $name = $this->conn->quoteIdentifier($name, true); + + return $name . ' ' . $this->conn->dataDict->getNativeDeclaration($field) . $unsigned . $default . $notnull . $autoinc; + } + /** * getDefaultDeclaration * Obtain DBMS specific SQL code portion needed to set a default value diff --git a/lib/Doctrine/Export/Pgsql.php b/lib/Doctrine/Export/Pgsql.php index fef0f339b..11fe7be49 100644 --- a/lib/Doctrine/Export/Pgsql.php +++ b/lib/Doctrine/Export/Pgsql.php @@ -43,7 +43,7 @@ class Doctrine_Export_Pgsql extends Doctrine_Export public function createDatabaseSql($name) { $query = 'CREATE DATABASE ' . $this->conn->quoteIdentifier($name); - + return $query; } @@ -57,7 +57,7 @@ class Doctrine_Export_Pgsql extends Doctrine_Export public function dropDatabaseSql($name) { $query = 'DROP DATABASE ' . $this->conn->quoteIdentifier($name); - + return $query; } @@ -124,7 +124,7 @@ class Doctrine_Export_Pgsql extends Doctrine_Export if ($check) { return true; } - + $sql = array(); if (isset($changes['add']) && is_array($changes['add'])) { @@ -177,10 +177,10 @@ class Doctrine_Export_Pgsql extends Doctrine_Export $changeName = $this->conn->quoteIdentifier($changes['name'], true); $sql[] = 'ALTER TABLE ' . $name . ' RENAME TO ' . $changeName; } - + return $sql; } - + /** * alter an existing table * @@ -276,7 +276,7 @@ class Doctrine_Export_Pgsql extends Doctrine_Export foreach ($sql as $query) { $this->conn->exec($query); } - return true; + return true; } /** @@ -324,7 +324,7 @@ class Doctrine_Export_Pgsql extends Doctrine_Export if ( ! $name) { throw new Doctrine_Export_Exception('no valid table name specified'); } - + if (empty($fields)) { throw new Doctrine_Export_Exception('no fields specified for table ' . $name); } @@ -347,7 +347,7 @@ class Doctrine_Export_Pgsql extends Doctrine_Export $sql[] = $this->createIndexSql($name, $index, $definition); } } - + if (isset($options['foreignKeys'])) { foreach ((array) $options['foreignKeys'] as $k => $definition) { @@ -356,8 +356,57 @@ class Doctrine_Export_Pgsql extends Doctrine_Export } } } - + return $sql; } -} \ No newline at end of file + /** + * Obtain DBMS specific SQL code portion needed to declare an integer type + * field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @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: + * + * unsigned + * Boolean flag that indicates whether the field should be + * declared as unsigned integer if possible. + * + * default + * Integer 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 getIntegerDeclaration($name, $field) + { + /** + if ( ! empty($field['unsigned'])) { + $this->conn->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer"; + } + */ + + if ( ! empty($field['autoincrement'])) { + $name = $this->conn->quoteIdentifier($name, true); + return $name . ' ' . $this->conn->dataDict->getNativeDeclaration($field); + } + + $default = ''; + if (array_key_exists('default', $field)) { + if ($field['default'] === '') { + $field['default'] = empty($field['notnull']) ? null : 0; + } + $default = ' DEFAULT '.$this->conn->quote($field['default'], $field['type']); + } elseif (empty($field['notnull'])) { + $default = ' DEFAULT NULL'; + } + + $notnull = empty($field['notnull']) ? '' : ' NOT NULL'; + $name = $this->conn->quoteIdentifier($name, true); + return $name . ' ' . $this->conn->dataDict->getNativeDeclaration($field) . $default . $notnull; + } +} diff --git a/lib/Doctrine/Export/Sqlite.php b/lib/Doctrine/Export/Sqlite.php index b7a246730..624c421d6 100644 --- a/lib/Doctrine/Export/Sqlite.php +++ b/lib/Doctrine/Export/Sqlite.php @@ -100,7 +100,7 @@ class Doctrine_Export_Sqlite extends Doctrine_Export * Obtain DBMS specific SQL code portion needed to set an index * declaration to be used in statements like CREATE TABLE. * - * @return string + * @return string */ public function getIndexFieldDeclarationList(array $fields) { @@ -162,15 +162,15 @@ class Doctrine_Export_Sqlite extends Doctrine_Export if ( ! $name) { throw new Doctrine_Export_Exception('no valid table name specified'); } - + if (empty($fields)) { throw new Doctrine_Export_Exception('no fields specified for table '.$name); } $queryFields = $this->getFieldDeclarationList($fields); - + $autoinc = false; foreach($fields as $field) { - if (isset($field['autoincrement']) && $field['autoincrement'] || + if (isset($field['autoincrement']) && $field['autoincrement'] || (isset($field['autoinc']) && $field['autoinc'])) { $autoinc = true; break; @@ -204,8 +204,8 @@ class Doctrine_Export_Sqlite extends Doctrine_Export } } return $query; - - + + /** try { @@ -309,7 +309,7 @@ class Doctrine_Export_Sqlite extends Doctrine_Export $this->conn->exec('INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (' . ($start-1) . ')'); return true; } catch(Doctrine_Connection_Exception $e) { - // Handle error + // Handle error try { $result = $db->exec('DROP TABLE ' . $sequenceName); @@ -332,7 +332,7 @@ class Doctrine_Export_Sqlite extends Doctrine_Export return 'DROP TABLE ' . $sequenceName; } - + public function alterTableSql($name, array $changes, $check = false) { if ( ! $name) { @@ -388,7 +388,7 @@ class Doctrine_Export_Sqlite extends Doctrine_Export $oldFieldName = $fieldName; } $oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true); - $query .= 'CHANGE ' . $oldFieldName . ' ' + $query .= 'CHANGE ' . $oldFieldName . ' ' . $this->getDeclaration($fieldName, $field['definition']); } } @@ -410,7 +410,61 @@ class Doctrine_Export_Sqlite extends Doctrine_Export } $name = $this->conn->quoteIdentifier($name, true); - + return 'ALTER TABLE ' . $name . ' ' . $query; } + + /** + * Obtain DBMS specific SQL code portion needed to declare an integer type + * field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @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: + * + * unsigned + * Boolean flag that indicates whether the field + * should be declared as unsigned integer if + * possible. + * + * default + * Integer 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 protected + */ + public function getIntegerDeclaration($name, array $field) + { + $default = $autoinc = ''; + $type = $this->conn->dataDict->getNativeDeclaration($field); + + $autoincrement = isset($field['autoincrement']) && $field['autoincrement']; + + if ($autoincrement) { + $autoinc = ' PRIMARY KEY AUTOINCREMENT'; + $type = 'INTEGER'; + } elseif (array_key_exists('default', $field)) { + if ($field['default'] === '') { + $field['default'] = empty($field['notnull']) ? null : 0; + } + $default = ' DEFAULT ' . $this->conn->quote($field['default'], $field['type']); + } elseif (empty($field['notnull'])) { + $default = ' DEFAULT NULL'; + } + + $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; + + // sqlite does not support unsigned attribute for autoinremented fields + $unsigned = (isset($field['unsigned']) && $field['unsigned'] && !$autoincrement) ? ' UNSIGNED' : ''; + + $name = $this->conn->quoteIdentifier($name, true); + return $name . ' ' . $type . $unsigned . $default . $notnull . $autoinc; + } }