From 5ed9eeffd7027d2a2cb08c90a8056fd15db6997e Mon Sep 17 00:00:00 2001 From: zYne Date: Tue, 24 Oct 2006 08:56:47 +0000 Subject: [PATCH] Import builder now supports default values and notnull constraints, added missing test case (Doctrine_Query_Update_TestCase), fixes #189 Ticket: 189 --- lib/Doctrine/DataDict/Sqlite.php | 10 ++++-- lib/Doctrine/Import/Builder.php | 16 +++++++++ lib/Doctrine/Schema/Column.php | 2 +- tests/ImportTestCase.php | 61 +++++++++++++++++++++++++++----- tests/QueryUpdateTestCase.php | 17 +++++++++ 5 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 tests/QueryUpdateTestCase.php diff --git a/lib/Doctrine/DataDict/Sqlite.php b/lib/Doctrine/DataDict/Sqlite.php index 03ab19418..d9798228d 100644 --- a/lib/Doctrine/DataDict/Sqlite.php +++ b/lib/Doctrine/DataDict/Sqlite.php @@ -280,7 +280,7 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict { $description = array(); $columns = array(); - foreach ($result as $key => $val) { + foreach($result as $key => $val) { $description = array( 'name' => $val['name'], 'type' => $val['type'], @@ -299,7 +299,13 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict { * @return array */ public function listTableIndexes($table) { - + $sql = 'PRAGMA index_list(' . $table . ')'; + $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + + $indexes = array(); + foreach($result as $key => $val) { + + } } /** * lists tables diff --git a/lib/Doctrine/Import/Builder.php b/lib/Doctrine/Import/Builder.php index f0552f5b7..41236a74c 100644 --- a/lib/Doctrine/Import/Builder.php +++ b/lib/Doctrine/Import/Builder.php @@ -79,6 +79,7 @@ class Doctrine_Import_Builder { return $this->suffix; } + public function buildRecord(Doctrine_Schema_Table $table) { if (empty($this->path)) throw new Doctrine_Import_Builder_Exception('No build target directory set.'); @@ -92,11 +93,26 @@ class Doctrine_Import_Builder { $columns = array(); $i = 0; + foreach($table as $name => $column) { $columns[$i] = ' $this->hasColumn(\'' . $column['name'] . '\', \'' . $column['type'] . '\''; if($column['length']) $columns[$i] .= ', ' . $column['length']; + else + $columns[$i] .= ', null'; + + $a = array(); + + if($column['default']) { + $a[] = '\'default\' => ' . var_export($column['default'], true); + } + if($column['notnull']) { + $a[] = '\'notnull\' => true'; + } + if( ! empty($a)) + $columns[$i] .= ', ' . 'array(' . implode(', +', $a) . ')'; $columns[$i] .= ');'; diff --git a/lib/Doctrine/Schema/Column.php b/lib/Doctrine/Schema/Column.php index 699627d8f..6ba8c7e1f 100644 --- a/lib/Doctrine/Schema/Column.php +++ b/lib/Doctrine/Schema/Column.php @@ -40,7 +40,7 @@ class Doctrine_Schema_Column extends Doctrine_Schema_Object implements IteratorA */ protected $definition = array('name' => '', 'type' => '', - 'length' => 0, + 'length' => null, 'unique' => false, 'primary' => false, 'notnull' => false, diff --git a/tests/ImportTestCase.php b/tests/ImportTestCase.php index e55879324..2db505a4a 100644 --- a/tests/ImportTestCase.php +++ b/tests/ImportTestCase.php @@ -9,14 +9,16 @@ * @version $Id$ * @package Doctrine */ -class Doctrine_Import_TestCase extends Doctrine_UnitTestCase -{ +class Doctrine_Import_TestCase extends Doctrine_UnitTestCase { private $tmpdir; private $suffix; private $schema; + public function prepareTables() { } + public function prepareData() { } + public function setUp() { parent::setUp(); @@ -41,17 +43,19 @@ class Doctrine_Import_TestCase extends Doctrine_UnitTestCase } public function testImportTable() { - $definition = array('name' => 'user'); + $definition = array('name' => 'import_test'); $table = new Doctrine_Schema_Table($definition); $def = array('name' => 'name', 'type' => 'string', - 'length' => 20); + 'length' => 20, + 'default' => 'someone'); $table->addColumn(new Doctrine_Schema_Column($def)); - + $def = array('name' => 'created', - 'type' => 'integer'); + 'type' => 'integer', + 'notnull' => true); $table->addColumn(new Doctrine_Schema_Column($def)); @@ -65,9 +69,50 @@ class Doctrine_Import_TestCase extends Doctrine_UnitTestCase } catch(Doctrine_Import_Builder_Exception $e) { $this->fail(); } - - unlink('tmp' . DIRECTORY_SEPARATOR . 'User.php'); + $this->assertTrue(file_exists('tmp' . DIRECTORY_SEPARATOR . 'ImportTest.php')); } + public function testImportedComponent() { + require_once('tmp' . DIRECTORY_SEPARATOR . 'ImportTest.php'); + + $r = new ImportTest(); + $columns = $r->getTable()->getColumns(); + + // id column is auto-created + + $this->assertEqual($columns['id'][0], 'integer'); + $this->assertEqual($columns['id'][1], 20); + + $this->assertEqual($columns['name'][0], 'string'); + $this->assertEqual($columns['name'][1], 20); + + $this->assertEqual($columns['created'][0], 'integer'); + + unlink('tmp' . DIRECTORY_SEPARATOR . 'ImportTest.php'); + } + public function testForeignKeySupport() { + $this->dbh->query('CREATE TABLE album ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + title VARCHAR(100), + artist VARCHAR(100) + )'); + + $this->dbh->query('CREATE TABLE track ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + album_id INTEGER, + dsk INTEGER, + posn INTEGER, + song VARCHAR(255), + FOREIGN KEY (album_id) REFERENCES album(id) + )'); + + + $sql = "PRAGMA table_info(track)"; + $sql = "PRAGMA foreign_key_list(track)"; + $result = $this->dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC); + + + } + /** public function testDatabaseConnectionIsReverseEngineeredToSchema() { diff --git a/tests/QueryUpdateTestCase.php b/tests/QueryUpdateTestCase.php new file mode 100644 index 000000000..611208b0c --- /dev/null +++ b/tests/QueryUpdateTestCase.php @@ -0,0 +1,17 @@ +parseQuery("UPDATE User u SET u.name = 'someone'"); + + $this->assertEqual($q->getQuery(), "UPDATE entity SET entity.name = 'someone' WHERE (entity.type = 0)"); + + $q = new Doctrine_Query(); + + $q->update('User u')->set('u.name', 'someone'); + + $this->assertEqual($q->getQuery(), "UPDATE entity SET entity.name = 'someone' WHERE (entity.type = 0)"); + } +} +?>