1
0
mirror of synced 2024-12-13 22:56:04 +03:00

sqlite foreign key support

This commit is contained in:
zYne 2007-03-22 12:47:37 +00:00
parent 8428338c21
commit 306dd80312
4 changed files with 32 additions and 12 deletions

View File

@ -144,9 +144,16 @@ class Doctrine_Export extends Doctrine_Connection_Module
} }
$queryFields = $this->getFieldDeclarationList($fields); $queryFields = $this->getFieldDeclarationList($fields);
if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) {
foreach($options['foreignKeys'] as $definition) {
$queryFields .= ', ' . $this->getForeignKeyDeclaration($definition);
}
}
if (isset($options['primary']) && ! empty($options['primary'])) { if (isset($options['primary']) && ! empty($options['primary'])) {
$queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')'; $queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
} }
if (isset($options['indexes']) && ! empty($options['indexes'])) { if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach($options['indexes'] as $index => $definition) { foreach($options['indexes'] as $index => $definition) {
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition); $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);

View File

@ -158,6 +158,13 @@ class Doctrine_Export_Sqlite extends Doctrine_Export
$queryFields.= ', PRIMARY KEY('.implode(', ', array_values($options['primary'])).')'; $queryFields.= ', PRIMARY KEY('.implode(', ', array_values($options['primary'])).')';
} }
// sqlite doesn't support foreign key declaration but it parses those anyway
if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) {
foreach($options['foreignKeys'] as $definition) {
$queryFields .= ', ' . $this->getForeignKeyDeclaration($definition);
}
}
if (isset($options['indexes']) && ! empty($options['indexes'])) { if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach($options['indexes'] as $index => $definition) { foreach($options['indexes'] as $index => $definition) {
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition); $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);

View File

@ -263,16 +263,16 @@ class Doctrine_Export_Mysql_TestCase extends Doctrine_UnitTestCase
public function testExportSupportsForeignKeys() public function testExportSupportsForeignKeys()
{ {
$r = new MysqlForeignKeyTest; $r = new ForeignKeyTest;
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mysql_foreign_key_test (id BIGINT AUTO_INCREMENT, name TEXT, code INT, content TEXT, parent_id BIGINT, FOREIGN KEY parent_id REFERENCES mysql_foreign_key_test(id), FOREIGN KEY id REFERENCES mysql_foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE, PRIMARY KEY(id)) ENGINE = INNODB'); $this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test (id BIGINT AUTO_INCREMENT, name TEXT, code INT, content TEXT, parent_id BIGINT, FOREIGN KEY parent_id REFERENCES foreign_key_test(id), FOREIGN KEY id REFERENCES foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE, PRIMARY KEY(id)) ENGINE = INNODB');
} }
public function testExportSupportsForeignKeysWithoutAttributes() public function testExportSupportsForeignKeysWithoutAttributes()
{ {
$r = new MysqlForeignKeyTest2; $r = new ForeignKeyTest2;
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE mysql_foreign_key_test2 (id BIGINT AUTO_INCREMENT, name TEXT, foreignkey BIGINT, FOREIGN KEY foreignkey REFERENCES mysql_foreign_key_test(id), PRIMARY KEY(id)) ENGINE = INNODB'); $this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test2 (id BIGINT AUTO_INCREMENT, name TEXT, foreignkey BIGINT, FOREIGN KEY foreignkey REFERENCES foreign_key_test(id), PRIMARY KEY(id)) ENGINE = INNODB');
} }
public function testExportSupportsForeignKeysForManyToManyRelations() public function testExportSupportsForeignKeysForManyToManyRelations()
@ -290,7 +290,7 @@ class Doctrine_Export_Mysql_TestCase extends Doctrine_UnitTestCase
} }
} }
class MysqlForeignKeyTest extends Doctrine_Record class ForeignKeyTest extends Doctrine_Record
{ {
public function setTableDefinition() public function setTableDefinition()
{ {
@ -299,12 +299,12 @@ class MysqlForeignKeyTest extends Doctrine_Record
$this->hasColumn('content', 'string', 4000); $this->hasColumn('content', 'string', 4000);
$this->hasColumn('parent_id', 'integer'); $this->hasColumn('parent_id', 'integer');
$this->hasOne('MysqlForeignKeyTest as Parent', $this->hasOne('ForeignKeyTest as Parent',
'MysqlForeignKeyTest.parent_id' 'ForeignKeyTest.parent_id'
); );
$this->hasMany('MysqlForeignKeyTest as Children', $this->hasMany('ForeignKeyTest as Children',
'MysqlForeignKeyTest.parent_id', 'ForeignKeyTest.parent_id',
array('onDelete' => 'CASCADE', array('onDelete' => 'CASCADE',
'onUpdate' => 'RESTRICT') 'onUpdate' => 'RESTRICT')
); );
@ -339,14 +339,14 @@ class MysqlGroup extends Doctrine_Record
$this->hasMany('MysqlUser', 'MysqlGroupMember.user_id'); $this->hasMany('MysqlUser', 'MysqlGroupMember.user_id');
} }
} }
class MysqlForeignKeyTest2 extends Doctrine_Record class ForeignKeyTest2 extends Doctrine_Record
{ {
public function setTableDefinition() public function setTableDefinition()
{ {
$this->hasColumn('name', 'string', null); $this->hasColumn('name', 'string', null);
$this->hasColumn('foreignkey', 'integer'); $this->hasColumn('foreignkey', 'integer');
$this->hasOne('MysqlForeignKeyTest', 'MysqlForeignKeyTest2.foreignkey'); $this->hasOne('ForeignKeyTest', 'ForeignKeyTest2.foreignkey');
} }
} }
class MysqlIndexTestRecord extends Doctrine_Record class MysqlIndexTestRecord extends Doctrine_Record

View File

@ -91,5 +91,11 @@ class Doctrine_Export_Sqlite_TestCase extends Doctrine_UnitTestCase {
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE sometable (id INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT, name VARCHAR(4), INDEX myindex (id ASC, name DESC))'); $this->assertEqual($this->adapter->pop(), 'CREATE TABLE sometable (id INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT, name VARCHAR(4), INDEX myindex (id ASC, name DESC))');
} }
public function testExportSupportsForeignKeys()
{
$r = new ForeignKeyTest;
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(2147483647), code INTEGER, content VARCHAR(4000), parent_id INTEGER, FOREIGN KEY parent_id REFERENCES foreign_key_test(id), FOREIGN KEY id REFERENCES foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE)');
}
} }
?> ?>