1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* components |
4
|
|
|
* |
5
|
|
|
* @author Wolfy-J |
6
|
|
|
*/ |
7
|
|
|
namespace Spiral\Database\Drivers\SQLite\Schemas; |
8
|
|
|
|
9
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractTable; |
10
|
|
|
|
11
|
|
|
class TableSchema extends AbstractTable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
protected function fetchColumns(): array |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Parsing column definitions. |
20
|
|
|
*/ |
21
|
|
|
$definition = $this->driver->query( |
22
|
|
|
"SELECT sql FROM 'sqlite_master' WHERE type = 'table' and name = ?", |
23
|
|
|
[$this->getName()] |
24
|
|
|
)->fetchColumn(); |
25
|
|
|
|
26
|
|
|
/* |
27
|
|
|
* There is not really many ways to get extra information about column in SQLite, let's parse |
28
|
|
|
* table schema. As mention, spiral SQLite schema reader will support fully only tables created |
29
|
|
|
* by spiral as we expecting every column definition be on new line. |
30
|
|
|
*/ |
31
|
|
|
$definition = explode("\n", $definition); |
32
|
|
|
|
33
|
|
|
$result = []; |
34
|
|
|
foreach ($this->columnSchemas(['table' => $definition]) as $schema) { |
35
|
|
|
//Making new column instance |
36
|
|
|
$result[] = ColumnSchema::createInstance( |
37
|
|
|
$this->getName(), |
38
|
|
|
$schema + ['quoted' => $this->driver->quote($schema['name'])] |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $result; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function fetchIndexes(): array |
49
|
|
|
{ |
50
|
|
|
return []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function fetchReferences(): array |
57
|
|
|
{ |
58
|
|
|
return []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Fetching primary keys from table. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
protected function fetchPrimaryKeys(): array |
67
|
|
|
{ |
68
|
|
|
$primaryKeys = []; |
69
|
|
|
foreach ($this->columnSchemas() as $column) { |
70
|
|
|
if (!empty($column['pk'])) { |
71
|
|
|
$primaryKeys[] = $column['name']; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $primaryKeys; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $include Include following parameters into each line. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
private function columnSchemas(array $include = []): array |
84
|
|
|
{ |
85
|
|
|
$columns = $this->driver->query( |
86
|
|
|
"PRAGMA TABLE_INFO(" . $this->driver->quote($this->getName()) . ")" |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$result = []; |
90
|
|
|
|
91
|
|
|
foreach ($columns as $column) { |
92
|
|
|
$result[] = $column + $include; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
} |