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