From 8110cf8e894b58677e7711fbe352a54109c00d1b Mon Sep 17 00:00:00 2001 From: zYne Date: Fri, 29 Dec 2006 13:45:41 +0000 Subject: [PATCH] Mysql string > 255 now converts to TEXT type --- lib/Doctrine.php | 6 ++- lib/Doctrine/Connection.php | 12 ++--- lib/Doctrine/DataDict/Mysql.php | 18 ++++--- lib/Doctrine/Export/Oracle.php | 81 ++++++++++++++++++-------------- tests/DataDict/MysqlTestCase.php | 5 ++ tests/Export/PgsqlTestCase.php | 1 - 6 files changed, 73 insertions(+), 50 deletions(-) diff --git a/lib/Doctrine.php b/lib/Doctrine.php index 682d73576..8c5cf804f 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -132,7 +132,11 @@ final class Doctrine { * default table type attribute */ const ATTR_DEFAULT_TABLE_TYPE = 21; - + const ATTR_DEF_TEXT_LENGTH = 30; + const ATTR_DEF_VARCHAR_LENGTH = 31; + const ATTR_DEF_TABLESPACE = 32; + const ATTR_EMULATE_DATABASE = 33; + const ATTR_DB_NAME_FORMAT = 34; /** TODO: REMOVE THE FOLLOWING CONSTANTS AND UPDATE THE DOCS ! */ diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index e6409e481..c2543501d 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -148,8 +148,11 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * @return Doctrine_Connection_Module connection module */ public function __get($name) { + if(isset($this->properties[$name])) + return $this->properties[$name]; + if( ! isset($this->modules[$name])) - throw new Doctrine_Connection_Exception('Unknown module ' . $name); + throw new Doctrine_Connection_Exception('Unknown module / property ' . $name); if($this->modules[$name] === false) { switch($name) { @@ -160,10 +163,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $class = 'Doctrine_' . ucwords($name) . '_' . $this->getName(); $this->modules[$name] = new $class($this); } - } - if(isset($this->properties[$name])) - return $this->properties[$name]; - + } return $this->modules[$name]; } @@ -223,7 +223,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun public function quoteIdentifier($str, $checkOption = true) { if ($checkOption && ! $this->getAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER)) { return $str; - } + } $str = str_replace($this->properties['identifier_quoting']['end'], $this->properties['identifier_quoting']['escape'] . $this->properties['identifier_quoting']['end'], $str); diff --git a/lib/Doctrine/DataDict/Mysql.php b/lib/Doctrine/DataDict/Mysql.php index 67a6058e1..6fde93e2b 100644 --- a/lib/Doctrine/DataDict/Mysql.php +++ b/lib/Doctrine/DataDict/Mysql.php @@ -130,7 +130,8 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict { * @return string DBMS specific SQL code portion that should be used to * declare the specified field. */ - public function getNativeDeclaration($field) { + public function getNativeDeclaration($field) + { switch ($field['type']) { case 'char': $length = (! empty($field['length'])) ? $field['length'] : false; @@ -140,12 +141,17 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict { case 'array': case 'object': case 'string': - if (empty($field['length']) && array_key_exists('default', $field)) { - $field['length'] = $this->conn->varchar_max_length; + + if ( ! isset($field['length'])) { + if(array_key_exists('default', $field)) { + $field['length'] = $this->conn->varchar_max_length; + } else { + $field['length'] = false; + } } - - $length = (! empty($field['length'])) ? $field['length'] : false; - $fixed = (! empty($field['fixed'])) ? $field['fixed'] : false; + + $length = ($field['length'] < $this->conn->varchar_max_length) ? $field['length'] : false; + $fixed = (isset($field['fixed'])) ? $field['fixed'] : false; return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR(255)') : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT'); diff --git a/lib/Doctrine/Export/Oracle.php b/lib/Doctrine/Export/Oracle.php index fae7ee4ae..e88c7c163 100644 --- a/lib/Doctrine/Export/Oracle.php +++ b/lib/Doctrine/Export/Oracle.php @@ -40,26 +40,28 @@ class Doctrine_Export_Oracle extends Doctrine_Export { * @return mixed MDB2_OK on success, a MDB2 error on failure * @access public */ - public function createDatabase($name) { - if (!$db->options['emulate_database']) - throw new Doctrine_Export_Oracle_Exception('database creation is only supported if the "emulate_database" option is enabled'); + public function createDatabase($name) + { + if ( ! $this->conn->getAttribute(Doctrine::ATTR_EMULATE_DATABASE)) + throw new Doctrine_Export_Oracle_Exception('database creation is only supported if the "emulate_database" attribute is enabled'); - $username = $db->options['database_name_prefix'].$name; - $password = $db->dsn['password'] ? $db->dsn['password'] : $name; - $tablespace = $db->options['default_tablespace'] - ? ' DEFAULT TABLESPACE '.$db->options['default_tablespace'] : ''; + $username = sprintf($this->conn->getAttribute(Doctrine::ATTR_DB_NAME_FORMAT), $name); + $password = $this->conn->dsn['password'] ? $this->conn->dsn['password'] : $name; - $query = 'CREATE USER '.$username.' IDENTIFIED BY '.$password.$tablespace; - $result = $db->standaloneQuery($query, null, true); + $tablespace = $this->conn->getAttribute(Doctrine::ATTR_DB_NAME_FORMAT) + ? ' DEFAULT TABLESPACE '.$this->conn->options['default_tablespace'] : ''; - $query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO '.$username; - $result = $db->standaloneQuery($query, null, true); + $query = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password . $tablespace; + $result = $this->conn->query($query); + + $query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username; + $result = $this->conn->query($query); if (PEAR::isError($result)) { $query = 'DROP USER '.$username.' CASCADE'; - $result2 = $db->standaloneQuery($query, null, true); + $result2 = $this->conn->query($query); if (PEAR::isError($result2)) { - return $db->raiseError($result2, null, null, + return $this->conn->raiseError($result2, null, null, 'could not setup the database user', __FUNCTION__); } return $result; @@ -68,19 +70,21 @@ class Doctrine_Export_Oracle extends Doctrine_Export { /** * drop an existing database * - * @param object $db database object that is extended by this class + * @param object $this->conn database object that is extended by this class * @param string $name name of the database that should be dropped * @return mixed MDB2_OK on success, a MDB2 error on failure * @access public */ - public function dropDatabase($name) { - if (!$db->options['emulate_database']) + public function dropDatabase($name) + { + if ( ! $this->conn->getAttribute(Doctrine::ATTR_EMULATE_DATABASE)) throw new Doctrine_Export_Oracle_Exception('database dropping is only supported if the "emulate_database" option is enabled'); - $username = $db->options['database_name_prefix'].$name; - return $db->standaloneQuery('DROP USER '.$username.' CASCADE', null, true); + $username = sprintf($this->conn->getAttribute(Doctrine::ATTR_DB_NAME_FORMAT), $name); + + return $this->conn->query('DROP USER ' . $username . ' CASCADE'); } /** * add an autoincrement sequence + trigger @@ -91,7 +95,8 @@ class Doctrine_Export_Oracle extends Doctrine_Export { * @return mixed MDB2_OK on success, a MDB2 error on failure * @access private */ - public function _makeAutoincrement($name, $table, $start = 1) { + public function _makeAutoincrement($name, $table, $start = 1) + { $table = strtoupper($table); $index_name = $table . '_AI_PK'; $definition = array( @@ -102,27 +107,27 @@ class Doctrine_Export_Oracle extends Doctrine_Export { /** if (PEAR::isError($result)) { - return $db->raiseError($result, null, null, + return $this->conn->raiseError($result, null, null, 'primary key for autoincrement PK could not be created', __FUNCTION__); } */ if (is_null($start)) { - $db->beginTransaction(); - $query = 'SELECT MAX(' . $db->quoteIdentifier($name, true) . ') FROM ' . $db->quoteIdentifier($table, true); + $this->conn->beginTransaction(); + $query = 'SELECT MAX(' . $this->conn->quoteIdentifier($name, true) . ') FROM ' . $this->conn->quoteIdentifier($table, true); $start = $this->db->queryOne($query, 'integer'); if (PEAR::isError($start)) { return $start; } ++$start; $result = $this->createSequence($table, $start); - $db->commit(); + $this->conn->commit(); } else { $result = $this->createSequence($table, $start); } /** if (PEAR::isError($result)) { - return $db->raiseError($result, null, null, + return $this->conn->raiseError($result, null, null, 'sequence for autoincrement PK could not be created', __FUNCTION__); } */ @@ -160,7 +165,8 @@ END; * @param string $table name of the table * @return void */ - public function dropAutoincrement($table) { + public function dropAutoincrement($table) + { $table = strtoupper($table); $trigger_name = $table . '_AI_PK'; $trigger_name_quoted = $this->conn->getDbh()->quote($trigger_name); @@ -169,7 +175,7 @@ END; $trigger = $this->conn->fetchOne($query); if($trigger) { - $trigger_name = $db->quoteIdentifier($table . '_AI_PK', true); + $trigger_name = $this->conn->quoteIdentifier($table . '_AI_PK', true); $trigger_sql = 'DROP TRIGGER ' . $trigger_name; // if throws exception, trigger for autoincrement PK could not be dropped @@ -215,7 +221,8 @@ END; * * @return void */ - public function createTable($name, $fields, $options = array()) { + public function createTable($name, $fields, $options = array()) + { $this->conn->beginTransaction(); $result = parent::createTable($name, $fields, $options); @@ -236,11 +243,12 @@ END; * @param string $name name of the table that should be dropped * @return void */ - public function dropTable($name) { - //$db->beginNestedTransaction(); + public function dropTable($name) + { + //$this->conn->beginNestedTransaction(); $result = $this->dropAutoincrement($name); $result = parent::dropTable($name); - //$db->completeNestedTransaction(); + //$this->conn->completeNestedTransaction(); return $result; } /** @@ -331,10 +339,11 @@ END; * actually perform them otherwise. * @return void */ - public function alterTable($name, array $changes, $check) { + public function alterTable($name, array $changes, $check) + { - foreach ($changes as $change_name => $change) { - switch ($change_name) { + foreach ($changes as $changeName => $change) { + switch ($changeName) { case 'add': case 'remove': case 'change': @@ -343,7 +352,7 @@ END; break; default: return $this->conn->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null, - 'change type "'.$change_name.'" not yet supported', __FUNCTION__); + 'change type "'.$changeName.'" not yet supported', __FUNCTION__); } } @@ -408,7 +417,7 @@ END; /** * create sequence * - * @param object $db database object that is extended by this class + * @param object $this->conn database object that is extended by this class * @param string $seqName name of the sequence to be created * @param string $start start value of the sequence; default is 1 * @return void @@ -422,7 +431,7 @@ END; /** * drop existing sequence * - * @param object $db database object that is extended by this class + * @param object $this->conn database object that is extended by this class * @param string $seqName name of the sequence to be dropped * @return void */ diff --git a/tests/DataDict/MysqlTestCase.php b/tests/DataDict/MysqlTestCase.php index 1701ac4de..3545b98c4 100644 --- a/tests/DataDict/MysqlTestCase.php +++ b/tests/DataDict/MysqlTestCase.php @@ -76,6 +76,11 @@ class Doctrine_DataDict_Mysql_TestCase extends Doctrine_Driver_UnitTestCase { $this->assertEqual($this->dataDict->GetNativeDeclaration($a), 'TEXT'); } + public function testGetNativeDeclarationSupportsStringTypeWithLongLength() { + $a = array('type' => 'string', 'length' => 2000); + + $this->assertEqual($this->dataDict->GetNativeDeclaration($a), 'TEXT'); + } public function testGetNativeDeclarationSupportsArrayType2() { $a = array('type' => 'array'); diff --git a/tests/Export/PgsqlTestCase.php b/tests/Export/PgsqlTestCase.php index 0a2f0113e..8bda821dc 100644 --- a/tests/Export/PgsqlTestCase.php +++ b/tests/Export/PgsqlTestCase.php @@ -2,7 +2,6 @@ class Doctrine_Export_Pgsql_TestCase extends Doctrine_UnitTestCase { public function testCreateDatabaseExecutesSql() { $this->export->createDatabase('db'); - $this->assertEqual($this->adapter->pop(), 'CREATE DATABASE db'); } public function testDropDatabaseExecutesSql() {