Repository navigation
Expand file tree
/
Copy pathMySQLTable.php
More file actions
251 lines (210 loc) · 6.61 KB
/
Copy pathMySQLTable.php
File metadata and controls
251 lines (210 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* This file is part of Cycle ORM package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Cycle\Database\Driver\MySQL\Schema;
use Cycle\Database\Exception\SchemaException;
use Cycle\Database\Schema\AbstractColumn;
use Cycle\Database\Schema\AbstractForeignKey;
use Cycle\Database\Schema\AbstractIndex;
use Cycle\Database\Schema\AbstractTable;
use Cycle\Database\Schema\State;
class MySQLTable extends AbstractTable
{
/**
* List of most common MySQL table engines.
*/
public const ENGINE_INNODB = 'InnoDB';
public const ENGINE_MYISAM = 'MyISAM';
public const ENGINE_MEMORY = 'Memory';
/**
* MySQL table engine.
*/
private string $engine = self::ENGINE_INNODB;
/**
* MySQL version.
*/
private ?string $version = null;
/**
* Memoized `SHOW INDEXES` result, shared between {@see fetchIndexes()} and
* {@see fetchPrimaryKeys()}.
*/
private ?array $indexRows = null;
/**
* Change table engine. Such operation will be applied only at moment of table creation.
*
* @psalm-param non-empty-string $engine
*
* @throws SchemaException
*/
public function setEngine(string $engine): self
{
if ($this->exists()) {
throw new SchemaException('Table engine can be set only at moment of creation');
}
$this->engine = $engine;
return $this;
}
/**
* @psalm-return non-empty-string
*/
public function getEngine(): string
{
return $this->engine;
}
/**
* Populate table schema with values from database.
*/
#[\Override]
protected function initSchema(State $state): void
{
parent::initSchema($state);
//Reading table schema
$this->engine = $this->driver->query(
'SHOW TABLE STATUS WHERE `Name` = ?',
[
$state->getName(),
],
)->fetch()['Engine'];
}
#[\Override]
protected function isIndexColumnSortingSupported(): bool
{
if (!$this->version) {
$this->version = $this->driver->query('SELECT VERSION() AS version')->fetch()['version'];
}
if (\str_contains($this->version, 'MariaDB')) {
return false;
}
return \version_compare($this->version, '8.0', '>=');
}
#[\Override]
protected function fetchColumns(): array
{
$query = "SHOW FULL COLUMNS FROM {$this->driver->identifier($this->getFullName())}";
$primaryKeys = $this->fetchPrimaryKeys();
$result = [];
foreach ($this->driver->query($query) as $schema) {
$result[] = MySQLColumn::createInstance(
$this->getFullName(),
$schema,
$this->driver->getTimezone(),
$primaryKeys,
);
}
return $result;
}
#[\Override]
protected function resetIntrospectionCache(): void
{
$this->indexRows = null;
}
#[\Override]
protected function fetchIndexes(): array
{
//Gluing all index definitions together
$schemas = [];
foreach ($this->indexRows() as $index) {
if ($index['Key_name'] === 'PRIMARY') {
//Skipping PRIMARY index
continue;
}
$schemas[$index['Key_name']][] = $index;
}
$result = [];
foreach ($schemas as $name => $index) {
$result[] = MySQLIndex::createInstance($this->getFullName(), $name, $index);
}
return $result;
}
#[\Override]
protected function fetchReferences(): array
{
$references = $this->driver->query(
'SELECT * FROM `information_schema`.`referential_constraints`
WHERE `constraint_schema` = ? AND `table_name` = ?',
[$this->driver->getSource(), $this->getFullName()],
)->fetchAll();
if ($references === []) {
return [];
}
// `key_column_usage` is an expensive view, it must not be queried per constraint.
$usage = [];
$rows = $this->driver->query(
'SELECT * FROM `information_schema`.`key_column_usage`
WHERE `table_schema` = ? AND `table_name` = ? AND `referenced_table_name` IS NOT NULL
ORDER BY `constraint_name`, `ordinal_position`',
[$this->driver->getSource(), $this->getFullName()],
);
foreach ($rows as $row) {
$usage[$row['CONSTRAINT_NAME']][] = $row;
}
$result = [];
foreach ($references as $schema) {
$schema['COLUMN_NAME'] = [];
$schema['REFERENCED_COLUMN_NAME'] = [];
foreach ($usage[$schema['CONSTRAINT_NAME']] ?? [] as $column) {
$schema['COLUMN_NAME'][] = $column['COLUMN_NAME'];
$schema['REFERENCED_COLUMN_NAME'][] = $column['REFERENCED_COLUMN_NAME'];
}
$result[] = MySQLForeignKey::createInstance(
$this->getFullName(),
$this->getPrefix(),
$schema,
);
}
return $result;
}
/**
* Fetching primary keys from table.
*/
#[\Override]
protected function fetchPrimaryKeys(): array
{
$primaryKeys = [];
foreach ($this->indexRows() as $index) {
if ($index['Key_name'] === 'PRIMARY') {
$primaryKeys[] = $index['Column_name'];
}
}
return $primaryKeys;
}
/**
* @psalm-param non-empty-string $name
*/
#[\Override]
protected function createColumn(string $name): AbstractColumn
{
return new MySQLColumn($this->getFullName(), $name, $this->driver->getTimezone());
}
/**
* @psalm-param non-empty-string $name
*/
#[\Override]
protected function createIndex(string $name): AbstractIndex
{
return new MySQLIndex($this->getFullName(), $name);
}
/**
* @psalm-param non-empty-string $name
*/
#[\Override]
protected function createForeign(string $name): AbstractForeignKey
{
return new MySQLForeignKey($this->getFullName(), $this->getPrefix(), $name);
}
/**
* `SHOW INDEXES` carries both the secondary indexes and the primary key, so it is executed once
* per introspection and split in PHP.
*/
private function indexRows(): array
{
return $this->indexRows ??= $this->driver
->query("SHOW INDEXES FROM {$this->driver->identifier($this->getFullName())}")
->fetchAll();
}
}