fixes in the sqlite manager driver

This commit is contained in:
Robin Appelman 2011-11-15 15:58:12 +01:00
parent ecf6f2ca2f
commit 09a53170a3
1 changed files with 49 additions and 23 deletions

View File

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