From 09a53170a309677b344b25d9820ef038a0e87929 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 15 Nov 2011 15:58:12 +0100 Subject: [PATCH] fixes in the sqlite manager driver --- lib/MDB2/Driver/Manager/sqlite3.php | 72 ++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/lib/MDB2/Driver/Manager/sqlite3.php b/lib/MDB2/Driver/Manager/sqlite3.php index 71b9597d202..a6fde69d6c4 100644 --- a/lib/MDB2/Driver/Manager/sqlite3.php +++ b/lib/MDB2/Driver/Manager/sqlite3.php @@ -531,9 +531,26 @@ class MDB2_Driver_Manager_sqlite3 extends MDB2_Driver_Manager_Common return MDB2_OK; } + if (empty($changes['remove']) and empty($changes['rename']) and empty($changes['change']) ){//if only rename or add changes are required, we can use ALTER TABLE + $query = ''; + if (!empty($changes['name'])) { + $change_name = $db->quoteIdentifier($changes['name'], true); + $query = 'RENAME TO ' . $change_name; + $db->exec("ALTER TABLE $name $query"); + } + + if (!empty($changes['add']) && is_array($changes['add'])) { + foreach ($changes['add'] as $field_name => $field) { + $query= 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field); + $db->exec("ALTER TABLE $name $query"); + } + } + return MDB2_OK; + } + $db->loadModule('Reverse', null, true); - // actually sqlite 2.x supports no ALTER TABLE at all .. so we emulate it + // for other operations we need to emulate them with sqlite3 $fields = $db->manager->listTableFields($name); if (PEAR::isError($fields)) { return $fields; @@ -633,16 +650,10 @@ class MDB2_Driver_Manager_sqlite3 extends MDB2_Driver_Manager_Common } } + //rename the old table so we can create the new one + $db->exec("ALTER TABLE $name RENAME TO __$name"); $data = null; - if (!empty($select_fields)) { - $query = 'SELECT '.implode(', ', $select_fields).' FROM '.$db->quoteIdentifier($name, true); - $data = $db->queryAll($query, null, MDB2_FETCHMODE_ORDERED); - } - $result = $this->dropTable($name); - if (PEAR::isError($result)) { - return $result; - } $result = $this->createTable($name_new, $fields, $options); if (PEAR::isError($result)) { @@ -657,20 +668,35 @@ class MDB2_Driver_Manager_sqlite3 extends MDB2_Driver_Manager_Common $this->createConstraint($name_new, $constraint, $definition); } - if (!empty($select_fields) && !empty($data)) { - $query = 'INSERT INTO '.$db->quoteIdentifier($name_new, true); - $query.= '('.implode(', ', array_slice(array_keys($fields), 0, count($select_fields))).')'; - $query.=' VALUES (?'.str_repeat(', ?', (count($select_fields) - 1)).')'; - $stmt =$db->prepare($query, null, MDB2_PREPARE_MANIP); - if (PEAR::isError($stmt)) { - return $stmt; - } - foreach ($data as $row) { - $result = $stmt->execute($row); - if (PEAR::isError($result)) { - return $result; - } - } + //fill the new table with data from the old one + if (!empty($select_fields)) { + $query = 'INSERT INTO '.$db->quoteIdentifier($name_new, true); + $query.= '('.implode(', ', array_slice(array_keys($fields), 0, count($select_fields))).')'; + $query .= ' SELECT '.implode(', ', $select_fields).' FROM '.$db->quoteIdentifier('__'.$name, true); + $db->exec($query); + } + +// if (!empty($select_fields) && !empty($data)) { +// $query = 'INSERT INTO '.$db->quoteIdentifier($name_new, true); +// $query.= '('.implode(', ', array_slice(array_keys($fields), 0, count($select_fields))).')'; +// $query.=' VALUES (?'.str_repeat(', ?', (count($select_fields) - 1)).')'; +// $stmt =$db->prepare($query, null, MDB2_PREPARE_MANIP); +// if (PEAR::isError($stmt)) { +// return $stmt; +// } +// foreach ($data as $row) { +// $result = $stmt->execute($row); +// if (PEAR::isError($result)) { +// return $result; +// } +// } +// } + echo "changes $name"; + + //remove the old table + $result = $this->dropTable('__'.$name); + if (PEAR::isError($result)) { + return $result; } return MDB2_OK; } -- 2.39.5