|
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\AbstractColumn; |
|
10
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractIndex; |
|
11
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractReference; |
|
12
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractTable; |
|
13
|
|
|
|
|
14
|
|
|
class SQLiteTable extends AbstractTable |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function fetchColumns(): array |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Parsing column definitions. |
|
23
|
|
|
*/ |
|
24
|
|
|
$definition = $this->driver->query( |
|
25
|
|
|
"SELECT 'sql' FROM 'sqlite_master' WHERE type = 'table' and name = ?", |
|
26
|
|
|
[$this->getName()] |
|
27
|
|
|
)->fetchColumn(); |
|
28
|
|
|
|
|
29
|
|
|
/* |
|
30
|
|
|
* There is not really many ways to get extra information about column in SQLite, let's parse |
|
31
|
|
|
* table schema. As mention, spiral SQLite schema reader will support fully only tables created |
|
32
|
|
|
* by spiral as we expecting every column definition be on new line. |
|
33
|
|
|
*/ |
|
34
|
|
|
$definition = explode("\n", $definition); |
|
35
|
|
|
|
|
36
|
|
|
$result = []; |
|
37
|
|
|
foreach ($this->columnSchemas(['table' => $definition]) as $schema) { |
|
38
|
|
|
//Making new column instance |
|
39
|
|
|
$result[] = SQLiteColumn::createInstance( |
|
40
|
|
|
$this->getName(), |
|
41
|
|
|
$schema + [ |
|
42
|
|
|
'quoted' => $this->driver->quote($schema['name']), |
|
43
|
|
|
'identifier' => $this->driver->identifier($schema['name']) |
|
44
|
|
|
] |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $result; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function fetchIndexes(): array |
|
55
|
|
|
{ |
|
56
|
|
|
$query = "PRAGMA index_list({$this->driver->quote($this->getName())})"; |
|
57
|
|
|
|
|
58
|
|
|
$result = []; |
|
59
|
|
|
foreach ($this->driver->query($query) as $schema) { |
|
60
|
|
|
//Index schema and all related columns |
|
61
|
|
|
$result[] = SQLiteIndex::createInstance( |
|
62
|
|
|
$this->getName(), |
|
63
|
|
|
$schema, |
|
64
|
|
|
$this->driver->query("PRAGMA INDEX_INFO({$this->driver->quote($schema['name'])})")->fetchAll() |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function fetchReferences(): array |
|
75
|
|
|
{ |
|
76
|
|
|
$query = "PRAGMA foreign_key_list({$this->driver->quote($this->getName())})"; |
|
77
|
|
|
|
|
78
|
|
|
$result = []; |
|
79
|
|
|
foreach ($this->driver->query($query) as $schema) { |
|
80
|
|
|
$result[] = SQLiteReference::createInstance( |
|
81
|
|
|
$this->getName(), |
|
82
|
|
|
$this->getPrefix(), |
|
83
|
|
|
$schema |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $result; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Fetching primary keys from table. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function fetchPrimaryKeys(): array |
|
96
|
|
|
{ |
|
97
|
|
|
$primaryKeys = []; |
|
98
|
|
|
foreach ($this->columnSchemas() as $column) { |
|
99
|
|
|
if (!empty($column['pk'])) { |
|
100
|
|
|
$primaryKeys[] = $column['name']; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $primaryKeys; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param array $include Include following parameters into each line. |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
private function columnSchemas(array $include = []): array |
|
113
|
|
|
{ |
|
114
|
|
|
$columns = $this->driver->query( |
|
115
|
|
|
"PRAGMA TABLE_INFO(" . $this->driver->quote($this->getName()) . ")" |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
$result = []; |
|
119
|
|
|
|
|
120
|
|
|
foreach ($columns as $column) { |
|
121
|
|
|
$result[] = $column + $include; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $result; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc} |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function createColumn(string $name): AbstractColumn |
|
131
|
|
|
{ |
|
132
|
|
|
return new SQLiteColumn($this->getName(), $name); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function createIndex(string $name): AbstractIndex |
|
139
|
|
|
{ |
|
140
|
|
|
return new SQLiteIndex($this->getName(), $name); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function createForeign(string $column): AbstractReference |
|
147
|
|
|
{ |
|
148
|
|
|
return new SQLiteReference($this->getName(), $this->getPrefix(), $column); |
|
149
|
|
|
} |
|
150
|
|
|
} |