-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Structure.php
276 lines (207 loc) 路 6.26 KB
/
Structure.php
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Database;
use Nette;
/**
* Cached reflection of database structure.
*/
class Structure implements IStructure
{
use Nette\SmartObject;
protected Connection $connection;
protected Nette\Caching\Cache $cache;
protected array $structure;
protected bool $isRebuilt = false;
public function __construct(Connection $connection, Nette\Caching\Storage $cacheStorage)
{
$this->connection = $connection;
$this->cache = new Nette\Caching\Cache($cacheStorage, 'Nette.Database.Structure.' . md5($this->connection->getDsn()));
}
public function getTables(): array
{
$this->needStructure();
return $this->structure['tables'];
}
public function getColumns(string $table): array
{
$this->needStructure();
$table = $this->resolveFQTableName($table);
return $this->structure['columns'][$table];
}
/**
* @return string|string[]|null
*/
public function getPrimaryKey(string $table): string|array|null
{
$this->needStructure();
$table = $this->resolveFQTableName($table);
return $this->structure['primary'][$table] ?? null;
}
public function getPrimaryAutoincrementKey(string $table): ?string
{
$primaryKey = $this->getPrimaryKey($table);
if (!$primaryKey) {
return null;
}
// Search for autoincrement key from multi primary key
if (is_array($primaryKey)) {
$keys = array_flip($primaryKey);
foreach ($this->getColumns($table) as $column) {
if (isset($keys[$column['name']]) && $column['autoincrement']) {
return $column['name'];
}
}
return null;
}
// Search for autoincrement key from simple primary key
foreach ($this->getColumns($table) as $column) {
if ($column['name'] === $primaryKey) {
return $column['autoincrement'] ? $column['name'] : null;
}
}
return null;
}
public function getPrimaryKeySequence(string $table): ?string
{
$this->needStructure();
$table = $this->resolveFQTableName($table);
if (!$this->connection->getDriver()->isSupported(Driver::SUPPORT_SEQUENCE)) {
return null;
}
$autoincrementPrimaryKeyName = $this->getPrimaryAutoincrementKey($table);
if (!$autoincrementPrimaryKeyName) {
return null;
}
// Search for sequence from simple primary key
foreach ($this->structure['columns'][$table] as $columnMeta) {
if ($columnMeta['name'] === $autoincrementPrimaryKeyName) {
return $columnMeta['vendor']['sequence'] ?? null;
}
}
return null;
}
public function getHasManyReference(string $table, string $targetTable = null): ?array
{
$this->needStructure();
$table = $this->resolveFQTableName($table);
if ($targetTable) {
$targetTable = $this->resolveFQTableName($targetTable);
foreach ($this->structure['hasMany'][$table] as $key => $value) {
if (strtolower($key) === $targetTable) {
return $this->structure['hasMany'][$table][$key];
}
}
return null;
} else {
return $this->structure['hasMany'][$table] ?? [];
}
}
public function getBelongsToReference(string $table, string $column = null): ?array
{
$this->needStructure();
$table = $this->resolveFQTableName($table);
if ($column) {
$column = strtolower($column);
return isset($this->structure['belongsTo'][$table][$column])
? [$this->structure['belongsTo'][$table][$column], $column]
: null;
} else {
return $this->structure['belongsTo'][$table] ?? [];
}
}
public function rebuild(): void
{
$this->structure = $this->loadStructure();
$this->cache->save('structure', $this->structure);
}
public function isRebuilt(): bool
{
return $this->isRebuilt;
}
protected function needStructure(): void
{
if (isset($this->structure)) {
return;
}
$this->structure = $this->cache->load('structure', \Closure::fromCallable([$this, 'loadStructure']));
}
protected function loadStructure(): array
{
$driver = $this->connection->getDriver();
$structure = [];
$structure['tables'] = $driver->getTables();
foreach ($structure['tables'] as $tablePair) {
if (isset($tablePair['fullName'])) {
$table = $tablePair['fullName'];
$structure['aliases'][strtolower($tablePair['name'])] = strtolower($table);
} else {
$table = $tablePair['name'];
}
$structure['columns'][strtolower($table)] = $columns = $driver->getColumns($table);
if (!$tablePair['view']) {
$structure['primary'][strtolower($table)] = $this->analyzePrimaryKey($columns);
$this->analyzeForeignKeys($structure, $table);
}
}
if (isset($structure['hasMany'])) {
foreach ($structure['hasMany'] as &$table) {
uksort($table, fn($a, $b): int => strlen($a) <=> strlen($b));
}
}
$this->isRebuilt = true;
return $structure;
}
protected function analyzePrimaryKey(array $columns)
{
$primary = [];
foreach ($columns as $column) {
if ($column['primary']) {
$primary[] = $column['name'];
}
}
if (count($primary) === 0) {
return null;
} elseif (count($primary) === 1) {
return reset($primary);
} else {
return $primary;
}
}
protected function analyzeForeignKeys(array &$structure, string $table): void
{
$lowerTable = strtolower($table);
$foreignKeys = $this->connection->getDriver()->getForeignKeys($table);
$fksColumnsCounts = [];
foreach ($foreignKeys as $foreignKey) {
$tmp = &$fksColumnsCounts[$foreignKey['name']];
$tmp++;
}
usort($foreignKeys, fn($a, $b): int => $fksColumnsCounts[$b['name']] <=> $fksColumnsCounts[$a['name']]);
foreach ($foreignKeys as $row) {
$structure['belongsTo'][$lowerTable][$row['local']] = $row['table'];
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'];
}
if (isset($structure['belongsTo'][$lowerTable])) {
uksort($structure['belongsTo'][$lowerTable], fn($a, $b): int => strlen($a) <=> strlen($b));
}
}
protected function resolveFQTableName(string $table): string
{
$name = strtolower($table);
if (isset($this->structure['columns'][$name])) {
return $name;
}
if (isset($this->structure['aliases'][$name])) {
return $this->structure['aliases'][$name];
}
if (!$this->isRebuilt()) {
$this->rebuild();
return $this->resolveFQTableName($table);
}
throw new Nette\InvalidArgumentException("Table '$name' does not exist.");
}
}