From 57fee969237f6f34c6df6a49636ec081ba769704 Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 30 Nov 2006 22:33:54 +0000 Subject: [PATCH] Small fixes --- lib/Doctrine/DataDict.php | 18 +-------- lib/Doctrine/DataDict/Sqlite.php | 67 +++++++++++++++++++++++++++++--- lib/Doctrine/Export.php | 23 ++++++++--- lib/Doctrine/Export/Sqlite.php | 32 +++++++-------- lib/Doctrine/Table.php | 64 +++++++++++++++++------------- 5 files changed, 132 insertions(+), 72 deletions(-) diff --git a/lib/Doctrine/DataDict.php b/lib/Doctrine/DataDict.php index cb428799f..4a66627f5 100644 --- a/lib/Doctrine/DataDict.php +++ b/lib/Doctrine/DataDict.php @@ -149,23 +149,7 @@ class Doctrine_DataDict { throw new Doctrine_Exception("Unknown column type $type"); endswitch; } - - /** - * Converts native database column type to doctrine data type - * - * @param string $column column type - * @param integer $length column length - * @param string $dbType Database driver name as returned by PDO::getAttribute(PDO::ATTR_DRIVER_NAME) - * @param string $dbVersion Database server version as return by PDO::getAttribute(PDO::ATTR_SERVER_VERSION) - * @return array of doctrine column type and column length. In future may also return a validator name. - * @throws Doctrine_Exception on unknown column type - * @author Jukka Hassinen - */ - public static function getDoctrineType($colType,$colLength, $dbType = null, $dbVersion = null) - { - return array($colType, $colLength); /* @todo FIXME i am incomplete*/ - } - + /** * checks for valid class name (uses camel case and underscores) * diff --git a/lib/Doctrine/DataDict/Sqlite.php b/lib/Doctrine/DataDict/Sqlite.php index 2d5339973..9617dd25d 100644 --- a/lib/Doctrine/DataDict/Sqlite.php +++ b/lib/Doctrine/DataDict/Sqlite.php @@ -56,10 +56,16 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module { public function getNativeDeclaration(array $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'].')') + case 'object': + case 'array': + case 'string': + case 'char': + case 'varchar': + $length = (isset($field['length']) && $field['length']) ? $field['length'] : null; + + $fixed = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false; + + return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$this->conn->getAttribute(Doctrine::ATTR_DEFAULT_TEXTFLD_LENGTH).')') : ($length ? 'VARCHAR('.$length.')' : 'TEXT'); case 'clob': if (!empty($field['length'])) { @@ -85,6 +91,7 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module { } } return 'LONGBLOB'; + case 'enum': case 'integer': if (!empty($field['length'])) { $length = $field['length']; @@ -106,8 +113,9 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module { case 'timestamp': return 'DATETIME'; case 'float': - return 'DOUBLE'.($db->options['fixed_float'] ? '('. - ($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' : ''); + case 'double': + 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'].')'; @@ -226,6 +234,53 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module { return array($type, $length, $unsigned, $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 = ''; + if(isset($field['autoincrement']) && $field['autoincrement']) { + $autoinc = ' PRIMARY KEY AUTOINCREMENT'; + } 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' : ''; + $unsigned = (isset($field['unsigned']) && $field['unsigned']) ? ' UNSIGNED' : ''; + + $name = $this->conn->quoteIdentifier($name, true); + return $name . ' ' . $this->getNativeDeclaration($field) . $unsigned . $default . $notnull . $autoinc; + } /** * lists all databases * diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index a9014ffa6..8ac914c9d 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -43,19 +43,20 @@ class Doctrine_Export extends Doctrine_Connection_Module { } /** * dropTable + * drop an existing table * - * @param string $table name of table that should be dropped from the database + * @param string $table name of table that should be dropped from the database * @throws PDOException * @return void */ public function dropTable($table) { - $this->conn->getDbh()->query('DROP TABLE '.$table); + $this->conn->getDbh()->query('DROP TABLE ' . $table); } /** * drop existing index * - * @param string $table name of table that should be used in method + * @param string $table name of table that should be used in method * @param string $name name of the index to be dropped * @return void */ @@ -233,6 +234,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { $fields[] = $this->conn->quoteIdentifier($field); } $query .= ' ('. implode(', ', $fields) . ')'; + return $this->conn->getDbh()->query($query); } @@ -331,18 +333,27 @@ class Doctrine_Export extends Doctrine_Connection_Module { /** * Get declaration of a number of field in bulk * - * @param string $fields a multidimensional associative array. + * @param array $fields a multidimensional associative array. * The first dimension determines the field name, while the second * dimension is keyed 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 - * Boolean value to be used as default for this field. + * 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. + * charset + * Text value with the default CHARACTER SET for this field. + * collation + * Text value with the default COLLATION for this field. * * @return string */ @@ -381,7 +392,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { * @return string DBMS specific SQL code portion that should be used to * declare the specified field. */ - public function getDeclaration($name, $field) { + public function getDeclaration($name, array $field) { $default = ''; if(isset($field['default'])) { diff --git a/lib/Doctrine/Export/Sqlite.php b/lib/Doctrine/Export/Sqlite.php index 2fc5d64a7..4fb9725a3 100644 --- a/lib/Doctrine/Export/Sqlite.php +++ b/lib/Doctrine/Export/Sqlite.php @@ -22,15 +22,15 @@ Doctrine::autoload('Doctrine_Export'); /** * Doctrine_Export_Sqlite * - * @package Doctrine - * @author Konsta Vesterinen + * @package Doctrine + * @author Konsta Vesterinen * @author Lukas Smith (PEAR MDB2 library) - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @category Object Relational Mapping - * @link www.phpdoctrine.com - * @since 1.0 - * @version $Revision$ - */ + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ class Doctrine_Export_Sqlite extends Doctrine_Export { /** * Get the stucture of a field into an array @@ -66,22 +66,22 @@ class Doctrine_Export_Sqlite extends Doctrine_Export { */ public function createIndex($table, $name, array $definition) { $table = $this->conn->quoteIdentifier($table, true); - $name = $this->dbh->getIndexName($name); - $query = "CREATE INDEX $name ON $table"; + $name = $this->conn->getIndexName($name); + $query = 'CREATE INDEX ' . $name . ' ON ' . $table; $fields = array(); - foreach ($definition['fields'] as $field_name => $field) { - $field_string = $field_name; - if (!empty($field['sorting'])) { + foreach ($definition['fields'] as $fieldName => $field) { + $fieldString = $fieldName; + if(isset($field['sorting'])) { switch ($field['sorting']) { case 'ascending': - $field_string.= ' ASC'; + $fieldString.= ' ASC'; break; case 'descending': - $field_string.= ' DESC'; + $fieldString.= ' DESC'; break; } } - $fields[] = $field_string; + $fields[] = $fieldString; } $query .= ' ('.implode(', ', $fields) . ')'; return $this->dbh->exec($query); diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index bdde4b099..59668330c 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -23,14 +23,14 @@ * each Doctrine_Table holds the information of foreignKeys and associations * * - * @author Konsta Vesterinen - * @package Doctrine - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @version $Revision$ - * @category Object Relational Mapping - * @link www.phpdoctrine.com - * @since 1.0 - */ + * @author Konsta Vesterinen + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ class Doctrine_Table extends Doctrine_Configurable implements Countable { /** * @var array $data temporary data which is then loaded into Doctrine_Record::$data @@ -58,9 +58,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { */ private $query; /** - * @var Doctrine_Connection $connection Doctrine_Connection object that created this table + * @var Doctrine_Connection $conn Doctrine_Connection object that created this table */ - private $connection; + private $conn; /** * @var string $name */ @@ -145,9 +145,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @return void */ public function __construct($name, Doctrine_Connection $conn) { - $this->connection = $conn; + $this->conn = $conn; - $this->setParent($this->connection); + $this->setParent($this->conn); $this->options['name'] = $name; @@ -242,8 +242,18 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { if($this->getAttribute(Doctrine::ATTR_CREATE_TABLES)) { if(Doctrine_DataDict::isValidClassname($class->getName())) { - $dict = new Doctrine_DataDict($this->getConnection()->getDBH()); - $dict->createTable($this->options['tableName'], $this->columns); + //$dict = new Doctrine_DataDict($this->getConnection()->getDBH()); + try { + $columns = array(); + foreach($this->columns as $name => $column) { + $definition = $column[2]; + $definition['type'] = $column[1]; + $columns[$name] = $definition; + } + $this->conn->export->createTable($this->options['tableName'], $columns); + } catch(Exception $e) { + + } } } @@ -262,7 +272,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { $this->query = "SELECT ".implode(", ",array_keys($this->columns))." FROM ".$this->getTableName(); // check if an instance of this table is already initialized - if( ! $this->connection->addTable($this)) + if( ! $this->conn->addTable($this)) throw new Doctrine_Table_Exception(); $this->repository = new Doctrine_Table_Repository($this); @@ -598,7 +608,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @return Doctrine_Connection */ public function getConnection() { - return $this->connection; + return $this->conn; } /** * hasRelatedComponent @@ -641,7 +651,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { $alias = $name; $name = $this->bound[$alias][3]; - $table = $this->connection->getTable($name); + $table = $this->conn->getTable($name); if($component == $this->options['name'] || in_array($component, $this->parents)) { @@ -694,7 +704,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { if($e2[0] != $component) throw new Doctrine_Table_Exception($e2[0] . ' doesn\'t match ' . $component); - $associationTable = $this->connection->getTable($e2[0]); + $associationTable = $this->conn->getTable($e2[0]); if(count($fields) > 1) { // SELF-REFERENCING THROUGH JOIN TABLE @@ -791,7 +801,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { $params = array_merge($id, array_values($this->options['inheritanceMap'])); - $stmt = $this->connection->execute($query,$params); + $stmt = $this->conn->execute($query,$params); $this->data = $stmt->fetch(PDO::FETCH_ASSOC); @@ -825,7 +835,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @return Doctrine_Collection */ public function findAll() { - $graph = new Doctrine_Query($this->connection); + $graph = new Doctrine_Query($this->conn); $users = $graph->query("FROM ".$this->options['name']); return $users; } @@ -839,7 +849,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @return Doctrine_Collection */ public function findBySql($dql, array $params = array()) { - $q = new Doctrine_Query($this->connection); + $q = new Doctrine_Query($this->conn); $users = $q->query("FROM ".$this->options['name']." WHERE ".$dql, $params); return $users; } @@ -901,7 +911,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { $params = array_merge(array($id), array_values($this->options['inheritanceMap'])); - $this->data = $this->connection->execute($query,$params)->fetch(PDO::FETCH_ASSOC); + $this->data = $this->conn->execute($query,$params)->fetch(PDO::FETCH_ASSOC); if($this->data === false) return false; @@ -914,7 +924,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @return integer */ public function count() { - $a = $this->connection->getDBH()->query("SELECT COUNT(1) FROM ".$this->options['tableName'])->fetch(PDO::FETCH_NUM); + $a = $this->conn->getDBH()->query("SELECT COUNT(1) FROM ".$this->options['tableName'])->fetch(PDO::FETCH_NUM); return current($a); } /** @@ -934,12 +944,12 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { */ public function execute($query, array $array = array(), $limit = null, $offset = null) { $coll = new Doctrine_Collection($this); - $query = $this->connection->modifyLimitQuery($query,$limit,$offset); + $query = $this->conn->modifyLimitQuery($query,$limit,$offset); if( ! empty($array)) { - $stmt = $this->connection->getDBH()->prepare($query); + $stmt = $this->conn->getDBH()->prepare($query); $stmt->execute($array); } else { - $stmt = $this->connection->getDBH()->query($query); + $stmt = $this->conn->getDBH()->query($query); } $data = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -1109,7 +1119,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { */ final public function getMaxIdentifier() { $sql = "SELECT MAX(".$this->getIdentifier().") FROM ".$this->getTableName(); - $stmt = $this->connection->getDBH()->query($sql); + $stmt = $this->conn->getDBH()->query($sql); $data = $stmt->fetch(PDO::FETCH_NUM); return isset($data[0])?$data[0]:1; }