1
0
mirror of synced 2024-12-13 14:56:01 +03:00

first draft for sqlite cascading delete emulation with triggers

This commit is contained in:
zYne 2007-03-22 13:32:43 +00:00
parent 306dd80312
commit 759c23f490
3 changed files with 80 additions and 7 deletions

View File

@ -159,14 +159,20 @@ class Doctrine_Export_Sqlite extends Doctrine_Export
}
// sqlite doesn't support foreign key declaration but it parses those anyway
$fk = array();
if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) {
foreach($options['foreignKeys'] as $definition) {
$queryFields .= ', ' . $this->getForeignKeyDeclaration($definition);
foreach ($options['foreignKeys'] as $definition) {
//$queryFields .= ', ' . $this->getForeignKeyDeclaration($definition);
if (isset($definition['onDelete'])) {
$fk[] = $definition;
}
}
}
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach($options['indexes'] as $index => $definition) {
foreach ($options['indexes'] as $index => $definition) {
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
}
}
@ -174,7 +180,38 @@ class Doctrine_Export_Sqlite extends Doctrine_Export
$name = $this->conn->quoteIdentifier($name, true);
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
return $this->conn->exec($query);
try {
if ( ! empty($fk)) {
$this->conn->beginTransaction();
}
$ret = $this->conn->exec($query);
if ( ! empty($fk)) {
foreach ($fk as $definition) {
$query = 'CREATE TRIGGER doctrine_' . $name . '_cscd_delete '
. 'AFTER DELETE ON ' . $name . ' FOR EACH STATEMENT '
. 'BEGIN '
. 'DELETE FROM ' . $definition['foreignTable'] . ' WHERE ';
$local = (array) $definition['local'];
foreach((array) $definition['foreign'] as $k => $field) {
$query .= $field . ' = old.' . $local[$k] . ';';
}
$query .= 'END;';
$this->conn->exec($query);
}
$this->conn->commit();
}
} catch(Doctrine_Exception $e) {
$this->conn->rollback();
throw $e;
}
}
/**
* create sequence

View File

@ -1121,9 +1121,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
if ( ! is_array($key)) {
$key = array($key);
}
foreach ($key as $k) {
if ( ! isset($this->data[$k])) {
throw new Doctrine_Exception("Primary key value for $k wasn't found");
throw new Doctrine_Table_Exception("Primary key value for $k wasn't found");
}
$id[] = $this->data[$k];
}

View File

@ -1,4 +1,35 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Export_Sqlite_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Export_Sqlite_TestCase extends Doctrine_UnitTestCase {
public function testCreateDatabaseDoesNotExecuteSql() {
try {
@ -91,11 +122,15 @@ 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))');
}
public function testExportSupportsForeignKeys()
public function testExportSupportsEmulationOfCascadingDeletes()
{
$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)');
$this->assertEqual($this->adapter->pop(), 'COMMIT');
$this->assertEqual($this->adapter->pop(), 'CREATE TRIGGER doctrine_foreign_key_test_cscd_delete AFTER DELETE ON foreign_key_test BEGIN DELETE FROM foreign_key_test WHERE parent_id = old.id;END;');
$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)');
$this->assertEqual($this->adapter->pop(), 'BEGIN TRANSACTION');
}
}
?>