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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
<?php
namespace Doctrine\DBAL\Tests\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\Type;
use function array_walk;
use function preg_replace;
use function sprintf;
use function strtoupper;
use function uniqid;
class OraclePlatformTest extends AbstractPlatformTestCase
{
/** @var OraclePlatform */
protected $platform;
/**
* @return mixed[][]
*/
public static function dataValidIdentifiers(): iterable
{
return [
['a'],
['foo'],
['Foo'],
['Foo123'],
['Foo#bar_baz$'],
['"a"'],
['"1"'],
['"foo_bar"'],
['"@$%&!"'],
];
}
/**
* @dataProvider dataValidIdentifiers
*/
public function testValidIdentifiers(string $identifier): void
{
$platform = $this->createPlatform();
$platform->assertValidIdentifier($identifier);
$this->addToAssertionCount(1);
}
/**
* @return mixed[][]
*/
public static function dataInvalidIdentifiers(): iterable
{
return [
['1'],
['abc&'],
['abc-def'],
['"'],
['"foo"bar"'],
];
}
/**
* @dataProvider dataInvalidIdentifiers
*/
public function testInvalidIdentifiers(string $identifier): void
{
$this->expectException(DBALException::class);
$platform = $this->createPlatform();
$platform->assertValidIdentifier($identifier);
}
/**
* @return OraclePlatform
*/
public function createPlatform(): AbstractPlatform
{
return new OraclePlatform();
}
public function getGenerateTableSql(): string
{
return 'CREATE TABLE test (id NUMBER(10) NOT NULL, test VARCHAR2(255) DEFAULT NULL NULL, PRIMARY KEY(id))';
}
/**
* {@inheritDoc}
*/
public function getGenerateTableWithMultiColumnUniqueIndexSql(): array
{
return [
'CREATE TABLE test (foo VARCHAR2(255) DEFAULT NULL NULL, bar VARCHAR2(255) DEFAULT NULL NULL)',
'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar)',
];
}
/**
* {@inheritDoc}
*/
public function getGenerateAlterTableSql(): array
{
return [
'ALTER TABLE mytable ADD (quota NUMBER(10) DEFAULT NULL NULL)',
"ALTER TABLE mytable MODIFY (baz VARCHAR2(255) DEFAULT 'def' NOT NULL, "
. "bloo NUMBER(1) DEFAULT '0' NOT NULL)",
'ALTER TABLE mytable DROP (foo)',
'ALTER TABLE mytable RENAME TO userlist',
];
}
public function testRLike(): void
{
$this->expectException(DBALException::class);
self::assertEquals('RLIKE', $this->platform->getRegexpExpression());
}
public function testGeneratesSqlSnippets(): void
{
self::assertEquals('"', $this->platform->getIdentifierQuoteCharacter());
self::assertEquals(
'column1 || column2 || column3',
$this->platform->getConcatExpression('column1', 'column2', 'column3')
);
}
public function testGeneratesTransactionsCommands(): void
{
self::assertEquals(
'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
$this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_UNCOMMITTED)
);
self::assertEquals(
'SET TRANSACTION ISOLATION LEVEL READ COMMITTED',
$this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_COMMITTED)
);
self::assertEquals(
'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
$this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::REPEATABLE_READ)
);
self::assertEquals(
'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
$this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::SERIALIZABLE)
);
}
public function testCreateDatabaseThrowsException(): void
{
$this->expectException(DBALException::class);
self::assertEquals('CREATE DATABASE foobar', $this->platform->getCreateDatabaseSQL('foobar'));
}
public function testDropDatabaseThrowsException(): void
{
self::assertEquals('DROP USER foobar CASCADE', $this->platform->getDropDatabaseSQL('foobar'));
}
public function testDropTable(): void
{
self::assertEquals('DROP TABLE foobar', $this->platform->getDropTableSQL('foobar'));
}
public function testGeneratesTypeDeclarationForIntegers(): void
{
self::assertEquals(
'NUMBER(10)',
$this->platform->getIntegerTypeDeclarationSQL([])
);
self::assertEquals(
'NUMBER(10)',
$this->platform->getIntegerTypeDeclarationSQL(['autoincrement' => true])
);
self::assertEquals(
'NUMBER(10)',
$this->platform->getIntegerTypeDeclarationSQL(
['autoincrement' => true, 'primary' => true]
)
);
}
public function testGeneratesTypeDeclarationsForStrings(): void
{
self::assertEquals(
'CHAR(10)',
$this->platform->getVarcharTypeDeclarationSQL(
['length' => 10, 'fixed' => true]
)
);
self::assertEquals(
'VARCHAR2(50)',
$this->platform->getVarcharTypeDeclarationSQL(['length' => 50])
);
self::assertEquals(
'VARCHAR2(255)',
$this->platform->getVarcharTypeDeclarationSQL([])
);
}
public function testPrefersIdentityColumns(): void
{
self::assertFalse($this->platform->prefersIdentityColumns());
}
public function testSupportsIdentityColumns(): void
{
self::assertFalse($this->platform->supportsIdentityColumns());
}
public function testSupportsSavePoints(): void
{
self::assertTrue($this->platform->supportsSavepoints());
}
protected function supportsCommentOnStatement(): bool
{
return true;
}
public function getGenerateIndexSql(): string
{
return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
}
public function getGenerateUniqueIndexSql(): string
{
return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
}
protected function getGenerateForeignKeySql(): string
{
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
}
/**
* @param mixed[] $options
*
* @dataProvider getGeneratesAdvancedForeignKeyOptionsSQLData
*/
public function testGeneratesAdvancedForeignKeyOptionsSQL(array $options, string $expectedSql): void
{
$foreignKey = new ForeignKeyConstraint(['foo'], 'foreign_table', ['bar'], null, $options);
self::assertSame($expectedSql, $this->platform->getAdvancedForeignKeyOptionsSQL($foreignKey));
}
/**
* @return mixed[][]
*/
public static function getGeneratesAdvancedForeignKeyOptionsSQLData(): iterable
{
return [
[[], ''],
[['onUpdate' => 'CASCADE'], ''],
[['onDelete' => 'CASCADE'], ' ON DELETE CASCADE'],
[['onDelete' => 'NO ACTION'], ''],
[['onDelete' => 'RESTRICT'], ''],
[['onUpdate' => 'SET NULL', 'onDelete' => 'SET NULL'], ' ON DELETE SET NULL'],
];
}
/**
* {@inheritdoc}
*/
public static function getReturnsForeignKeyReferentialActionSQL(): iterable
{
return [
['CASCADE', 'CASCADE'],
['SET NULL', 'SET NULL'],
['NO ACTION', ''],
['RESTRICT', ''],
['CaScAdE', 'CASCADE'],
];
}
public function testModifyLimitQuery(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
self::assertEquals('SELECT a.* FROM (SELECT * FROM user) a WHERE ROWNUM <= 10', $sql);
}
public function testModifyLimitQueryWithEmptyOffset(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user', 10);
self::assertEquals('SELECT a.* FROM (SELECT * FROM user) a WHERE ROWNUM <= 10', $sql);
}
public function testModifyLimitQueryWithNonEmptyOffset(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user', 10, 10);
self::assertEquals(
'SELECT * FROM ('
. 'SELECT a.*, ROWNUM AS doctrine_rownum FROM (SELECT * FROM user) a WHERE ROWNUM <= 20'
. ') WHERE doctrine_rownum >= 11',
$sql
);
}
public function testModifyLimitQueryWithEmptyLimit(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user', null, 10);
self::assertEquals(
'SELECT * FROM ('
. 'SELECT a.*, ROWNUM AS doctrine_rownum FROM (SELECT * FROM user) a'
. ') WHERE doctrine_rownum >= 11',
$sql
);
}
public function testModifyLimitQueryWithAscOrderBy(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user ORDER BY username ASC', 10);
self::assertEquals('SELECT a.* FROM (SELECT * FROM user ORDER BY username ASC) a WHERE ROWNUM <= 10', $sql);
}
public function testModifyLimitQueryWithDescOrderBy(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10);
self::assertEquals('SELECT a.* FROM (SELECT * FROM user ORDER BY username DESC) a WHERE ROWNUM <= 10', $sql);
}
public function testGenerateTableWithAutoincrement(): void
{
$columnName = strtoupper('id' . uniqid());
$tableName = strtoupper('table' . uniqid());
$table = new Table($tableName);
$column = $table->addColumn($columnName, 'integer');
$column->setAutoincrement(true);
$targets = [
sprintf('CREATE TABLE %s (%s NUMBER(10) NOT NULL)', $tableName, $columnName),
sprintf(
'DECLARE constraints_Count NUMBER;'
. ' BEGIN'
. ' SELECT COUNT(CONSTRAINT_NAME)'
. ' INTO constraints_Count'
. ' FROM USER_CONSTRAINTS'
. " WHERE TABLE_NAME = '%s' AND CONSTRAINT_TYPE = 'P';"
. " IF constraints_Count = 0 OR constraints_Count = ''"
. ' THEN EXECUTE IMMEDIATE'
. " 'ALTER TABLE %s ADD CONSTRAINT %s_AI_PK PRIMARY KEY (%s)';"
. ' END IF;'
. ' END;',
$tableName,
$tableName,
$tableName,
$columnName
),
sprintf('CREATE SEQUENCE %s_SEQ START WITH 1 MINVALUE 1 INCREMENT BY 1', $tableName),
sprintf(
'CREATE TRIGGER %s_AI_PK BEFORE INSERT ON %s FOR EACH ROW DECLARE last_Sequence NUMBER;'
. ' last_InsertID NUMBER;'
. ' BEGIN SELECT %s_SEQ.NEXTVAL'
. ' INTO :NEW.%s FROM DUAL;'
. ' IF (:NEW.%s IS NULL OR :NEW.%s = 0)'
. ' THEN SELECT %s_SEQ.NEXTVAL INTO :NEW.%s FROM DUAL;'
. ' ELSE SELECT NVL(Last_Number, 0) INTO last_Sequence'
. " FROM User_Sequences WHERE Sequence_Name = '%s_SEQ';"
. ' SELECT :NEW.%s INTO last_InsertID FROM DUAL;'
. ' WHILE (last_InsertID > last_Sequence) LOOP'
. ' SELECT %s_SEQ.NEXTVAL INTO last_Sequence FROM DUAL;'
. ' END LOOP;'
. ' END IF;'
. ' END;',
$tableName,
$tableName,
$tableName,
$columnName,
$columnName,
$columnName,
$tableName,
$columnName,
$tableName,
$columnName,
$tableName
),
];
$statements = $this->platform->getCreateTableSQL($table);
//strip all the whitespace from the statements
array_walk($statements, static function (&$value): void {
$value = preg_replace('/\s+/', ' ', $value);
});
foreach ($targets as $key => $sql) {
self::assertArrayHasKey($key, $statements);
self::assertEquals($sql, $statements[$key]);
}
}
/**
* {@inheritDoc}
*/
public function getCreateTableColumnCommentsSQL(): array
{
return [
'CREATE TABLE test (id NUMBER(10) NOT NULL, PRIMARY KEY(id))',
"COMMENT ON COLUMN test.id IS 'This is a comment'",
];
}
/**
* {@inheritDoc}
*/
public function getCreateTableColumnTypeCommentsSQL(): array
{
return [
'CREATE TABLE test (id NUMBER(10) NOT NULL, data CLOB NOT NULL, PRIMARY KEY(id))',
"COMMENT ON COLUMN test.data IS '(DC2Type:array)'",
];
}
/**
* {@inheritDoc}
*/
public function getAlterTableColumnCommentsSQL(): array
{
return [
'ALTER TABLE mytable ADD (quota NUMBER(10) NOT NULL)',
"COMMENT ON COLUMN mytable.quota IS 'A comment'",
"COMMENT ON COLUMN mytable.foo IS ''",
"COMMENT ON COLUMN mytable.baz IS 'B comment'",
];
}
public function getBitAndComparisonExpressionSql(string $value1, string $value2): string
{
return 'BITAND(' . $value1 . ', ' . $value2 . ')';
}
public function getBitOrComparisonExpressionSql(string $value1, string $value2): string
{
return '(' . $value1 . '-' .
$this->getBitAndComparisonExpressionSql($value1, $value2)
. '+' . $value2 . ')';
}
/**
* {@inheritDoc}
*/
protected function getQuotedColumnInPrimaryKeySQL(): array
{
return ['CREATE TABLE "quoted" ("create" VARCHAR2(255) NOT NULL, PRIMARY KEY("create"))'];
}
/**
* {@inheritDoc}
*/
protected function getQuotedColumnInIndexSQL(): array
{
return [
'CREATE TABLE "quoted" ("create" VARCHAR2(255) NOT NULL)',
'CREATE INDEX IDX_22660D028FD6E0FB ON "quoted" ("create")',
];
}
/**
* {@inheritDoc}
*/
protected function getQuotedNameInIndexSQL(): array
{
return [
'CREATE TABLE test (column1 VARCHAR2(255) NOT NULL)',
'CREATE INDEX "key" ON test (column1)',
];
}
/**
* {@inheritDoc}
*/
protected function getQuotedColumnInForeignKeySQL(): array
{
return [
'CREATE TABLE "quoted" ("create" VARCHAR2(255) NOT NULL, foo VARCHAR2(255) NOT NULL, '
. '"bar" VARCHAR2(255) NOT NULL)',
'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY ("create", foo, "bar")'
. ' REFERENCES foreign ("create", bar, "foo-bar")',
'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD FOREIGN KEY ("create", foo, "bar")'
. ' REFERENCES foo ("create", bar, "foo-bar")',
'ALTER TABLE "quoted" ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY ("create", foo, "bar")'
. ' REFERENCES "foo-bar" ("create", bar, "foo-bar")',
];
}
public function testAlterTableNotNULL(): void
{
$tableDiff = new TableDiff('mytable');
$tableDiff->changedColumns['foo'] = new ColumnDiff(
'foo',
new Column(
'foo',
Type::getType('string'),
['default' => 'bla', 'notnull' => true]
),
['type']
);
$tableDiff->changedColumns['bar'] = new ColumnDiff(
'bar',
new Column(
'baz',
Type::getType('string'),
['default' => 'bla', 'notnull' => true]
),
['type', 'notnull']
);
$tableDiff->changedColumns['metar'] = new ColumnDiff(
'metar',
new Column(
'metar',
Type::getType('string'),
['length' => 2000, 'notnull' => false]
),
['notnull']
);
$expectedSql = [
"ALTER TABLE mytable MODIFY (foo VARCHAR2(255) DEFAULT 'bla', baz VARCHAR2(255) DEFAULT 'bla' NOT NULL, "
. 'metar VARCHAR2(2000) DEFAULT NULL NULL)',
];
self::assertEquals($expectedSql, $this->platform->getAlterTableSQL($tableDiff));
}
public function testInitializesDoctrineTypeMappings(): void
{
self::assertTrue($this->platform->hasDoctrineTypeMappingFor('long raw'));
self::assertSame('blob', $this->platform->getDoctrineTypeMapping('long raw'));
self::assertTrue($this->platform->hasDoctrineTypeMappingFor('raw'));
self::assertSame('binary', $this->platform->getDoctrineTypeMapping('raw'));
self::assertTrue($this->platform->hasDoctrineTypeMappingFor('date'));
self::assertSame('date', $this->platform->getDoctrineTypeMapping('date'));
}
protected function getBinaryMaxLength(): int
{
return 2000;
}
public function testReturnsBinaryTypeDeclarationSQL(): void
{
self::assertSame('RAW(255)', $this->platform->getBinaryTypeDeclarationSQL([]));
self::assertSame('RAW(2000)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 0]));
self::assertSame('RAW(2000)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 2000]));
self::assertSame('RAW(255)', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
self::assertSame('RAW(2000)', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 0]));
self::assertSame(
'RAW(2000)',
$this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 2000])
);
}
public function testReturnsBinaryTypeLongerThanMaxDeclarationSQL(): void
{
self::assertSame('BLOB', $this->platform->getBinaryTypeDeclarationSQL(['length' => 2001]));
self::assertSame('BLOB', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 2001]));
}
public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType(): void
{
$table1 = new Table('mytable');
$table1->addColumn('column_varbinary', 'binary');
$table1->addColumn('column_binary', 'binary', ['fixed' => true]);
$table2 = new Table('mytable');
$table2->addColumn('column_varbinary', 'binary', ['fixed' => true]);
$table2->addColumn('column_binary', 'binary');
$comparator = new Comparator();
// VARBINARY -> BINARY
// BINARY -> VARBINARY
self::assertEmpty($this->platform->getAlterTableSQL($comparator->diffTable($table1, $table2)));
}
public function testUsesSequenceEmulatedIdentityColumns(): void
{
self::assertTrue($this->platform->usesSequenceEmulatedIdentityColumns());
}
public function testReturnsIdentitySequenceName(): void
{
self::assertSame('MYTABLE_SEQ', $this->platform->getIdentitySequenceName('mytable', 'mycolumn'));
self::assertSame('"mytable_SEQ"', $this->platform->getIdentitySequenceName('"mytable"', 'mycolumn'));
self::assertSame('MYTABLE_SEQ', $this->platform->getIdentitySequenceName('mytable', '"mycolumn"'));
self::assertSame('"mytable_SEQ"', $this->platform->getIdentitySequenceName('"mytable"', '"mycolumn"'));
}
/**
* @dataProvider dataCreateSequenceWithCache
*/
public function testCreateSequenceWithCache(int $cacheSize, string $expectedSql): void
{
$sequence = new Sequence('foo', 1, 1, $cacheSize);
self::assertStringContainsString($expectedSql, $this->platform->getCreateSequenceSQL($sequence));
}
/**
* @return mixed[][]
*/
public static function dataCreateSequenceWithCache(): iterable
{
return [
[1, 'NOCACHE'],
[0, 'NOCACHE'],
[3, 'CACHE 3'],
];
}
/**
* {@inheritDoc}
*/
protected function getAlterTableRenameIndexSQL(): array
{
return ['ALTER INDEX idx_foo RENAME TO idx_bar'];
}
/**
* {@inheritDoc}
*/
protected function getQuotedAlterTableRenameIndexSQL(): array
{
return [
'ALTER INDEX "create" RENAME TO "select"',
'ALTER INDEX "foo" RENAME TO "bar"',
];
}
/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableRenameColumnSQL(): array
{
return [
'ALTER TABLE mytable RENAME COLUMN unquoted1 TO unquoted',
'ALTER TABLE mytable RENAME COLUMN unquoted2 TO "where"',
'ALTER TABLE mytable RENAME COLUMN unquoted3 TO "foo"',
'ALTER TABLE mytable RENAME COLUMN "create" TO reserved_keyword',
'ALTER TABLE mytable RENAME COLUMN "table" TO "from"',
'ALTER TABLE mytable RENAME COLUMN "select" TO "bar"',
'ALTER TABLE mytable RENAME COLUMN quoted1 TO quoted',
'ALTER TABLE mytable RENAME COLUMN quoted2 TO "and"',
'ALTER TABLE mytable RENAME COLUMN quoted3 TO "baz"',
];
}
/**
* {@inheritdoc}
*/
protected function getQuotedAlterTableChangeColumnLengthSQL(): array
{
self::markTestIncomplete('Not implemented yet');
}
/**
* {@inheritDoc}
*/
protected function getAlterTableRenameIndexInSchemaSQL(): array
{
return ['ALTER INDEX myschema.idx_foo RENAME TO idx_bar'];
}
/**
* {@inheritDoc}
*/
protected function getQuotedAlterTableRenameIndexInSchemaSQL(): array
{
return [
'ALTER INDEX "schema"."create" RENAME TO "select"',
'ALTER INDEX "schema"."foo" RENAME TO "bar"',
];
}
protected function getQuotesDropForeignKeySQL(): string
{
return 'ALTER TABLE "table" DROP CONSTRAINT "select"';
}
public function testReturnsGuidTypeDeclarationSQL(): void
{
self::assertSame('CHAR(36)', $this->platform->getGuidTypeDeclarationSQL([]));
}
/**
* {@inheritdoc}
*/
public function getAlterTableRenameColumnSQL(): array
{
return ['ALTER TABLE foo RENAME COLUMN bar TO baz'];
}
/**
* @param string[] $expectedSql
*
* @dataProvider getReturnsDropAutoincrementSQL
*/
public function testReturnsDropAutoincrementSQL(string $table, array $expectedSql): void
{
self::assertSame($expectedSql, $this->platform->getDropAutoincrementSql($table));
}
/**
* @return mixed[][]
*/
public static function getReturnsDropAutoincrementSQL(): iterable
{
return [
[
'myTable',
[
'DROP TRIGGER MYTABLE_AI_PK',
'DROP SEQUENCE MYTABLE_SEQ',
'ALTER TABLE MYTABLE DROP CONSTRAINT MYTABLE_AI_PK',
],
],
[
'"myTable"',
[
'DROP TRIGGER "myTable_AI_PK"',
'DROP SEQUENCE "myTable_SEQ"',
'ALTER TABLE "myTable" DROP CONSTRAINT "myTable_AI_PK"',
],
],
[
'table',
[
'DROP TRIGGER TABLE_AI_PK',
'DROP SEQUENCE TABLE_SEQ',
'ALTER TABLE "TABLE" DROP CONSTRAINT TABLE_AI_PK',
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getQuotesTableIdentifiersInAlterTableSQL(): array
{
return [
'ALTER TABLE "foo" DROP CONSTRAINT fk1',
'ALTER TABLE "foo" DROP CONSTRAINT fk2',
'ALTER TABLE "foo" ADD (bloo NUMBER(10) NOT NULL)',
'ALTER TABLE "foo" MODIFY (bar NUMBER(10) DEFAULT NULL NULL)',
'ALTER TABLE "foo" RENAME COLUMN id TO war',
'ALTER TABLE "foo" DROP (baz)',
'ALTER TABLE "foo" RENAME TO "table"',
'ALTER TABLE "table" ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
'ALTER TABLE "table" ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
];
}
/**
* {@inheritdoc}
*/
protected function getCommentOnColumnSQL(): array
{
return [
'COMMENT ON COLUMN foo.bar IS \'comment\'',
'COMMENT ON COLUMN "Foo"."BAR" IS \'comment\'',
'COMMENT ON COLUMN "select"."from" IS \'comment\'',
];
}
public function testAltersTableColumnCommentWithExplicitlyQuotedIdentifiers(): void
{
$table1 = new Table('"foo"', [new Column('"bar"', Type::getType('integer'))]);
$table2 = new Table('"foo"', [new Column('"bar"', Type::getType('integer'), ['comment' => 'baz'])]);
$comparator = new Comparator();
$tableDiff = $comparator->diffTable($table1, $table2);
self::assertInstanceOf(TableDiff::class, $tableDiff);
self::assertSame(
['COMMENT ON COLUMN "foo"."bar" IS \'baz\''],
$this->platform->getAlterTableSQL($tableDiff)
);
}
public function testQuotedTableNames(): void
{
$table = new Table('"test"');
$table->addColumn('"id"', 'integer', ['autoincrement' => true]);
// assert tabel
self::assertTrue($table->isQuoted());
self::assertEquals('test', $table->getName());
self::assertEquals('"test"', $table->getQuotedName($this->platform));
$sql = $this->platform->getCreateTableSQL($table);
self::assertEquals('CREATE TABLE "test" ("id" NUMBER(10) NOT NULL)', $sql[0]);
self::assertEquals('CREATE SEQUENCE "test_SEQ" START WITH 1 MINVALUE 1 INCREMENT BY 1', $sql[2]);
$createTriggerStatement = <<<EOD
CREATE TRIGGER "test_AI_PK"
BEFORE INSERT
ON "test"
FOR EACH ROW
DECLARE
last_Sequence NUMBER;
last_InsertID NUMBER;
BEGIN
SELECT "test_SEQ".NEXTVAL INTO :NEW."id" FROM DUAL;
IF (:NEW."id" IS NULL OR :NEW."id" = 0) THEN
SELECT "test_SEQ".NEXTVAL INTO :NEW."id" FROM DUAL;
ELSE
SELECT NVL(Last_Number, 0) INTO last_Sequence
FROM User_Sequences
WHERE Sequence_Name = 'test_SEQ';
SELECT :NEW."id" INTO last_InsertID FROM DUAL;
WHILE (last_InsertID > last_Sequence) LOOP
SELECT "test_SEQ".NEXTVAL INTO last_Sequence FROM DUAL;
END LOOP;
END IF;
END;
EOD;
self::assertEquals($createTriggerStatement, $sql[3]);
}
/**
* @dataProvider getReturnsGetListTableColumnsSQL
*/
public function testReturnsGetListTableColumnsSQL(?string $database, string $expectedSql): void
{
// note: this assertion is a bit strict, as it compares a full SQL string.
// Should this break in future, then please try to reduce the matching to substring matching while reworking
// the tests
self::assertEquals($expectedSql, $this->platform->getListTableColumnsSQL('"test"', $database));
}
/**
* @return mixed[][]
*/
public static function getReturnsGetListTableColumnsSQL(): iterable
{
return [
[
null,
<<<'SQL'
SELECT c.*,
(
SELECT d.comments
FROM user_col_comments d
WHERE d.TABLE_NAME = c.TABLE_NAME
AND d.COLUMN_NAME = c.COLUMN_NAME
) AS comments
FROM user_tab_columns c
WHERE c.table_name = 'test'
ORDER BY c.column_id
SQL
,
],
[
'/',
<<<'SQL'
SELECT c.*,
(
SELECT d.comments
FROM user_col_comments d
WHERE d.TABLE_NAME = c.TABLE_NAME
AND d.COLUMN_NAME = c.COLUMN_NAME
) AS comments
FROM user_tab_columns c
WHERE c.table_name = 'test'
ORDER BY c.column_id
SQL
,
],
[
'scott',
<<<'SQL'
SELECT c.*,
(
SELECT d.comments
FROM all_col_comments d
WHERE d.TABLE_NAME = c.TABLE_NAME AND d.OWNER = c.OWNER
AND d.COLUMN_NAME = c.COLUMN_NAME
) AS comments
FROM all_tab_columns c
WHERE c.table_name = 'test' AND c.owner = 'SCOTT'
ORDER BY c.column_id
SQL
,
],
];
}
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(): string
{
return 'CONSTRAINT "select" UNIQUE (foo)';
}
protected function getQuotesReservedKeywordInIndexDeclarationSQL(): string
{
return 'INDEX "select" (foo)';
}
protected function getQuotesReservedKeywordInTruncateTableSQL(): string
{
return 'TRUNCATE TABLE "select"';
}
/**
* {@inheritdoc}
*/
protected function getAlterStringToFixedStringSQL(): array
{
return ['ALTER TABLE mytable MODIFY (name CHAR(2) DEFAULT NULL)'];
}
/**
* {@inheritdoc}
*/
protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL(): array
{
return ['ALTER INDEX idx_foo RENAME TO idx_foo_renamed'];
}
public function testQuotesDatabaseNameInListSequencesSQL(): void
{
self::assertStringContainsStringIgnoringCase(
"'Foo''Bar\\'",
$this->platform->getListSequencesSQL("Foo'Bar\\")
);
}
public function testQuotesTableNameInListTableIndexesSQL(): void
{
self::assertStringContainsStringIgnoringCase(
"'Foo''Bar\\'",
$this->platform->getListTableIndexesSQL("Foo'Bar\\")
);
}
public function testQuotesTableNameInListTableForeignKeysSQL(): void
{
self::assertStringContainsStringIgnoringCase(
"'Foo''Bar\\'",
$this->platform->getListTableForeignKeysSQL("Foo'Bar\\")
);
}
public function testQuotesTableNameInListTableConstraintsSQL(): void
{
self::assertStringContainsStringIgnoringCase(
"'Foo''Bar\\'",
$this->platform->getListTableConstraintsSQL("Foo'Bar\\")
);
}
public function testQuotesTableNameInListTableColumnsSQL(): void
{
self::assertStringContainsStringIgnoringCase(
"'Foo''Bar\\'",
$this->platform->getListTableColumnsSQL("Foo'Bar\\")
);
}
public function testQuotesDatabaseNameInListTableColumnsSQL(): void
{
self::assertStringContainsStringIgnoringCase(
"'Foo''Bar\\'",
$this->platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\")
);
}
}