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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Setup\Declaration\Schema\Dto;
use Magento\Framework\Setup\Declaration\Schema\Dto\Constraints\Internal;
use Magento\Framework\Setup\Declaration\Schema\Dto\Constraints\Reference;
/**
* Table structural element
* Aggregate inside itself: columns, constraints and indexes
* Resource is also specified on this strucural element
*/
class Table extends GenericElement implements
ElementInterface,
ElementDiffAwareInterface
{
/**
* In case if we will need to change this object: add, modify or drop, we will need
* to define it by its type
*/
const TYPE = 'table';
/**
* @var Constraint[]
*/
private $constraints = [];
/**
* @var Column[]
*/
private $columns = [];
/**
* @var string
*/
protected $type = 'table';
/**
* @var Index[]
*/
private $indexes = [];
/**
* @var string
*/
private $resource;
/**
* @var string
*/
private $engine;
/**
* @var string
*/
private $nameWithoutPrefix;
/**
* @var null|string
*/
private $comment;
/**
* @var string
*/
private $onCreate;
/**
* @var string
*/
private $charset;
/**
* @var string
*/
private $collation;
/**
* @param string $name
* @param string $type
* @param string $nameWithoutPrefix
* @param string $resource
* @param string $engine
* @param string $charset
* @param string $collation
* @param string $onCreate
* @param string|null $comment
* @param array $columns
* @param array $indexes
* @param array $constraints
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
string $name,
string $type,
string $nameWithoutPrefix,
string $resource,
string $engine,
string $charset,
string $collation,
string $onCreate,
string $comment = null,
array $columns = [],
array $indexes = [],
array $constraints = []
) {
parent::__construct($name, $type);
$this->columns = $columns;
$this->indexes = $indexes;
$this->constraints = $constraints;
$this->resource = $resource;
$this->engine = $engine;
$this->nameWithoutPrefix = $nameWithoutPrefix;
$this->comment = $comment;
$this->onCreate = $onCreate;
$this->charset = $charset;
$this->collation = $collation;
}
/**
* Return different table constraints.
*
* It can be constraint like unique key or reference to another table, etc
*
* @return Constraint[]
*/
public function getConstraints()
{
return $this->constraints;
}
/**
* Get constraint by name.
*
* @param string $name
* @return Constraint | bool
*/
public function getConstraintByName($name)
{
return $this->constraints[$name] ?? false;
}
/**
* This method lookup only for foreign keys constraints
*
* @return Reference[]
*/
public function getReferenceConstraints()
{
$constraints = [];
foreach ($this->getConstraints() as $constraint) {
if ($constraint instanceof Reference) {
$constraints[$constraint->getName()] = $constraint;
}
}
return $constraints;
}
/**
* Returns primary constraint
*
* As primary constraint always have one name
* and can be only one for table
* it name is allocated into it constraint
*
* @return bool|Internal
*/
public function getPrimaryConstraint()
{
return $this->constraints[Internal::PRIMARY_NAME] ?? false;
}
/**
* Retrieve internal constraints
*
* @return array
*/
public function getInternalConstraints() : array
{
$constraints = [];
foreach ($this->getConstraints() as $constraint) {
if ($constraint instanceof Internal) {
$constraints[] = $constraint;
}
}
return $constraints;
}
/**
* Get index by name
*
* @param string $name
* @return Index | bool
*/
public function getIndexByName($name)
{
return $this->indexes[$name] ?? false;
}
/**
* Return all columns.
*
* Note, table always must have columns
*
* @return Column[]
*/
public function getColumns()
{
return $this->columns;
}
/**
* Return all indexes, that are applied to table
*
* @return Index[]
*/
public function getIndexes()
{
return $this->indexes;
}
/**
* Retrieve shard name, on which table will exists
*
* @return string
*/
public function getResource()
{
return $this->resource;
}
/**
* Add constraints
*
* This is workaround, as any DTO object couldnt be changed after instantiation.
* However there is case, when we have 2 tables with constraints in different tables,
* that depends to each other table. So we need to setup DTO first and only then pass
* problematic constraints to it, in order to avoid circular dependency.
*
* @param Constraint[] $constraints
*/
public function addConstraints(array $constraints)
{
$this->constraints = array_replace($this->constraints, $constraints);
}
/**
* Add columns
*
* @param Column[] $columns
*/
public function addColumns(array $columns)
{
$this->columns = array_replace($this->columns, $columns);
}
/**
* Retrieve information about trigger
*
* @return string
*/
public function getOnCreate()
{
return $this->onCreate;
}
/**
* If column exists - retrieve column
*
* @param string $nameOrId
* @return Column | bool
*/
public function getColumnByName($nameOrId)
{
if (isset($this->columns[$nameOrId])) {
return $this->columns[$nameOrId];
}
return false;
}
/**
* Retrieve elements by specific type
*
* Allowed types: columns, constraints, indexes...
*
* @param string $type
* @return ElementInterface[]
*/
public function getElementsByType($type)
{
if (!isset($this->{$type})) {
throw new \InvalidArgumentException(sprintf("Type %s is not defined", $type));
}
return $this->{$type};
}
/**
* Add indexes
*
* This is workaround, as any DTO object couldnt be changed after instantiation.
* However there is case, when we depends on column definition we need modify our indexes
*
* @param array $indexes
*/
public function addIndexes(array $indexes)
{
$this->indexes = array_replace($this->indexes, $indexes);
}
/**
* @inheritdoc
*/
public function getElementType()
{
return self::TYPE;
}
/**
* Get engine name
*
* @return string
*/
public function getEngine(): string
{
return $this->engine;
}
/**
* @inheritdoc
*/
public function getDiffSensitiveParams()
{
return [
'resource' => $this->getResource(),
'engine' => $this->getEngine(),
'comment' => $this->getComment(),
'charset' => $this->getCharset(),
'collation' => $this->getCollation()
];
}
/**
* Return charset of table
*
* @return string
*/
public function getCharset() : string
{
return $this->charset;
}
/**
* Return charset of table
*
* @return string
*/
public function getCollation() : string
{
return $this->collation;
}
/**
* Get name without prefix
*
* @return string
*/
public function getNameWithoutPrefix(): string
{
return $this->nameWithoutPrefix;
}
/**
* Get comment
*
* @return null|string
*/
public function getComment()
{
return $this->comment;
}
}