-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AbstractSQLServerPlatformTestCase.php
1814 lines (1599 loc) · 75.5 KB
/
AbstractSQLServerPlatformTestCase.php
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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Doctrine\DBAL\Tests\Platforms;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\Type;
/**
* @extends AbstractPlatformTestCase<SQLServer2012Platform>
*/
abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCase
{
public function getGenerateTableSql(): string
{
return 'CREATE TABLE test (id INT IDENTITY NOT NULL, test NVARCHAR(255), PRIMARY KEY (id))';
}
/**
* {@inheritDoc}
*/
public function getGenerateTableWithMultiColumnUniqueIndexSql(): array
{
return [
'CREATE TABLE test (foo NVARCHAR(255), bar NVARCHAR(255))',
'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar)'
. ' WHERE foo IS NOT NULL AND bar IS NOT NULL',
];
}
/**
* {@inheritDoc}
*/
public function getGenerateAlterTableSql(): array
{
return [
'ALTER TABLE mytable ADD quota INT',
'ALTER TABLE mytable DROP COLUMN foo',
'ALTER TABLE mytable ALTER COLUMN baz NVARCHAR(255) NOT NULL',
"ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_78240498 DEFAULT 'def' FOR baz",
'ALTER TABLE mytable ALTER COLUMN bloo BIT NOT NULL',
"ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_CECED971 DEFAULT '0' FOR bloo",
"sp_rename 'mytable', 'userlist'",
"DECLARE @sql NVARCHAR(MAX) = N''; " .
"SELECT @sql += N'EXEC sp_rename N''' + dc.name + ''', N''' " .
"+ REPLACE(dc.name, '6B2BD609', 'E2B58069') + ''', ''OBJECT'';' " .
'FROM sys.default_constraints dc ' .
'JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id ' .
"WHERE tbl.name = 'userlist';" .
'EXEC sp_executesql @sql',
];
}
public function testDoesNotSupportRegexp(): void
{
$this->expectException(Exception::class);
$this->platform->getRegexpExpression();
}
public function testGeneratesSqlSnippets(): void
{
self::assertEquals('CONVERT(date, GETDATE())', $this->platform->getCurrentDateSQL());
self::assertEquals('CONVERT(time, GETDATE())', $this->platform->getCurrentTimeSQL());
self::assertEquals('CURRENT_TIMESTAMP', $this->platform->getCurrentTimestampSQL());
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 REPEATABLE READ',
$this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::REPEATABLE_READ)
);
self::assertEquals(
'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
$this->platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::SERIALIZABLE)
);
}
public function testGeneratesDDLSnippets(): void
{
$dropDatabaseExpectation = 'DROP DATABASE foobar';
self::assertEquals('SELECT * FROM sys.databases', $this->platform->getListDatabasesSQL());
self::assertEquals('CREATE DATABASE foobar', $this->platform->getCreateDatabaseSQL('foobar'));
self::assertEquals($dropDatabaseExpectation, $this->platform->getDropDatabaseSQL('foobar'));
self::assertEquals('DROP TABLE foobar', $this->platform->getDropTableSQL('foobar'));
}
public function testGeneratesTypeDeclarationForIntegers(): void
{
self::assertEquals(
'INT',
$this->platform->getIntegerTypeDeclarationSQL([])
);
self::assertEquals(
'INT IDENTITY',
$this->platform->getIntegerTypeDeclarationSQL(['autoincrement' => true])
);
self::assertEquals(
'INT IDENTITY',
$this->platform->getIntegerTypeDeclarationSQL(
['autoincrement' => true, 'primary' => true]
)
);
}
public function testGeneratesTypeDeclarationsForStrings(): void
{
self::assertEquals(
'NCHAR(10)',
$this->platform->getVarcharTypeDeclarationSQL(
['length' => 10, 'fixed' => true]
)
);
self::assertEquals(
'NVARCHAR(50)',
$this->platform->getVarcharTypeDeclarationSQL(['length' => 50])
);
self::assertEquals(
'NVARCHAR(255)',
$this->platform->getVarcharTypeDeclarationSQL([])
);
self::assertSame('VARCHAR(MAX)', $this->platform->getClobTypeDeclarationSQL([]));
self::assertSame(
'VARCHAR(MAX)',
$this->platform->getClobTypeDeclarationSQL(['length' => 5, 'fixed' => true])
);
}
public function testPrefersIdentityColumns(): void
{
self::assertTrue($this->platform->prefersIdentityColumns());
}
public function testSupportsIdentityColumns(): void
{
self::assertTrue($this->platform->supportsIdentityColumns());
}
public function testSupportsCreateDropDatabase(): void
{
self::assertTrue($this->platform->supportsCreateDropDatabase());
}
public function testSupportsSchemas(): void
{
self::assertTrue($this->platform->supportsSchemas());
}
public function testDoesNotSupportSavePoints(): void
{
self::assertTrue($this->platform->supportsSavepoints());
}
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) WHERE test IS NOT NULL AND test2 IS NOT NULL';
}
protected function getGenerateForeignKeySql(): string
{
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
}
public function testModifyLimitQuery(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
self::assertEquals('SELECT * FROM user ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY', $sql);
}
public function testModifyLimitQueryWithEmptyOffset(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user', 10);
self::assertEquals('SELECT * FROM user ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY', $sql);
}
public function testModifyLimitQueryWithOffset(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10, 5);
self::assertEquals('SELECT * FROM user ORDER BY username DESC OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY', $sql);
}
public function testModifyLimitQueryWithAscOrderBy(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user ORDER BY username ASC', 10);
self::assertEquals('SELECT * FROM user ORDER BY username ASC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY', $sql);
}
public function testModifyLimitQueryWithLowercaseOrderBy(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user order by username', 10);
self::assertEquals('SELECT * FROM user order by username OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY', $sql);
}
public function testModifyLimitQueryWithDescOrderBy(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT * FROM user ORDER BY username DESC', 10);
self::assertEquals('SELECT * FROM user ORDER BY username DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY', $sql);
}
public function testModifyLimitQueryWithMultipleOrderBy(): void
{
$sql = $this->platform->modifyLimitQuery(
'SELECT * FROM user ORDER BY username DESC, usereamil ASC',
10
);
self::assertEquals(
'SELECT * FROM user ORDER BY username DESC, usereamil ASC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
}
public function testModifyLimitQueryWithSubSelect(): void
{
$sql = $this->platform->modifyLimitQuery(
'SELECT * FROM (SELECT u.id as uid, u.name as uname) dctrn_result',
10
);
self::assertEquals(
'SELECT * FROM ('
. 'SELECT u.id as uid, u.name as uname'
. ') dctrn_result ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
}
public function testModifyLimitQueryWithSubSelectAndOrder(): void
{
$sql = $this->platform->modifyLimitQuery(
'SELECT * FROM ('
. 'SELECT u.id as uid, u.name as uname'
. ') dctrn_result ORDER BY uname DESC',
10
);
self::assertEquals(
'SELECT * FROM ('
. 'SELECT u.id as uid, u.name as uname'
. ') dctrn_result ORDER BY uname DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
$sql = $this->platform->modifyLimitQuery(
'SELECT * FROM (SELECT u.id, u.name) dctrn_result ORDER BY name DESC',
10
);
self::assertEquals(
'SELECT * FROM ('
. 'SELECT u.id, u.name'
. ') dctrn_result ORDER BY name DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
}
public function testModifyLimitQueryWithSubSelectAndMultipleOrder(): void
{
$sql = $this->platform->modifyLimitQuery(
'SELECT * FROM (SELECT u.id as uid, u.name as uname) dctrn_result ORDER BY uname DESC, uid ASC',
10,
5
);
self::assertEquals(
'SELECT * FROM ('
. 'SELECT u.id as uid, u.name as uname'
. ') dctrn_result ORDER BY uname DESC, uid ASC OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
$sql = $this->platform->modifyLimitQuery(
'SELECT * FROM (SELECT u.id uid, u.name uname) dctrn_result ORDER BY uname DESC, uid ASC',
10,
5
);
self::assertEquals(
'SELECT * FROM ('
. 'SELECT u.id uid, u.name uname'
. ') dctrn_result ORDER BY uname DESC, uid ASC OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
$sql = $this->platform->modifyLimitQuery(
'SELECT * FROM (SELECT u.id, u.name) dctrn_result ORDER BY name DESC, id ASC',
10,
5
);
self::assertEquals(
'SELECT * FROM ('
. 'SELECT u.id, u.name'
. ') dctrn_result ORDER BY name DESC, id ASC OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
}
public function testModifyLimitQueryWithFromColumnNames(): void
{
$sql = $this->platform->modifyLimitQuery('SELECT a.fromFoo, fromBar FROM foo', 10);
self::assertEquals(
'SELECT a.fromFoo, fromBar FROM foo ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
}
public function testModifyLimitQueryWithExtraLongQuery(): void
{
$query = 'SELECT table1.column1, table2.column2, table3.column3, table4.column4, '
. 'table5.column5, table6.column6, table7.column7, table8.column8'
. ' FROM table1, table2, table3, table4, table5, table6, table7, table8'
. ' WHERE (table1.column1 = table2.column2) AND (table1.column1 = table3.column3)'
. ' AND (table1.column1 = table4.column4) AND (table1.column1 = table5.column5)'
. ' AND (table1.column1 = table6.column6) AND (table1.column1 = table7.column7)'
. ' AND (table1.column1 = table8.column8)'
. ' AND (table2.column2 = table3.column3) AND (table2.column2 = table4.column4)'
. ' AND (table2.column2 = table5.column5) AND (table2.column2 = table6.column6)'
. ' AND (table2.column2 = table7.column7) AND (table2.column2 = table8.column8)'
. ' AND (table3.column3 = table4.column4) AND (table3.column3 = table5.column5)'
. ' AND (table3.column3 = table6.column6) AND (table3.column3 = table7.column7)'
. ' AND (table3.column3 = table8.column8)'
. ' AND (table4.column4 = table5.column5) AND (table4.column4 = table6.column6)'
. ' AND (table4.column4 = table7.column7) AND (table4.column4 = table8.column8)'
. ' AND (table5.column5 = table6.column6) AND (table5.column5 = table7.column7)'
. ' AND (table5.column5 = table8.column8)'
. ' AND (table6.column6 = table7.column7) AND (table6.column6 = table8.column8)'
. ' AND (table7.column7 = table8.column8)';
$expected = 'SELECT table1.column1, table2.column2, table3.column3, table4.column4, '
. 'table5.column5, table6.column6, table7.column7, table8.column8'
. ' FROM table1, table2, table3, table4, table5, table6, table7, table8'
. ' WHERE (table1.column1 = table2.column2) AND (table1.column1 = table3.column3)'
. ' AND (table1.column1 = table4.column4) AND (table1.column1 = table5.column5)'
. ' AND (table1.column1 = table6.column6) AND (table1.column1 = table7.column7)'
. ' AND (table1.column1 = table8.column8)'
. ' AND (table2.column2 = table3.column3) AND (table2.column2 = table4.column4)'
. ' AND (table2.column2 = table5.column5) AND (table2.column2 = table6.column6)'
. ' AND (table2.column2 = table7.column7) AND (table2.column2 = table8.column8)'
. ' AND (table3.column3 = table4.column4) AND (table3.column3 = table5.column5)'
. ' AND (table3.column3 = table6.column6) AND (table3.column3 = table7.column7)'
. ' AND (table3.column3 = table8.column8)'
. ' AND (table4.column4 = table5.column5) AND (table4.column4 = table6.column6)'
. ' AND (table4.column4 = table7.column7) AND (table4.column4 = table8.column8)'
. ' AND (table5.column5 = table6.column6) AND (table5.column5 = table7.column7)'
. ' AND (table5.column5 = table8.column8)'
. ' AND (table6.column6 = table7.column7) AND (table6.column6 = table8.column8)'
. ' AND (table7.column7 = table8.column8)'
. ' ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY';
self::assertEquals(
$expected,
$this->platform->modifyLimitQuery($query, 10)
);
}
public function testModifyLimitQueryWithOrderByClause(): void
{
$sql = 'SELECT m0_.NOMBRE AS NOMBRE0, m0_.FECHAINICIO AS FECHAINICIO1, m0_.FECHAFIN AS FECHAFIN2'
. ' FROM MEDICION m0_'
. ' INNER JOIN ESTUDIO e1_ ON m0_.ESTUDIO_ID = e1_.ID'
. ' INNER JOIN CLIENTE c2_ ON e1_.CLIENTE_ID = c2_.ID'
. ' INNER JOIN USUARIO u3_ ON c2_.ID = u3_.CLIENTE_ID'
. ' WHERE u3_.ID = ? ORDER BY m0_.FECHAINICIO DESC';
$expected = 'SELECT m0_.NOMBRE AS NOMBRE0, m0_.FECHAINICIO AS FECHAINICIO1, m0_.FECHAFIN AS FECHAFIN2'
. ' FROM MEDICION m0_'
. ' INNER JOIN ESTUDIO e1_ ON m0_.ESTUDIO_ID = e1_.ID'
. ' INNER JOIN CLIENTE c2_ ON e1_.CLIENTE_ID = c2_.ID'
. ' INNER JOIN USUARIO u3_ ON c2_.ID = u3_.CLIENTE_ID'
. ' WHERE u3_.ID = ? ORDER BY m0_.FECHAINICIO DESC OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY';
$actual = $this->platform->modifyLimitQuery($sql, 10, 5);
self::assertEquals($expected, $actual);
}
public function testModifyLimitQueryWithSubSelectInSelectList(): void
{
$sql = $this->platform->modifyLimitQuery(
'SELECT ' .
'u.id, ' .
'(u.foo/2) foodiv, ' .
'CONCAT(u.bar, u.baz) barbaz, ' .
'(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) ' .
'FROM profile p WHERE p.user_id = u.id) login_count ' .
'FROM user u ' .
"WHERE u.status = 'disabled'",
10
);
self::assertEquals(
'SELECT ' .
'u.id, ' .
'(u.foo/2) foodiv, ' .
'CONCAT(u.bar, u.baz) barbaz, ' .
'(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) ' .
'FROM profile p WHERE p.user_id = u.id) login_count ' .
'FROM user u ' .
"WHERE u.status = 'disabled' " .
'ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
}
public function testModifyLimitQueryWithSubSelectInSelectListAndOrderByClause(): void
{
$sql = $this->platform->modifyLimitQuery(
'SELECT ' .
'u.id, ' .
'(u.foo/2) foodiv, ' .
'CONCAT(u.bar, u.baz) barbaz, ' .
'(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) ' .
'FROM profile p WHERE p.user_id = u.id) login_count ' .
'FROM user u ' .
"WHERE u.status = 'disabled' " .
'ORDER BY u.username DESC',
10,
5
);
self::assertEquals(
'SELECT ' .
'u.id, ' .
'(u.foo/2) foodiv, ' .
'CONCAT(u.bar, u.baz) barbaz, ' .
'(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) ' .
'FROM profile p WHERE p.user_id = u.id) login_count ' .
'FROM user u ' .
"WHERE u.status = 'disabled' " .
'ORDER BY u.username DESC ' .
'OFFSET 5 ROWS FETCH NEXT 10 ROWS ONLY',
$sql
);
}
public function testModifyLimitQueryWithAggregateFunctionInOrderByClause(): void
{
$sql = $this->platform->modifyLimitQuery(
'SELECT ' .
'MAX(heading_id) aliased, ' .
'code ' .
'FROM operator_model_operator ' .
'GROUP BY code ' .
'ORDER BY MAX(heading_id) DESC',
1,
0
);
self::assertEquals(
'SELECT ' .
'MAX(heading_id) aliased, ' .
'code ' .
'FROM operator_model_operator ' .
'GROUP BY code ' .
'ORDER BY MAX(heading_id) DESC ' .
'OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY',
$sql
);
}
public function testModifyLimitSubqueryWithJoinAndSubqueryOrderedByColumnFromBaseTable(): void
{
$querySql = 'SELECT DISTINCT id_0, name_1 '
. 'FROM ('
. 'SELECT t1.id AS id_0, t2.name AS name_1 '
. 'FROM table_parent t1 '
. 'LEFT JOIN join_table t2 ON t1.id = t2.table_id'
. ') dctrn_result '
. 'ORDER BY id_0 ASC';
$alteredSql = 'SELECT DISTINCT id_0, name_1 '
. 'FROM ('
. 'SELECT t1.id AS id_0, t2.name AS name_1 '
. 'FROM table_parent t1 '
. 'LEFT JOIN join_table t2 ON t1.id = t2.table_id'
. ') dctrn_result '
. 'ORDER BY id_0 ASC OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY';
$sql = $this->platform->modifyLimitQuery($querySql, 5);
self::assertEquals($alteredSql, $sql);
}
public function testModifyLimitSubqueryWithJoinAndSubqueryOrderedByColumnFromJoinTable(): void
{
$querySql = 'SELECT DISTINCT id_0, name_1 '
. 'FROM ('
. 'SELECT t1.id AS id_0, t2.name AS name_1 '
. 'FROM table_parent t1 '
. 'LEFT JOIN join_table t2 ON t1.id = t2.table_id'
. ') dctrn_result '
. 'ORDER BY name_1 ASC';
$alteredSql = 'SELECT DISTINCT id_0, name_1 '
. 'FROM ('
. 'SELECT t1.id AS id_0, t2.name AS name_1 '
. 'FROM table_parent t1 '
. 'LEFT JOIN join_table t2 ON t1.id = t2.table_id'
. ') dctrn_result '
. 'ORDER BY name_1 ASC OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY';
$sql = $this->platform->modifyLimitQuery($querySql, 5);
self::assertEquals($alteredSql, $sql);
}
public function testModifyLimitSubqueryWithJoinAndSubqueryOrderedByColumnsFromBothTables(): void
{
$querySql = 'SELECT DISTINCT id_0, name_1, foo_2 '
. 'FROM ('
. 'SELECT t1.id AS id_0, t2.name AS name_1, t2.foo AS foo_2 '
. 'FROM table_parent t1 '
. 'LEFT JOIN join_table t2 ON t1.id = t2.table_id'
. ') dctrn_result '
. 'ORDER BY name_1 ASC, foo_2 DESC';
$alteredSql = 'SELECT DISTINCT id_0, name_1, foo_2 '
. 'FROM ('
. 'SELECT t1.id AS id_0, t2.name AS name_1, t2.foo AS foo_2 '
. 'FROM table_parent t1 '
. 'LEFT JOIN join_table t2 ON t1.id = t2.table_id'
. ') dctrn_result '
. 'ORDER BY name_1 ASC, foo_2 DESC OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY';
$sql = $this->platform->modifyLimitQuery($querySql, 5);
self::assertEquals($alteredSql, $sql);
}
public function testModifyLimitSubquerySimple(): void
{
$query = 'SELECT DISTINCT id_0 FROM ('
. 'SELECT k0_.id AS id_0, k0_.column AS column_1 FROM key_table k0_ WHERE (k0_.where_column IN (1))'
. ') dctrn_result';
$expected = 'SELECT DISTINCT id_0 FROM ('
. 'SELECT k0_.id AS id_0, k0_.column AS column_1 FROM key_table k0_ WHERE (k0_.where_column IN (1))'
. ') dctrn_result ORDER BY 1 OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY';
self::assertEquals(
$expected,
$this->platform->modifyLimitQuery($query, 20)
);
}
public function testQuoteIdentifier(): void
{
self::assertEquals('[fo][o]', $this->platform->quoteIdentifier('fo]o'));
self::assertEquals('[test]', $this->platform->quoteIdentifier('test'));
self::assertEquals('[test].[test]', $this->platform->quoteIdentifier('test.test'));
}
public function testQuoteSingleIdentifier(): void
{
self::assertEquals('[fo][o]', $this->platform->quoteSingleIdentifier('fo]o'));
self::assertEquals('[test]', $this->platform->quoteSingleIdentifier('test'));
self::assertEquals('[test.test]', $this->platform->quoteSingleIdentifier('test.test'));
}
public function testCreateClusteredIndex(): void
{
$idx = new Index('idx', ['id']);
$idx->addFlag('clustered');
self::assertEquals('CREATE CLUSTERED INDEX idx ON tbl (id)', $this->platform->getCreateIndexSQL($idx, 'tbl'));
}
public function testCreateNonClusteredPrimaryKeyInTable(): void
{
$table = new Table('tbl');
$table->addColumn('id', 'integer');
$table->setPrimaryKey(['id']);
$table->getIndex('primary')->addFlag('nonclustered');
self::assertEquals(
['CREATE TABLE tbl (id INT NOT NULL, PRIMARY KEY NONCLUSTERED (id))'],
$this->platform->getCreateTableSQL($table)
);
}
public function testCreateNonClusteredPrimaryKey(): void
{
$idx = new Index('idx', ['id'], false, true);
$idx->addFlag('nonclustered');
self::assertEquals(
'ALTER TABLE tbl ADD PRIMARY KEY NONCLUSTERED (id)',
$this->platform->getCreatePrimaryKeySQL($idx, 'tbl')
);
}
public function testAlterAddPrimaryKey(): void
{
$idx = new Index('idx', ['id'], false, true);
self::assertEquals('ALTER TABLE tbl ADD PRIMARY KEY (id)', $this->platform->getCreateIndexSQL($idx, 'tbl'));
}
/**
* {@inheritDoc}
*/
protected function getQuotedColumnInPrimaryKeySQL(): array
{
return ['CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL, PRIMARY KEY ([create]))'];
}
/**
* {@inheritDoc}
*/
protected function getQuotedColumnInIndexSQL(): array
{
return [
'CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL)',
'CREATE INDEX IDX_22660D028FD6E0FB ON [quoted] ([create])',
];
}
/**
* {@inheritDoc}
*/
protected function getQuotedNameInIndexSQL(): array
{
return [
'CREATE TABLE test (column1 NVARCHAR(255) NOT NULL)',
'CREATE INDEX [key] ON test (column1)',
];
}
/**
* {@inheritDoc}
*/
protected function getQuotedColumnInForeignKeySQL(): array
{
return [
'CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL, '
. 'foo NVARCHAR(255) NOT NULL, [bar] NVARCHAR(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 testGetCreateSchemaSQL(): void
{
$schemaName = 'schema';
$sql = $this->platform->getCreateSchemaSQL($schemaName);
self::assertEquals('CREATE SCHEMA ' . $schemaName, $sql);
}
public function testCreateTableWithSchemaColumnComments(): void
{
$table = new Table('testschema.test');
$table->addColumn('id', 'integer', ['comment' => 'This is a comment']);
$table->setPrimaryKey(['id']);
$expectedSql = [
'CREATE TABLE testschema.test (id INT NOT NULL, PRIMARY KEY (id))',
"EXEC sp_addextendedproperty N'MS_Description', N'This is a comment', "
. "N'SCHEMA', 'testschema', N'TABLE', 'test', N'COLUMN', id",
];
self::assertEquals($expectedSql, $this->platform->getCreateTableSQL($table));
}
public function testAlterTableWithSchemaColumnComments(): void
{
$tableDiff = new TableDiff('testschema.mytable');
$tableDiff->addedColumns['quota'] = new Column('quota', Type::getType('integer'), ['comment' => 'A comment']);
$expectedSql = [
'ALTER TABLE testschema.mytable ADD quota INT NOT NULL',
"EXEC sp_addextendedproperty N'MS_Description', N'A comment', "
. "N'SCHEMA', 'testschema', N'TABLE', 'mytable', N'COLUMN', quota",
];
self::assertEquals($expectedSql, $this->platform->getAlterTableSQL($tableDiff));
}
public function testAlterTableWithSchemaDropColumnComments(): void
{
$tableDiff = new TableDiff('testschema.mytable');
$tableDiff->changedColumns['quota'] = new ColumnDiff(
'quota',
new Column('quota', Type::getType('integer'), []),
['comment'],
new Column('quota', Type::getType('integer'), ['comment' => 'A comment'])
);
$expectedSql = [
"EXEC sp_dropextendedproperty N'MS_Description'"
. ", N'SCHEMA', 'testschema', N'TABLE', 'mytable', N'COLUMN', quota",
];
self::assertEquals($expectedSql, $this->platform->getAlterTableSQL($tableDiff));
}
public function testAlterTableWithSchemaUpdateColumnComments(): void
{
$tableDiff = new TableDiff('testschema.mytable');
$tableDiff->changedColumns['quota'] = new ColumnDiff(
'quota',
new Column('quota', Type::getType('integer'), ['comment' => 'B comment']),
['comment'],
new Column('quota', Type::getType('integer'), ['comment' => 'A comment'])
);
$expectedSql = ["EXEC sp_updateextendedproperty N'MS_Description', N'B comment', "
. "N'SCHEMA', 'testschema', N'TABLE', 'mytable', N'COLUMN', quota",
];
self::assertEquals($expectedSql, $this->platform->getAlterTableSQL($tableDiff));
}
/**
* {@inheritDoc}
*/
public function getCreateTableColumnCommentsSQL(): array
{
return [
'CREATE TABLE test (id INT NOT NULL, PRIMARY KEY (id))',
"EXEC sp_addextendedproperty N'MS_Description', N'This is a comment', "
. "N'SCHEMA', 'dbo', N'TABLE', 'test', N'COLUMN', id",
];
}
/**
* {@inheritDoc}
*/
public function getAlterTableColumnCommentsSQL(): array
{
return [
'ALTER TABLE mytable ADD quota INT NOT NULL',
"EXEC sp_addextendedproperty N'MS_Description', N'A comment', "
. "N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', quota",
];
}
/**
* {@inheritDoc}
*/
public function getCreateTableColumnTypeCommentsSQL(): array
{
return [
'CREATE TABLE test (id INT NOT NULL, data VARCHAR(MAX) NOT NULL, PRIMARY KEY (id))',
"EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:array)', "
. "N'SCHEMA', 'dbo', N'TABLE', 'test', N'COLUMN', data",
];
}
public function testGeneratesCreateTableSQLWithColumnComments(): void
{
$table = new Table('mytable');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('comment_null', 'integer', ['comment' => null]);
$table->addColumn('comment_false', 'integer', ['comment' => false]);
$table->addColumn('comment_empty_string', 'integer', ['comment' => '']);
$table->addColumn('comment_integer_0', 'integer', ['comment' => 0]);
$table->addColumn('comment_float_0', 'integer', ['comment' => 0.0]);
$table->addColumn('comment_string_0', 'integer', ['comment' => '0']);
$table->addColumn('comment', 'integer', ['comment' => 'Doctrine 0wnz you!']);
$table->addColumn(
'`comment_quoted`',
'integer',
['comment' => 'Doctrine 0wnz comments for explicitly quoted columns!']
);
$table->addColumn('create', 'integer', ['comment' => 'Doctrine 0wnz comments for reserved keyword columns!']);
$table->addColumn('commented_type', 'object');
$table->addColumn('commented_type_with_comment', 'array', ['comment' => 'Doctrine array type.']);
$table->addColumn('comment_with_string_literal_char', 'string', ['comment' => "O'Reilly"]);
$table->setPrimaryKey(['id']);
self::assertEquals(
[
'CREATE TABLE mytable (id INT IDENTITY NOT NULL, comment_null INT NOT NULL, '
. 'comment_false INT NOT NULL, '
. 'comment_empty_string INT NOT NULL, '
. 'comment_integer_0 INT NOT NULL, '
. 'comment_float_0 INT NOT NULL, '
. 'comment_string_0 INT NOT NULL, '
. 'comment INT NOT NULL, '
. '[comment_quoted] INT NOT NULL, '
. '[create] INT NOT NULL, '
. 'commented_type VARCHAR(MAX) NOT NULL, '
. 'commented_type_with_comment VARCHAR(MAX) NOT NULL, '
. 'comment_with_string_literal_char NVARCHAR(255) NOT NULL, '
. 'PRIMARY KEY (id))',
"EXEC sp_addextendedproperty N'MS_Description', "
. "N'0', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_integer_0",
"EXEC sp_addextendedproperty N'MS_Description', "
. "N'0', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_float_0",
"EXEC sp_addextendedproperty N'MS_Description', "
. "N'0', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_string_0",
"EXEC sp_addextendedproperty N'MS_Description', "
. "N'Doctrine 0wnz you!', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment",
"EXEC sp_addextendedproperty N'MS_Description', "
. "N'Doctrine 0wnz comments for explicitly quoted columns!', "
. "N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', [comment_quoted]",
"EXEC sp_addextendedproperty N'MS_Description', "
. "N'Doctrine 0wnz comments for reserved keyword columns!', "
. "N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', [create]",
"EXEC sp_addextendedproperty N'MS_Description', "
. "N'(DC2Type:object)', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', commented_type",
"EXEC sp_addextendedproperty N'MS_Description', "
. "N'Doctrine array type.(DC2Type:array)', "
. "N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', commented_type_with_comment",
"EXEC sp_addextendedproperty N'MS_Description', N'O''Reilly', "
. "N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_with_string_literal_char",
],
$this->platform->getCreateTableSQL($table)
);
}
public function testGeneratesAlterTableSQLWithColumnComments(): void
{
$table = new Table('mytable');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('comment_null', 'integer', ['comment' => null]);
$table->addColumn('comment_false', 'integer', ['comment' => false]);
$table->addColumn('comment_empty_string', 'integer', ['comment' => '']);
$table->addColumn('comment_integer_0', 'integer', ['comment' => 0]);
$table->addColumn('comment_float_0', 'integer', ['comment' => 0.0]);
$table->addColumn('comment_string_0', 'integer', ['comment' => '0']);
$table->addColumn('comment', 'integer', ['comment' => 'Doctrine 0wnz you!']);
$table->addColumn(
'`comment_quoted`',
'integer',
['comment' => 'Doctrine 0wnz comments for explicitly quoted columns!']
);
$table->addColumn('create', 'integer', ['comment' => 'Doctrine 0wnz comments for reserved keyword columns!']);
$table->addColumn('commented_type', 'object');
$table->addColumn('commented_type_with_comment', 'array', ['comment' => 'Doctrine array type.']);
$table->addColumn('comment_with_string_literal_quote_char', 'array', ['comment' => "O'Reilly"]);
$table->setPrimaryKey(['id']);
$tableDiff = new TableDiff('mytable');
$tableDiff->fromTable = $table;
$tableDiff->addedColumns['added_comment_none']
= new Column('added_comment_none', Type::getType('integer'));
$tableDiff->addedColumns['added_comment_null']
= new Column('added_comment_null', Type::getType('integer'), ['comment' => null]);
$tableDiff->addedColumns['added_comment_false']
= new Column('added_comment_false', Type::getType('integer'), ['comment' => false]);
$tableDiff->addedColumns['added_comment_empty_string']
= new Column('added_comment_empty_string', Type::getType('integer'), ['comment' => '']);
$tableDiff->addedColumns['added_comment_integer_0']
= new Column('added_comment_integer_0', Type::getType('integer'), ['comment' => 0]);
$tableDiff->addedColumns['added_comment_float_0']
= new Column('added_comment_float_0', Type::getType('integer'), ['comment' => 0.0]);
$tableDiff->addedColumns['added_comment_string_0']
= new Column('added_comment_string_0', Type::getType('integer'), ['comment' => '0']);
$tableDiff->addedColumns['added_comment']
= new Column('added_comment', Type::getType('integer'), ['comment' => 'Doctrine']);
$tableDiff->addedColumns['`added_comment_quoted`']
= new Column('`added_comment_quoted`', Type::getType('integer'), ['comment' => 'rulez']);
$tableDiff->addedColumns['select']
= new Column('select', Type::getType('integer'), ['comment' => '666']);
$tableDiff->addedColumns['added_commented_type']
= new Column('added_commented_type', Type::getType('object'));
$tableDiff->addedColumns['added_commented_type_with_comment']
= new Column('added_commented_type_with_comment', Type::getType('array'), ['comment' => '666']);
$tableDiff->addedColumns['added_comment_with_string_literal_char']
= new Column('added_comment_with_string_literal_char', Type::getType('string'), ['comment' => "''"]);
$tableDiff->renamedColumns['comment_float_0']
= new Column('comment_double_0', Type::getType('decimal'), ['comment' => 'Double for real!']);
// Add comment to non-commented column.
$tableDiff->changedColumns['id'] = new ColumnDiff(
'id',
new Column('id', Type::getType('integer'), ['autoincrement' => true, 'comment' => 'primary']),
['comment'],
new Column('id', Type::getType('integer'), ['autoincrement' => true])
);
// Remove comment from null-commented column.
$tableDiff->changedColumns['comment_null'] = new ColumnDiff(
'comment_null',
new Column('comment_null', Type::getType('string')),
['type'],
new Column('comment_null', Type::getType('integer'), ['comment' => null])
);
// Add comment to false-commented column.
$tableDiff->changedColumns['comment_false'] = new ColumnDiff(
'comment_false',
new Column('comment_false', Type::getType('integer'), ['comment' => 'false']),
['comment'],
new Column('comment_false', Type::getType('integer'), ['comment' => false])
);
// Change type to custom type from empty string commented column.
$tableDiff->changedColumns['comment_empty_string'] = new ColumnDiff(
'comment_empty_string',
new Column('comment_empty_string', Type::getType('object')),
['type'],
new Column('comment_empty_string', Type::getType('integer'), ['comment' => ''])
);
// Change comment to false-comment from zero-string commented column.
$tableDiff->changedColumns['comment_string_0'] = new ColumnDiff(
'comment_string_0',
new Column('comment_string_0', Type::getType('integer'), ['comment' => false]),
['comment'],
new Column('comment_string_0', Type::getType('integer'), ['comment' => '0'])
);
// Remove comment from regular commented column.
$tableDiff->changedColumns['comment'] = new ColumnDiff(
'comment',
new Column('comment', Type::getType('integer')),
['comment'],
new Column('comment', Type::getType('integer'), ['comment' => 'Doctrine 0wnz you!'])
);
// Change comment and change type to custom type from regular commented column.
$tableDiff->changedColumns['`comment_quoted`'] = new ColumnDiff(
'`comment_quoted`',
new Column('`comment_quoted`', Type::getType('array'), ['comment' => 'Doctrine array.']),
['comment', 'type'],
new Column('`comment_quoted`', Type::getType('integer'), ['comment' => 'Doctrine 0wnz you!'])
);
// Remove comment and change type to custom type from regular commented column.
$tableDiff->changedColumns['create'] = new ColumnDiff(
'create',
new Column('create', Type::getType('object')),
['comment', 'type'],
new Column(
'create',
Type::getType('integer'),
['comment' => 'Doctrine 0wnz comments for reserved keyword columns!']
)
);
// Add comment and change custom type to regular type from non-commented column.
$tableDiff->changedColumns['commented_type'] = new ColumnDiff(
'commented_type',
new Column('commented_type', Type::getType('integer'), ['comment' => 'foo']),
['comment', 'type'],
new Column('commented_type', Type::getType('object'))
);
// Remove comment from commented custom type column.
$tableDiff->changedColumns['commented_type_with_comment'] = new ColumnDiff(
'commented_type_with_comment',
new Column('commented_type_with_comment', Type::getType('array')),
['comment'],
new Column('commented_type_with_comment', Type::getType('array'), ['comment' => 'Doctrine array type.'])
);
// Change comment from comment with string literal char column.
$tableDiff->changedColumns['comment_with_string_literal_char'] = new ColumnDiff(
'comment_with_string_literal_char',
new Column('comment_with_string_literal_char', Type::getType('string'), ['comment' => "'"]),
['comment'],
new Column('comment_with_string_literal_char', Type::getType('array'), ['comment' => "O'Reilly"])
);
$tableDiff->removedColumns['comment_integer_0']
= new Column('comment_integer_0', Type::getType('integer'), ['comment' => 0]);
self::assertEquals(
[
// Renamed columns.
"sp_rename 'mytable.comment_float_0', 'comment_double_0', 'COLUMN'",