Browse Source

sqlite.. work in progress

Maurits van der Schee 5 years ago
parent
commit
6d0f5eb3af

+ 92
- 29
api.php View File

@@ -4006,8 +4006,31 @@ namespace Tqdev\PhpCrudApi\Column {
4006 4006
             return true;
4007 4007
         }
4008 4008
 
4009
+        public function updateColumnSqlite(string $tableName, string $columnName, /* object */ $changes): bool
4010
+        {
4011
+            $table = $this->reflection->getTable($tableName);
4012
+            $column = $table->getColumn($columnName);
4013
+
4014
+            // remove constraints on other column
4015
+            $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
4016
+            $columns = [];
4017
+            foreach ($table->getColumnNames() as $name) {
4018
+                if ($name == $columnName) {
4019
+                    $columns[] = $newColumn;
4020
+                } else {
4021
+                    $columns[] = $table->getColumn($name);
4022
+                }
4023
+            }
4024
+            $newTable = new ReflectedTable($table->getName(), $table->getType(), $columns);
4025
+            return $this->db->definition()->updateColumnsSqlite($newTable);
4026
+        }
4027
+
4009 4028
         public function updateColumn(string $tableName, string $columnName, /* object */ $changes): bool
4010 4029
         {
4030
+            if ($this->db->getDriver() == 'sqlite') {
4031
+                return $this->updateColumnSqlite($tableName, $columnName, $changes);
4032
+            }
4033
+
4011 4034
             $table = $this->reflection->getTable($tableName);
4012 4035
             $column = $table->getColumn($columnName);
4013 4036
 
@@ -4045,7 +4068,8 @@ namespace Tqdev\PhpCrudApi\Column {
4045 4068
                     return false;
4046 4069
                 }
4047 4070
             }
4048
-            if ($newColumn->getType() != $column->getType() ||
4071
+            if (
4072
+                $newColumn->getType() != $column->getType() ||
4049 4073
                 $newColumn->getLength() != $column->getLength() ||
4050 4074
                 $newColumn->getPrecision() != $column->getPrecision() ||
4051 4075
                 $newColumn->getScale() != $column->getScale()
@@ -5270,6 +5294,11 @@ namespace Tqdev\PhpCrudApi\Database {
5270 5294
         private $columns;
5271 5295
         private $converter;
5272 5296
 
5297
+        public function getDriver(): string
5298
+        {
5299
+            return $this->driver;
5300
+        }
5301
+
5273 5302
         private function getDsn(): string
5274 5303
         {
5275 5304
             switch ($this->driver) {
@@ -5302,6 +5331,7 @@ namespace Tqdev\PhpCrudApi\Database {
5302 5331
                 case 'sqlite':
5303 5332
                     return [
5304 5333
                         'PRAGMA foreign_keys = on;',
5334
+                        'PRAGMA writable_schema = on;',
5305 5335
                     ];
5306 5336
             }
5307 5337
         }
@@ -5661,6 +5691,8 @@ namespace Tqdev\PhpCrudApi\Database {
5661 5691
                 case 'pgsql':
5662 5692
                 case 'sqlsrv':
5663 5693
                     return '';
5694
+                case 'sqlite':
5695
+                    return $column->getPk() ? ' AUTOINCREMENT' : '';
5664 5696
             }
5665 5697
         }
5666 5698
 
@@ -5684,6 +5716,8 @@ namespace Tqdev\PhpCrudApi\Database {
5684 5716
                     return "ALTER TABLE $p1 RENAME TO $p2";
5685 5717
                 case 'sqlsrv':
5686 5718
                     return "EXEC sp_rename $p1, $p2";
5719
+                case 'sqlite':
5720
+                    return "ALTER TABLE $p1 RENAME TO $p2";
5687 5721
             }
5688 5722
         }
5689 5723
 
@@ -5702,6 +5736,8 @@ namespace Tqdev\PhpCrudApi\Database {
5702 5736
                 case 'sqlsrv':
5703 5737
                     $p4 = $this->quote($tableName . '.' . $columnName);
5704 5738
                     return "EXEC sp_rename $p4, $p3, 'COLUMN'";
5739
+                case 'sqlite':
5740
+                    return "ALTER TABLE $p1 RENAME COLUMN $p2 TO $p3";
5705 5741
             }
5706 5742
         }
5707 5743
 
@@ -5860,12 +5896,22 @@ namespace Tqdev\PhpCrudApi\Database {
5860 5896
                 $f4 = $this->quote($newColumn->getFk());
5861 5897
                 $f5 = $this->quote($this->getPrimaryKey($newColumn->getFk()));
5862 5898
                 $f6 = $this->quote($tableName . '_' . $pkColumn . '_pkey');
5863
-                $fields[] = "$f1 $f2";
5864
-                if ($newColumn->getPk()) {
5865
-                    $constraints[] = "CONSTRAINT $f6 PRIMARY KEY ($f1)";
5866
-                }
5867
-                if ($newColumn->getFk()) {
5868
-                    $constraints[] = "CONSTRAINT $f3 FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
5899
+                if ($this->driver == 'sqlite') {
5900
+                    if ($newColumn->getPk()) {
5901
+                        $f2 = str_replace('NULL', 'NULL PRIMARY KEY', $f2);
5902
+                    }
5903
+                    $fields[] = "$f1 $f2";
5904
+                    if ($newColumn->getFk()) {
5905
+                        $constraints[] = "FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
5906
+                    }
5907
+                } else {
5908
+                    $fields[] = "$f1 $f2";
5909
+                    if ($newColumn->getPk()) {
5910
+                        $constraints[] = "CONSTRAINT $f6 PRIMARY KEY ($f1)";
5911
+                    }
5912
+                    if ($newColumn->getFk()) {
5913
+                        $constraints[] = "CONSTRAINT $f3 FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
5914
+                    }
5869 5915
                 }
5870 5916
             }
5871 5917
             $p2 = implode(',', array_merge($fields, $constraints));
@@ -5885,6 +5931,8 @@ namespace Tqdev\PhpCrudApi\Database {
5885 5931
                     return "ALTER TABLE $p1 ADD COLUMN $p2 $p3";
5886 5932
                 case 'sqlsrv':
5887 5933
                     return "ALTER TABLE $p1 ADD $p2 $p3";
5934
+                case 'sqlite':
5935
+                    return "ALTER TABLE $p1 ADD COLUMN $p2 $p3";
5888 5936
             }
5889 5937
         }
5890 5938
 
@@ -5898,6 +5946,8 @@ namespace Tqdev\PhpCrudApi\Database {
5898 5946
                     return "DROP TABLE $p1 CASCADE;";
5899 5947
                 case 'sqlsrv':
5900 5948
                     return "DROP TABLE $p1;";
5949
+                case 'sqlite':
5950
+                    return "DROP TABLE $p1;";
5901 5951
             }
5902 5952
         }
5903 5953
 
@@ -5912,44 +5962,56 @@ namespace Tqdev\PhpCrudApi\Database {
5912 5962
                     return "ALTER TABLE $p1 DROP COLUMN $p2 CASCADE;";
5913 5963
                 case 'sqlsrv':
5914 5964
                     return "ALTER TABLE $p1 DROP COLUMN $p2;";
5965
+                case 'sqlite':
5966
+                    return "ALTER TABLE $p1 DROP COLUMN $p2;";
5915 5967
             }
5916 5968
         }
5917 5969
 
5918 5970
         public function renameTable(string $tableName, string $newTableName)
5919 5971
         {
5920 5972
             $sql = $this->getTableRenameSQL($tableName, $newTableName);
5921
-            return $this->query($sql);
5973
+            return $this->query($sql, []);
5922 5974
         }
5923 5975
 
5924 5976
         public function renameColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
5925 5977
         {
5926 5978
             $sql = $this->getColumnRenameSQL($tableName, $columnName, $newColumn);
5927
-            return $this->query($sql);
5979
+            return $this->query($sql, []);
5980
+        }
5981
+
5982
+        public function updateColumnsSqlite(ReflectedTable $table)
5983
+        {
5984
+            $create = $this->getAddTableSQL($table);
5985
+            $name = $table->getName();
5986
+            $sql = "UPDATE SQLITE_MASTER SET SQL = ? WHERE NAME = ?;";
5987
+            $result = $this->query($sql, [$create, $name]);
5988
+            $this->query('VACUUM;', []);
5989
+            return $result;
5928 5990
         }
5929 5991
 
5930 5992
         public function retypeColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
5931 5993
         {
5932 5994
             $sql = $this->getColumnRetypeSQL($tableName, $columnName, $newColumn);
5933
-            return $this->query($sql);
5995
+            return $this->query($sql, []);
5934 5996
         }
5935 5997
 
5936 5998
         public function setColumnNullable(string $tableName, string $columnName, ReflectedColumn $newColumn)
5937 5999
         {
5938 6000
             $sql = $this->getSetColumnNullableSQL($tableName, $columnName, $newColumn);
5939
-            return $this->query($sql);
6001
+            return $this->query($sql, []);
5940 6002
         }
5941 6003
 
5942 6004
         public function addColumnPrimaryKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
5943 6005
         {
5944 6006
             $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
5945
-            $this->query($sql);
6007
+            $this->query($sql, []);
5946 6008
             if ($this->canAutoIncrement($newColumn)) {
5947 6009
                 $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
5948
-                $this->query($sql);
6010
+                $this->query($sql, []);
5949 6011
                 $sql = $this->getSetColumnPkSequenceStartSQL($tableName, $columnName, $newColumn);
5950
-                $this->query($sql);
6012
+                $this->query($sql, []);
5951 6013
                 $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
5952
-                $this->query($sql);
6014
+                $this->query($sql, []);
5953 6015
             }
5954 6016
             return true;
5955 6017
         }
@@ -5958,56 +6020,56 @@ namespace Tqdev\PhpCrudApi\Database {
5958 6020
         {
5959 6021
             if ($this->canAutoIncrement($newColumn)) {
5960 6022
                 $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
5961
-                $this->query($sql);
6023
+                $this->query($sql, []);
5962 6024
                 $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
5963
-                $this->query($sql);
6025
+                $this->query($sql, []);
5964 6026
             }
5965 6027
             $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
5966
-            $this->query($sql);
6028
+            $this->query($sql, []);
5967 6029
             return true;
5968 6030
         }
5969 6031
 
5970 6032
         public function addColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
5971 6033
         {
5972 6034
             $sql = $this->getAddColumnFkConstraintSQL($tableName, $columnName, $newColumn);
5973
-            return $this->query($sql);
6035
+            return $this->query($sql, []);
5974 6036
         }
5975 6037
 
5976 6038
         public function removeColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
5977 6039
         {
5978 6040
             $sql = $this->getRemoveColumnFkConstraintSQL($tableName, $columnName, $newColumn);
5979
-            return $this->query($sql);
6041
+            return $this->query($sql, []);
5980 6042
         }
5981 6043
 
5982 6044
         public function addTable(ReflectedTable $newTable)
5983 6045
         {
5984 6046
             $sql = $this->getAddTableSQL($newTable);
5985
-            return $this->query($sql);
6047
+            return $this->query($sql, []);
5986 6048
         }
5987 6049
 
5988 6050
         public function addColumn(string $tableName, ReflectedColumn $newColumn)
5989 6051
         {
5990 6052
             $sql = $this->getAddColumnSQL($tableName, $newColumn);
5991
-            return $this->query($sql);
6053
+            return $this->query($sql, []);
5992 6054
         }
5993 6055
 
5994 6056
         public function removeTable(string $tableName)
5995 6057
         {
5996 6058
             $sql = $this->getRemoveTableSQL($tableName);
5997
-            return $this->query($sql);
6059
+            return $this->query($sql, []);
5998 6060
         }
5999 6061
 
6000 6062
         public function removeColumn(string $tableName, string $columnName)
6001 6063
         {
6002 6064
             $sql = $this->getRemoveColumnSQL($tableName, $columnName);
6003
-            return $this->query($sql);
6065
+            return $this->query($sql, []);
6004 6066
         }
6005 6067
 
6006
-        private function query(string $sql): bool
6068
+        private function query(string $sql, array $arguments): bool
6007 6069
         {
6008 6070
             $stmt = $this->pdo->prepare($sql);
6009
-            //echo "- $sql -- []\n";
6010
-            return $stmt->execute();
6071
+            //echo "- $sql -- " . json_encode($arguments) . "\n";
6072
+            return $stmt->execute($arguments);
6011 6073
         }
6012 6074
     }
6013 6075
 }
@@ -6034,7 +6096,7 @@ namespace Tqdev\PhpCrudApi\Database {
6034 6096
             $this->typeConverter = new TypeConverter($driver);
6035 6097
         }
6036 6098
 
6037
-        private function createSqlLiteReflectionTables() /*: void */
6099
+        private function updateSqlLiteReflectionTables() /*: void */
6038 6100
         {
6039 6101
             $reflection = $this->query('SELECT "name" FROM "sqlite_master" WHERE "type" = \'table\' and name like \'sys/%\';', []);
6040 6102
             if (count($reflection) == 0) {
@@ -6103,7 +6165,7 @@ namespace Tqdev\PhpCrudApi\Database {
6103 6165
                 case 'sqlsrv':
6104 6166
                     return 'SELECT o.name as "TABLE_NAME", o.xtype as "TABLE_TYPE" FROM sysobjects o WHERE o.xtype IN (\'U\', \'V\') ORDER BY "TABLE_NAME"';
6105 6167
                 case 'sqlite':
6106
-                    $this->createSqlLiteReflectionTables();
6168
+                    $this->updateSqlLiteReflectionTables();
6107 6169
                     return 'SELECT t.name as "TABLE_NAME", t.type as "TABLE_TYPE" FROM "sys/tables" t WHERE t.type IN (\'table\', \'view\') AND \'\' <> ? ORDER BY "TABLE_NAME"';
6108 6170
             }
6109 6171
         }
@@ -6118,6 +6180,7 @@ namespace Tqdev\PhpCrudApi\Database {
6118 6180
                 case 'sqlsrv':
6119 6181
                     return 'SELECT c.name AS "COLUMN_NAME", c.is_nullable AS "IS_NULLABLE", t.Name AS "DATA_TYPE", (c.max_length/2) AS "CHARACTER_MAXIMUM_LENGTH", c.precision AS "NUMERIC_PRECISION", c.scale AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM sys.columns c INNER JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE c.object_id = OBJECT_ID(?) AND \'\' <> ?';
6120 6182
                 case 'sqlite':
6183
+                    $this->updateSqlLiteReflectionTables();
6121 6184
                     return 'SELECT "name" AS "COLUMN_NAME", case when "notnull"==1 then \'no\' else \'yes\' end as "IS_NULLABLE", "type" AS "DATA_TYPE", 2147483647 AS "CHARACTER_MAXIMUM_LENGTH", 0 AS "NUMERIC_PRECISION", 0 AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM "sys/columns" WHERE "self" = ? AND \'\' <> ?';
6122 6185
             }
6123 6186
         }

+ 25
- 1
src/Tqdev/PhpCrudApi/Column/DefinitionService.php View File

@@ -29,8 +29,31 @@ class DefinitionService
29 29
         return true;
30 30
     }
31 31
 
32
+    public function updateColumnSqlite(string $tableName, string $columnName, /* object */ $changes): bool
33
+    {
34
+        $table = $this->reflection->getTable($tableName);
35
+        $column = $table->getColumn($columnName);
36
+
37
+        // remove constraints on other column
38
+        $newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
39
+        $columns = [];
40
+        foreach ($table->getColumnNames() as $name) {
41
+            if ($name == $columnName) {
42
+                $columns[] = $newColumn;
43
+            } else {
44
+                $columns[] = $table->getColumn($name);
45
+            }
46
+        }
47
+        $newTable = new ReflectedTable($table->getName(), $table->getType(), $columns);
48
+        return $this->db->definition()->updateColumnsSqlite($newTable);
49
+    }
50
+
32 51
     public function updateColumn(string $tableName, string $columnName, /* object */ $changes): bool
33 52
     {
53
+        if ($this->db->getDriver() == 'sqlite') {
54
+            return $this->updateColumnSqlite($tableName, $columnName, $changes);
55
+        }
56
+
34 57
         $table = $this->reflection->getTable($tableName);
35 58
         $column = $table->getColumn($columnName);
36 59
 
@@ -68,7 +91,8 @@ class DefinitionService
68 91
                 return false;
69 92
             }
70 93
         }
71
-        if ($newColumn->getType() != $column->getType() ||
94
+        if (
95
+            $newColumn->getType() != $column->getType() ||
72 96
             $newColumn->getLength() != $column->getLength() ||
73 97
             $newColumn->getPrecision() != $column->getPrecision() ||
74 98
             $newColumn->getScale() != $column->getScale()

+ 6
- 0
src/Tqdev/PhpCrudApi/Database/GenericDB.php View File

@@ -23,6 +23,11 @@ class GenericDB
23 23
     private $columns;
24 24
     private $converter;
25 25
 
26
+    public function getDriver(): string
27
+    {
28
+        return $this->driver;
29
+    }
30
+
26 31
     private function getDsn(): string
27 32
     {
28 33
         switch ($this->driver) {
@@ -55,6 +60,7 @@ class GenericDB
55 60
             case 'sqlite':
56 61
                 return [
57 62
                     'PRAGMA foreign_keys = on;',
63
+                    'PRAGMA writable_schema = on;',
58 64
                 ];
59 65
         }
60 66
     }

+ 58
- 26
src/Tqdev/PhpCrudApi/Database/GenericDefinition.php View File

@@ -73,6 +73,8 @@ class GenericDefinition
73 73
             case 'pgsql':
74 74
             case 'sqlsrv':
75 75
                 return '';
76
+            case 'sqlite':
77
+                return $column->getPk() ? ' AUTOINCREMENT' : '';
76 78
         }
77 79
     }
78 80
 
@@ -96,6 +98,8 @@ class GenericDefinition
96 98
                 return "ALTER TABLE $p1 RENAME TO $p2";
97 99
             case 'sqlsrv':
98 100
                 return "EXEC sp_rename $p1, $p2";
101
+            case 'sqlite':
102
+                return "ALTER TABLE $p1 RENAME TO $p2";
99 103
         }
100 104
     }
101 105
 
@@ -114,6 +118,8 @@ class GenericDefinition
114 118
             case 'sqlsrv':
115 119
                 $p4 = $this->quote($tableName . '.' . $columnName);
116 120
                 return "EXEC sp_rename $p4, $p3, 'COLUMN'";
121
+            case 'sqlite':
122
+                return "ALTER TABLE $p1 RENAME COLUMN $p2 TO $p3";
117 123
         }
118 124
     }
119 125
 
@@ -272,12 +278,22 @@ class GenericDefinition
272 278
             $f4 = $this->quote($newColumn->getFk());
273 279
             $f5 = $this->quote($this->getPrimaryKey($newColumn->getFk()));
274 280
             $f6 = $this->quote($tableName . '_' . $pkColumn . '_pkey');
275
-            $fields[] = "$f1 $f2";
276
-            if ($newColumn->getPk()) {
277
-                $constraints[] = "CONSTRAINT $f6 PRIMARY KEY ($f1)";
278
-            }
279
-            if ($newColumn->getFk()) {
280
-                $constraints[] = "CONSTRAINT $f3 FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
281
+            if ($this->driver == 'sqlite') {
282
+                if ($newColumn->getPk()) {
283
+                    $f2 = str_replace('NULL', 'NULL PRIMARY KEY', $f2);
284
+                }
285
+                $fields[] = "$f1 $f2";
286
+                if ($newColumn->getFk()) {
287
+                    $constraints[] = "FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
288
+                }
289
+            } else {
290
+                $fields[] = "$f1 $f2";
291
+                if ($newColumn->getPk()) {
292
+                    $constraints[] = "CONSTRAINT $f6 PRIMARY KEY ($f1)";
293
+                }
294
+                if ($newColumn->getFk()) {
295
+                    $constraints[] = "CONSTRAINT $f3 FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
296
+                }
281 297
             }
282 298
         }
283 299
         $p2 = implode(',', array_merge($fields, $constraints));
@@ -297,6 +313,8 @@ class GenericDefinition
297 313
                 return "ALTER TABLE $p1 ADD COLUMN $p2 $p3";
298 314
             case 'sqlsrv':
299 315
                 return "ALTER TABLE $p1 ADD $p2 $p3";
316
+            case 'sqlite':
317
+                return "ALTER TABLE $p1 ADD COLUMN $p2 $p3";
300 318
         }
301 319
     }
302 320
 
@@ -310,6 +328,8 @@ class GenericDefinition
310 328
                 return "DROP TABLE $p1 CASCADE;";
311 329
             case 'sqlsrv':
312 330
                 return "DROP TABLE $p1;";
331
+            case 'sqlite':
332
+                return "DROP TABLE $p1;";
313 333
         }
314 334
     }
315 335
 
@@ -324,44 +344,56 @@ class GenericDefinition
324 344
                 return "ALTER TABLE $p1 DROP COLUMN $p2 CASCADE;";
325 345
             case 'sqlsrv':
326 346
                 return "ALTER TABLE $p1 DROP COLUMN $p2;";
347
+            case 'sqlite':
348
+                return "ALTER TABLE $p1 DROP COLUMN $p2;";
327 349
         }
328 350
     }
329 351
 
330 352
     public function renameTable(string $tableName, string $newTableName)
331 353
     {
332 354
         $sql = $this->getTableRenameSQL($tableName, $newTableName);
333
-        return $this->query($sql);
355
+        return $this->query($sql, []);
334 356
     }
335 357
 
336 358
     public function renameColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
337 359
     {
338 360
         $sql = $this->getColumnRenameSQL($tableName, $columnName, $newColumn);
339
-        return $this->query($sql);
361
+        return $this->query($sql, []);
362
+    }
363
+
364
+    public function updateColumnsSqlite(ReflectedTable $table)
365
+    {
366
+        $create = $this->getAddTableSQL($table);
367
+        $name = $table->getName();
368
+        $sql = "UPDATE SQLITE_MASTER SET SQL = ? WHERE NAME = ?;";
369
+        $result = $this->query($sql, [$create, $name]);
370
+        $this->query('VACUUM;', []);
371
+        return $result;
340 372
     }
341 373
 
342 374
     public function retypeColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
343 375
     {
344 376
         $sql = $this->getColumnRetypeSQL($tableName, $columnName, $newColumn);
345
-        return $this->query($sql);
377
+        return $this->query($sql, []);
346 378
     }
347 379
 
348 380
     public function setColumnNullable(string $tableName, string $columnName, ReflectedColumn $newColumn)
349 381
     {
350 382
         $sql = $this->getSetColumnNullableSQL($tableName, $columnName, $newColumn);
351
-        return $this->query($sql);
383
+        return $this->query($sql, []);
352 384
     }
353 385
 
354 386
     public function addColumnPrimaryKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
355 387
     {
356 388
         $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
357
-        $this->query($sql);
389
+        $this->query($sql, []);
358 390
         if ($this->canAutoIncrement($newColumn)) {
359 391
             $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
360
-            $this->query($sql);
392
+            $this->query($sql, []);
361 393
             $sql = $this->getSetColumnPkSequenceStartSQL($tableName, $columnName, $newColumn);
362
-            $this->query($sql);
394
+            $this->query($sql, []);
363 395
             $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
364
-            $this->query($sql);
396
+            $this->query($sql, []);
365 397
         }
366 398
         return true;
367 399
     }
@@ -370,55 +402,55 @@ class GenericDefinition
370 402
     {
371 403
         if ($this->canAutoIncrement($newColumn)) {
372 404
             $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
373
-            $this->query($sql);
405
+            $this->query($sql, []);
374 406
             $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
375
-            $this->query($sql);
407
+            $this->query($sql, []);
376 408
         }
377 409
         $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
378
-        $this->query($sql);
410
+        $this->query($sql, []);
379 411
         return true;
380 412
     }
381 413
 
382 414
     public function addColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
383 415
     {
384 416
         $sql = $this->getAddColumnFkConstraintSQL($tableName, $columnName, $newColumn);
385
-        return $this->query($sql);
417
+        return $this->query($sql, []);
386 418
     }
387 419
 
388 420
     public function removeColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
389 421
     {
390 422
         $sql = $this->getRemoveColumnFkConstraintSQL($tableName, $columnName, $newColumn);
391
-        return $this->query($sql);
423
+        return $this->query($sql, []);
392 424
     }
393 425
 
394 426
     public function addTable(ReflectedTable $newTable)
395 427
     {
396 428
         $sql = $this->getAddTableSQL($newTable);
397
-        return $this->query($sql);
429
+        return $this->query($sql, []);
398 430
     }
399 431
 
400 432
     public function addColumn(string $tableName, ReflectedColumn $newColumn)
401 433
     {
402 434
         $sql = $this->getAddColumnSQL($tableName, $newColumn);
403
-        return $this->query($sql);
435
+        return $this->query($sql, []);
404 436
     }
405 437
 
406 438
     public function removeTable(string $tableName)
407 439
     {
408 440
         $sql = $this->getRemoveTableSQL($tableName);
409
-        return $this->query($sql);
441
+        return $this->query($sql, []);
410 442
     }
411 443
 
412 444
     public function removeColumn(string $tableName, string $columnName)
413 445
     {
414 446
         $sql = $this->getRemoveColumnSQL($tableName, $columnName);
415
-        return $this->query($sql);
447
+        return $this->query($sql, []);
416 448
     }
417 449
 
418
-    private function query(string $sql): bool
450
+    private function query(string $sql, array $arguments): bool
419 451
     {
420 452
         $stmt = $this->pdo->prepare($sql);
421
-        //echo "- $sql -- []\n";
422
-        return $stmt->execute();
453
+        //echo "- $sql -- " . json_encode($arguments) . "\n";
454
+        return $stmt->execute($arguments);
423 455
     }
424 456
 }

+ 3
- 2
src/Tqdev/PhpCrudApi/Database/GenericReflection.php View File

@@ -21,7 +21,7 @@ class GenericReflection
21 21
         $this->typeConverter = new TypeConverter($driver);
22 22
     }
23 23
 
24
-    private function createSqlLiteReflectionTables() /*: void */
24
+    private function updateSqlLiteReflectionTables() /*: void */
25 25
     {
26 26
         $reflection = $this->query('SELECT "name" FROM "sqlite_master" WHERE "type" = \'table\' and name like \'sys/%\';', []);
27 27
         if (count($reflection) == 0) {
@@ -90,7 +90,7 @@ class GenericReflection
90 90
             case 'sqlsrv':
91 91
                 return 'SELECT o.name as "TABLE_NAME", o.xtype as "TABLE_TYPE" FROM sysobjects o WHERE o.xtype IN (\'U\', \'V\') ORDER BY "TABLE_NAME"';
92 92
             case 'sqlite':
93
-                $this->createSqlLiteReflectionTables();
93
+                $this->updateSqlLiteReflectionTables();
94 94
                 return 'SELECT t.name as "TABLE_NAME", t.type as "TABLE_TYPE" FROM "sys/tables" t WHERE t.type IN (\'table\', \'view\') AND \'\' <> ? ORDER BY "TABLE_NAME"';
95 95
         }
96 96
     }
@@ -105,6 +105,7 @@ class GenericReflection
105 105
             case 'sqlsrv':
106 106
                 return 'SELECT c.name AS "COLUMN_NAME", c.is_nullable AS "IS_NULLABLE", t.Name AS "DATA_TYPE", (c.max_length/2) AS "CHARACTER_MAXIMUM_LENGTH", c.precision AS "NUMERIC_PRECISION", c.scale AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM sys.columns c INNER JOIN sys.types t ON c.user_type_id = t.user_type_id WHERE c.object_id = OBJECT_ID(?) AND \'\' <> ?';
107 107
             case 'sqlite':
108
+                $this->updateSqlLiteReflectionTables();
108 109
                 return 'SELECT "name" AS "COLUMN_NAME", case when "notnull"==1 then \'no\' else \'yes\' end as "IS_NULLABLE", "type" AS "DATA_TYPE", 2147483647 AS "CHARACTER_MAXIMUM_LENGTH", 0 AS "NUMERIC_PRECISION", 0 AS "NUMERIC_SCALE", \'\' AS "COLUMN_TYPE" FROM "sys/columns" WHERE "self" = ? AND \'\' <> ?';
109 110
         }
110 111
     }

Loading…
Cancel
Save