]> source.dussan.org Git - nextcloud-server.git/commitdiff
fixes in the sqlite manager driver
authorRobin Appelman <icewind1991@gmail.com>
Tue, 15 Nov 2011 14:58:12 +0000 (15:58 +0100)
committerRobin Appelman <icewind1991@gmail.com>
Sun, 8 Jan 2012 00:57:53 +0000 (01:57 +0100)
lib/MDB2/Driver/Manager/sqlite3.php

index 71b9597d202428c8d2997be85575ad3f88852aeb..a6fde69d6c4352a8078c537969f5d2e96f58bfac 100644 (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;
     }