Commit 57fee969 authored by zYne's avatar zYne

Small fixes

parent 1775fc96
...@@ -149,23 +149,7 @@ class Doctrine_DataDict { ...@@ -149,23 +149,7 @@ class Doctrine_DataDict {
throw new Doctrine_Exception("Unknown column type $type"); throw new Doctrine_Exception("Unknown column type $type");
endswitch; 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 <Jukka.Hassinen@BrainAlliance.com>
*/
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) * checks for valid class name (uses camel case and underscores)
* *
......
...@@ -56,10 +56,16 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module { ...@@ -56,10 +56,16 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module {
public function getNativeDeclaration(array $field) { public function getNativeDeclaration(array $field) {
switch ($field['type']) { switch ($field['type']) {
case 'text': case 'text':
$length = !empty($field['length']) case 'object':
? $field['length'] : false; case 'array':
$fixed = !empty($field['fixed']) ? $field['fixed'] : false; case 'string':
return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$db->options['default_text_field_length'].')') 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'); : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
case 'clob': case 'clob':
if (!empty($field['length'])) { if (!empty($field['length'])) {
...@@ -85,6 +91,7 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module { ...@@ -85,6 +91,7 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module {
} }
} }
return 'LONGBLOB'; return 'LONGBLOB';
case 'enum':
case 'integer': case 'integer':
if (!empty($field['length'])) { if (!empty($field['length'])) {
$length = $field['length']; $length = $field['length'];
...@@ -106,8 +113,9 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module { ...@@ -106,8 +113,9 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module {
case 'timestamp': case 'timestamp':
return 'DATETIME'; return 'DATETIME';
case 'float': case 'float':
return 'DOUBLE'.($db->options['fixed_float'] ? '('. case 'double':
($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' : ''); return 'DOUBLE';//($db->options['fixed_float'] ? '('.
//($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' : '');
case 'decimal': case 'decimal':
$length = !empty($field['length']) ? $field['length'] : 18; $length = !empty($field['length']) ? $field['length'] : 18;
return 'DECIMAL('.$length.','.$db->options['decimal_places'].')'; return 'DECIMAL('.$length.','.$db->options['decimal_places'].')';
...@@ -226,6 +234,53 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module { ...@@ -226,6 +234,53 @@ class Doctrine_DataDict_Sqlite extends Doctrine_Connection_Module {
return array($type, $length, $unsigned, $fixed); 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 * lists all databases
* *
......
...@@ -43,19 +43,20 @@ class Doctrine_Export extends Doctrine_Connection_Module { ...@@ -43,19 +43,20 @@ class Doctrine_Export extends Doctrine_Connection_Module {
} }
/** /**
* dropTable * 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 * @throws PDOException
* @return void * @return void
*/ */
public function dropTable($table) { public function dropTable($table) {
$this->conn->getDbh()->query('DROP TABLE '.$table); $this->conn->getDbh()->query('DROP TABLE ' . $table);
} }
/** /**
* drop existing index * 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 * @param string $name name of the index to be dropped
* @return void * @return void
*/ */
...@@ -233,6 +234,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { ...@@ -233,6 +234,7 @@ class Doctrine_Export extends Doctrine_Connection_Module {
$fields[] = $this->conn->quoteIdentifier($field); $fields[] = $this->conn->quoteIdentifier($field);
} }
$query .= ' ('. implode(', ', $fields) . ')'; $query .= ' ('. implode(', ', $fields) . ')';
return $this->conn->getDbh()->query($query); return $this->conn->getDbh()->query($query);
} }
...@@ -331,18 +333,27 @@ class Doctrine_Export extends Doctrine_Connection_Module { ...@@ -331,18 +333,27 @@ class Doctrine_Export extends Doctrine_Connection_Module {
/** /**
* Get declaration of a number of field in bulk * 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 * The first dimension determines the field name, while the second
* dimension is keyed with the name of the properties * dimension is keyed with the name of the properties
* of the field being declared as array indexes. Currently, the types * of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows: * 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 * default
* Boolean value to be used as default for this field. * Text value to be used as default for this field.
* *
* notnull * notnull
* Boolean flag that indicates whether this field is constrained * Boolean flag that indicates whether this field is constrained
* to not be set to null. * 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 * @return string
*/ */
...@@ -381,7 +392,7 @@ class Doctrine_Export extends Doctrine_Connection_Module { ...@@ -381,7 +392,7 @@ class Doctrine_Export extends Doctrine_Connection_Module {
* @return string DBMS specific SQL code portion that should be used to * @return string DBMS specific SQL code portion that should be used to
* declare the specified field. * declare the specified field.
*/ */
public function getDeclaration($name, $field) { public function getDeclaration($name, array $field) {
$default = ''; $default = '';
if(isset($field['default'])) { if(isset($field['default'])) {
......
...@@ -22,15 +22,15 @@ Doctrine::autoload('Doctrine_Export'); ...@@ -22,15 +22,15 @@ Doctrine::autoload('Doctrine_Export');
/** /**
* Doctrine_Export_Sqlite * Doctrine_Export_Sqlite
* *
* @package Doctrine * @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library) * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping * @category Object Relational Mapping
* @link www.phpdoctrine.com * @link www.phpdoctrine.com
* @since 1.0 * @since 1.0
* @version $Revision$ * @version $Revision$
*/ */
class Doctrine_Export_Sqlite extends Doctrine_Export { class Doctrine_Export_Sqlite extends Doctrine_Export {
/** /**
* Get the stucture of a field into an array * Get the stucture of a field into an array
...@@ -66,22 +66,22 @@ class Doctrine_Export_Sqlite extends Doctrine_Export { ...@@ -66,22 +66,22 @@ class Doctrine_Export_Sqlite extends Doctrine_Export {
*/ */
public function createIndex($table, $name, array $definition) { public function createIndex($table, $name, array $definition) {
$table = $this->conn->quoteIdentifier($table, true); $table = $this->conn->quoteIdentifier($table, true);
$name = $this->dbh->getIndexName($name); $name = $this->conn->getIndexName($name);
$query = "CREATE INDEX $name ON $table"; $query = 'CREATE INDEX ' . $name . ' ON ' . $table;
$fields = array(); $fields = array();
foreach ($definition['fields'] as $field_name => $field) { foreach ($definition['fields'] as $fieldName => $field) {
$field_string = $field_name; $fieldString = $fieldName;
if (!empty($field['sorting'])) { if(isset($field['sorting'])) {
switch ($field['sorting']) { switch ($field['sorting']) {
case 'ascending': case 'ascending':
$field_string.= ' ASC'; $fieldString.= ' ASC';
break; break;
case 'descending': case 'descending':
$field_string.= ' DESC'; $fieldString.= ' DESC';
break; break;
} }
} }
$fields[] = $field_string; $fields[] = $fieldString;
} }
$query .= ' ('.implode(', ', $fields) . ')'; $query .= ' ('.implode(', ', $fields) . ')';
return $this->dbh->exec($query); return $this->dbh->exec($query);
......
...@@ -23,14 +23,14 @@ ...@@ -23,14 +23,14 @@
* each Doctrine_Table holds the information of foreignKeys and associations * each Doctrine_Table holds the information of foreignKeys and associations
* *
* *
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @package Doctrine * @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$ * @version $Revision$
* @category Object Relational Mapping * @category Object Relational Mapping
* @link www.phpdoctrine.com * @link www.phpdoctrine.com
* @since 1.0 * @since 1.0
*/ */
class Doctrine_Table extends Doctrine_Configurable implements Countable { class Doctrine_Table extends Doctrine_Configurable implements Countable {
/** /**
* @var array $data temporary data which is then loaded into Doctrine_Record::$data * @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 { ...@@ -58,9 +58,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
*/ */
private $query; 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 * @var string $name
*/ */
...@@ -145,9 +145,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -145,9 +145,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @return void * @return void
*/ */
public function __construct($name, Doctrine_Connection $conn) { 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; $this->options['name'] = $name;
...@@ -242,8 +242,18 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -242,8 +242,18 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
if($this->getAttribute(Doctrine::ATTR_CREATE_TABLES)) { if($this->getAttribute(Doctrine::ATTR_CREATE_TABLES)) {
if(Doctrine_DataDict::isValidClassname($class->getName())) { if(Doctrine_DataDict::isValidClassname($class->getName())) {
$dict = new Doctrine_DataDict($this->getConnection()->getDBH()); //$dict = new Doctrine_DataDict($this->getConnection()->getDBH());
$dict->createTable($this->options['tableName'], $this->columns); 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 { ...@@ -262,7 +272,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$this->query = "SELECT ".implode(", ",array_keys($this->columns))." FROM ".$this->getTableName(); $this->query = "SELECT ".implode(", ",array_keys($this->columns))." FROM ".$this->getTableName();
// check if an instance of this table is already initialized // 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(); throw new Doctrine_Table_Exception();
$this->repository = new Doctrine_Table_Repository($this); $this->repository = new Doctrine_Table_Repository($this);
...@@ -598,7 +608,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -598,7 +608,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @return Doctrine_Connection * @return Doctrine_Connection
*/ */
public function getConnection() { public function getConnection() {
return $this->connection; return $this->conn;
} }
/** /**
* hasRelatedComponent * hasRelatedComponent
...@@ -641,7 +651,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -641,7 +651,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$alias = $name; $alias = $name;
$name = $this->bound[$alias][3]; $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)) { if($component == $this->options['name'] || in_array($component, $this->parents)) {
...@@ -694,7 +704,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -694,7 +704,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
if($e2[0] != $component) if($e2[0] != $component)
throw new Doctrine_Table_Exception($e2[0] . ' doesn\'t match ' . $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) { if(count($fields) > 1) {
// SELF-REFERENCING THROUGH JOIN TABLE // SELF-REFERENCING THROUGH JOIN TABLE
...@@ -791,7 +801,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -791,7 +801,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$params = array_merge($id, array_values($this->options['inheritanceMap'])); $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); $this->data = $stmt->fetch(PDO::FETCH_ASSOC);
...@@ -825,7 +835,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -825,7 +835,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @return Doctrine_Collection * @return Doctrine_Collection
*/ */
public function findAll() { public function findAll() {
$graph = new Doctrine_Query($this->connection); $graph = new Doctrine_Query($this->conn);
$users = $graph->query("FROM ".$this->options['name']); $users = $graph->query("FROM ".$this->options['name']);
return $users; return $users;
} }
...@@ -839,7 +849,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -839,7 +849,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @return Doctrine_Collection * @return Doctrine_Collection
*/ */
public function findBySql($dql, array $params = array()) { 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); $users = $q->query("FROM ".$this->options['name']." WHERE ".$dql, $params);
return $users; return $users;
} }
...@@ -901,7 +911,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -901,7 +911,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$params = array_merge(array($id), array_values($this->options['inheritanceMap'])); $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) if($this->data === false)
return false; return false;
...@@ -914,7 +924,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -914,7 +924,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @return integer * @return integer
*/ */
public function count() { 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); return current($a);
} }
/** /**
...@@ -934,12 +944,12 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -934,12 +944,12 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
*/ */
public function execute($query, array $array = array(), $limit = null, $offset = null) { public function execute($query, array $array = array(), $limit = null, $offset = null) {
$coll = new Doctrine_Collection($this); $coll = new Doctrine_Collection($this);
$query = $this->connection->modifyLimitQuery($query,$limit,$offset); $query = $this->conn->modifyLimitQuery($query,$limit,$offset);
if( ! empty($array)) { if( ! empty($array)) {
$stmt = $this->connection->getDBH()->prepare($query); $stmt = $this->conn->getDBH()->prepare($query);
$stmt->execute($array); $stmt->execute($array);
} else { } else {
$stmt = $this->connection->getDBH()->query($query); $stmt = $this->conn->getDBH()->query($query);
} }
$data = $stmt->fetchAll(PDO::FETCH_ASSOC); $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
...@@ -1109,7 +1119,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -1109,7 +1119,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
*/ */
final public function getMaxIdentifier() { final public function getMaxIdentifier() {
$sql = "SELECT MAX(".$this->getIdentifier().") FROM ".$this->getTableName(); $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); $data = $stmt->fetch(PDO::FETCH_NUM);
return isset($data[0])?$data[0]:1; return isset($data[0])?$data[0]:1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment