1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Database\Schemas; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* TableSchema helper used to store original table elements and run comparation between them. |
13
|
|
|
* |
14
|
|
|
* Attention: this state IS MUTABLE! |
15
|
|
|
*/ |
16
|
|
|
class TableState |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $name = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ColumnInterface[] |
25
|
|
|
*/ |
26
|
|
|
private $columns = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var IndexInterface[] |
30
|
|
|
*/ |
31
|
|
|
private $indexes = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ReferenceInterface[] |
35
|
|
|
*/ |
36
|
|
|
private $foreigns = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Primary key columns are stored separately from other indexes and can be modified only during |
40
|
|
|
* table creation. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $primaryKeys = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
*/ |
49
|
|
|
public function __construct(string $name) |
50
|
|
|
{ |
51
|
|
|
$this->name = $name; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set table name. Operation will be applied at moment of saving. |
56
|
|
|
* |
57
|
|
|
* @param string $name |
58
|
|
|
*/ |
59
|
|
|
public function setName(string $name) |
60
|
|
|
{ |
61
|
|
|
$this->name = $name; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function getName() |
68
|
|
|
{ |
69
|
|
|
return $this->name; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
* |
75
|
|
|
* Array key points to initial element name. |
76
|
|
|
* |
77
|
|
|
* @return ColumnInterface[] |
78
|
|
|
*/ |
79
|
|
|
public function getColumns() |
80
|
|
|
{ |
81
|
|
|
return $this->columns; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
* |
87
|
|
|
* Array key points to initial element name. |
88
|
|
|
* |
89
|
|
|
* @return IndexInterface[] |
90
|
|
|
*/ |
91
|
|
|
public function getIndexes() |
92
|
|
|
{ |
93
|
|
|
return $this->indexes; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
* |
99
|
|
|
* Array key points to initial element name. |
100
|
|
|
* |
101
|
|
|
* @return ReferenceInterface[] |
102
|
|
|
*/ |
103
|
|
|
public function getForeigns() |
104
|
|
|
{ |
105
|
|
|
return $this->foreigns; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set table primary keys. |
110
|
|
|
* |
111
|
|
|
* @param array $columns |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function setPrimaryKeys(array $columns) |
116
|
|
|
{ |
117
|
|
|
$this->primaryKeys = $columns; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function getPrimaryKeys() |
126
|
|
|
{ |
127
|
|
|
return $this->primaryKeys; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $name |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function knowsColumn(string $name): bool |
136
|
|
|
{ |
137
|
|
|
return isset($this->columns[$name]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $name |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function knowsIndex(string $name): bool |
146
|
|
|
{ |
147
|
|
|
return isset($this->indexes[$name]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $name |
152
|
|
|
* |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function knowsForeign(string $name): bool |
156
|
|
|
{ |
157
|
|
|
return isset($this->foreigns[$name]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
* |
163
|
|
|
* Lookup is performed based on initial column name. |
164
|
|
|
*/ |
165
|
|
|
public function hasColumn(string $name): bool |
166
|
|
|
{ |
167
|
|
|
return !empty($this->findColumn($name)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function hasIndex(array $columns = []): bool |
174
|
|
|
{ |
175
|
|
|
return !empty($this->findIndex($columns)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
public function hasForeign($column): bool |
182
|
|
|
{ |
183
|
|
|
return !empty($this->findForeign($column)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Register new column element. |
188
|
|
|
* |
189
|
|
|
* @param ColumnInterface $column |
190
|
|
|
* |
191
|
|
|
* @return ColumnInterface |
192
|
|
|
*/ |
193
|
|
|
public function registerColumn(ColumnInterface $column): ColumnInterface |
194
|
|
|
{ |
195
|
|
|
$this->columns[$column->getName()] = $column; |
196
|
|
|
|
197
|
|
|
return $column; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Register new index element. |
202
|
|
|
* |
203
|
|
|
* @param IndexInterface $index |
204
|
|
|
* |
205
|
|
|
* @return IndexInterface |
206
|
|
|
*/ |
207
|
|
|
public function registerIndex(IndexInterface $index): IndexInterface |
208
|
|
|
{ |
209
|
|
|
$this->indexes[$index->getName()] = $index; |
210
|
|
|
|
211
|
|
|
return $index; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Register new foreign key element. |
216
|
|
|
* |
217
|
|
|
* @param ReferenceInterface $foreign |
218
|
|
|
* |
219
|
|
|
* @return ReferenceInterface |
220
|
|
|
*/ |
221
|
|
|
public function registerReference(ReferenceInterface $foreign): ReferenceInterface |
222
|
|
|
{ |
223
|
|
|
$this->foreigns[$foreign->getName()] = $foreign; |
224
|
|
|
|
225
|
|
|
return $foreign; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Drop column from table schema. |
230
|
|
|
* |
231
|
|
|
* @param ColumnInterface $column |
232
|
|
|
* |
233
|
|
|
* @return self |
234
|
|
|
*/ |
235
|
|
|
public function forgetColumn(ColumnInterface $column): TableState |
236
|
|
|
{ |
237
|
|
|
foreach ($this->columns as $name => $columnSchema) { |
238
|
|
|
if ($columnSchema == $column) { |
239
|
|
|
unset($this->columns[$name]); |
240
|
|
|
break; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Drop index from table schema using it's name or forming columns. |
249
|
|
|
* |
250
|
|
|
* @param IndexInterface $index |
251
|
|
|
* |
252
|
|
|
* @return self |
253
|
|
|
*/ |
254
|
|
|
public function forgetIndex(IndexInterface $index): TableState |
255
|
|
|
{ |
256
|
|
|
foreach ($this->indexes as $name => $indexSchema) { |
257
|
|
|
if ($indexSchema == $index) { |
258
|
|
|
unset($this->indexes[$name]); |
259
|
|
|
break; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Drop foreign key from table schema using it's forming column. |
268
|
|
|
* |
269
|
|
|
* @param ReferenceInterface $foreign |
270
|
|
|
* |
271
|
|
|
* @return self |
272
|
|
|
*/ |
273
|
|
|
public function forgetForeign(ReferenceInterface $foreign): TableState |
274
|
|
|
{ |
275
|
|
|
foreach ($this->foreigns as $name => $foreignSchema) { |
276
|
|
|
if ($foreignSchema == $foreign) { |
277
|
|
|
unset($this->foreigns[$name]); |
278
|
|
|
break; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Find column by it's name or return null. |
287
|
|
|
* |
288
|
|
|
* @param string $name |
289
|
|
|
* |
290
|
|
|
* @return null|ColumnInterface |
291
|
|
|
*/ |
292
|
|
|
public function findColumn(string $name) |
293
|
|
|
{ |
294
|
|
|
foreach ($this->columns as $column) { |
295
|
|
|
if ($column->getName() == $name) { |
296
|
|
|
return $column; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return null; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Find index by it's columns or return null. |
305
|
|
|
* |
306
|
|
|
* @param array $columns |
307
|
|
|
* |
308
|
|
|
* @return null|IndexInterface |
309
|
|
|
*/ |
310
|
|
|
public function findIndex(array $columns) |
311
|
|
|
{ |
312
|
|
|
foreach ($this->indexes as $index) { |
313
|
|
|
if ($index->getColumns() == $columns) { |
314
|
|
|
return $index; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return null; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Find foreign key by it's column or return null. |
323
|
|
|
* |
324
|
|
|
* @param string $column |
325
|
|
|
* |
326
|
|
|
* @return null|ReferenceInterface |
327
|
|
|
*/ |
328
|
|
|
public function findForeign(string $column) |
329
|
|
|
{ |
330
|
|
|
foreach ($this->foreigns as $reference) { |
331
|
|
|
if ($reference->getColumn() == $column) { |
332
|
|
|
return $reference; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return null; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Remount elements under their current name. |
341
|
|
|
*/ |
342
|
|
|
public function remountElements() |
343
|
|
|
{ |
344
|
|
|
$columns = []; |
345
|
|
|
foreach ($this->columns as $column) { |
346
|
|
|
$columns[$column->getName()] = $column; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
$indexes = []; |
350
|
|
|
foreach ($this->indexes as $index) { |
351
|
|
|
$indexes[$index->getName()] = $index; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$references = []; |
355
|
|
|
foreach ($this->foreigns as $reference) { |
356
|
|
|
$references[$reference->getName()] = $reference; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
$this->columns = $columns; |
360
|
|
|
$this->indexes = $indexes; |
361
|
|
|
$this->foreigns = $references; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Re-populate schema elements using other state as source. Elements will be cloned under their |
366
|
|
|
* schema name. |
367
|
|
|
* |
368
|
|
|
* @param self $source |
369
|
|
|
* |
370
|
|
|
* @return self |
371
|
|
|
*/ |
372
|
|
|
public function syncState(self $source): self |
373
|
|
|
{ |
374
|
|
|
$this->name = $source->name; |
375
|
|
|
$this->primaryKeys = $source->primaryKeys; |
376
|
|
|
|
377
|
|
|
$this->columns = []; |
378
|
|
|
foreach ($source->columns as $name => $column) { |
379
|
|
|
$this->columns[$name] = clone $column; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$this->indexes = []; |
383
|
|
|
foreach ($source->indexes as $name => $index) { |
384
|
|
|
$this->indexes[$name] = clone $index; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$this->foreigns = []; |
388
|
|
|
foreach ($source->foreigns as $name => $reference) { |
389
|
|
|
$this->foreigns[$name] = clone $reference; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
return $this; |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|