1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\TestSupport; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
use Yiisoft\Db\Constraint\CheckConstraint; |
9
|
|
|
use Yiisoft\Db\Constraint\Constraint; |
10
|
|
|
use Yiisoft\Db\Constraint\DefaultValueConstraint; |
11
|
|
|
use Yiisoft\Db\Constraint\ForeignKeyConstraint; |
12
|
|
|
use Yiisoft\Db\Constraint\IndexConstraint; |
13
|
|
|
use Yiisoft\Db\Schema\ColumnSchema; |
14
|
|
|
use Yiisoft\Db\Schema\Schema; |
15
|
|
|
|
16
|
|
|
use Yiisoft\Db\Schema\TableSchemaInterface; |
17
|
|
|
use function array_keys; |
18
|
|
|
use function fclose; |
19
|
|
|
use function fopen; |
20
|
|
|
use function gettype; |
21
|
|
|
use function is_array; |
22
|
|
|
use function json_encode; |
23
|
|
|
use function ksort; |
24
|
|
|
use function print_r; |
25
|
|
|
use function sort; |
26
|
|
|
use function sprintf; |
27
|
|
|
use function strtolower; |
28
|
|
|
|
29
|
|
|
trait TestSchemaTrait |
30
|
|
|
{ |
31
|
|
|
public function testGetTableSchemasWithAttrCase(): void |
32
|
|
|
{ |
33
|
|
|
$db = $this->getConnection(false); |
|
|
|
|
34
|
|
|
|
35
|
|
|
$db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); |
36
|
|
|
|
37
|
|
|
$this->assertCount(count($db->getSchema()->getTableNames()), $db->getSchema()->getTableSchemas()); |
|
|
|
|
38
|
|
|
|
39
|
|
|
$db->getActivePDO()->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); |
40
|
|
|
|
41
|
|
|
$this->assertCount(count($db->getSchema()->getTableNames()), $db->getSchema()->getTableSchemas()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testGetSchemaPrimaryKeys(): void |
45
|
|
|
{ |
46
|
|
|
$db = $this->getConnection(false); |
47
|
|
|
|
48
|
|
|
$tablePks = $db->getSchema()->getSchemaPrimaryKeys(); |
49
|
|
|
|
50
|
|
|
/** @psalm-suppress RedundantCondition */ |
51
|
|
|
$this->assertIsArray($tablePks); |
|
|
|
|
52
|
|
|
$this->assertContainsOnlyInstancesOf(Constraint::class, $tablePks); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testGetSchemaChecks(): void |
56
|
|
|
{ |
57
|
|
|
$db = $this->getConnection(false); |
58
|
|
|
|
59
|
|
|
$tableChecks = $db->getSchema()->getSchemaChecks(); |
60
|
|
|
|
61
|
|
|
/** @psalm-suppress RedundantCondition */ |
62
|
|
|
$this->assertIsArray($tableChecks); |
63
|
|
|
|
64
|
|
|
foreach ($tableChecks as $checks) { |
65
|
|
|
$this->assertIsArray($checks); |
66
|
|
|
$this->assertContainsOnlyInstancesOf(CheckConstraint::class, $checks); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGetSchemaDefaultValues(): void |
71
|
|
|
{ |
72
|
|
|
$db = $this->getConnection(false); |
73
|
|
|
|
74
|
|
|
$tableDefaultValues = $db->getSchema()->getSchemaDefaultValues(); |
75
|
|
|
|
76
|
|
|
/** @psalm-suppress RedundantCondition */ |
77
|
|
|
$this->assertIsArray($tableDefaultValues); |
78
|
|
|
|
79
|
|
|
foreach ($tableDefaultValues as $defaultValues) { |
80
|
|
|
$this->assertIsArray($defaultValues); |
81
|
|
|
$this->assertContainsOnlyInstancesOf(DefaultValueConstraint::class, $defaultValues); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testGetSchemaForeignKeys(): void |
86
|
|
|
{ |
87
|
|
|
$db = $this->getConnection(false); |
88
|
|
|
|
89
|
|
|
$tableForeignKeys = $db->getSchema()->getSchemaForeignKeys(); |
90
|
|
|
|
91
|
|
|
/** @psalm-suppress RedundantCondition */ |
92
|
|
|
$this->assertIsArray($tableForeignKeys); |
93
|
|
|
|
94
|
|
|
foreach ($tableForeignKeys as $foreignKeys) { |
95
|
|
|
$this->assertIsArray($foreignKeys); |
96
|
|
|
$this->assertContainsOnlyInstancesOf(ForeignKeyConstraint::class, $foreignKeys); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetSchemaIndexes(): void |
101
|
|
|
{ |
102
|
|
|
$db = $this->getConnection(false); |
103
|
|
|
|
104
|
|
|
$tableIndexes = $db->getSchema()->getSchemaIndexes(); |
105
|
|
|
|
106
|
|
|
/** @psalm-suppress RedundantCondition */ |
107
|
|
|
$this->assertIsArray($tableIndexes); |
108
|
|
|
|
109
|
|
|
foreach ($tableIndexes as $indexes) { |
110
|
|
|
$this->assertIsArray($indexes); |
111
|
|
|
$this->assertContainsOnlyInstancesOf(IndexConstraint::class, $indexes); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetSchemaUniques(): void |
116
|
|
|
{ |
117
|
|
|
$db = $this->getConnection(false); |
118
|
|
|
|
119
|
|
|
$tableUniques = $db->getSchema()->getSchemaUniques(); |
120
|
|
|
|
121
|
|
|
/** @psalm-suppress RedundantCondition */ |
122
|
|
|
$this->assertIsArray($tableUniques); |
123
|
|
|
|
124
|
|
|
foreach ($tableUniques as $uniques) { |
125
|
|
|
$this->assertIsArray($uniques); |
126
|
|
|
$this->assertContainsOnlyInstancesOf(Constraint::class, $uniques); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testGetNonExistingTableSchema(): void |
131
|
|
|
{ |
132
|
|
|
$this->assertNull($this->getConnection()->getSchema()->getTableSchema('nonexisting_table')); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testSchemaCache(): void |
136
|
|
|
{ |
137
|
|
|
$db = $this->getConnection(); |
138
|
|
|
|
139
|
|
|
$schema = $db->getSchema(); |
140
|
|
|
|
141
|
|
|
$this->assertNotNull($this->schemaCache); |
|
|
|
|
142
|
|
|
$this->schemaCache->setEnable(true); |
143
|
|
|
|
144
|
|
|
$noCacheTable = $schema->getTableSchema('type', true); |
145
|
|
|
$cachedTable = $schema->getTableSchema('type', false); |
146
|
|
|
|
147
|
|
|
$this->assertEquals($noCacheTable, $cachedTable); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$db->createCommand()->renameTable('type', 'type_test'); |
150
|
|
|
|
151
|
|
|
$noCacheTable = $schema->getTableSchema('type', true); |
152
|
|
|
|
153
|
|
|
$this->assertNotSame($noCacheTable, $cachedTable); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$db->createCommand()->renameTable('type_test', 'type'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @depends testSchemaCache |
160
|
|
|
*/ |
161
|
|
|
public function testRefreshTableSchema(): void |
162
|
|
|
{ |
163
|
|
|
$schema = $this->getConnection()->getSchema(); |
164
|
|
|
|
165
|
|
|
$this->assertNotNull($this->schemaCache); |
166
|
|
|
|
167
|
|
|
$this->schemaCache->setEnable(true); |
168
|
|
|
|
169
|
|
|
$noCacheTable = $schema->getTableSchema('type', true); |
170
|
|
|
|
171
|
|
|
$schema->refreshTableSchema('type'); |
172
|
|
|
|
173
|
|
|
$refreshedTable = $schema->getTableSchema('type', false); |
174
|
|
|
|
175
|
|
|
$this->assertNotSame($noCacheTable, $refreshedTable); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testCompositeFk(): void |
179
|
|
|
{ |
180
|
|
|
$schema = $this->getConnection()->getSchema(); |
181
|
|
|
|
182
|
|
|
$table = $schema->getTableSchema('composite_fk'); |
183
|
|
|
|
184
|
|
|
$this->assertNotNull($table); |
185
|
|
|
|
186
|
|
|
$fk = $table->getForeignKeys(); |
187
|
|
|
|
188
|
|
|
$this->assertCount(1, $fk); |
189
|
|
|
$this->assertTrue(isset($fk['FK_composite_fk_order_item'])); |
|
|
|
|
190
|
|
|
$this->assertEquals('order_item', $fk['FK_composite_fk_order_item'][0]); |
191
|
|
|
$this->assertEquals('order_id', $fk['FK_composite_fk_order_item']['order_id']); |
192
|
|
|
$this->assertEquals('item_id', $fk['FK_composite_fk_order_item']['item_id']); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testGetPDOType(): void |
196
|
|
|
{ |
197
|
|
|
$values = [ |
198
|
|
|
[null, PDO::PARAM_NULL], |
199
|
|
|
['', PDO::PARAM_STR], |
200
|
|
|
['hello', PDO::PARAM_STR], |
201
|
|
|
[0, PDO::PARAM_INT], |
202
|
|
|
[1, PDO::PARAM_INT], |
203
|
|
|
[1337, PDO::PARAM_INT], |
204
|
|
|
[true, PDO::PARAM_BOOL], |
205
|
|
|
[false, PDO::PARAM_BOOL], |
206
|
|
|
[$fp = fopen(__FILE__, 'rb'), PDO::PARAM_LOB], |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
$schema = $this->getConnection()->getSchema(); |
210
|
|
|
|
211
|
|
|
foreach ($values as $value) { |
212
|
|
|
$this->assertEquals($value[1], $schema->getPdoType($value[0]), 'type for value ' . print_r($value[0], true) . ' does not match.'); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
fclose($fp); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testColumnSchema(): void |
219
|
|
|
{ |
220
|
|
|
$columns = $this->getExpectedColumns(); |
|
|
|
|
221
|
|
|
|
222
|
|
|
$table = $this->getConnection(false)->getSchema()->getTableSchema('type', true); |
223
|
|
|
$this->assertNotNull($table); |
224
|
|
|
|
225
|
|
|
$expectedColNames = array_keys($columns); |
226
|
|
|
|
227
|
|
|
sort($expectedColNames); |
228
|
|
|
|
229
|
|
|
$colNames = $table->getColumnNames(); |
230
|
|
|
|
231
|
|
|
sort($colNames); |
232
|
|
|
|
233
|
|
|
$this->assertEquals($expectedColNames, $colNames); |
234
|
|
|
|
235
|
|
|
foreach ($table->getColumns() as $name => $column) { |
236
|
|
|
$expected = $columns[$name]; |
237
|
|
|
$this->assertSame( |
|
|
|
|
238
|
|
|
$expected['dbType'], |
239
|
|
|
$column->getDbType(), |
240
|
|
|
"dbType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}." |
241
|
|
|
); |
242
|
|
|
$this->assertSame( |
243
|
|
|
$expected['phpType'], |
244
|
|
|
$column->getPhpType(), |
245
|
|
|
"phpType of column $name does not match. type is {$column->getType()}, dbType is {$column->getDbType()}." |
246
|
|
|
); |
247
|
|
|
$this->assertSame($expected['type'], $column->getType(), "type of column $name does not match."); |
248
|
|
|
$this->assertSame( |
249
|
|
|
$expected['allowNull'], |
250
|
|
|
$column->isAllowNull(), |
251
|
|
|
"allowNull of column $name does not match." |
252
|
|
|
); |
253
|
|
|
$this->assertSame( |
254
|
|
|
$expected['autoIncrement'], |
255
|
|
|
$column->isAutoIncrement(), |
256
|
|
|
"autoIncrement of column $name does not match." |
257
|
|
|
); |
258
|
|
|
$this->assertSame( |
259
|
|
|
$expected['enumValues'], |
260
|
|
|
$column->getEnumValues(), |
261
|
|
|
"enumValues of column $name does not match." |
262
|
|
|
); |
263
|
|
|
$this->assertSame($expected['size'], $column->getSize(), "size of column $name does not match."); |
264
|
|
|
$this->assertSame( |
265
|
|
|
$expected['precision'], |
266
|
|
|
$column->getPrecision(), |
267
|
|
|
"precision of column $name does not match." |
268
|
|
|
); |
269
|
|
|
$this->assertSame($expected['scale'], $column->getScale(), "scale of column $name does not match."); |
270
|
|
|
if (\is_object($expected['defaultValue'])) { |
271
|
|
|
$this->assertIsObject( |
|
|
|
|
272
|
|
|
$column->getDefaultValue(), |
273
|
|
|
"defaultValue of column $name is expected to be an object but it is not." |
274
|
|
|
); |
275
|
|
|
$this->assertEquals( |
276
|
|
|
(string) $expected['defaultValue'], |
277
|
|
|
(string) $column->getDefaultValue(), |
278
|
|
|
"defaultValue of column $name does not match." |
279
|
|
|
); |
280
|
|
|
} else { |
281
|
|
|
$this->assertEquals( |
282
|
|
|
$expected['defaultValue'], |
283
|
|
|
$column->getDefaultValue(), |
284
|
|
|
"defaultValue of column $name does not match." |
285
|
|
|
); |
286
|
|
|
} |
287
|
|
|
/* Pgsql only */ |
288
|
|
|
if (isset($expected['dimension'])) { |
289
|
|
|
/** @psalm-suppress UndefinedMethod */ |
290
|
|
|
$this->assertSame( |
291
|
|
|
$expected['dimension'], |
292
|
|
|
$column->getDimension(), |
293
|
|
|
"dimension of column $name does not match" |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function testColumnSchemaDbTypecastWithEmptyCharType(): void |
300
|
|
|
{ |
301
|
|
|
$columnSchema = new ColumnSchema(); |
302
|
|
|
|
303
|
|
|
$columnSchema->setType(Schema::TYPE_CHAR); |
304
|
|
|
|
305
|
|
|
$this->assertSame('', $columnSchema->dbTypecast('')); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function testNegativeDefaultValues(): void |
309
|
|
|
{ |
310
|
|
|
$schema = $this->getConnection()->getSchema(); |
311
|
|
|
|
312
|
|
|
$table = $schema->getTableSchema('negative_default_values'); |
313
|
|
|
|
314
|
|
|
$this->assertNotNull($table); |
315
|
|
|
$this->assertEquals(-123, $table->getColumn('tinyint_col')?->getDefaultValue()); |
316
|
|
|
$this->assertEquals(-123, $table->getColumn('smallint_col')?->getDefaultValue()); |
317
|
|
|
$this->assertEquals(-123, $table->getColumn('int_col')?->getDefaultValue()); |
318
|
|
|
$this->assertEquals(-123, $table->getColumn('bigint_col')?->getDefaultValue()); |
319
|
|
|
$this->assertEquals(-12345.6789, $table->getColumn('float_col')?->getDefaultValue()); |
320
|
|
|
$this->assertEquals(-33.22, $table->getColumn('numeric_col')?->getDefaultValue()); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function testContraintTablesExistance(): void |
324
|
|
|
{ |
325
|
|
|
$tableNames = [ |
326
|
|
|
'T_constraints_1', |
327
|
|
|
'T_constraints_2', |
328
|
|
|
'T_constraints_3', |
329
|
|
|
'T_constraints_4', |
330
|
|
|
]; |
331
|
|
|
|
332
|
|
|
$schema = $this->getConnection()->getSchema(); |
333
|
|
|
|
334
|
|
|
foreach ($tableNames as $tableName) { |
335
|
|
|
$tableSchema = $schema->getTableSchema($tableName); |
336
|
|
|
$this->assertInstanceOf(TableSchemaInterface::class, $tableSchema, $tableName); |
|
|
|
|
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
public function testGetColumnNoExist(): void |
341
|
|
|
{ |
342
|
|
|
$schema = $this->getConnection()->getSchema(); |
343
|
|
|
$table = $schema->getTableSchema('negative_default_values'); |
344
|
|
|
|
345
|
|
|
$this->assertNotNull($table); |
346
|
|
|
$this->assertNull($table->getColumn('no_exist')); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
public function testQuoterEscapingValue() |
350
|
|
|
{ |
351
|
|
|
$this->markTestSkipped('Very long test - only for check quoteValue'); |
|
|
|
|
352
|
|
|
$template = 'aaaaa{1}aaa{1}aaaabbbbb{2}bbbb{2}bbbb'; |
353
|
|
|
|
354
|
|
|
$db = $this->getConnection(true); |
355
|
|
|
$quoter = $db->getQuoter(); |
356
|
|
|
|
357
|
|
|
$db->createCommand('delete from {{quoter}}')->execute(); |
358
|
|
|
|
359
|
|
|
for($symbol1 = 1; $symbol1 <= 127; $symbol1++) { |
360
|
|
|
for($symbol2 = 1; $symbol2 <= 127; $symbol2++) { |
361
|
|
|
$quotedName = $quoter->quoteValue('test_' . $symbol1 . '_' . $symbol2); |
362
|
|
|
$testString = str_replace(['{1}', '{2}',], [chr($symbol1), chr($symbol2)], $template); |
363
|
|
|
|
364
|
|
|
$quoteValue = $quoter->quoteValue($testString); |
365
|
|
|
|
366
|
|
|
$db->createCommand('insert into {{quoter}}([[name]], [[description]]) values('. $quotedName . ', ' . $quoteValue . ')')->execute(); |
367
|
|
|
$result = $db->createCommand('select * from {{quoter}} where [[name]]=' . $quotedName)->queryOne(); |
368
|
|
|
$this->assertEquals($testString, $result['description']); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
private function assertMetadataEquals($expected, $actual): void |
374
|
|
|
{ |
375
|
|
|
switch (strtolower(gettype($expected))) { |
376
|
|
|
case 'object': |
377
|
|
|
$this->assertIsObject($actual); |
378
|
|
|
break; |
379
|
|
|
case 'array': |
380
|
|
|
$this->assertIsArray($actual); |
381
|
|
|
break; |
382
|
|
|
case 'null': |
383
|
|
|
$this->assertNull($actual); |
384
|
|
|
break; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
if (is_array($expected)) { |
388
|
|
|
$this->normalizeArrayKeys($expected, false); |
389
|
|
|
/** @psalm-suppress PossiblyInvalidArgument */ |
390
|
|
|
$this->normalizeArrayKeys($actual, false); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$this->normalizeConstraints($expected, $actual); |
394
|
|
|
|
395
|
|
|
if (is_array($expected)) { |
396
|
|
|
$this->normalizeArrayKeys($expected, true); |
397
|
|
|
$this->normalizeArrayKeys($actual, true); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
$this->assertEquals($expected, $actual); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
private function normalizeArrayKeys(array &$array, bool $caseSensitive): void |
404
|
|
|
{ |
405
|
|
|
$newArray = []; |
406
|
|
|
|
407
|
|
|
foreach ($array as $value) { |
408
|
|
|
if ($value instanceof Constraint) { |
409
|
|
|
$key = (array) $value; |
410
|
|
|
unset( |
411
|
|
|
$key["\000Yiisoft\Db\Constraint\Constraint\000name"], |
412
|
|
|
$key["\u0000Yiisoft\\Db\\Constraint\\ForeignKeyConstraint\u0000foreignSchemaName"] |
413
|
|
|
); |
414
|
|
|
|
415
|
|
|
foreach ($key as $keyName => $keyValue) { |
416
|
|
|
if ($keyValue instanceof AnyCaseValue) { |
417
|
|
|
$key[$keyName] = $keyValue->value; |
418
|
|
|
} elseif ($keyValue instanceof AnyValue) { |
419
|
|
|
$key[$keyName] = '[AnyValue]'; |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
ksort($key, SORT_STRING); |
424
|
|
|
|
425
|
|
|
$newArray[$caseSensitive |
426
|
|
|
? json_encode($key, JSON_THROW_ON_ERROR) |
427
|
|
|
: strtolower(json_encode($key, JSON_THROW_ON_ERROR))] = $value; |
428
|
|
|
} else { |
429
|
|
|
$newArray[] = $value; |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
ksort($newArray, SORT_STRING); |
434
|
|
|
|
435
|
|
|
$array = $newArray; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
private function normalizeConstraints(&$expected, &$actual): void |
439
|
|
|
{ |
440
|
|
|
if (is_array($expected)) { |
441
|
|
|
foreach ($expected as $key => $value) { |
442
|
|
|
if (!$value instanceof Constraint || !isset($actual[$key]) || !$actual[$key] instanceof Constraint) { |
443
|
|
|
continue; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
$this->normalizeConstraintPair($value, $actual[$key]); |
447
|
|
|
} |
448
|
|
|
} elseif ($expected instanceof Constraint && $actual instanceof Constraint) { |
449
|
|
|
$this->normalizeConstraintPair($expected, $actual); |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
private function normalizeConstraintPair(Constraint $expectedConstraint, Constraint $actualConstraint): void |
454
|
|
|
{ |
455
|
|
|
if (get_class($expectedConstraint) !== get_class($actualConstraint)) { |
456
|
|
|
return; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
foreach (array_keys((array) $expectedConstraint) as $name) { |
460
|
|
|
if ($expectedConstraint->getName() instanceof AnyValue) { |
461
|
|
|
$actualConstraint->name($expectedConstraint->getName()); |
462
|
|
|
} elseif ($expectedConstraint->getName() instanceof AnyCaseValue) { |
463
|
|
|
$actualConstraintName = $actualConstraint->getName(); |
464
|
|
|
$this->assertIsString($actualConstraintName); |
|
|
|
|
465
|
|
|
$actualConstraint->name(new AnyCaseValue($actualConstraintName)); |
|
|
|
|
466
|
|
|
} |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
public function constraintsProviderTrait(): array |
471
|
|
|
{ |
472
|
|
|
return [ |
473
|
|
|
'1: primary key' => [ |
474
|
|
|
'T_constraints_1', |
475
|
|
|
Schema::PRIMARY_KEY, |
476
|
|
|
(new Constraint()) |
477
|
|
|
->name(AnyValue::getInstance()) |
478
|
|
|
->columnNames(['C_id']), |
479
|
|
|
], |
480
|
|
|
'1: check' => [ |
481
|
|
|
'T_constraints_1', |
482
|
|
|
Schema::CHECKS, |
483
|
|
|
[ |
484
|
|
|
(new CheckConstraint()) |
485
|
|
|
->name(AnyValue::getInstance()) |
486
|
|
|
->columnNames(['C_check']) |
487
|
|
|
->expression("C_check <> ''"), |
488
|
|
|
], |
489
|
|
|
], |
490
|
|
|
'1: unique' => [ |
491
|
|
|
'T_constraints_1', |
492
|
|
|
Schema::UNIQUES, |
493
|
|
|
[ |
494
|
|
|
(new Constraint()) |
495
|
|
|
->name('CN_unique') |
496
|
|
|
->columnNames(['C_unique']), |
497
|
|
|
], |
498
|
|
|
], |
499
|
|
|
'1: index' => [ |
500
|
|
|
'T_constraints_1', |
501
|
|
|
Schema::INDEXES, |
502
|
|
|
[ |
503
|
|
|
(new IndexConstraint()) |
504
|
|
|
->name(AnyValue::getInstance()) |
505
|
|
|
->columnNames(['C_id']) |
506
|
|
|
->unique(true) |
507
|
|
|
->primary(true), |
508
|
|
|
(new IndexConstraint()) |
509
|
|
|
->name('CN_unique') |
510
|
|
|
->columnNames(['C_unique']) |
511
|
|
|
->primary(false) |
512
|
|
|
->unique(true), |
513
|
|
|
], |
514
|
|
|
], |
515
|
|
|
'1: default' => ['T_constraints_1', Schema::DEFAULT_VALUES, false], |
516
|
|
|
|
517
|
|
|
'2: primary key' => [ |
518
|
|
|
'T_constraints_2', |
519
|
|
|
Schema::PRIMARY_KEY, |
520
|
|
|
(new Constraint()) |
521
|
|
|
->name('CN_pk') |
522
|
|
|
->columnNames(['C_id_1', 'C_id_2']), |
523
|
|
|
], |
524
|
|
|
'2: unique' => [ |
525
|
|
|
'T_constraints_2', |
526
|
|
|
Schema::UNIQUES, |
527
|
|
|
[ |
528
|
|
|
(new Constraint()) |
529
|
|
|
->name('CN_constraints_2_multi') |
530
|
|
|
->columnNames(['C_index_2_1', 'C_index_2_2']), |
531
|
|
|
], |
532
|
|
|
], |
533
|
|
|
'2: index' => [ |
534
|
|
|
'T_constraints_2', |
535
|
|
|
Schema::INDEXES, |
536
|
|
|
[ |
537
|
|
|
(new IndexConstraint()) |
538
|
|
|
->name(AnyValue::getInstance()) |
539
|
|
|
->columnNames(['C_id_1', 'C_id_2']) |
540
|
|
|
->unique(true) |
541
|
|
|
->primary(true), |
542
|
|
|
(new IndexConstraint()) |
543
|
|
|
->name('CN_constraints_2_single') |
544
|
|
|
->columnNames(['C_index_1']) |
545
|
|
|
->primary(false) |
546
|
|
|
->unique(false), |
547
|
|
|
(new IndexConstraint()) |
548
|
|
|
->name('CN_constraints_2_multi') |
549
|
|
|
->columnNames(['C_index_2_1', 'C_index_2_2']) |
550
|
|
|
->primary(false) |
551
|
|
|
->unique(true), |
552
|
|
|
], |
553
|
|
|
], |
554
|
|
|
'2: check' => ['T_constraints_2', Schema::CHECKS, []], |
555
|
|
|
'2: default' => ['T_constraints_2', Schema::DEFAULT_VALUES, false], |
556
|
|
|
|
557
|
|
|
'3: primary key' => ['T_constraints_3', Schema::PRIMARY_KEY, null], |
558
|
|
|
'3: foreign key' => [ |
559
|
|
|
'T_constraints_3', |
560
|
|
|
Schema::FOREIGN_KEYS, |
561
|
|
|
[ |
562
|
|
|
(new ForeignKeyConstraint()) |
563
|
|
|
->name('CN_constraints_3') |
564
|
|
|
->columnNames(['C_fk_id_1', 'C_fk_id_2']) |
565
|
|
|
->foreignTableName('T_constraints_2') |
566
|
|
|
->foreignColumnNames(['C_id_1', 'C_id_2']) |
567
|
|
|
->onDelete('CASCADE') |
568
|
|
|
->onUpdate('CASCADE'), |
569
|
|
|
], |
570
|
|
|
], |
571
|
|
|
'3: unique' => ['T_constraints_3', Schema::UNIQUES, []], |
572
|
|
|
'3: index' => [ |
573
|
|
|
'T_constraints_3', |
574
|
|
|
Schema::INDEXES, |
575
|
|
|
[ |
576
|
|
|
(new IndexConstraint()) |
577
|
|
|
->name('CN_constraints_3') |
578
|
|
|
->columnNames(['C_fk_id_1', 'C_fk_id_2']) |
579
|
|
|
->unique(false) |
580
|
|
|
->primary(false), |
581
|
|
|
], |
582
|
|
|
], |
583
|
|
|
'3: check' => ['T_constraints_3', Schema::CHECKS, []], |
584
|
|
|
'3: default' => ['T_constraints_3', Schema::DEFAULT_VALUES, false], |
585
|
|
|
|
586
|
|
|
'4: primary key' => [ |
587
|
|
|
'T_constraints_4', |
588
|
|
|
Schema::PRIMARY_KEY, |
589
|
|
|
(new Constraint()) |
590
|
|
|
->name(AnyValue::getInstance()) |
591
|
|
|
->columnNames(['C_id']), |
592
|
|
|
], |
593
|
|
|
'4: unique' => [ |
594
|
|
|
'T_constraints_4', |
595
|
|
|
Schema::UNIQUES, |
596
|
|
|
[ |
597
|
|
|
(new Constraint()) |
598
|
|
|
->name('CN_constraints_4') |
599
|
|
|
->columnNames(['C_col_1', 'C_col_2']), |
600
|
|
|
], |
601
|
|
|
], |
602
|
|
|
'4: check' => ['T_constraints_4', Schema::CHECKS, []], |
603
|
|
|
'4: default' => ['T_constraints_4', Schema::DEFAULT_VALUES, false], |
604
|
|
|
]; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
public function pdoAttributesProviderTrait(): array |
608
|
|
|
{ |
609
|
|
|
return [ |
610
|
|
|
[[PDO::ATTR_EMULATE_PREPARES => true]], |
611
|
|
|
[[PDO::ATTR_EMULATE_PREPARES => false]], |
612
|
|
|
]; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
public function tableSchemaCachePrefixesProviderTrait(): array |
616
|
|
|
{ |
617
|
|
|
$configs = [ |
618
|
|
|
[ |
619
|
|
|
'prefix' => '', |
620
|
|
|
'name' => 'type', |
621
|
|
|
], |
622
|
|
|
[ |
623
|
|
|
'prefix' => '', |
624
|
|
|
'name' => '{{%type}}', |
625
|
|
|
], |
626
|
|
|
[ |
627
|
|
|
'prefix' => 'ty', |
628
|
|
|
'name' => '{{%pe}}', |
629
|
|
|
], |
630
|
|
|
]; |
631
|
|
|
|
632
|
|
|
$data = []; |
633
|
|
|
foreach ($configs as $config) { |
634
|
|
|
foreach ($configs as $testConfig) { |
635
|
|
|
if ($config === $testConfig) { |
636
|
|
|
continue; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
$description = sprintf( |
640
|
|
|
"%s (with '%s' prefix) against %s (with '%s' prefix)", |
641
|
|
|
$config['name'], |
642
|
|
|
$config['prefix'], |
643
|
|
|
$testConfig['name'], |
644
|
|
|
$testConfig['prefix'] |
645
|
|
|
); |
646
|
|
|
$data[$description] = [ |
647
|
|
|
$config['prefix'], |
648
|
|
|
$config['name'], |
649
|
|
|
$testConfig['prefix'], |
650
|
|
|
$testConfig['name'], |
651
|
|
|
]; |
652
|
|
|
} |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
return $data; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
public function lowercaseConstraintsProviderTrait(): array |
659
|
|
|
{ |
660
|
|
|
return $this->constraintsProvider(); |
|
|
|
|
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
public function uppercaseConstraintsProviderTrait(): array |
664
|
|
|
{ |
665
|
|
|
return $this->constraintsProvider(); |
666
|
|
|
} |
667
|
|
|
} |
668
|
|
|
|