Commit 5ed9eeff authored by zYne's avatar zYne

Import builder now supports default values and notnull constraints, added...

Import builder now supports default values and notnull constraints, added missing test case (Doctrine_Query_Update_TestCase), fixes #189
Ticket: 189
parent daaab94c
...@@ -280,7 +280,7 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict { ...@@ -280,7 +280,7 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict {
$description = array(); $description = array();
$columns = array(); $columns = array();
foreach ($result as $key => $val) { foreach($result as $key => $val) {
$description = array( $description = array(
'name' => $val['name'], 'name' => $val['name'],
'type' => $val['type'], 'type' => $val['type'],
...@@ -299,7 +299,13 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict { ...@@ -299,7 +299,13 @@ class Doctrine_DataDict_Sqlite extends Doctrine_DataDict {
* @return array * @return array
*/ */
public function listTableIndexes($table) { 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 * lists tables
......
...@@ -79,6 +79,7 @@ class Doctrine_Import_Builder { ...@@ -79,6 +79,7 @@ class Doctrine_Import_Builder {
return $this->suffix; return $this->suffix;
} }
public function buildRecord(Doctrine_Schema_Table $table) { public function buildRecord(Doctrine_Schema_Table $table) {
if (empty($this->path)) if (empty($this->path))
throw new Doctrine_Import_Builder_Exception('No build target directory set.'); throw new Doctrine_Import_Builder_Exception('No build target directory set.');
...@@ -92,11 +93,26 @@ class Doctrine_Import_Builder { ...@@ -92,11 +93,26 @@ class Doctrine_Import_Builder {
$columns = array(); $columns = array();
$i = 0; $i = 0;
foreach($table as $name => $column) { foreach($table as $name => $column) {
$columns[$i] = ' $this->hasColumn(\'' . $column['name'] . '\', \'' . $column['type'] . '\''; $columns[$i] = ' $this->hasColumn(\'' . $column['name'] . '\', \'' . $column['type'] . '\'';
if($column['length']) if($column['length'])
$columns[$i] .= ', ' . $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] .= ');'; $columns[$i] .= ');';
......
...@@ -40,7 +40,7 @@ class Doctrine_Schema_Column extends Doctrine_Schema_Object implements IteratorA ...@@ -40,7 +40,7 @@ class Doctrine_Schema_Column extends Doctrine_Schema_Object implements IteratorA
*/ */
protected $definition = array('name' => '', protected $definition = array('name' => '',
'type' => '', 'type' => '',
'length' => 0, 'length' => null,
'unique' => false, 'unique' => false,
'primary' => false, 'primary' => false,
'notnull' => false, 'notnull' => false,
......
...@@ -9,14 +9,16 @@ ...@@ -9,14 +9,16 @@
* @version $Id$ * @version $Id$
* @package Doctrine * @package Doctrine
*/ */
class Doctrine_Import_TestCase extends Doctrine_UnitTestCase class Doctrine_Import_TestCase extends Doctrine_UnitTestCase {
{
private $tmpdir; private $tmpdir;
private $suffix; private $suffix;
private $schema; private $schema;
public function prepareTables() { }
public function prepareData() { }
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
...@@ -41,17 +43,19 @@ class Doctrine_Import_TestCase extends Doctrine_UnitTestCase ...@@ -41,17 +43,19 @@ class Doctrine_Import_TestCase extends Doctrine_UnitTestCase
} }
public function testImportTable() { public function testImportTable() {
$definition = array('name' => 'user'); $definition = array('name' => 'import_test');
$table = new Doctrine_Schema_Table($definition); $table = new Doctrine_Schema_Table($definition);
$def = array('name' => 'name', $def = array('name' => 'name',
'type' => 'string', 'type' => 'string',
'length' => 20); 'length' => 20,
'default' => 'someone');
$table->addColumn(new Doctrine_Schema_Column($def)); $table->addColumn(new Doctrine_Schema_Column($def));
$def = array('name' => 'created', $def = array('name' => 'created',
'type' => 'integer'); 'type' => 'integer',
'notnull' => true);
$table->addColumn(new Doctrine_Schema_Column($def)); $table->addColumn(new Doctrine_Schema_Column($def));
...@@ -65,9 +69,50 @@ class Doctrine_Import_TestCase extends Doctrine_UnitTestCase ...@@ -65,9 +69,50 @@ class Doctrine_Import_TestCase extends Doctrine_UnitTestCase
} catch(Doctrine_Import_Builder_Exception $e) { } catch(Doctrine_Import_Builder_Exception $e) {
$this->fail(); $this->fail();
} }
$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);
unlink('tmp' . DIRECTORY_SEPARATOR . 'User.php');
} }
/** /**
public function testDatabaseConnectionIsReverseEngineeredToSchema() public function testDatabaseConnectionIsReverseEngineeredToSchema()
{ {
......
<?php
class Doctrine_Query_Update_TestCase extends Doctrine_UnitTestCase {
public function testUpdateAllWithColumnAggregationInheritance() {
$q = new Doctrine_Query();
$q->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)");
}
}
?>
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