1
0
mirror of synced 2025-01-17 22:11:41 +03:00

Import builder now supports default values and notnull constraints, added missing test case (Doctrine_Query_Update_TestCase), fixes #189

Ticket: 189
This commit is contained in:
zYne 2006-10-24 08:56:47 +00:00
parent daaab94cfe
commit 5ed9eeffd7
5 changed files with 95 additions and 11 deletions

View File

@ -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

View File

@ -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] .= ');';

View File

@ -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,

View File

@ -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()
{

View File

@ -0,0 +1,17 @@
<?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)");
}
}
?>