|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Schema; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\DBALException; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Visitor\Visitor; |
|
9
|
|
|
use Doctrine\DBAL\Types\Type; |
|
10
|
|
|
use function array_keys; |
|
11
|
|
|
use function array_merge; |
|
12
|
|
|
use function array_search; |
|
13
|
|
|
use function array_unique; |
|
14
|
|
|
use function in_array; |
|
15
|
|
|
use function is_string; |
|
16
|
|
|
use function preg_match; |
|
17
|
|
|
use function strlen; |
|
18
|
|
|
use function strtolower; |
|
19
|
|
|
use function uksort; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Object Representation of a table. |
|
23
|
|
|
*/ |
|
24
|
|
|
class Table extends AbstractAsset |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var Column[] */ |
|
27
|
|
|
protected $_columns = []; |
|
28
|
|
|
|
|
29
|
|
|
/** @var Index[] */ |
|
30
|
|
|
private $implicitIndexes = []; |
|
31
|
|
|
|
|
32
|
|
|
/** @var Index[] */ |
|
33
|
|
|
protected $_indexes = []; |
|
34
|
|
|
|
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
protected $_primaryKeyName = false; |
|
37
|
|
|
|
|
38
|
|
|
/** @var UniqueConstraint[] */ |
|
39
|
|
|
protected $_uniqueConstraints = []; |
|
40
|
|
|
|
|
41
|
|
|
/** @var ForeignKeyConstraint[] */ |
|
42
|
|
|
protected $_fkConstraints = []; |
|
43
|
|
|
|
|
44
|
|
|
/** @var mixed[] */ |
|
45
|
|
|
protected $_options = []; |
|
46
|
|
|
|
|
47
|
|
|
/** @var SchemaConfig|null */ |
|
48
|
|
|
protected $_schemaConfig = null; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $tableName |
|
52
|
|
|
* @param Column[] $columns |
|
53
|
|
|
* @param Index[] $indexes |
|
54
|
|
|
* @param UniqueConstraint[] $uniqueConstraints |
|
55
|
|
|
* @param ForeignKeyConstraint[] $fkConstraints |
|
56
|
|
|
* @param mixed[] $options |
|
57
|
|
|
* |
|
58
|
|
|
* @throws DBALException |
|
59
|
|
|
*/ |
|
60
|
18284 |
|
public function __construct( |
|
61
|
|
|
$tableName, |
|
62
|
|
|
array $columns = [], |
|
63
|
|
|
array $indexes = [], |
|
64
|
|
|
array $uniqueConstraints = [], |
|
65
|
|
|
array $fkConstraints = [], |
|
66
|
|
|
array $options = [] |
|
67
|
|
|
) { |
|
68
|
18284 |
|
if (strlen($tableName) === 0) { |
|
69
|
1602 |
|
throw DBALException::invalidTableName($tableName); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
18283 |
|
$this->_setName($tableName); |
|
73
|
|
|
|
|
74
|
18283 |
|
foreach ($columns as $column) { |
|
75
|
16785 |
|
$this->_addColumn($column); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
18282 |
|
foreach ($indexes as $idx) { |
|
79
|
16710 |
|
$this->_addIndex($idx); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
18280 |
|
foreach ($uniqueConstraints as $uniqueConstraint) { |
|
83
|
|
|
$this->_addUniqueConstraint($uniqueConstraint); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
18280 |
|
foreach ($fkConstraints as $fkConstraint) { |
|
87
|
15924 |
|
$this->_addForeignKeyConstraint($fkConstraint); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
18280 |
|
$this->_options = $options; |
|
91
|
18280 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
17293 |
|
public function setSchemaConfig(SchemaConfig $schemaConfig) |
|
97
|
|
|
{ |
|
98
|
17293 |
|
$this->_schemaConfig = $schemaConfig; |
|
99
|
17293 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets the Primary Key. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string[] $columnNames |
|
105
|
|
|
* @param string|false $indexName |
|
106
|
|
|
* |
|
107
|
|
|
* @return self |
|
108
|
|
|
*/ |
|
109
|
17884 |
|
public function setPrimaryKey(array $columnNames, $indexName = false) |
|
110
|
|
|
{ |
|
111
|
17884 |
|
$this->_addIndex($this->_createIndex($columnNames, $indexName ?: 'primary', true, true)); |
|
112
|
|
|
|
|
113
|
17884 |
|
foreach ($columnNames as $columnName) { |
|
114
|
17884 |
|
$column = $this->getColumn($columnName); |
|
115
|
17884 |
|
$column->setNotnull(true); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
17884 |
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param mixed[] $columnNames |
|
123
|
|
|
* @param string|null $indexName |
|
124
|
|
|
* @param string[] $flags |
|
125
|
|
|
* @param mixed[] $options |
|
126
|
|
|
* |
|
127
|
|
|
* @return self |
|
128
|
|
|
*/ |
|
129
|
|
|
public function addUniqueConstraint(array $columnNames, $indexName = null, array $flags = [], array $options = []) |
|
130
|
|
|
{ |
|
131
|
|
|
if ($indexName === null) { |
|
132
|
|
|
$indexName = $this->_generateIdentifierName( |
|
133
|
|
|
array_merge([$this->getName()], $columnNames), |
|
134
|
|
|
'uniq', |
|
135
|
|
|
$this->_getMaxIdentifierLength() |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->_addUniqueConstraint($this->_createUniqueConstraint($columnNames, $indexName, $flags, $options)); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string[] $columnNames |
|
144
|
|
|
* @param string|null $indexName |
|
145
|
|
|
* @param string[] $flags |
|
146
|
|
|
* @param mixed[] $options |
|
147
|
|
|
* |
|
148
|
|
|
* @return self |
|
149
|
|
|
*/ |
|
150
|
16305 |
|
public function addIndex(array $columnNames, $indexName = null, array $flags = [], array $options = []) |
|
151
|
|
|
{ |
|
152
|
16305 |
|
if ($indexName === null) { |
|
153
|
15817 |
|
$indexName = $this->_generateIdentifierName( |
|
154
|
15817 |
|
array_merge([$this->getName()], $columnNames), |
|
155
|
15817 |
|
'idx', |
|
156
|
15817 |
|
$this->_getMaxIdentifierLength() |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
16305 |
|
return $this->_addIndex($this->_createIndex($columnNames, $indexName, false, false, $flags, $options)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Drops the primary key from this table. |
|
165
|
|
|
* |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
15033 |
|
public function dropPrimaryKey() |
|
169
|
|
|
{ |
|
170
|
15033 |
|
$this->dropIndex($this->_primaryKeyName); |
|
171
|
15033 |
|
$this->_primaryKeyName = false; |
|
|
|
|
|
|
172
|
15033 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Drops an index from this table. |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $indexName The index name. |
|
178
|
|
|
* |
|
179
|
|
|
* @return void |
|
180
|
|
|
* |
|
181
|
|
|
* @throws SchemaException If the index does not exist. |
|
182
|
|
|
*/ |
|
183
|
15057 |
|
public function dropIndex($indexName) |
|
184
|
|
|
{ |
|
185
|
15057 |
|
$indexName = $this->normalizeIdentifier($indexName); |
|
186
|
|
|
|
|
187
|
15057 |
|
if (! $this->hasIndex($indexName)) { |
|
188
|
|
|
throw SchemaException::indexDoesNotExist($indexName, $this->_name); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
15057 |
|
unset($this->_indexes[$indexName]); |
|
192
|
15057 |
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param string[] $columnNames |
|
196
|
|
|
* @param string|null $indexName |
|
197
|
|
|
* @param mixed[] $options |
|
198
|
|
|
* |
|
199
|
|
|
* @return self |
|
200
|
|
|
*/ |
|
201
|
17175 |
|
public function addUniqueIndex(array $columnNames, $indexName = null, array $options = []) |
|
202
|
|
|
{ |
|
203
|
17175 |
|
if ($indexName === null) { |
|
204
|
16841 |
|
$indexName = $this->_generateIdentifierName( |
|
205
|
16841 |
|
array_merge([$this->getName()], $columnNames), |
|
206
|
16841 |
|
'uniq', |
|
207
|
16841 |
|
$this->_getMaxIdentifierLength() |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
17175 |
|
return $this->_addIndex($this->_createIndex($columnNames, $indexName, true, false, [], $options)); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Renames an index. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $oldIndexName The name of the index to rename from. |
|
218
|
|
|
* @param string|null $newIndexName The name of the index to rename to. |
|
219
|
|
|
* If null is given, the index name will be auto-generated. |
|
220
|
|
|
* |
|
221
|
|
|
* @return self This table instance. |
|
222
|
|
|
* |
|
223
|
|
|
* @throws SchemaException If no index exists for the given current name |
|
224
|
|
|
* or if an index with the given new name already exists on this table. |
|
225
|
|
|
*/ |
|
226
|
14374 |
|
public function renameIndex($oldIndexName, $newIndexName = null) |
|
227
|
|
|
{ |
|
228
|
14374 |
|
$oldIndexName = $this->normalizeIdentifier($oldIndexName); |
|
229
|
14374 |
|
$normalizedNewIndexName = $this->normalizeIdentifier($newIndexName); |
|
230
|
|
|
|
|
231
|
14374 |
|
if ($oldIndexName === $normalizedNewIndexName) { |
|
232
|
460 |
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
14374 |
|
if (! $this->hasIndex($oldIndexName)) { |
|
236
|
377 |
|
throw SchemaException::indexDoesNotExist($oldIndexName, $this->_name); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
14373 |
|
if ($this->hasIndex($normalizedNewIndexName)) { |
|
240
|
352 |
|
throw SchemaException::indexAlreadyExists($normalizedNewIndexName, $this->_name); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
14372 |
|
$oldIndex = $this->_indexes[$oldIndexName]; |
|
244
|
|
|
|
|
245
|
14372 |
|
if ($oldIndex->isPrimary()) { |
|
246
|
452 |
|
$this->dropPrimaryKey(); |
|
247
|
|
|
|
|
248
|
452 |
|
return $this->setPrimaryKey($oldIndex->getColumns(), $newIndexName ?? false); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
14372 |
|
unset($this->_indexes[$oldIndexName]); |
|
252
|
|
|
|
|
253
|
14372 |
|
if ($oldIndex->isUnique()) { |
|
254
|
453 |
|
return $this->addUniqueIndex($oldIndex->getColumns(), $newIndexName, $oldIndex->getOptions()); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
14371 |
|
return $this->addIndex($oldIndex->getColumns(), $newIndexName, $oldIndex->getFlags(), $oldIndex->getOptions()); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Checks if an index begins in the order of the given columns. |
|
262
|
|
|
* |
|
263
|
|
|
* @param string[] $columnNames |
|
264
|
|
|
* |
|
265
|
|
|
* @return bool |
|
266
|
|
|
*/ |
|
267
|
14558 |
|
public function columnsAreIndexed(array $columnNames) |
|
268
|
|
|
{ |
|
269
|
14558 |
|
foreach ($this->getIndexes() as $index) { |
|
270
|
|
|
/** @var $index Index */ |
|
271
|
14558 |
|
if ($index->spansColumns($columnNames)) { |
|
272
|
14558 |
|
return true; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return false; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @param string $columnName |
|
281
|
|
|
* @param string $typeName |
|
282
|
|
|
* @param mixed[] $options |
|
283
|
|
|
* |
|
284
|
|
|
* @return Column |
|
285
|
|
|
*/ |
|
286
|
18164 |
|
public function addColumn($columnName, $typeName, array $options = []) |
|
287
|
|
|
{ |
|
288
|
18164 |
|
$column = new Column($columnName, Type::getType($typeName), $options); |
|
289
|
|
|
|
|
290
|
18164 |
|
$this->_addColumn($column); |
|
291
|
|
|
|
|
292
|
18164 |
|
return $column; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Change Column Details. |
|
297
|
|
|
* |
|
298
|
|
|
* @param string $columnName |
|
299
|
|
|
* @param mixed[] $options |
|
300
|
|
|
* |
|
301
|
|
|
* @return self |
|
302
|
|
|
*/ |
|
303
|
|
|
public function changeColumn($columnName, array $options) |
|
304
|
|
|
{ |
|
305
|
|
|
$column = $this->getColumn($columnName); |
|
306
|
|
|
|
|
307
|
|
|
$column->setOptions($options); |
|
308
|
|
|
|
|
309
|
|
|
return $this; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Drops a Column from the Table. |
|
314
|
|
|
* |
|
315
|
|
|
* @param string $columnName |
|
316
|
|
|
* |
|
317
|
|
|
* @return self |
|
318
|
|
|
*/ |
|
319
|
|
|
public function dropColumn($columnName) |
|
320
|
|
|
{ |
|
321
|
|
|
$columnName = $this->normalizeIdentifier($columnName); |
|
322
|
15400 |
|
|
|
323
|
|
|
unset($this->_columns[$columnName]); |
|
324
|
15400 |
|
|
|
325
|
|
|
return $this; |
|
326
|
15400 |
|
} |
|
327
|
|
|
|
|
328
|
15400 |
|
/** |
|
329
|
|
|
* Adds a foreign key constraint. |
|
330
|
|
|
* |
|
331
|
|
|
* Name is inferred from the local columns. |
|
332
|
|
|
* |
|
333
|
|
|
* @param Table|string $foreignTable Table schema instance or table name |
|
334
|
|
|
* @param string[] $localColumnNames |
|
335
|
|
|
* @param string[] $foreignColumnNames |
|
336
|
|
|
* @param mixed[] $options |
|
337
|
|
|
* @param string|null $name |
|
338
|
1485 |
|
* |
|
339
|
|
|
* @return self |
|
340
|
1485 |
|
*/ |
|
341
|
|
|
public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = [], $name = null) |
|
342
|
1485 |
|
{ |
|
343
|
|
|
if (! $name) { |
|
344
|
1485 |
|
$name = $this->_generateIdentifierName( |
|
345
|
|
|
array_merge((array) $this->getName(), $localColumnNames), |
|
346
|
|
|
'fk', |
|
347
|
|
|
$this->_getMaxIdentifierLength() |
|
348
|
|
|
); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
if ($foreignTable instanceof Table) { |
|
352
|
|
|
foreach ($foreignColumnNames as $columnName) { |
|
353
|
|
|
if (! $foreignTable->hasColumn($columnName)) { |
|
354
|
|
|
throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName()); |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
foreach ($localColumnNames as $columnName) { |
|
360
|
17118 |
|
if (! $this->hasColumn($columnName)) { |
|
361
|
|
|
throw SchemaException::columnDoesNotExist($columnName, $this->_name); |
|
362
|
17118 |
|
} |
|
363
|
17072 |
|
} |
|
364
|
17072 |
|
|
|
365
|
17072 |
|
$constraint = new ForeignKeyConstraint( |
|
366
|
17072 |
|
$localColumnNames, |
|
367
|
|
|
$foreignTable, |
|
368
|
|
|
$foreignColumnNames, |
|
369
|
|
|
$name, |
|
370
|
17118 |
|
$options |
|
371
|
|
|
); |
|
372
|
|
|
|
|
373
|
|
|
return $this->_addForeignKeyConstraint($constraint); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* @param string $name |
|
378
|
|
|
* @param mixed $value |
|
379
|
|
|
* |
|
380
|
|
|
* @return self |
|
381
|
|
|
*/ |
|
382
|
|
|
public function addOption($name, $value) |
|
383
|
|
|
{ |
|
384
|
|
|
$this->_options[$name] = $value; |
|
385
|
|
|
|
|
386
|
|
|
return $this; |
|
387
|
10643 |
|
} |
|
388
|
|
|
|
|
389
|
10643 |
|
/** |
|
390
|
|
|
* Returns whether this table has a foreign key constraint with the given name. |
|
391
|
|
|
* |
|
392
|
|
|
* @param string $constraintName |
|
393
|
|
|
* |
|
394
|
|
|
* @return bool |
|
395
|
|
|
*/ |
|
396
|
|
|
public function hasForeignKey($constraintName) |
|
397
|
|
|
{ |
|
398
|
|
|
$constraintName = $this->normalizeIdentifier($constraintName); |
|
399
|
|
|
|
|
400
|
|
|
return isset($this->_fkConstraints[$constraintName]); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* Returns the foreign key constraint with the given name. |
|
405
|
|
|
* |
|
406
|
|
|
* @param string $constraintName The constraint name. |
|
407
|
17119 |
|
* |
|
408
|
|
|
* @return ForeignKeyConstraint |
|
409
|
17119 |
|
* |
|
410
|
16686 |
|
* @throws SchemaException If the foreign key does not exist. |
|
411
|
16686 |
|
*/ |
|
412
|
1072 |
|
public function getForeignKey($constraintName) |
|
413
|
|
|
{ |
|
414
|
|
|
$constraintName = $this->normalizeIdentifier($constraintName); |
|
415
|
|
|
|
|
416
|
|
|
if (! $this->hasForeignKey($constraintName)) { |
|
417
|
17118 |
|
throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name); |
|
418
|
17118 |
|
} |
|
419
|
1141 |
|
|
|
420
|
|
|
return $this->_fkConstraints[$constraintName]; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
17117 |
|
/** |
|
424
|
17117 |
|
* Removes the foreign key constraint with the given name. |
|
425
|
89 |
|
* |
|
426
|
89 |
|
* @param string $constraintName The constraint name. |
|
427
|
89 |
|
* |
|
428
|
89 |
|
* @return void |
|
429
|
|
|
* |
|
430
|
|
|
* @throws SchemaException |
|
431
|
17117 |
|
*/ |
|
432
|
|
|
public function removeForeignKey($constraintName) |
|
433
|
|
|
{ |
|
434
|
|
|
$constraintName = $this->normalizeIdentifier($constraintName); |
|
435
|
|
|
|
|
436
|
|
|
if (! $this->hasForeignKey($constraintName)) { |
|
437
|
|
|
throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name); |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
15311 |
|
unset($this->_fkConstraints[$constraintName]); |
|
441
|
|
|
} |
|
442
|
15311 |
|
|
|
443
|
|
|
/** |
|
444
|
15311 |
|
* Returns whether this table has a unique constraint with the given name. |
|
445
|
|
|
* |
|
446
|
|
|
* @param string $constraintName |
|
447
|
|
|
* |
|
448
|
|
|
* @return bool |
|
449
|
|
|
*/ |
|
450
|
|
|
public function hasUniqueConstraint($constraintName) |
|
451
|
|
|
{ |
|
452
|
|
|
$constraintName = $this->normalizeIdentifier($constraintName); |
|
453
|
|
|
|
|
454
|
14362 |
|
return isset($this->_uniqueConstraints[$constraintName]); |
|
455
|
|
|
} |
|
456
|
14362 |
|
|
|
457
|
|
|
/** |
|
458
|
14362 |
|
* Returns the unique constraint with the given name. |
|
459
|
|
|
* |
|
460
|
|
|
* @param string $constraintName The constraint name. |
|
461
|
|
|
* |
|
462
|
|
|
* @return UniqueConstraint |
|
463
|
|
|
* |
|
464
|
|
|
* @throws SchemaException If the foreign key does not exist. |
|
465
|
|
|
*/ |
|
466
|
|
|
public function getUniqueConstraint($constraintName) |
|
467
|
|
|
{ |
|
468
|
|
|
$constraintName = $this->normalizeIdentifier($constraintName); |
|
469
|
|
|
|
|
470
|
335 |
|
if (! $this->hasUniqueConstraint($constraintName)) { |
|
471
|
|
|
throw SchemaException::uniqueConstraintDoesNotExist($constraintName, $this->_name); |
|
472
|
335 |
|
} |
|
473
|
|
|
|
|
474
|
335 |
|
return $this->_uniqueConstraints[$constraintName]; |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
/** |
|
478
|
335 |
|
* Removes the unique constraint with the given name. |
|
479
|
|
|
* |
|
480
|
|
|
* @param string $constraintName The constraint name. |
|
481
|
|
|
* |
|
482
|
|
|
* @return void |
|
483
|
|
|
* |
|
484
|
|
|
* @throws SchemaException |
|
485
|
|
|
*/ |
|
486
|
|
|
public function removeUniqueConstraint($constraintName) |
|
487
|
|
|
{ |
|
488
|
|
|
$constraintName = $this->normalizeIdentifier($constraintName); |
|
489
|
|
|
|
|
490
|
336 |
|
if (! $this->hasUniqueConstraint($constraintName)) { |
|
491
|
|
|
throw SchemaException::uniqueConstraintDoesNotExist($constraintName, $this->_name); |
|
492
|
336 |
|
} |
|
493
|
|
|
|
|
494
|
336 |
|
unset($this->_uniqueConstraints[$constraintName]); |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
/** |
|
498
|
336 |
|
* Returns ordered list of columns (primary keys are first, then foreign keys, then the rest) |
|
499
|
336 |
|
* |
|
500
|
|
|
* @return Column[] |
|
501
|
|
|
*/ |
|
502
|
|
|
public function getColumns() |
|
503
|
|
|
{ |
|
504
|
|
|
$columns = $this->_columns; |
|
505
|
|
|
$pkCols = []; |
|
506
|
|
|
$fkCols = []; |
|
507
|
|
|
|
|
508
|
|
|
$primaryKey = $this->getPrimaryKey(); |
|
509
|
|
|
|
|
510
|
|
|
if ($primaryKey !== null) { |
|
511
|
|
|
$pkCols = $primaryKey->getColumns(); |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
foreach ($this->getForeignKeys() as $fk) { |
|
515
|
|
|
/** @var ForeignKeyConstraint $fk */ |
|
516
|
|
|
$fkCols = array_merge($fkCols, $fk->getColumns()); |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
$colNames = array_unique(array_merge($pkCols, $fkCols, array_keys($columns))); |
|
520
|
|
|
|
|
521
|
|
|
uksort($columns, static function ($a, $b) use ($colNames) { |
|
522
|
|
|
return array_search($a, $colNames) >= array_search($b, $colNames); |
|
523
|
|
|
}); |
|
524
|
|
|
|
|
525
|
|
|
return $columns; |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
|
|
/** |
|
529
|
|
|
* Returns whether this table has a Column with the given name. |
|
530
|
|
|
* |
|
531
|
|
|
* @param string $columnName The column name. |
|
532
|
|
|
* |
|
533
|
|
|
* @return bool |
|
534
|
|
|
*/ |
|
535
|
|
|
public function hasColumn($columnName) |
|
536
|
|
|
{ |
|
537
|
|
|
$columnName = $this->normalizeIdentifier($columnName); |
|
538
|
|
|
|
|
539
|
|
|
return isset($this->_columns[$columnName]); |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
/** |
|
543
|
|
|
* Returns the Column with the given name. |
|
544
|
|
|
* |
|
545
|
|
|
* @param string $columnName The column name. |
|
546
|
|
|
* |
|
547
|
|
|
* @return Column |
|
548
|
|
|
* |
|
549
|
|
|
* @throws SchemaException If the column does not exist. |
|
550
|
|
|
*/ |
|
551
|
|
|
public function getColumn($columnName) |
|
552
|
|
|
{ |
|
553
|
|
|
$columnName = $this->normalizeIdentifier($columnName); |
|
554
|
|
|
|
|
555
|
|
|
if (! $this->hasColumn($columnName)) { |
|
556
|
|
|
throw SchemaException::columnDoesNotExist($columnName, $this->_name); |
|
557
|
|
|
} |
|
558
|
|
|
|
|
559
|
|
|
return $this->_columns[$columnName]; |
|
560
|
18041 |
|
} |
|
561
|
|
|
|
|
562
|
18041 |
|
/** |
|
563
|
18041 |
|
* Returns the primary key. |
|
564
|
18041 |
|
* |
|
565
|
|
|
* @return Index|null The primary key, or null if this Table has no primary key. |
|
566
|
18041 |
|
*/ |
|
567
|
|
|
public function getPrimaryKey() |
|
568
|
18041 |
|
{ |
|
569
|
17803 |
|
return $this->hasPrimaryKey() |
|
570
|
|
|
? $this->getIndex($this->_primaryKeyName) |
|
571
|
|
|
: null; |
|
572
|
18041 |
|
} |
|
573
|
|
|
|
|
574
|
17230 |
|
/** |
|
575
|
|
|
* Returns the primary key columns. |
|
576
|
|
|
* |
|
577
|
18041 |
|
* @return string[] |
|
578
|
|
|
* |
|
579
|
|
|
* @throws DBALException |
|
580
|
17880 |
|
*/ |
|
581
|
18041 |
|
public function getPrimaryKeyColumns() |
|
582
|
|
|
{ |
|
583
|
18041 |
|
$primaryKey = $this->getPrimaryKey(); |
|
584
|
|
|
|
|
585
|
|
|
if ($primaryKey === null) { |
|
586
|
|
|
throw new DBALException('Table ' . $this->getName() . ' has no primary key.'); |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
return $primaryKey->getColumns(); |
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
/** |
|
593
|
18068 |
|
* Returns whether this table has a primary key. |
|
594
|
|
|
* |
|
595
|
18068 |
|
* @return bool |
|
596
|
|
|
*/ |
|
597
|
18068 |
|
public function hasPrimaryKey() |
|
598
|
|
|
{ |
|
599
|
|
|
return $this->_primaryKeyName && $this->hasIndex($this->_primaryKeyName); |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
/** |
|
603
|
|
|
* Returns whether this table has an Index with the given name. |
|
604
|
|
|
* |
|
605
|
|
|
* @param string $indexName The index name. |
|
606
|
|
|
* |
|
607
|
|
|
* @return bool |
|
608
|
|
|
*/ |
|
609
|
17964 |
|
public function hasIndex($indexName) |
|
610
|
|
|
{ |
|
611
|
17964 |
|
$indexName = $this->normalizeIdentifier($indexName); |
|
612
|
|
|
|
|
613
|
17964 |
|
return isset($this->_indexes[$indexName]); |
|
614
|
1452 |
|
} |
|
615
|
|
|
|
|
616
|
|
|
/** |
|
617
|
17963 |
|
* Returns the Index with the given name. |
|
618
|
|
|
* |
|
619
|
|
|
* @param string $indexName The index name. |
|
620
|
|
|
* |
|
621
|
|
|
* @return Index |
|
622
|
|
|
* |
|
623
|
|
|
* @throws SchemaException If the index does not exist. |
|
624
|
|
|
*/ |
|
625
|
18046 |
|
public function getIndex($indexName) |
|
626
|
|
|
{ |
|
627
|
18046 |
|
$indexName = $this->normalizeIdentifier($indexName); |
|
628
|
17808 |
|
|
|
629
|
18046 |
|
if (! $this->hasIndex($indexName)) { |
|
630
|
|
|
throw SchemaException::indexDoesNotExist($indexName, $this->_name); |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
return $this->_indexes[$indexName]; |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
/** |
|
637
|
|
|
* @return Index[] |
|
638
|
|
|
*/ |
|
639
|
15028 |
|
public function getIndexes() |
|
640
|
|
|
{ |
|
641
|
15028 |
|
return $this->_indexes; |
|
642
|
|
|
} |
|
643
|
15028 |
|
|
|
644
|
|
|
/** |
|
645
|
|
|
* Returns the unique constraints. |
|
646
|
|
|
* |
|
647
|
15028 |
|
* @return UniqueConstraint[] |
|
648
|
|
|
*/ |
|
649
|
|
|
public function getUniqueConstraints() |
|
650
|
|
|
{ |
|
651
|
|
|
return $this->_uniqueConstraints; |
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
|
|
/** |
|
655
|
18049 |
|
* Returns the foreign key constraints. |
|
656
|
|
|
* |
|
657
|
18049 |
|
* @return ForeignKeyConstraint[] |
|
658
|
|
|
*/ |
|
659
|
|
|
public function getForeignKeys() |
|
660
|
|
|
{ |
|
661
|
|
|
return $this->_fkConstraints; |
|
662
|
|
|
} |
|
663
|
|
|
|
|
664
|
|
|
/** |
|
665
|
|
|
* @param string $name |
|
666
|
|
|
* |
|
667
|
17860 |
|
* @return bool |
|
668
|
|
|
*/ |
|
669
|
17860 |
|
public function hasOption($name) |
|
670
|
|
|
{ |
|
671
|
17860 |
|
return isset($this->_options[$name]); |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
/** |
|
675
|
|
|
* @param string $name |
|
676
|
|
|
* |
|
677
|
|
|
* @return mixed |
|
678
|
|
|
*/ |
|
679
|
|
|
public function getOption($name) |
|
680
|
|
|
{ |
|
681
|
|
|
return $this->_options[$name]; |
|
682
|
|
|
} |
|
683
|
17838 |
|
|
|
684
|
|
|
/** |
|
685
|
17838 |
|
* @return mixed[] |
|
686
|
|
|
*/ |
|
687
|
17838 |
|
public function getOptions() |
|
688
|
1327 |
|
{ |
|
689
|
|
|
return $this->_options; |
|
690
|
|
|
} |
|
691
|
17837 |
|
|
|
692
|
|
|
/** |
|
693
|
|
|
* @return void |
|
694
|
|
|
*/ |
|
695
|
|
|
public function visit(Visitor $visitor) |
|
696
|
|
|
{ |
|
697
|
18028 |
|
$visitor->acceptTable($this); |
|
698
|
|
|
|
|
699
|
18028 |
|
foreach ($this->getColumns() as $column) { |
|
700
|
|
|
$visitor->acceptColumn($this, $column); |
|
701
|
|
|
} |
|
702
|
|
|
|
|
703
|
|
|
foreach ($this->getIndexes() as $index) { |
|
704
|
|
|
$visitor->acceptIndex($this, $index); |
|
705
|
|
|
} |
|
706
|
|
|
|
|
707
|
17917 |
|
foreach ($this->getForeignKeys() as $constraint) { |
|
708
|
|
|
$visitor->acceptForeignKey($this, $constraint); |
|
709
|
17917 |
|
} |
|
710
|
|
|
} |
|
711
|
|
|
|
|
712
|
|
|
/** |
|
713
|
|
|
* Clone of a Table triggers a deep clone of all affected assets. |
|
714
|
|
|
* |
|
715
|
|
|
* @return void |
|
716
|
|
|
*/ |
|
717
|
18059 |
|
public function __clone() |
|
718
|
|
|
{ |
|
719
|
18059 |
|
foreach ($this->_columns as $k => $column) { |
|
720
|
|
|
$this->_columns[$k] = clone $column; |
|
721
|
|
|
} |
|
722
|
|
|
|
|
723
|
|
|
foreach ($this->_indexes as $k => $index) { |
|
724
|
|
|
$this->_indexes[$k] = clone $index; |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
15132 |
|
foreach ($this->_fkConstraints as $k => $fk) { |
|
728
|
|
|
$this->_fkConstraints[$k] = clone $fk; |
|
729
|
15132 |
|
$this->_fkConstraints[$k]->setLocalTable($this); |
|
730
|
|
|
} |
|
731
|
|
|
} |
|
732
|
|
|
|
|
733
|
|
|
/** |
|
734
|
|
|
* @return int |
|
735
|
|
|
*/ |
|
736
|
|
|
protected function _getMaxIdentifierLength() |
|
737
|
14689 |
|
{ |
|
738
|
|
|
return $this->_schemaConfig instanceof SchemaConfig |
|
739
|
14689 |
|
? $this->_schemaConfig->getMaxIdentifierLength() |
|
740
|
|
|
: 63; |
|
741
|
|
|
} |
|
742
|
|
|
|
|
743
|
|
|
/** |
|
744
|
|
|
* @return void |
|
745
|
17931 |
|
* |
|
746
|
|
|
* @throws SchemaException |
|
747
|
17931 |
|
*/ |
|
748
|
|
|
protected function _addColumn(Column $column) |
|
749
|
|
|
{ |
|
750
|
|
|
$columnName = $column->getName(); |
|
751
|
|
|
$columnName = $this->normalizeIdentifier($columnName); |
|
752
|
|
|
|
|
753
|
17071 |
|
if (isset($this->_columns[$columnName])) { |
|
754
|
|
|
throw SchemaException::columnAlreadyExists($this->getName(), $columnName); |
|
755
|
17071 |
|
} |
|
756
|
|
|
|
|
757
|
17071 |
|
$this->_columns[$columnName] = $column; |
|
758
|
17068 |
|
} |
|
759
|
|
|
|
|
760
|
|
|
/** |
|
761
|
17071 |
|
* Adds an index to the table. |
|
762
|
15523 |
|
* |
|
763
|
|
|
* @return self |
|
764
|
|
|
* |
|
765
|
17071 |
|
* @throws SchemaException |
|
766
|
154 |
|
*/ |
|
767
|
|
|
protected function _addIndex(Index $indexCandidate) |
|
768
|
17071 |
|
{ |
|
769
|
|
|
$indexName = $indexCandidate->getName(); |
|
770
|
|
|
$indexName = $this->normalizeIdentifier($indexName); |
|
771
|
|
|
$replacedImplicitIndexes = []; |
|
772
|
|
|
|
|
773
|
|
|
foreach ($this->implicitIndexes as $name => $implicitIndex) { |
|
774
|
|
|
if (! $implicitIndex->isFullfilledBy($indexCandidate) || ! isset($this->_indexes[$name])) { |
|
775
|
16413 |
|
continue; |
|
776
|
|
|
} |
|
777
|
16413 |
|
|
|
778
|
16412 |
|
$replacedImplicitIndexes[] = $name; |
|
779
|
|
|
} |
|
780
|
|
|
|
|
781
|
16413 |
|
if ((isset($this->_indexes[$indexName]) && ! in_array($indexName, $replacedImplicitIndexes, true)) || |
|
782
|
16401 |
|
($this->_primaryKeyName !== false && $indexCandidate->isPrimary()) |
|
783
|
|
|
) { |
|
784
|
|
|
throw SchemaException::indexAlreadyExists($indexName, $this->_name); |
|
785
|
16413 |
|
} |
|
786
|
15320 |
|
|
|
787
|
15320 |
|
foreach ($replacedImplicitIndexes as $name) { |
|
788
|
|
|
unset($this->_indexes[$name], $this->implicitIndexes[$name]); |
|
789
|
16413 |
|
} |
|
790
|
|
|
|
|
791
|
|
|
if ($indexCandidate->isPrimary()) { |
|
792
|
|
|
$this->_primaryKeyName = $indexName; |
|
793
|
|
|
} |
|
794
|
17407 |
|
|
|
795
|
|
|
$this->_indexes[$indexName] = $indexCandidate; |
|
796
|
17407 |
|
|
|
797
|
17146 |
|
return $this; |
|
798
|
17407 |
|
} |
|
799
|
|
|
|
|
800
|
|
|
/** |
|
801
|
|
|
* @return self |
|
802
|
|
|
*/ |
|
803
|
|
|
protected function _addUniqueConstraint(UniqueConstraint $constraint) |
|
804
|
|
|
{ |
|
805
|
|
|
$name = strlen($constraint->getName()) |
|
806
|
18193 |
|
? $constraint->getName() |
|
807
|
|
|
: $this->_generateIdentifierName( |
|
808
|
18193 |
|
array_merge((array) $this->getName(), $constraint->getLocalColumns()), |
|
|
|
|
|
|
809
|
18193 |
|
'fk', |
|
810
|
|
|
$this->_getMaxIdentifierLength() |
|
811
|
18193 |
|
); |
|
812
|
1427 |
|
|
|
813
|
|
|
$name = $this->normalizeIdentifier($name); |
|
814
|
|
|
|
|
815
|
18193 |
|
$this->_uniqueConstraints[$name] = $constraint; |
|
816
|
18193 |
|
|
|
817
|
|
|
// If there is already an index that fulfills this requirements drop the request. In the case of __construct |
|
818
|
|
|
// calling this method during hydration from schema-details all the explicitly added indexes lead to duplicates. |
|
819
|
|
|
// This creates computation overhead in this case, however no duplicate indexes are ever added (column based). |
|
820
|
|
|
$indexName = $this->_generateIdentifierName( |
|
821
|
|
|
array_merge([$this->getName()], $constraint->getColumns()), |
|
822
|
|
|
'idx', |
|
823
|
|
|
$this->_getMaxIdentifierLength() |
|
824
|
|
|
); |
|
825
|
18008 |
|
|
|
826
|
|
|
$indexCandidate = $this->_createIndex($constraint->getColumns(), $indexName, true, false); |
|
827
|
18008 |
|
|
|
828
|
18008 |
|
foreach ($this->_indexes as $existingIndex) { |
|
829
|
18008 |
|
if ($indexCandidate->isFullfilledBy($existingIndex)) { |
|
830
|
|
|
return $this; |
|
831
|
18008 |
|
} |
|
832
|
13794 |
|
} |
|
833
|
13790 |
|
|
|
834
|
|
|
$this->implicitIndexes[$this->normalizeIdentifier($indexName)] = $indexCandidate; |
|
835
|
|
|
|
|
836
|
730 |
|
return $this; |
|
837
|
|
|
} |
|
838
|
|
|
|
|
839
|
18008 |
|
/** |
|
840
|
18008 |
|
* @return self |
|
841
|
|
|
*/ |
|
842
|
1303 |
|
protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint) |
|
843
|
|
|
{ |
|
844
|
|
|
$constraint->setLocalTable($this); |
|
845
|
18008 |
|
|
|
846
|
730 |
|
$name = strlen($constraint->getName()) |
|
847
|
|
|
? $constraint->getName() |
|
848
|
|
|
: $this->_generateIdentifierName( |
|
849
|
18008 |
|
array_merge((array) $this->getName(), $constraint->getLocalColumns()), |
|
850
|
17886 |
|
'fk', |
|
851
|
|
|
$this->_getMaxIdentifierLength() |
|
852
|
|
|
); |
|
853
|
18008 |
|
|
|
854
|
|
|
$name = $this->normalizeIdentifier($name); |
|
855
|
18008 |
|
|
|
856
|
|
|
$this->_fkConstraints[$name] = $constraint; |
|
857
|
|
|
|
|
858
|
|
|
// add an explicit index on the foreign key columns. |
|
859
|
|
|
// If there is already an index that fulfills this requirements drop the request. In the case of __construct |
|
860
|
|
|
// calling this method during hydration from schema-details all the explicitly added indexes lead to duplicates. |
|
861
|
|
|
// This creates computation overhead in this case, however no duplicate indexes are ever added (column based). |
|
862
|
|
|
$indexName = $this->_generateIdentifierName( |
|
863
|
|
|
array_merge([$this->getName()], $constraint->getColumns()), |
|
864
|
|
|
'idx', |
|
865
|
|
|
$this->_getMaxIdentifierLength() |
|
866
|
|
|
); |
|
867
|
|
|
|
|
868
|
|
|
$indexCandidate = $this->_createIndex($constraint->getColumns(), $indexName, false, false); |
|
869
|
|
|
|
|
870
|
|
|
foreach ($this->_indexes as $existingIndex) { |
|
871
|
|
|
if ($indexCandidate->isFullfilledBy($existingIndex)) { |
|
872
|
|
|
return $this; |
|
873
|
|
|
} |
|
874
|
|
|
} |
|
875
|
|
|
|
|
876
|
|
|
$this->_addIndex($indexCandidate); |
|
877
|
|
|
$this->implicitIndexes[$this->normalizeIdentifier($indexName)] = $indexCandidate; |
|
878
|
|
|
|
|
879
|
|
|
return $this; |
|
880
|
|
|
} |
|
881
|
|
|
|
|
882
|
|
|
/** |
|
883
|
|
|
* Normalizes a given identifier. |
|
884
|
|
|
* |
|
885
|
|
|
* Trims quotes and lowercases the given identifier. |
|
886
|
|
|
* |
|
887
|
|
|
* @param string|null $identifier The identifier to normalize. |
|
888
|
|
|
* |
|
889
|
|
|
* @return string The normalized identifier. |
|
890
|
|
|
*/ |
|
891
|
|
|
private function normalizeIdentifier($identifier) |
|
892
|
|
|
{ |
|
893
|
|
|
if ($identifier === null) { |
|
894
|
|
|
return ''; |
|
895
|
|
|
} |
|
896
|
|
|
|
|
897
|
|
|
return $this->trimQuotes(strtolower($identifier)); |
|
898
|
|
|
} |
|
899
|
|
|
|
|
900
|
17280 |
|
/** |
|
901
|
|
|
* @param mixed[] $columns |
|
902
|
17280 |
|
* @param string $indexName |
|
903
|
|
|
* @param mixed[] $flags |
|
904
|
17280 |
|
* @param mixed[] $options |
|
905
|
17121 |
|
* |
|
906
|
2394 |
|
* @return UniqueConstraint |
|
907
|
2394 |
|
* |
|
908
|
2394 |
|
* @throws SchemaException |
|
909
|
17280 |
|
*/ |
|
910
|
|
|
private function _createUniqueConstraint(array $columns, $indexName, array $flags = [], array $options = []) |
|
911
|
|
|
{ |
|
912
|
17280 |
|
if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) { |
|
913
|
|
|
throw SchemaException::indexNameInvalid($indexName); |
|
914
|
17280 |
|
} |
|
915
|
|
|
|
|
916
|
|
|
foreach ($columns as $index => $value) { |
|
917
|
|
|
if (is_string($index)) { |
|
918
|
|
|
$columnName = $index; |
|
919
|
|
|
} else { |
|
920
|
17280 |
|
$columnName = $value; |
|
921
|
17280 |
|
} |
|
922
|
17280 |
|
|
|
923
|
17280 |
|
if (! $this->hasColumn($columnName)) { |
|
924
|
|
|
throw SchemaException::columnDoesNotExist($columnName, $this->_name); |
|
925
|
|
|
} |
|
926
|
17280 |
|
} |
|
927
|
|
|
|
|
928
|
17280 |
|
return new UniqueConstraint($indexName, $columns, $flags, $options); |
|
929
|
17254 |
|
} |
|
930
|
15822 |
|
|
|
931
|
|
|
/** |
|
932
|
|
|
* @param mixed[] $columns |
|
933
|
|
|
* @param string $indexName |
|
934
|
17256 |
|
* @param bool $isUnique |
|
935
|
17256 |
|
* @param bool $isPrimary |
|
936
|
|
|
* @param string[] $flags |
|
937
|
17256 |
|
* @param mixed[] $options |
|
938
|
|
|
* |
|
939
|
|
|
* @return Index |
|
940
|
|
|
* |
|
941
|
|
|
* @throws SchemaException |
|
942
|
|
|
*/ |
|
943
|
|
|
private function _createIndex(array $columns, $indexName, $isUnique, $isPrimary, array $flags = [], array $options = []) |
|
944
|
|
|
{ |
|
945
|
|
|
if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) { |
|
946
|
|
|
throw SchemaException::indexNameInvalid($indexName); |
|
947
|
|
|
} |
|
948
|
|
|
|
|
949
|
18197 |
|
foreach ($columns as $index => $value) { |
|
950
|
|
|
if (is_string($index)) { |
|
951
|
18197 |
|
$columnName = $index; |
|
952
|
452 |
|
} else { |
|
953
|
|
|
$columnName = $value; |
|
954
|
|
|
} |
|
955
|
18197 |
|
|
|
956
|
|
|
if (! $this->hasColumn($columnName)) { |
|
957
|
|
|
throw SchemaException::columnDoesNotExist($columnName, $this->_name); |
|
958
|
|
|
} |
|
959
|
|
|
} |
|
960
|
|
|
|
|
961
|
|
|
return new Index($indexName, $columns, $isUnique, $isPrimary, $flags, $options); |
|
962
|
|
|
} |
|
963
|
|
|
} |
|
964
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.