瀏覽代碼

Update DB\Sqlite

Visman 3 年之前
父節點
當前提交
bfecbd573c
共有 1 個文件被更改,包括 29 次插入26 次删除
  1. 29 26
      app/Core/DB/Sqlite.php

+ 29 - 26
app/Core/DB/Sqlite.php

@@ -335,6 +335,33 @@ class Sqlite
         return false !== $this->db->exec($createQuery) ? $tmpTable : null;
     }
 
+    /**
+     * Пересоздает таблицу из временной с помощью insert запроса
+     */
+    protected function tmpToTable(array $schema, string $insertQuery): bool
+    {
+        if (! \preg_match('%^INSERT INTO "(.*?)".+FROM "(.*?)"%s', $insertQuery, $matches)) {
+            return false;
+        }
+
+        $tmpTable = $matches[1];
+        $table    = $matches[2];
+
+        $result = false !== $this->db->exec($insertQuery);
+        $result = $result && $this->dropTable($table);
+        $result = $result && $this->renameTable($tmpTable, $table);
+
+        foreach ($schema as $key => $query) {
+            if ('TABLE' === $key) {
+                continue;
+            }
+
+            $result = $result && false !== $this->db->exec($query);
+        }
+
+        return $result;
+    }
+
     /**
      * Проверяет наличие таблицы в базе
      */
@@ -532,19 +559,7 @@ class Sqlite
             SELECT {$tmp}
             FROM \"{$table}\"";
 
-        $result = $result && false !== $this->db->exec($query);
-        $result = $result && $this->dropTable($table);
-        $result = $result && $this->renameTable($tmpTable, $table);
-
-        foreach ($schema as $key => $query) {
-            if ('TABLE' === $key) {
-                continue;
-            }
-
-            $result = $result && false !== $this->db->exec($query);
-        }
-
-        return $result;
+        return $result && $this->tmpToTable($schema, $query);
     }
 
     /**
@@ -591,19 +606,7 @@ class Sqlite
                 SELECT {$tmp}
                 FROM \"{$table}\"";
 
-            $result = $result && false !== $this->db->exec($query);
-            $result = $result && $this->dropTable($table);
-            $result = $result && $this->renameTable($tmpTable, $table);
-
-            foreach ($schema as $key => $query) {
-                if ('TABLE' === $key) {
-                    continue;
-                }
-
-                $result = $result && false !== $this->db->exec($query);
-            }
-
-            return $result;
+            return $result && $this->tmpToTable($schema, $query);
         } else {
             $index  = $table . '_' . $index;