1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license https://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\db\sqlite; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\NotSupportedException; |
12
|
|
|
use yii\db\CheckConstraint; |
13
|
|
|
use yii\db\ColumnSchema; |
14
|
|
|
use yii\db\Constraint; |
15
|
|
|
use yii\db\ConstraintFinderInterface; |
16
|
|
|
use yii\db\ConstraintFinderTrait; |
17
|
|
|
use yii\db\Expression; |
18
|
|
|
use yii\db\ForeignKeyConstraint; |
19
|
|
|
use yii\db\IndexConstraint; |
20
|
|
|
use yii\db\SqlToken; |
21
|
|
|
use yii\db\TableSchema; |
22
|
|
|
use yii\db\Transaction; |
23
|
|
|
use yii\helpers\ArrayHelper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Schema is the class for retrieving metadata from a SQLite (2/3) database. |
27
|
|
|
* |
28
|
|
|
* @property-write string $transactionIsolationLevel The transaction isolation level to use for this |
29
|
|
|
* transaction. This can be either [[Transaction::READ_UNCOMMITTED]] or [[Transaction::SERIALIZABLE]]. |
30
|
|
|
* |
31
|
|
|
* @author Qiang Xue <[email protected]> |
32
|
|
|
* @since 2.0 |
33
|
|
|
*/ |
34
|
|
|
class Schema extends \yii\db\Schema implements ConstraintFinderInterface |
35
|
|
|
{ |
36
|
|
|
use ConstraintFinderTrait; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array mapping from physical column types (keys) to abstract column types (values) |
40
|
|
|
*/ |
41
|
|
|
public $typeMap = [ |
42
|
|
|
'tinyint' => self::TYPE_TINYINT, |
43
|
|
|
'bit' => self::TYPE_SMALLINT, |
44
|
|
|
'boolean' => self::TYPE_BOOLEAN, |
45
|
|
|
'bool' => self::TYPE_BOOLEAN, |
46
|
|
|
'smallint' => self::TYPE_SMALLINT, |
47
|
|
|
'mediumint' => self::TYPE_INTEGER, |
48
|
|
|
'int' => self::TYPE_INTEGER, |
49
|
|
|
'integer' => self::TYPE_INTEGER, |
50
|
|
|
'bigint' => self::TYPE_BIGINT, |
51
|
|
|
'float' => self::TYPE_FLOAT, |
52
|
|
|
'double' => self::TYPE_DOUBLE, |
53
|
|
|
'real' => self::TYPE_FLOAT, |
54
|
|
|
'decimal' => self::TYPE_DECIMAL, |
55
|
|
|
'numeric' => self::TYPE_DECIMAL, |
56
|
|
|
'tinytext' => self::TYPE_TEXT, |
57
|
|
|
'mediumtext' => self::TYPE_TEXT, |
58
|
|
|
'longtext' => self::TYPE_TEXT, |
59
|
|
|
'text' => self::TYPE_TEXT, |
60
|
|
|
'varchar' => self::TYPE_STRING, |
61
|
|
|
'string' => self::TYPE_STRING, |
62
|
|
|
'char' => self::TYPE_CHAR, |
63
|
|
|
'blob' => self::TYPE_BINARY, |
64
|
|
|
'datetime' => self::TYPE_DATETIME, |
65
|
|
|
'year' => self::TYPE_DATE, |
66
|
|
|
'date' => self::TYPE_DATE, |
67
|
|
|
'time' => self::TYPE_TIME, |
68
|
|
|
'timestamp' => self::TYPE_TIMESTAMP, |
69
|
|
|
'enum' => self::TYPE_STRING, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
protected $tableQuoteCharacter = '`'; |
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
protected $columnQuoteCharacter = '`'; |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
protected function findTableNames($schema = '') |
86
|
|
|
{ |
87
|
|
|
$sql = "SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence' ORDER BY tbl_name"; |
88
|
|
|
return $this->db->createCommand($sql)->queryColumn(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
65 |
|
protected function loadTableSchema($name) |
95
|
|
|
{ |
96
|
65 |
|
$table = new TableSchema(); |
97
|
65 |
|
$table->name = $name; |
98
|
65 |
|
$table->fullName = $name; |
99
|
|
|
|
100
|
65 |
|
if ($this->findColumns($table)) { |
101
|
65 |
|
$this->findConstraints($table); |
102
|
65 |
|
return $table; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
protected function loadTablePrimaryKey($tableName) |
112
|
|
|
{ |
113
|
|
|
return $this->loadTableConstraints($tableName, 'primaryKey'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
protected function loadTableForeignKeys($tableName) |
120
|
|
|
{ |
121
|
|
|
$foreignKeys = $this->db->createCommand('PRAGMA FOREIGN_KEY_LIST (' . $this->quoteValue($tableName) . ')')->queryAll(); |
122
|
|
|
$foreignKeys = $this->normalizePdoRowKeyCase($foreignKeys, true); |
123
|
|
|
$foreignKeys = ArrayHelper::index($foreignKeys, null, 'table'); |
124
|
|
|
ArrayHelper::multisort($foreignKeys, 'seq', SORT_ASC, SORT_NUMERIC); |
125
|
|
|
$result = []; |
126
|
|
|
foreach ($foreignKeys as $table => $foreignKey) { |
127
|
|
|
$result[] = new ForeignKeyConstraint([ |
128
|
|
|
'columnNames' => ArrayHelper::getColumn($foreignKey, 'from'), |
129
|
|
|
'foreignTableName' => $table, |
130
|
|
|
'foreignColumnNames' => ArrayHelper::getColumn($foreignKey, 'to'), |
131
|
|
|
'onDelete' => isset($foreignKey[0]['on_delete']) ? $foreignKey[0]['on_delete'] : null, |
132
|
|
|
'onUpdate' => isset($foreignKey[0]['on_update']) ? $foreignKey[0]['on_update'] : null, |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
protected function loadTableIndexes($tableName) |
143
|
|
|
{ |
144
|
|
|
return $this->loadTableConstraints($tableName, 'indexes'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
protected function loadTableUniques($tableName) |
151
|
|
|
{ |
152
|
|
|
return $this->loadTableConstraints($tableName, 'uniques'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
protected function loadTableChecks($tableName) |
159
|
|
|
{ |
160
|
|
|
$sql = $this->db->createCommand('SELECT `sql` FROM `sqlite_master` WHERE name = :tableName', [ |
161
|
|
|
':tableName' => $tableName, |
162
|
|
|
])->queryScalar(); |
163
|
|
|
/** @var SqlToken[]|SqlToken[][]|SqlToken[][][] $code */ |
164
|
|
|
$code = (new SqlTokenizer($sql))->tokenize(); |
|
|
|
|
165
|
|
|
$pattern = (new SqlTokenizer('any CREATE any TABLE any()'))->tokenize(); |
166
|
|
|
if (!$code[0]->matches($pattern, 0, $firstMatchIndex, $lastMatchIndex)) { |
|
|
|
|
167
|
|
|
return []; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$createTableToken = $code[0][$lastMatchIndex - 1]; |
171
|
|
|
$result = []; |
172
|
|
|
$offset = 0; |
173
|
|
|
while (true) { |
174
|
|
|
$pattern = (new SqlTokenizer('any CHECK()'))->tokenize(); |
175
|
|
|
if (!$createTableToken->matches($pattern, $offset, $firstMatchIndex, $offset)) { |
176
|
|
|
break; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$checkSql = $createTableToken[$offset - 1]->getSql(); |
180
|
|
|
$name = null; |
181
|
|
|
$pattern = (new SqlTokenizer('CONSTRAINT any'))->tokenize(); |
182
|
|
|
if (isset($createTableToken[$firstMatchIndex - 2]) && $createTableToken->matches($pattern, $firstMatchIndex - 2)) { |
183
|
|
|
$name = $createTableToken[$firstMatchIndex - 1]->content; |
184
|
|
|
} |
185
|
|
|
$result[] = new CheckConstraint([ |
186
|
|
|
'name' => $name, |
187
|
|
|
'expression' => $checkSql, |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $result; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritdoc} |
196
|
|
|
* @throws NotSupportedException if this method is called. |
197
|
|
|
*/ |
198
|
|
|
protected function loadTableDefaultValues($tableName) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
throw new NotSupportedException('SQLite does not support default value constraints.'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Creates a query builder for the MySQL database. |
205
|
|
|
* This method may be overridden by child classes to create a DBMS-specific query builder. |
206
|
|
|
* @return QueryBuilder query builder instance |
207
|
|
|
*/ |
208
|
83 |
|
public function createQueryBuilder() |
209
|
|
|
{ |
210
|
83 |
|
return Yii::createObject(QueryBuilder::class, [$this->db]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
* @return ColumnSchemaBuilder column schema builder instance |
216
|
|
|
*/ |
217
|
|
|
public function createColumnSchemaBuilder($type, $length = null) |
218
|
|
|
{ |
219
|
|
|
return Yii::createObject(ColumnSchemaBuilder::class, [$type, $length]); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Collects the table column metadata. |
224
|
|
|
* @param TableSchema $table the table metadata |
225
|
|
|
* @return bool whether the table exists in the database |
226
|
|
|
*/ |
227
|
65 |
|
protected function findColumns($table) |
228
|
|
|
{ |
229
|
65 |
|
$sql = 'PRAGMA table_info(' . $this->quoteSimpleTableName($table->name) . ')'; |
230
|
65 |
|
$columns = $this->db->createCommand($sql)->queryAll(); |
231
|
65 |
|
if (empty($columns)) { |
232
|
|
|
return false; |
233
|
|
|
} |
234
|
|
|
|
235
|
65 |
|
foreach ($columns as $info) { |
236
|
65 |
|
$column = $this->loadColumnSchema($info); |
237
|
65 |
|
$table->columns[$column->name] = $column; |
238
|
65 |
|
if ($column->isPrimaryKey) { |
239
|
55 |
|
$table->primaryKey[] = $column->name; |
240
|
|
|
} |
241
|
|
|
} |
242
|
65 |
|
if (count($table->primaryKey) === 1 && !strncasecmp($table->columns[$table->primaryKey[0]]->dbType, 'int', 3)) { |
243
|
55 |
|
$table->sequenceName = ''; |
244
|
55 |
|
$table->columns[$table->primaryKey[0]]->autoIncrement = true; |
245
|
|
|
} |
246
|
|
|
|
247
|
65 |
|
return true; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Collects the foreign key column details for the given table. |
252
|
|
|
* @param TableSchema $table the table metadata |
253
|
|
|
*/ |
254
|
65 |
|
protected function findConstraints($table) |
255
|
|
|
{ |
256
|
65 |
|
$sql = 'PRAGMA foreign_key_list(' . $this->quoteSimpleTableName($table->name) . ')'; |
257
|
65 |
|
$keys = $this->db->createCommand($sql)->queryAll(); |
258
|
65 |
|
foreach ($keys as $key) { |
259
|
|
|
$id = (int) $key['id']; |
260
|
|
|
if (!isset($table->foreignKeys[$id])) { |
261
|
|
|
$table->foreignKeys[$id] = [$key['table'], $key['from'] => $key['to']]; |
262
|
|
|
} else { |
263
|
|
|
// composite FK |
264
|
|
|
$table->foreignKeys[$id][$key['from']] = $key['to']; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns all unique indexes for the given table. |
271
|
|
|
* |
272
|
|
|
* Each array element is of the following structure: |
273
|
|
|
* |
274
|
|
|
* ```php |
275
|
|
|
* [ |
276
|
|
|
* 'IndexName1' => ['col1' [, ...]], |
277
|
|
|
* 'IndexName2' => ['col2' [, ...]], |
278
|
|
|
* ] |
279
|
|
|
* ``` |
280
|
|
|
* |
281
|
|
|
* @param TableSchema $table the table metadata |
282
|
|
|
* @return array all unique indexes for the given table. |
283
|
|
|
*/ |
284
|
|
|
public function findUniqueIndexes($table) |
285
|
|
|
{ |
286
|
|
|
$sql = 'PRAGMA index_list(' . $this->quoteSimpleTableName($table->name) . ')'; |
287
|
|
|
$indexes = $this->db->createCommand($sql)->queryAll(); |
288
|
|
|
$uniqueIndexes = []; |
289
|
|
|
|
290
|
|
|
foreach ($indexes as $index) { |
291
|
|
|
$indexName = $index['name']; |
292
|
|
|
$indexInfo = $this->db->createCommand('PRAGMA index_info(' . $this->quoteValue($index['name']) . ')')->queryAll(); |
293
|
|
|
|
294
|
|
|
if ($index['unique']) { |
295
|
|
|
$uniqueIndexes[$indexName] = []; |
296
|
|
|
foreach ($indexInfo as $row) { |
297
|
|
|
$uniqueIndexes[$indexName][] = $row['name']; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $uniqueIndexes; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Loads the column information into a [[ColumnSchema]] object. |
307
|
|
|
* @param array $info column information |
308
|
|
|
* @return ColumnSchema the column schema object |
309
|
|
|
*/ |
310
|
65 |
|
protected function loadColumnSchema($info) |
311
|
|
|
{ |
312
|
65 |
|
$column = $this->createColumnSchema(); |
313
|
65 |
|
$column->name = $info['name']; |
314
|
65 |
|
$column->allowNull = !$info['notnull']; |
315
|
65 |
|
$column->isPrimaryKey = $info['pk'] != 0; |
316
|
|
|
|
317
|
65 |
|
$column->dbType = strtolower($info['type']); |
318
|
65 |
|
$column->unsigned = strpos($column->dbType, 'unsigned') !== false; |
319
|
|
|
|
320
|
65 |
|
$column->type = self::TYPE_STRING; |
321
|
65 |
|
if (preg_match('/^(\w+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) { |
322
|
65 |
|
$type = strtolower($matches[1]); |
323
|
65 |
|
if (isset($this->typeMap[$type])) { |
324
|
65 |
|
$column->type = $this->typeMap[$type]; |
325
|
|
|
} |
326
|
|
|
|
327
|
65 |
|
if (!empty($matches[2])) { |
328
|
56 |
|
$values = explode(',', $matches[2]); |
329
|
56 |
|
$column->size = $column->precision = (int) $values[0]; |
330
|
56 |
|
if (isset($values[1])) { |
331
|
|
|
$column->scale = (int) $values[1]; |
332
|
|
|
} |
333
|
56 |
|
if ($column->size === 1 && ($type === 'tinyint' || $type === 'bit')) { |
334
|
|
|
$column->type = 'boolean'; |
335
|
56 |
|
} elseif ($type === 'bit') { |
336
|
|
|
if ($column->size > 32) { |
337
|
|
|
$column->type = 'bigint'; |
338
|
|
|
} elseif ($column->size === 32) { |
339
|
|
|
$column->type = 'integer'; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
} |
344
|
65 |
|
$column->phpType = $this->getColumnPhpType($column); |
345
|
|
|
|
346
|
65 |
|
if (!$column->isPrimaryKey) { |
347
|
65 |
|
if ($info['dflt_value'] === 'null' || $info['dflt_value'] === '' || $info['dflt_value'] === null) { |
348
|
65 |
|
$column->defaultValue = null; |
349
|
|
|
} elseif ($column->type === 'timestamp' && $info['dflt_value'] === 'CURRENT_TIMESTAMP') { |
350
|
|
|
$column->defaultValue = new Expression('CURRENT_TIMESTAMP'); |
351
|
|
|
} else { |
352
|
|
|
$value = trim($info['dflt_value'], "'\""); |
353
|
|
|
$column->defaultValue = $column->phpTypecast($value); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
357
|
65 |
|
return $column; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Sets the isolation level of the current transaction. |
362
|
|
|
* @param string $level The transaction isolation level to use for this transaction. |
363
|
|
|
* This can be either [[Transaction::READ_UNCOMMITTED]] or [[Transaction::SERIALIZABLE]]. |
364
|
|
|
* @throws NotSupportedException when unsupported isolation levels are used. |
365
|
|
|
* SQLite only supports SERIALIZABLE and READ UNCOMMITTED. |
366
|
|
|
* @see https://www.sqlite.org/pragma.html#pragma_read_uncommitted |
367
|
|
|
*/ |
368
|
|
|
public function setTransactionIsolationLevel($level) |
369
|
|
|
{ |
370
|
|
|
switch ($level) { |
371
|
|
|
case Transaction::SERIALIZABLE: |
372
|
|
|
$this->db->createCommand('PRAGMA read_uncommitted = False;')->execute(); |
373
|
|
|
break; |
374
|
|
|
case Transaction::READ_UNCOMMITTED: |
375
|
|
|
$this->db->createCommand('PRAGMA read_uncommitted = True;')->execute(); |
376
|
|
|
break; |
377
|
|
|
default: |
378
|
|
|
throw new NotSupportedException(get_class($this) . ' only supports transaction isolation levels READ UNCOMMITTED and SERIALIZABLE.'); |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Returns table columns info. |
384
|
|
|
* @param string $tableName table name |
385
|
|
|
* @return array |
386
|
|
|
*/ |
387
|
|
|
private function loadTableColumnsInfo($tableName) |
388
|
|
|
{ |
389
|
|
|
$tableColumns = $this->db->createCommand('PRAGMA TABLE_INFO (' . $this->quoteValue($tableName) . ')')->queryAll(); |
390
|
|
|
$tableColumns = $this->normalizePdoRowKeyCase($tableColumns, true); |
391
|
|
|
|
392
|
|
|
return ArrayHelper::index($tableColumns, 'cid'); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Loads multiple types of constraints and returns the specified ones. |
397
|
|
|
* @param string $tableName table name. |
398
|
|
|
* @param string $returnType return type: |
399
|
|
|
* - primaryKey |
400
|
|
|
* - indexes |
401
|
|
|
* - uniques |
402
|
|
|
* @return mixed constraints. |
403
|
|
|
*/ |
404
|
|
|
private function loadTableConstraints($tableName, $returnType) |
405
|
|
|
{ |
406
|
|
|
$indexes = $this->db->createCommand('PRAGMA INDEX_LIST (' . $this->quoteValue($tableName) . ')')->queryAll(); |
407
|
|
|
$indexes = $this->normalizePdoRowKeyCase($indexes, true); |
408
|
|
|
$tableColumns = null; |
409
|
|
|
if (!empty($indexes) && !isset($indexes[0]['origin'])) { |
410
|
|
|
/* |
411
|
|
|
* SQLite may not have an "origin" column in INDEX_LIST |
412
|
|
|
* See https://www.sqlite.org/src/info/2743846cdba572f6 |
413
|
|
|
*/ |
414
|
|
|
$tableColumns = $this->loadTableColumnsInfo($tableName); |
415
|
|
|
} |
416
|
|
|
$result = [ |
417
|
|
|
'primaryKey' => null, |
418
|
|
|
'indexes' => [], |
419
|
|
|
'uniques' => [], |
420
|
|
|
]; |
421
|
|
|
foreach ($indexes as $index) { |
422
|
|
|
$columns = $this->db->createCommand('PRAGMA INDEX_INFO (' . $this->quoteValue($index['name']) . ')')->queryAll(); |
423
|
|
|
$columns = $this->normalizePdoRowKeyCase($columns, true); |
424
|
|
|
ArrayHelper::multisort($columns, 'seqno', SORT_ASC, SORT_NUMERIC); |
425
|
|
|
if ($tableColumns !== null) { |
426
|
|
|
// SQLite may not have an "origin" column in INDEX_LIST |
427
|
|
|
$index['origin'] = 'c'; |
428
|
|
|
if (!empty($columns) && $tableColumns[$columns[0]['cid']]['pk'] > 0) { |
429
|
|
|
$index['origin'] = 'pk'; |
430
|
|
|
} elseif ($index['unique'] && $this->isSystemIdentifier($index['name'])) { |
431
|
|
|
$index['origin'] = 'u'; |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
$result['indexes'][] = new IndexConstraint([ |
435
|
|
|
'isPrimary' => $index['origin'] === 'pk', |
436
|
|
|
'isUnique' => (bool) $index['unique'], |
437
|
|
|
'name' => $index['name'], |
438
|
|
|
'columnNames' => ArrayHelper::getColumn($columns, 'name'), |
439
|
|
|
]); |
440
|
|
|
if ($index['origin'] === 'u') { |
441
|
|
|
$result['uniques'][] = new Constraint([ |
442
|
|
|
'name' => $index['name'], |
443
|
|
|
'columnNames' => ArrayHelper::getColumn($columns, 'name'), |
444
|
|
|
]); |
445
|
|
|
} elseif ($index['origin'] === 'pk') { |
446
|
|
|
$result['primaryKey'] = new Constraint([ |
447
|
|
|
'columnNames' => ArrayHelper::getColumn($columns, 'name'), |
448
|
|
|
]); |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
if ($result['primaryKey'] === null) { |
453
|
|
|
/* |
454
|
|
|
* Additional check for PK in case of INTEGER PRIMARY KEY with ROWID |
455
|
|
|
* See https://www.sqlite.org/lang_createtable.html#primkeyconst |
456
|
|
|
*/ |
457
|
|
|
if ($tableColumns === null) { |
458
|
|
|
$tableColumns = $this->loadTableColumnsInfo($tableName); |
459
|
|
|
} |
460
|
|
|
foreach ($tableColumns as $tableColumn) { |
461
|
|
|
if ($tableColumn['pk'] > 0) { |
462
|
|
|
$result['primaryKey'] = new Constraint([ |
463
|
|
|
'columnNames' => [$tableColumn['name']], |
464
|
|
|
]); |
465
|
|
|
break; |
466
|
|
|
} |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
foreach ($result as $type => $data) { |
471
|
|
|
$this->setTableMetadata($tableName, $type, $data); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
return $result[$returnType]; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Return whether the specified identifier is a SQLite system identifier. |
479
|
|
|
* @param string $identifier |
480
|
|
|
* @return bool |
481
|
|
|
* @see https://www.sqlite.org/src/artifact/74108007d286232f |
482
|
|
|
*/ |
483
|
|
|
private function isSystemIdentifier($identifier) |
484
|
|
|
{ |
485
|
|
|
return strncmp($identifier, 'sqlite_', 7) === 0; |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
|