From cec372dfe1c27101889e8ae5e5c4409ec5a281d9 Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 25 Sep 2006 21:08:02 +0000 Subject: [PATCH] Doctrine_DataDict_Sqlite driver --- Doctrine/Connection.php | 10 ++- Doctrine/DataDict.php | 2 +- Doctrine/DataDict/Sqlite.php | 28 ++++++- Doctrine/Schema/Column.php | 127 +++++++++---------------------- draft/DB.php | 7 ++ tests/DataDictSqliteTestCase.php | 62 +++++++++++++++ 6 files changed, 137 insertions(+), 99 deletions(-) create mode 100644 tests/DataDictSqliteTestCase.php diff --git a/Doctrine/Connection.php b/Doctrine/Connection.php index 29267765a..db5f47262 100644 --- a/Doctrine/Connection.php +++ b/Doctrine/Connection.php @@ -94,11 +94,19 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun /** * returns the database handler of which this connection uses * - * @return object PDO the database handler + * @return PDO the database handler */ public function getDBH() { return $this->dbh; } + /** + * returns a datadict object + * + * @return Doctrine_DataDict + */ + public function getDataDict() { + + } /** * returns the regular expression operator * (implemented by the connection drivers) diff --git a/Doctrine/DataDict.php b/Doctrine/DataDict.php index cf4af576d..7b954ce8a 100644 --- a/Doctrine/DataDict.php +++ b/Doctrine/DataDict.php @@ -28,7 +28,7 @@ */ class Doctrine_DataDict { - private $dbh; + protected $dbh; public function __construct(PDO $dbh) { $file = Doctrine::getPath().DIRECTORY_SEPARATOR."Doctrine".DIRECTORY_SEPARATOR."adodb-hack".DIRECTORY_SEPARATOR."adodb.inc.php"; diff --git a/Doctrine/DataDict/Sqlite.php b/Doctrine/DataDict/Sqlite.php index cb1c81e55..125700858 100644 --- a/Doctrine/DataDict/Sqlite.php +++ b/Doctrine/DataDict/Sqlite.php @@ -27,7 +27,7 @@ * @version $Id$ */ -class Doctrine_DataDict_Sqlite { +class Doctrine_DataDict_Sqlite extends Doctrine_DataDict { /** * lists all databases * @@ -78,7 +78,23 @@ class Doctrine_DataDict_Sqlite { * @return array */ public function listTableColumns($table) { - + + $sql = "PRAGMA table_info($table)"; + $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + + $description = array(); + $columns = array(); + foreach ($result as $key => $val) { + $description = array( + 'name' => $val['name'], + 'type' => $val['type'], + 'notnull' => (bool) $val['notnull'], + 'default' => $val['dflt_value'], + 'primary' => (bool) $val['pk'], + ); + $columns[$val['name']] = new Doctrine_Schema_Column($description); + } + return $columns; } /** * lists table constraints @@ -90,13 +106,17 @@ class Doctrine_DataDict_Sqlite { } /** - * lists table constraints + * lists tables * * @param string|null $database * @return array */ public function listTables($database = null) { - + $sql = "SELECT name FROM sqlite_master WHERE type='table' " + . "UNION ALL SELECT name FROM sqlite_temp_master " + . "WHERE type='table' ORDER BY name"; + + return $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); } /** * lists table triggers diff --git a/Doctrine/Schema/Column.php b/Doctrine/Schema/Column.php index 9b522f15f..29add2bc4 100644 --- a/Doctrine/Schema/Column.php +++ b/Doctrine/Schema/Column.php @@ -33,102 +33,43 @@ * class Doctrine_Schema_Column * Holds information on a database table field */ -class Doctrine_Schema_Column extends Doctrine_Schema_Object - implements IteratorAggregate -{ - - /** Aggregations: */ - - /** Compositions: */ - var $m_Vector = array(); - - /*** Attributes: ***/ - +class Doctrine_Schema_Column extends Doctrine_Schema_Object implements IteratorAggregate { /** - * Column name - * @access public + * column definitions + * @var array $definition */ - public $name; - - /** - * Column type e.g. varchar, char, int etc. - * @access public - */ - public $type; - - /** - * Field max length - * @access public - */ - public $length; - - /** - * Is an autoincrement column - * @access public - */ - public $autoincrement; - - /** - * Default field value - * @access public - */ - public $default; - - /** - * Is not null - * @access public - */ - public $notNull; - - /** - * Column comment - * @access public - */ - public $description; - - /** - * Column level check constraint - * @access public - */ - public $check; - - /** - * Character encoding e.g. ISO-8859-1 or UTF-8 etc. - * @access public - */ - public $charset; - - - /** - * - * @return - * @access public - */ - public function __toString( ) { - - } // end of member function __toString - - /** - * - * @return - * @access public - */ - public function __clone( ) { - - } // end of member function __clone - - /** - * - * @return bool - * @access public - */ - public function isValid( ) { - - } // end of member function isValid - - - + private $definition = array('name' => '', + 'type' => '', + 'unique' => false, + 'primary' => false, + 'notnull' => false, + 'default' => null, + ); + public function __construct(array $definition) { + foreach($this->definition as $key => $val) { + if(isset($definition[$key])) + $this->definition[$key] = $definition[$key]; + } + } + public function getName() { + return $this->definition['name']; + } + public function getType() { + return $this->definition['type']; + } + public function isUnique() { + return $this->definition['unique']; + } + public function isPrimaryKey() { + return $this->definition['primary']; + } + public function defaultValue() { + return $this->definition['default']; + } + public function isNotNull() { + return $this->definition['notnull']; + } } // end of Doctrine_Schema_Column diff --git a/draft/DB.php b/draft/DB.php index 3018047d7..cf06e7f27 100644 --- a/draft/DB.php +++ b/draft/DB.php @@ -360,6 +360,13 @@ class Doctrine_DB2 implements Countable, IteratorAggregate { return $rows; } + /** + * fetchAll + */ + public function fetchAssoc($statement, $params = array()) { + if( ! $params) + $this->query($statement); + } /** * lastInsertId * diff --git a/tests/DataDictSqliteTestCase.php b/tests/DataDictSqliteTestCase.php new file mode 100644 index 000000000..3ce6ddb05 --- /dev/null +++ b/tests/DataDictSqliteTestCase.php @@ -0,0 +1,62 @@ +dbh->query("CREATE TABLE test (col_null NULL, + col_int INTEGER NOT NULL, + col_real REAL, + col_text TEXT DEFAULT 'default' NOT NULL, + col_blob BLOB)"); + + $this->dict = new Doctrine_DataDict_Sqlite($this->dbh); + $this->columns = $this->dict->listTableColumns('test'); + } + public function testListTables() { + $result = $this->dict->listTables(); + + } + public function testIntegerType() { + $this->assertEqual($this->columns['col_int']->isUnique(), false); + $this->assertEqual($this->columns['col_int']->isNotNull(), true); + $this->assertEqual($this->columns['col_int']->defaultValue(), null); + $this->assertEqual($this->columns['col_int']->isPrimaryKey(), false); + $this->assertEqual($this->columns['col_int']->getType(), 'INTEGER'); + $this->assertEqual($this->columns['col_int']->getName(), 'col_int'); + } + public function testNullType() { + $this->assertEqual($this->columns['col_null']->isUnique(), false); + $this->assertEqual($this->columns['col_null']->isNotNull(), false); + $this->assertEqual($this->columns['col_null']->defaultValue(), null); + $this->assertEqual($this->columns['col_null']->isPrimaryKey(), false); + $this->assertEqual($this->columns['col_null']->getType(), 'numeric'); + $this->assertEqual($this->columns['col_null']->getName(), 'col_null'); + } + public function testTextType() { + $this->assertEqual($this->columns['col_text']->isUnique(), false); + $this->assertEqual($this->columns['col_text']->isNotNull(), true); + $this->assertEqual($this->columns['col_text']->defaultValue(), 'default'); + $this->assertEqual($this->columns['col_text']->isPrimaryKey(), false); + $this->assertEqual($this->columns['col_text']->getType(), 'TEXT'); + $this->assertEqual($this->columns['col_text']->getName(), 'col_text'); + } + public function testBlobType() { + $this->assertEqual($this->columns['col_blob']->isUnique(), false); + $this->assertEqual($this->columns['col_blob']->isNotNull(), false); + $this->assertEqual($this->columns['col_blob']->defaultValue(), null); + $this->assertEqual($this->columns['col_blob']->isPrimaryKey(), false); + $this->assertEqual($this->columns['col_blob']->getType(), 'BLOB'); + $this->assertEqual($this->columns['col_blob']->getName(), 'col_blob'); + } + public function testRealType() { + $this->assertEqual($this->columns['col_real']->isUnique(), false); + $this->assertEqual($this->columns['col_real']->isNotNull(), false); + $this->assertEqual($this->columns['col_real']->defaultValue(), null); + $this->assertEqual($this->columns['col_real']->isPrimaryKey(), false); + $this->assertEqual($this->columns['col_real']->getType(), 'REAL'); + $this->assertEqual($this->columns['col_real']->getName(), 'col_real'); + } +} +?>