Completed
Push — master ( a4015c...46069b )
by Marco
18s queued 13s
created

testColumnCollationDeclarationSQL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Platforms;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\ColumnDiff;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Schema\TableDiff;
9
use Doctrine\DBAL\TransactionIsolationLevel;
10
use Doctrine\DBAL\Types\Type;
11
use function sprintf;
12
13
abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCase
14
{
15
    public function getGenerateTableSql()
16
    {
17
        return 'CREATE TABLE test (id INT IDENTITY NOT NULL, test NVARCHAR(255), PRIMARY KEY (id))';
18
    }
19
20
    public function getGenerateTableWithMultiColumnUniqueIndexSql()
21
    {
22
        return array(
23
            'CREATE TABLE test (foo NVARCHAR(255), bar NVARCHAR(255))',
24
            'CREATE UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA ON test (foo, bar) WHERE foo IS NOT NULL AND bar IS NOT NULL'
25
        );
26
    }
27
28
    public function getGenerateAlterTableSql()
29
    {
30
        return array(
31
            'ALTER TABLE mytable ADD quota INT',
32
            'ALTER TABLE mytable DROP COLUMN foo',
33
            'ALTER TABLE mytable ALTER COLUMN baz NVARCHAR(255) NOT NULL',
34
            "ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_78240498 DEFAULT 'def' FOR baz",
35
            'ALTER TABLE mytable ALTER COLUMN bloo BIT NOT NULL',
36
            "ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_CECED971 DEFAULT '0' FOR bloo",
37
            "sp_RENAME 'mytable', 'userlist'",
38
            "DECLARE @sql NVARCHAR(MAX) = N''; " .
39
            "SELECT @sql += N'EXEC sp_rename N''' + dc.name + ''', N''' " .
40
            "+ REPLACE(dc.name, '6B2BD609', 'E2B58069') + ''', ''OBJECT'';' " .
41
            "FROM sys.default_constraints dc " .
42
            "JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id " .
43
            "WHERE tbl.name = 'userlist';" .
44
            "EXEC sp_executesql @sql"
45
        );
46
    }
47
48
    /**
49
     * @expectedException \Doctrine\DBAL\DBALException
50
     */
51
    public function testDoesNotSupportRegexp()
52
    {
53
        $this->_platform->getRegexpExpression();
54
    }
55
56
    public function testGeneratesSqlSnippets()
57
    {
58
        self::assertEquals('CONVERT(date, GETDATE())', $this->_platform->getCurrentDateSQL());
59
        self::assertEquals('CONVERT(time, GETDATE())', $this->_platform->getCurrentTimeSQL());
60
        self::assertEquals('CURRENT_TIMESTAMP', $this->_platform->getCurrentTimestampSQL());
61
        self::assertEquals('"', $this->_platform->getIdentifierQuoteCharacter(), 'Identifier quote character is not correct');
62
        self::assertEquals('(column1 + column2 + column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation expression is not correct');
63
    }
64
65
    public function testGeneratesTransactionsCommands()
66
    {
67
        self::assertEquals(
68
            'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
69
            $this->_platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_UNCOMMITTED)
70
        );
71
        self::assertEquals(
72
            'SET TRANSACTION ISOLATION LEVEL READ COMMITTED',
73
            $this->_platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::READ_COMMITTED)
74
        );
75
        self::assertEquals(
76
            'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ',
77
            $this->_platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::REPEATABLE_READ)
78
        );
79
        self::assertEquals(
80
            'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
81
            $this->_platform->getSetTransactionIsolationSQL(TransactionIsolationLevel::SERIALIZABLE)
82
        );
83
    }
84
85
    public function testGeneratesDDLSnippets()
86
    {
87
        $dropDatabaseExpectation = 'DROP DATABASE foobar';
88
89
        self::assertEquals('SELECT * FROM sys.databases', $this->_platform->getListDatabasesSQL());
90
        self::assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
91
        self::assertEquals($dropDatabaseExpectation, $this->_platform->getDropDatabaseSQL('foobar'));
92
        self::assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
93
    }
94
95
    public function testGeneratesTypeDeclarationForIntegers()
96
    {
97
        self::assertEquals(
98
                'INT',
99
                $this->_platform->getIntegerTypeDeclarationSQL(array())
100
        );
101
        self::assertEquals(
102
                'INT IDENTITY',
103
                $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
104
        ));
105
        self::assertEquals(
106
                'INT IDENTITY',
107
                $this->_platform->getIntegerTypeDeclarationSQL(
108
                        array('autoincrement' => true, 'primary' => true)
109
        ));
110
    }
111
112
    public function testGeneratesTypeDeclarationsForStrings()
113
    {
114
        self::assertEquals(
115
                'NCHAR(10)',
116
                $this->_platform->getVarcharTypeDeclarationSQL(
117
                        array('length' => 10, 'fixed' => true)
118
        ));
119
        self::assertEquals(
120
                'NVARCHAR(50)',
121
                $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
122
                'Variable string declaration is not correct'
123
        );
124
        self::assertEquals(
125
                'NVARCHAR(255)',
126
                $this->_platform->getVarcharTypeDeclarationSQL(array()),
127
                'Long string declaration is not correct'
128
        );
129
        self::assertSame('VARCHAR(MAX)', $this->_platform->getClobTypeDeclarationSQL(array()));
130
        self::assertSame(
131
            'VARCHAR(MAX)',
132
            $this->_platform->getClobTypeDeclarationSQL(array('length' => 5, 'fixed' => true))
133
        );
134
    }
135
136
    public function testPrefersIdentityColumns()
137
    {
138
        self::assertTrue($this->_platform->prefersIdentityColumns());
139
    }
140
141
    public function testSupportsIdentityColumns()
142
    {
143
        self::assertTrue($this->_platform->supportsIdentityColumns());
144
    }
145
146
    public function testSupportsCreateDropDatabase()
147
    {
148
        self::assertTrue($this->_platform->supportsCreateDropDatabase());
149
    }
150
151
    public function testSupportsSchemas()
152
    {
153
        self::assertTrue($this->_platform->supportsSchemas());
154
    }
155
156
    public function testDoesNotSupportSavePoints()
157
    {
158
        self::assertTrue($this->_platform->supportsSavepoints());
159
    }
160
161
    public function getGenerateIndexSql()
162
    {
163
        return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
164
    }
165
166
    public function getGenerateUniqueIndexSql()
167
    {
168
        return 'CREATE UNIQUE INDEX index_name ON test (test, test2) WHERE test IS NOT NULL AND test2 IS NOT NULL';
169
    }
170
171
    public function getGenerateForeignKeySql()
172
    {
173
        return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
174
    }
175
176
    public function testModifyLimitQuery()
177
    {
178
        $querySql = 'SELECT * FROM user';
179
        $alteredSql = 'SELECT TOP 10 * FROM user';
180
        $sql = $this->_platform->modifyLimitQuery($querySql, 10, 0);
181
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
182
    }
183
184
    public function testModifyLimitQueryWithEmptyOffset()
185
    {
186
        $querySql = 'SELECT * FROM user';
187
        $alteredSql = 'SELECT TOP 10 * FROM user';
188
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
189
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
190
    }
191
192
    public function testModifyLimitQueryWithOffset()
193
    {
194
        if ( ! $this->_platform->supportsLimitOffset()) {
195
            $this->markTestSkipped(sprintf('Platform "%s" does not support offsets in result limiting.', $this->_platform->getName()));
196
        }
197
198
        $querySql = 'SELECT * FROM user ORDER BY username DESC';
199
        $alteredSql = 'SELECT TOP 15 * FROM user ORDER BY username DESC';
200
        $sql = $this->_platform->modifyLimitQuery($querySql, 10, 5);
201
202
        $this->expectCteWithMinAndMaxRowNums($alteredSql, 6, 15, $sql);
203
    }
204
205
    public function testModifyLimitQueryWithAscOrderBy()
206
    {
207
        $querySql = 'SELECT * FROM user ORDER BY username ASC';
208
        $alteredSql = 'SELECT TOP 10 * FROM user ORDER BY username ASC';
209
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
210
211
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
212
    }
213
214
    public function testModifyLimitQueryWithLowercaseOrderBy()
215
    {
216
        $querySql = 'SELECT * FROM user order by username';
217
        $alteredSql = 'SELECT TOP 10 * FROM user order by username';
218
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
219
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
220
    }
221
222
    public function testModifyLimitQueryWithDescOrderBy()
223
    {
224
        $querySql = 'SELECT * FROM user ORDER BY username DESC';
225
        $alteredSql = 'SELECT TOP 10 * FROM user ORDER BY username DESC';
226
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
227
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
228
    }
229
230
    public function testModifyLimitQueryWithMultipleOrderBy()
231
    {
232
        $querySql = 'SELECT * FROM user ORDER BY username DESC, usereamil ASC';
233
        $alteredSql = 'SELECT TOP 10 * FROM user ORDER BY username DESC, usereamil ASC';
234
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
235
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
236
    }
237
238
    public function testModifyLimitQueryWithSubSelect()
239
    {
240
        $querySql = 'SELECT * FROM (SELECT u.id as uid, u.name as uname) dctrn_result';
241
        $alteredSql = 'SELECT TOP 10 * FROM (SELECT u.id as uid, u.name as uname) dctrn_result';
242
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
243
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
244
    }
245
246
    public function testModifyLimitQueryWithSubSelectAndOrder()
247
    {
248
        $querySql = 'SELECT * FROM (SELECT u.id as uid, u.name as uname ORDER BY u.name DESC) dctrn_result';
249
        $alteredSql = 'SELECT TOP 10 * FROM (SELECT u.id as uid, u.name as uname) dctrn_result';
250
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
251
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
252
253
        $querySql = 'SELECT * FROM (SELECT u.id, u.name ORDER BY u.name DESC) dctrn_result';
254
        $alteredSql = 'SELECT TOP 10 * FROM (SELECT u.id, u.name) dctrn_result';
255
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
256
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
257
    }
258
259
    public function testModifyLimitQueryWithSubSelectAndMultipleOrder()
260
    {
261
        if ( ! $this->_platform->supportsLimitOffset()) {
262
            $this->markTestSkipped(sprintf('Platform "%s" does not support offsets in result limiting.', $this->_platform->getName()));
263
        }
264
265
        $querySql = 'SELECT * FROM (SELECT u.id as uid, u.name as uname ORDER BY u.name DESC, id ASC) dctrn_result';
266
        $alteredSql = 'SELECT TOP 15 * FROM (SELECT u.id as uid, u.name as uname) dctrn_result';
267
        $sql = $this->_platform->modifyLimitQuery($querySql, 10, 5);
268
        $this->expectCteWithMinAndMaxRowNums($alteredSql, 6, 15, $sql);
269
270
        $querySql = 'SELECT * FROM (SELECT u.id uid, u.name uname ORDER BY u.name DESC, id ASC) dctrn_result';
271
        $alteredSql = 'SELECT TOP 15 * FROM (SELECT u.id uid, u.name uname) dctrn_result';
272
        $sql = $this->_platform->modifyLimitQuery($querySql, 10, 5);
273
        $this->expectCteWithMinAndMaxRowNums($alteredSql, 6, 15, $sql);
274
275
        $querySql = 'SELECT * FROM (SELECT u.id, u.name ORDER BY u.name DESC, id ASC) dctrn_result';
276
        $alteredSql = 'SELECT TOP 15 * FROM (SELECT u.id, u.name) dctrn_result';
277
        $sql = $this->_platform->modifyLimitQuery($querySql, 10, 5);
278
        $this->expectCteWithMinAndMaxRowNums($alteredSql, 6, 15, $sql);
279
    }
280
281
    public function testModifyLimitQueryWithFromColumnNames()
282
    {
283
        $querySql = 'SELECT a.fromFoo, fromBar FROM foo';
284
        $alteredSql = 'SELECT TOP 10 a.fromFoo, fromBar FROM foo';
285
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
286
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
287
    }
288
289
    /**
290
     * @group DBAL-927
291
     */
292
    public function testModifyLimitQueryWithExtraLongQuery()
293
    {
294
        $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 ';
295
        $query.= '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) ';
296
        $query.= '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) ';
297
        $query.= '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)';
298
299
        $alteredSql = 'SELECT TOP 10 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 ';
300
        $alteredSql.= '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) ';
301
        $alteredSql.= '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) ';
302
        $alteredSql.= '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)';
303
304
        $sql = $this->_platform->modifyLimitQuery($query, 10);
305
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
306
    }
307
308
    /**
309
     * @group DDC-2470
310
     */
311
    public function testModifyLimitQueryWithOrderByClause()
312
    {
313
        if ( ! $this->_platform->supportsLimitOffset()) {
314
            $this->markTestSkipped(sprintf('Platform "%s" does not support offsets in result limiting.', $this->_platform->getName()));
315
        }
316
317
        $sql        = 'SELECT m0_.NOMBRE AS NOMBRE0, m0_.FECHAINICIO AS FECHAINICIO1, m0_.FECHAFIN AS FECHAFIN2 FROM MEDICION m0_ WITH (NOLOCK) 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';
318
        $alteredSql = 'SELECT TOP 15 m0_.NOMBRE AS NOMBRE0, m0_.FECHAINICIO AS FECHAINICIO1, m0_.FECHAFIN AS FECHAFIN2 FROM MEDICION m0_ WITH (NOLOCK) 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';
319
        $actual     = $this->_platform->modifyLimitQuery($sql, 10, 5);
320
321
        $this->expectCteWithMinAndMaxRowNums($alteredSql, 6, 15, $actual);
322
    }
323
324
    /**
325
     * @group DBAL-713
326
     */
327
    public function testModifyLimitQueryWithSubSelectInSelectList()
328
    {
329
        $querySql = "SELECT " .
330
            "u.id, " .
331
            "(u.foo/2) foodiv, " .
332
            "CONCAT(u.bar, u.baz) barbaz, " .
333
            "(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) FROM profile p WHERE p.user_id = u.id) login_count " .
334
            "FROM user u " .
335
            "WHERE u.status = 'disabled'";
336
        $alteredSql = "SELECT TOP 10 " .
337
            "u.id, " .
338
            "(u.foo/2) foodiv, " .
339
            "CONCAT(u.bar, u.baz) barbaz, " .
340
            "(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) FROM profile p WHERE p.user_id = u.id) login_count " .
341
            "FROM user u " .
342
            "WHERE u.status = 'disabled'";
343
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
344
345
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
346
    }
347
348
    /**
349
     * @group DBAL-713
350
     */
351
    public function testModifyLimitQueryWithSubSelectInSelectListAndOrderByClause()
352
    {
353
        if ( ! $this->_platform->supportsLimitOffset()) {
354
            $this->markTestSkipped(sprintf('Platform "%s" does not support offsets in result limiting.', $this->_platform->getName()));
355
        }
356
357
        $querySql = "SELECT " .
358
            "u.id, " .
359
            "(u.foo/2) foodiv, " .
360
            "CONCAT(u.bar, u.baz) barbaz, " .
361
            "(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) FROM profile p WHERE p.user_id = u.id) login_count " .
362
            "FROM user u " .
363
            "WHERE u.status = 'disabled' " .
364
            "ORDER BY u.username DESC";
365
        $alteredSql = "SELECT TOP 15 " .
366
            "u.id, " .
367
            "(u.foo/2) foodiv, " .
368
            "CONCAT(u.bar, u.baz) barbaz, " .
369
            "(SELECT (SELECT COUNT(*) FROM login l WHERE l.profile_id = p.id) FROM profile p WHERE p.user_id = u.id) login_count " .
370
            "FROM user u " .
371
            "WHERE u.status = 'disabled' " .
372
            "ORDER BY u.username DESC";
373
        $sql = $this->_platform->modifyLimitQuery($querySql, 10, 5);
374
        $this->expectCteWithMinAndMaxRowNums($alteredSql, 6, 15, $sql);
375
    }
376
377
    /**
378
     * @group DBAL-834
379
     */
380
    public function testModifyLimitQueryWithAggregateFunctionInOrderByClause()
381
    {
382
        $querySql = "SELECT " .
383
            "MAX(heading_id) aliased, " .
384
            "code " .
385
            "FROM operator_model_operator " .
386
            "GROUP BY code " .
387
            "ORDER BY MAX(heading_id) DESC";
388
        $alteredSql = "SELECT TOP 1 " .
389
            "MAX(heading_id) aliased, " .
390
            "code " .
391
            "FROM operator_model_operator " .
392
            "GROUP BY code " .
393
            "ORDER BY MAX(heading_id) DESC";
394
        $sql = $this->_platform->modifyLimitQuery($querySql, 1, 0);
395
        $this->expectCteWithMaxRowNum($alteredSql, 1, $sql);
396
    }
397
398
    /**
399
     * @throws \Doctrine\DBAL\DBALException
400
     */
401
    public function testModifyLimitSubqueryWithJoinAndSubqueryOrderedByColumnFromBaseTable()
402
    {
403
        $querySql = "SELECT DISTINCT id_0, name_1 "
404
            . "FROM ("
405
            . "SELECT t1.id AS id_0, t2.name AS name_1 "
406
            . "FROM table_parent t1 "
407
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id "
408
            . "ORDER BY t1.id ASC"
409
            . ") dctrn_result "
410
            . "ORDER BY id_0 ASC";
411
        $alteredSql = "SELECT DISTINCT TOP 5 id_0, name_1 "
412
            . "FROM ("
413
            . "SELECT t1.id AS id_0, t2.name AS name_1 "
414
            . "FROM table_parent t1 "
415
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id"
416
            . ") dctrn_result "
417
            . "ORDER BY id_0 ASC";
418
        $sql = $this->_platform->modifyLimitQuery($querySql, 5);
419
        $this->expectCteWithMaxRowNum($alteredSql, 5, $sql);
420
    }
421
422
    /**
423
     * @throws \Doctrine\DBAL\DBALException
424
     */
425
    public function testModifyLimitSubqueryWithJoinAndSubqueryOrderedByColumnFromJoinTable()
426
    {
427
        $querySql = "SELECT DISTINCT id_0, name_1 "
428
            . "FROM ("
429
            . "SELECT t1.id AS id_0, t2.name AS name_1 "
430
            . "FROM table_parent t1 "
431
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id "
432
            . "ORDER BY t2.name ASC"
433
            . ") dctrn_result "
434
            . "ORDER BY name_1 ASC";
435
        $alteredSql = "SELECT DISTINCT TOP 5 id_0, name_1 "
436
            . "FROM ("
437
            . "SELECT t1.id AS id_0, t2.name AS name_1 "
438
            . "FROM table_parent t1 "
439
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id"
440
            . ") dctrn_result "
441
            . "ORDER BY name_1 ASC";
442
        $sql = $this->_platform->modifyLimitQuery($querySql, 5);
443
        $this->expectCteWithMaxRowNum($alteredSql, 5, $sql);
444
    }
445
446
    /**
447
     * @throws \Doctrine\DBAL\DBALException
448
     */
449
    public function testModifyLimitSubqueryWithJoinAndSubqueryOrderedByColumnsFromBothTables()
450
    {
451
        $querySql = "SELECT DISTINCT id_0, name_1, foo_2 "
452
            . "FROM ("
453
            . "SELECT t1.id AS id_0, t2.name AS name_1, t2.foo AS foo_2 "
454
            . "FROM table_parent t1 "
455
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id "
456
            . "ORDER BY t2.name ASC, t2.foo DESC"
457
            . ") dctrn_result "
458
            . "ORDER BY name_1 ASC, foo_2 DESC";
459
        $alteredSql = "SELECT DISTINCT TOP 5 id_0, name_1, foo_2 "
460
            . "FROM ("
461
            . "SELECT t1.id AS id_0, t2.name AS name_1, t2.foo AS foo_2 "
462
            . "FROM table_parent t1 "
463
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id"
464
            . ") dctrn_result "
465
            . "ORDER BY name_1 ASC, foo_2 DESC";
466
        $sql = $this->_platform->modifyLimitQuery($querySql, 5);
467
        $this->expectCteWithMaxRowNum($alteredSql, 5, $sql);
468
    }
469
470
    public function testModifyLimitSubquerySimple()
471
    {
472
        $querySql = "SELECT DISTINCT id_0 FROM "
473
            . "(SELECT k0_.id AS id_0, k0_.field AS field_1 "
474
            . "FROM key_table k0_ WHERE (k0_.where_field IN (1))) dctrn_result";
475
        $alteredSql = "SELECT DISTINCT TOP 20 id_0 FROM (SELECT k0_.id AS id_0, k0_.field AS field_1 "
476
            . "FROM key_table k0_ WHERE (k0_.where_field IN (1))) dctrn_result";
477
        $sql = $this->_platform->modifyLimitQuery($querySql, 20);
478
        $this->expectCteWithMaxRowNum($alteredSql, 20, $sql);
479
    }
480
481
    /**
482
     * @group DDC-1360
483
     */
484
    public function testQuoteIdentifier()
485
    {
486
        self::assertEquals('[fo][o]', $this->_platform->quoteIdentifier('fo]o'));
487
        self::assertEquals('[test]', $this->_platform->quoteIdentifier('test'));
488
        self::assertEquals('[test].[test]', $this->_platform->quoteIdentifier('test.test'));
489
    }
490
491
    /**
492
     * @group DDC-1360
493
     */
494
    public function testQuoteSingleIdentifier()
495
    {
496
        self::assertEquals('[fo][o]', $this->_platform->quoteSingleIdentifier('fo]o'));
497
        self::assertEquals('[test]', $this->_platform->quoteSingleIdentifier('test'));
498
        self::assertEquals('[test.test]', $this->_platform->quoteSingleIdentifier('test.test'));
499
    }
500
501
    /**
502
     * @group DBAL-220
503
     */
504
    public function testCreateClusteredIndex()
505
    {
506
        $idx = new \Doctrine\DBAL\Schema\Index('idx', array('id'));
507
        $idx->addFlag('clustered');
508
        self::assertEquals('CREATE CLUSTERED INDEX idx ON tbl (id)', $this->_platform->getCreateIndexSQL($idx, 'tbl'));
509
    }
510
511
    /**
512
     * @group DBAL-220
513
     */
514
    public function testCreateNonClusteredPrimaryKeyInTable()
515
    {
516
        $table = new \Doctrine\DBAL\Schema\Table("tbl");
517
        $table->addColumn("id", "integer");
518
        $table->setPrimaryKey(Array("id"));
519
        $table->getIndex('primary')->addFlag('nonclustered');
520
521
        self::assertEquals(array('CREATE TABLE tbl (id INT NOT NULL, PRIMARY KEY NONCLUSTERED (id))'), $this->_platform->getCreateTableSQL($table));
522
    }
523
524
    /**
525
     * @group DBAL-220
526
     */
527
    public function testCreateNonClusteredPrimaryKey()
528
    {
529
        $idx = new \Doctrine\DBAL\Schema\Index('idx', array('id'), false, true);
530
        $idx->addFlag('nonclustered');
531
        self::assertEquals('ALTER TABLE tbl ADD PRIMARY KEY NONCLUSTERED (id)', $this->_platform->getCreatePrimaryKeySQL($idx, 'tbl'));
532
    }
533
534
    public function testAlterAddPrimaryKey()
535
    {
536
        $idx = new \Doctrine\DBAL\Schema\Index('idx', array('id'), false, true);
537
        self::assertEquals('ALTER TABLE tbl ADD PRIMARY KEY (id)', $this->_platform->getCreateIndexSQL($idx, 'tbl'));
538
    }
539
540
    protected function getQuotedColumnInPrimaryKeySQL()
541
    {
542
        return array(
543
            'CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL, PRIMARY KEY ([create]))',
544
        );
545
    }
546
547
    protected function getQuotedColumnInIndexSQL()
548
    {
549
        return array(
550
            'CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL)',
551
            'CREATE INDEX IDX_22660D028FD6E0FB ON [quoted] ([create])',
552
        );
553
    }
554
555
    protected function getQuotedNameInIndexSQL()
556
    {
557
        return array(
558
            'CREATE TABLE test (column1 NVARCHAR(255) NOT NULL)',
559
            'CREATE INDEX [key] ON test (column1)',
560
        );
561
    }
562
563
    protected function getQuotedColumnInForeignKeySQL()
564
    {
565
        return array(
566
            'CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL, foo NVARCHAR(255) NOT NULL, [bar] NVARCHAR(255) NOT NULL)',
567
            'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY ([create], foo, [bar]) REFERENCES [foreign] ([create], bar, [foo-bar])',
568
            'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD FOREIGN KEY ([create], foo, [bar]) REFERENCES foo ([create], bar, [foo-bar])',
569
            'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY ([create], foo, [bar]) REFERENCES [foo-bar] ([create], bar, [foo-bar])',
570
        );
571
    }
572
573
    public function testGetCreateSchemaSQL()
574
    {
575
        $schemaName = 'schema';
576
        $sql = $this->_platform->getCreateSchemaSQL($schemaName);
577
        self::assertEquals('CREATE SCHEMA ' . $schemaName, $sql);
578
    }
579
580
    public function testCreateTableWithSchemaColumnComments()
581
    {
582
        $table = new Table('testschema.test');
583
        $table->addColumn('id', 'integer', ['comment' => 'This is a comment']);
584
        $table->setPrimaryKey(['id']);
585
586
        $expectedSql = [
587
            'CREATE TABLE testschema.test (id INT NOT NULL, PRIMARY KEY (id))',
588
            "EXEC sp_addextendedproperty N'MS_Description', N'This is a comment', N'SCHEMA', 'testschema', N'TABLE', 'test', N'COLUMN', id",
589
        ];
590
591
        self::assertEquals($expectedSql, $this->_platform->getCreateTableSQL($table));
592
    }
593
594
    public function testAlterTableWithSchemaColumnComments()
595
    {
596
        $tableDiff                        = new TableDiff('testschema.mytable');
597
        $tableDiff->addedColumns['quota'] = new Column('quota', Type::getType('integer'), ['comment' => 'A comment']);
598
599
        $expectedSql = [
600
            'ALTER TABLE testschema.mytable ADD quota INT NOT NULL',
601
            "EXEC sp_addextendedproperty N'MS_Description', N'A comment', N'SCHEMA', 'testschema', N'TABLE', 'mytable', N'COLUMN', quota",
602
        ];
603
604
        self::assertEquals($expectedSql, $this->_platform->getAlterTableSQL($tableDiff));
605
    }
606
607
    public function testAlterTableWithSchemaDropColumnComments()
608
    {
609
        $tableDiff                          = new TableDiff('testschema.mytable');
610
        $tableDiff->changedColumns['quota'] = new ColumnDiff(
611
            'quota',
612
            new Column('quota', Type::getType('integer'), []),
613
            ['comment'],
614
            new Column('quota', Type::getType('integer'), ['comment' => 'A comment'])
615
        );
616
617
        $expectedSql = ["EXEC sp_dropextendedproperty N'MS_Description', N'SCHEMA', 'testschema', N'TABLE', 'mytable', N'COLUMN', quota"];
618
619
        self::assertEquals($expectedSql, $this->_platform->getAlterTableSQL($tableDiff));
620
    }
621
622
    public function testAlterTableWithSchemaUpdateColumnComments()
623
    {
624
        $tableDiff                          = new TableDiff('testschema.mytable');
625
        $tableDiff->changedColumns['quota'] = new ColumnDiff(
626
            'quota',
627
            new Column('quota', Type::getType('integer'), ['comment' => 'B comment']),
628
            ['comment'],
629
            new Column('quota', Type::getType('integer'), ['comment' => 'A comment'])
630
        );
631
632
        $expectedSql = ["EXEC sp_updateextendedproperty N'MS_Description', N'B comment', N'SCHEMA', 'testschema', N'TABLE', 'mytable', N'COLUMN', quota"];
633
634
        self::assertEquals($expectedSql, $this->_platform->getAlterTableSQL($tableDiff));
635
    }
636
637
    /**
638
     * @group DBAL-543
639
     */
640
    public function getCreateTableColumnCommentsSQL()
641
    {
642
        return array(
643
            "CREATE TABLE test (id INT NOT NULL, PRIMARY KEY (id))",
644
            "EXEC sp_addextendedproperty N'MS_Description', N'This is a comment', N'SCHEMA', 'dbo', N'TABLE', 'test', N'COLUMN', id",
645
        );
646
    }
647
648
    /**
649
     * @group DBAL-543
650
     */
651
    public function getAlterTableColumnCommentsSQL()
652
    {
653
        return array(
654
            "ALTER TABLE mytable ADD quota INT NOT NULL",
655
            "EXEC sp_addextendedproperty N'MS_Description', N'A comment', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', quota",
656
            // todo
657
            //"EXEC sp_addextendedproperty N'MS_Description', N'B comment', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', baz",
658
        );
659
    }
660
661
    /**
662
     * @group DBAL-543
663
     */
664
    public function getCreateTableColumnTypeCommentsSQL()
665
    {
666
        return array(
667
            "CREATE TABLE test (id INT NOT NULL, data VARCHAR(MAX) NOT NULL, PRIMARY KEY (id))",
668
            "EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:array)', N'SCHEMA', 'dbo', N'TABLE', 'test', N'COLUMN', data",
669
        );
670
    }
671
672
    /**
673
     * @group DBAL-543
674
     */
675
    public function testGeneratesCreateTableSQLWithColumnComments()
676
    {
677
        $table = new Table('mytable');
678
        $table->addColumn('id', 'integer', array('autoincrement' => true));
679
        $table->addColumn('comment_null', 'integer', array('comment' => null));
680
        $table->addColumn('comment_false', 'integer', array('comment' => false));
681
        $table->addColumn('comment_empty_string', 'integer', array('comment' => ''));
682
        $table->addColumn('comment_integer_0', 'integer', array('comment' => 0));
683
        $table->addColumn('comment_float_0', 'integer', array('comment' => 0.0));
684
        $table->addColumn('comment_string_0', 'integer', array('comment' => '0'));
685
        $table->addColumn('comment', 'integer', array('comment' => 'Doctrine 0wnz you!'));
686
        $table->addColumn('`comment_quoted`', 'integer', array('comment' => 'Doctrine 0wnz comments for explicitly quoted columns!'));
687
        $table->addColumn('create', 'integer', array('comment' => 'Doctrine 0wnz comments for reserved keyword columns!'));
688
        $table->addColumn('commented_type', 'object');
689
        $table->addColumn('commented_type_with_comment', 'array', array('comment' => 'Doctrine array type.'));
690
        $table->addColumn('comment_with_string_literal_char', 'string', array('comment' => "O'Reilly"));
691
        $table->setPrimaryKey(array('id'));
692
693
        self::assertEquals(
694
            array(
695
                "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))",
696
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_integer_0",
697
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_float_0",
698
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_string_0",
699
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine 0wnz you!', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment",
700
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine 0wnz comments for explicitly quoted columns!', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', [comment_quoted]",
701
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine 0wnz comments for reserved keyword columns!', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', [create]",
702
                "EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:object)', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', commented_type",
703
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine array type.(DC2Type:array)', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', commented_type_with_comment",
704
                "EXEC sp_addextendedproperty N'MS_Description', N'O''Reilly', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_with_string_literal_char",
705
            ),
706
            $this->_platform->getCreateTableSQL($table)
707
        );
708
    }
709
710
    /**
711
     * @group DBAL-543
712
     * @group DBAL-1011
713
     */
714
    public function testGeneratesAlterTableSQLWithColumnComments()
715
    {
716
        $table = new Table('mytable');
717
        $table->addColumn('id', 'integer', array('autoincrement' => true));
718
        $table->addColumn('comment_null', 'integer', array('comment' => null));
719
        $table->addColumn('comment_false', 'integer', array('comment' => false));
720
        $table->addColumn('comment_empty_string', 'integer', array('comment' => ''));
721
        $table->addColumn('comment_integer_0', 'integer', array('comment' => 0));
722
        $table->addColumn('comment_float_0', 'integer', array('comment' => 0.0));
723
        $table->addColumn('comment_string_0', 'integer', array('comment' => '0'));
724
        $table->addColumn('comment', 'integer', array('comment' => 'Doctrine 0wnz you!'));
725
        $table->addColumn('`comment_quoted`', 'integer', array('comment' => 'Doctrine 0wnz comments for explicitly quoted columns!'));
726
        $table->addColumn('create', 'integer', array('comment' => 'Doctrine 0wnz comments for reserved keyword columns!'));
727
        $table->addColumn('commented_type', 'object');
728
        $table->addColumn('commented_type_with_comment', 'array', array('comment' => 'Doctrine array type.'));
729
        $table->addColumn('comment_with_string_literal_quote_char', 'array', array('comment' => "O'Reilly"));
730
        $table->setPrimaryKey(array('id'));
731
732
        $tableDiff = new TableDiff('mytable');
733
        $tableDiff->fromTable = $table;
734
        $tableDiff->addedColumns['added_comment_none'] = new Column('added_comment_none', Type::getType('integer'));
735
        $tableDiff->addedColumns['added_comment_null'] = new Column('added_comment_null', Type::getType('integer'), array('comment' => null));
736
        $tableDiff->addedColumns['added_comment_false'] = new Column('added_comment_false', Type::getType('integer'), array('comment' => false));
737
        $tableDiff->addedColumns['added_comment_empty_string'] = new Column('added_comment_empty_string', Type::getType('integer'), array('comment' => ''));
738
        $tableDiff->addedColumns['added_comment_integer_0'] = new Column('added_comment_integer_0', Type::getType('integer'), array('comment' => 0));
739
        $tableDiff->addedColumns['added_comment_float_0'] = new Column('added_comment_float_0', Type::getType('integer'), array('comment' => 0.0));
740
        $tableDiff->addedColumns['added_comment_string_0'] = new Column('added_comment_string_0', Type::getType('integer'), array('comment' => '0'));
741
        $tableDiff->addedColumns['added_comment'] = new Column('added_comment', Type::getType('integer'), array('comment' => 'Doctrine'));
742
        $tableDiff->addedColumns['`added_comment_quoted`'] = new Column('`added_comment_quoted`', Type::getType('integer'), array('comment' => 'rulez'));
743
        $tableDiff->addedColumns['select'] = new Column('select', Type::getType('integer'), array('comment' => '666'));
744
        $tableDiff->addedColumns['added_commented_type'] = new Column('added_commented_type', Type::getType('object'));
745
        $tableDiff->addedColumns['added_commented_type_with_comment'] = new Column('added_commented_type_with_comment', Type::getType('array'), array('comment' => '666'));
746
        $tableDiff->addedColumns['added_comment_with_string_literal_char'] = new Column('added_comment_with_string_literal_char', Type::getType('string'), array('comment' => "''"));
747
748
        $tableDiff->renamedColumns['comment_float_0'] = new Column('comment_double_0', Type::getType('decimal'), array('comment' => 'Double for real!'));
749
750
        // Add comment to non-commented column.
751
        $tableDiff->changedColumns['id'] = new ColumnDiff(
752
            'id',
753
            new Column('id', Type::getType('integer'), array('autoincrement' => true, 'comment' => 'primary')),
754
            array('comment'),
755
            new Column('id', Type::getType('integer'), array('autoincrement' => true))
756
        );
757
758
        // Remove comment from null-commented column.
759
        $tableDiff->changedColumns['comment_null'] = new ColumnDiff(
760
            'comment_null',
761
            new Column('comment_null', Type::getType('string')),
762
            array('type'),
763
            new Column('comment_null', Type::getType('integer'), array('comment' => null))
764
        );
765
766
        // Add comment to false-commented column.
767
        $tableDiff->changedColumns['comment_false'] = new ColumnDiff(
768
            'comment_false',
769
            new Column('comment_false', Type::getType('integer'), array('comment' => 'false')),
770
            array('comment'),
771
            new Column('comment_false', Type::getType('integer'), array('comment' => false))
772
        );
773
774
        // Change type to custom type from empty string commented column.
775
        $tableDiff->changedColumns['comment_empty_string'] = new ColumnDiff(
776
            'comment_empty_string',
777
            new Column('comment_empty_string', Type::getType('object')),
778
            array('type'),
779
            new Column('comment_empty_string', Type::getType('integer'), array('comment' => ''))
780
        );
781
782
        // Change comment to false-comment from zero-string commented column.
783
        $tableDiff->changedColumns['comment_string_0'] = new ColumnDiff(
784
            'comment_string_0',
785
            new Column('comment_string_0', Type::getType('integer'), array('comment' => false)),
786
            array('comment'),
787
            new Column('comment_string_0', Type::getType('integer'), array('comment' => '0'))
788
        );
789
790
        // Remove comment from regular commented column.
791
        $tableDiff->changedColumns['comment'] = new ColumnDiff(
792
            'comment',
793
            new Column('comment', Type::getType('integer')),
794
            array('comment'),
795
            new Column('comment', Type::getType('integer'), array('comment' => 'Doctrine 0wnz you!'))
796
        );
797
798
        // Change comment and change type to custom type from regular commented column.
799
        $tableDiff->changedColumns['`comment_quoted`'] = new ColumnDiff(
800
            '`comment_quoted`',
801
            new Column('`comment_quoted`', Type::getType('array'), array('comment' => 'Doctrine array.')),
802
            array('comment', 'type'),
803
            new Column('`comment_quoted`', Type::getType('integer'), array('comment' => 'Doctrine 0wnz you!'))
804
        );
805
806
        // Remove comment and change type to custom type from regular commented column.
807
        $tableDiff->changedColumns['create'] = new ColumnDiff(
808
            'create',
809
            new Column('create', Type::getType('object')),
810
            array('comment', 'type'),
811
            new Column('create', Type::getType('integer'), array('comment' => 'Doctrine 0wnz comments for reserved keyword columns!'))
812
        );
813
814
        // Add comment and change custom type to regular type from non-commented column.
815
        $tableDiff->changedColumns['commented_type'] = new ColumnDiff(
816
            'commented_type',
817
            new Column('commented_type', Type::getType('integer'), array('comment' => 'foo')),
818
            array('comment', 'type'),
819
            new Column('commented_type', Type::getType('object'))
820
        );
821
822
        // Remove comment from commented custom type column.
823
        $tableDiff->changedColumns['commented_type_with_comment'] = new ColumnDiff(
824
            'commented_type_with_comment',
825
            new Column('commented_type_with_comment', Type::getType('array')),
826
            array('comment'),
827
            new Column('commented_type_with_comment', Type::getType('array'), array('comment' => 'Doctrine array type.'))
828
        );
829
830
        // Change comment from comment with string literal char column.
831
        $tableDiff->changedColumns['comment_with_string_literal_char'] = new ColumnDiff(
832
            'comment_with_string_literal_char',
833
            new Column('comment_with_string_literal_char', Type::getType('string'), array('comment' => "'")),
834
            array('comment'),
835
            new Column('comment_with_string_literal_char', Type::getType('array'), array('comment' => "O'Reilly"))
836
        );
837
838
        $tableDiff->removedColumns['comment_integer_0'] = new Column('comment_integer_0', Type::getType('integer'), array('comment' => 0));
839
840
        self::assertEquals(
841
            array(
842
                // Renamed columns.
843
                "sp_RENAME 'mytable.comment_float_0', 'comment_double_0', 'COLUMN'",
844
845
                // Added columns.
846
                "ALTER TABLE mytable ADD added_comment_none INT NOT NULL",
847
                "ALTER TABLE mytable ADD added_comment_null INT NOT NULL",
848
                "ALTER TABLE mytable ADD added_comment_false INT NOT NULL",
849
                "ALTER TABLE mytable ADD added_comment_empty_string INT NOT NULL",
850
                "ALTER TABLE mytable ADD added_comment_integer_0 INT NOT NULL",
851
                "ALTER TABLE mytable ADD added_comment_float_0 INT NOT NULL",
852
                "ALTER TABLE mytable ADD added_comment_string_0 INT NOT NULL",
853
                "ALTER TABLE mytable ADD added_comment INT NOT NULL",
854
                "ALTER TABLE mytable ADD [added_comment_quoted] INT NOT NULL",
855
                "ALTER TABLE mytable ADD [select] INT NOT NULL",
856
                "ALTER TABLE mytable ADD added_commented_type VARCHAR(MAX) NOT NULL",
857
                "ALTER TABLE mytable ADD added_commented_type_with_comment VARCHAR(MAX) NOT NULL",
858
                "ALTER TABLE mytable ADD added_comment_with_string_literal_char NVARCHAR(255) NOT NULL",
859
                "ALTER TABLE mytable DROP COLUMN comment_integer_0",
860
                "ALTER TABLE mytable ALTER COLUMN comment_null NVARCHAR(255) NOT NULL",
861
                "ALTER TABLE mytable ALTER COLUMN comment_empty_string VARCHAR(MAX) NOT NULL",
862
                "ALTER TABLE mytable ALTER COLUMN [comment_quoted] VARCHAR(MAX) NOT NULL",
863
                "ALTER TABLE mytable ALTER COLUMN [create] VARCHAR(MAX) NOT NULL",
864
                "ALTER TABLE mytable ALTER COLUMN commented_type INT NOT NULL",
865
866
                // Added columns.
867
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', added_comment_integer_0",
868
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', added_comment_float_0",
869
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', added_comment_string_0",
870
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', added_comment",
871
                "EXEC sp_addextendedproperty N'MS_Description', N'rulez', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', [added_comment_quoted]",
872
                "EXEC sp_addextendedproperty N'MS_Description', N'666', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', [select]",
873
                "EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:object)', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', added_commented_type",
874
                "EXEC sp_addextendedproperty N'MS_Description', N'666(DC2Type:array)', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', added_commented_type_with_comment",
875
                "EXEC sp_addextendedproperty N'MS_Description', N'''''', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', added_comment_with_string_literal_char",
876
877
                // Changed columns.
878
                "EXEC sp_addextendedproperty N'MS_Description', N'primary', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', id",
879
                "EXEC sp_addextendedproperty N'MS_Description', N'false', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_false",
880
                "EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:object)', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_empty_string",
881
                "EXEC sp_dropextendedproperty N'MS_Description', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_string_0",
882
                "EXEC sp_dropextendedproperty N'MS_Description', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment",
883
                "EXEC sp_updateextendedproperty N'MS_Description', N'Doctrine array.(DC2Type:array)', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', [comment_quoted]",
884
                "EXEC sp_updateextendedproperty N'MS_Description', N'(DC2Type:object)', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', [create]",
885
                "EXEC sp_updateextendedproperty N'MS_Description', N'foo', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', commented_type",
886
                "EXEC sp_updateextendedproperty N'MS_Description', N'(DC2Type:array)', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', commented_type_with_comment",
887
                "EXEC sp_updateextendedproperty N'MS_Description', N'''', N'SCHEMA', 'dbo', N'TABLE', 'mytable', N'COLUMN', comment_with_string_literal_char",
888
            ),
889
            $this->_platform->getAlterTableSQL($tableDiff)
890
        );
891
    }
892
893
    /**
894
     * @group DBAL-122
895
     */
896
    public function testInitializesDoctrineTypeMappings()
897
    {
898
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('bigint'));
899
        self::assertSame('bigint', $this->_platform->getDoctrineTypeMapping('bigint'));
900
901
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('numeric'));
902
        self::assertSame('decimal', $this->_platform->getDoctrineTypeMapping('numeric'));
903
904
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('bit'));
905
        self::assertSame('boolean', $this->_platform->getDoctrineTypeMapping('bit'));
906
907
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('smallint'));
908
        self::assertSame('smallint', $this->_platform->getDoctrineTypeMapping('smallint'));
909
910
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('decimal'));
911
        self::assertSame('decimal', $this->_platform->getDoctrineTypeMapping('decimal'));
912
913
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('smallmoney'));
914
        self::assertSame('integer', $this->_platform->getDoctrineTypeMapping('smallmoney'));
915
916
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('int'));
917
        self::assertSame('integer', $this->_platform->getDoctrineTypeMapping('int'));
918
919
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('tinyint'));
920
        self::assertSame('smallint', $this->_platform->getDoctrineTypeMapping('tinyint'));
921
922
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('money'));
923
        self::assertSame('integer', $this->_platform->getDoctrineTypeMapping('money'));
924
925
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('float'));
926
        self::assertSame('float', $this->_platform->getDoctrineTypeMapping('float'));
927
928
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('real'));
929
        self::assertSame('float', $this->_platform->getDoctrineTypeMapping('real'));
930
931
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('double'));
932
        self::assertSame('float', $this->_platform->getDoctrineTypeMapping('double'));
933
934
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('double precision'));
935
        self::assertSame('float', $this->_platform->getDoctrineTypeMapping('double precision'));
936
937
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('smalldatetime'));
938
        self::assertSame('datetime', $this->_platform->getDoctrineTypeMapping('smalldatetime'));
939
940
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('datetime'));
941
        self::assertSame('datetime', $this->_platform->getDoctrineTypeMapping('datetime'));
942
943
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('char'));
944
        self::assertSame('string', $this->_platform->getDoctrineTypeMapping('char'));
945
946
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('varchar'));
947
        self::assertSame('string', $this->_platform->getDoctrineTypeMapping('varchar'));
948
949
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('text'));
950
        self::assertSame('text', $this->_platform->getDoctrineTypeMapping('text'));
951
952
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('nchar'));
953
        self::assertSame('string', $this->_platform->getDoctrineTypeMapping('nchar'));
954
955
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('nvarchar'));
956
        self::assertSame('string', $this->_platform->getDoctrineTypeMapping('nvarchar'));
957
958
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('ntext'));
959
        self::assertSame('text', $this->_platform->getDoctrineTypeMapping('ntext'));
960
961
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('binary'));
962
        self::assertSame('binary', $this->_platform->getDoctrineTypeMapping('binary'));
963
964
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('varbinary'));
965
        self::assertSame('binary', $this->_platform->getDoctrineTypeMapping('varbinary'));
966
967
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('image'));
968
        self::assertSame('blob', $this->_platform->getDoctrineTypeMapping('image'));
969
970
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('uniqueidentifier'));
971
        self::assertSame('guid', $this->_platform->getDoctrineTypeMapping('uniqueidentifier'));
972
    }
973
974
    protected function getBinaryMaxLength()
975
    {
976
        return 8000;
977
    }
978
979
    public function testReturnsBinaryTypeDeclarationSQL()
980
    {
981
        self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array()));
982
        self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
983
        self::assertSame('VARBINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 8000)));
984
        self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 8001)));
985
986
        self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
987
        self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
988
        self::assertSame('BINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 8000)));
989
        self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 8001)));
990
    }
991
992
    /**
993
     * @group DBAL-234
994
     */
995
    protected function getAlterTableRenameIndexSQL()
996
    {
997
        return array(
998
            "EXEC sp_RENAME N'mytable.idx_foo', N'idx_bar', N'INDEX'",
999
        );
1000
    }
1001
1002
    /**
1003
     * @group DBAL-234
1004
     */
1005
    protected function getQuotedAlterTableRenameIndexSQL()
1006
    {
1007
        return array(
1008
            "EXEC sp_RENAME N'[table].[create]', N'[select]', N'INDEX'",
1009
            "EXEC sp_RENAME N'[table].[foo]', N'[bar]', N'INDEX'",
1010
        );
1011
    }
1012
1013
    /**
1014
     * @group DBAL-825
1015
     */
1016
    public function testChangeColumnsTypeWithDefaultValue()
1017
    {
1018
        $tableName = 'column_def_change_type';
1019
        $table     = new Table($tableName);
1020
1021
        $table->addColumn('col_int', 'smallint', array('default' => 666));
1022
        $table->addColumn('col_string', 'string', array('default' => 'foo'));
1023
1024
        $tableDiff = new TableDiff($tableName);
1025
        $tableDiff->fromTable = $table;
1026
        $tableDiff->changedColumns['col_int'] = new ColumnDiff(
1027
            'col_int',
1028
            new Column('col_int', Type::getType('integer'), array('default' => 666)),
1029
            array('type'),
1030
            new Column('col_int', Type::getType('smallint'), array('default' => 666))
1031
        );
1032
1033
        $tableDiff->changedColumns['col_string'] = new ColumnDiff(
1034
            'col_string',
1035
            new Column('col_string', Type::getType('string'), array('default' => 666, 'fixed' => true)),
1036
            array('fixed'),
1037
            new Column('col_string', Type::getType('string'), array('default' => 666))
1038
        );
1039
1040
        $expected = $this->_platform->getAlterTableSQL($tableDiff);
1041
1042
        self::assertSame(
1043
            $expected,
1044
            array(
1045
                'ALTER TABLE column_def_change_type DROP CONSTRAINT DF_829302E0_FA2CB292',
1046
                'ALTER TABLE column_def_change_type ALTER COLUMN col_int INT NOT NULL',
1047
                'ALTER TABLE column_def_change_type ADD CONSTRAINT DF_829302E0_FA2CB292 DEFAULT 666 FOR col_int',
1048
                'ALTER TABLE column_def_change_type DROP CONSTRAINT DF_829302E0_2725A6D0',
1049
                'ALTER TABLE column_def_change_type ALTER COLUMN col_string NCHAR(255) NOT NULL',
1050
                "ALTER TABLE column_def_change_type ADD CONSTRAINT DF_829302E0_2725A6D0 DEFAULT '666' FOR col_string",
1051
            )
1052
        );
1053
    }
1054
1055
    /**
1056
     * {@inheritdoc}
1057
     */
1058
    protected function getQuotedAlterTableRenameColumnSQL()
1059
    {
1060
        return array(
1061
            "sp_RENAME 'mytable.unquoted1', 'unquoted', 'COLUMN'",
1062
            "sp_RENAME 'mytable.unquoted2', '[where]', 'COLUMN'",
1063
            "sp_RENAME 'mytable.unquoted3', '[foo]', 'COLUMN'",
1064
            "sp_RENAME 'mytable.[create]', 'reserved_keyword', 'COLUMN'",
1065
            "sp_RENAME 'mytable.[table]', '[from]', 'COLUMN'",
1066
            "sp_RENAME 'mytable.[select]', '[bar]', 'COLUMN'",
1067
            "sp_RENAME 'mytable.quoted1', 'quoted', 'COLUMN'",
1068
            "sp_RENAME 'mytable.quoted2', '[and]', 'COLUMN'",
1069
            "sp_RENAME 'mytable.quoted3', '[baz]', 'COLUMN'",
1070
        );
1071
    }
1072
1073
    /**
1074
     * {@inheritdoc}
1075
     */
1076
    protected function getQuotedAlterTableChangeColumnLengthSQL()
1077
    {
1078
        $this->markTestIncomplete('Not implemented yet');
1079
    }
1080
1081
    /**
1082
     * @group DBAL-807
1083
     */
1084
    protected function getAlterTableRenameIndexInSchemaSQL()
1085
    {
1086
        return array(
1087
            "EXEC sp_RENAME N'myschema.mytable.idx_foo', N'idx_bar', N'INDEX'",
1088
        );
1089
    }
1090
1091
    /**
1092
     * @group DBAL-807
1093
     */
1094
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
1095
    {
1096
        return array(
1097
            "EXEC sp_RENAME N'[schema].[table].[create]', N'[select]', N'INDEX'",
1098
            "EXEC sp_RENAME N'[schema].[table].[foo]', N'[bar]', N'INDEX'",
1099
        );
1100
    }
1101
1102
    protected function getQuotesDropForeignKeySQL()
1103
    {
1104
        return 'ALTER TABLE [table] DROP CONSTRAINT [select]';
1105
    }
1106
1107
    protected function getQuotesDropConstraintSQL()
1108
    {
1109
        return 'ALTER TABLE [table] DROP CONSTRAINT [select]';
1110
    }
1111
1112
    /**
1113
     * @dataProvider getGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL
1114
     * @group DBAL-830
1115
     */
1116
    public function testGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL($table, $column, $expectedSql)
1117
    {
1118
        self::assertSame($expectedSql, $this->_platform->getDefaultConstraintDeclarationSQL($table, $column));
0 ignored issues
show
Bug introduced by
The method getDefaultConstraintDeclarationSQL() does not exist on Doctrine\DBAL\Platforms\AbstractPlatform. It seems like you code against a sub-type of Doctrine\DBAL\Platforms\AbstractPlatform such as Doctrine\DBAL\Platforms\SQLServerPlatform. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

1118
        self::assertSame($expectedSql, $this->_platform->/** @scrutinizer ignore-call */ getDefaultConstraintDeclarationSQL($table, $column));
Loading history...
1119
    }
1120
1121
    public function getGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL()
1122
    {
1123
        return array(
1124
            // Unquoted identifiers non-reserved keywords.
1125
            array('mytable', array('name' => 'mycolumn', 'default' => 'foo'), " CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR mycolumn"),
1126
            // Quoted identifiers non-reserved keywords.
1127
            array('`mytable`', array('name' => '`mycolumn`', 'default' => 'foo'), " CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR [mycolumn]"),
1128
            // Unquoted identifiers reserved keywords.
1129
            array('table', array('name' => 'select', 'default' => 'foo'), " CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"),
1130
            // Quoted identifiers reserved keywords.
1131
            array('`table`', array('name' => '`select`', 'default' => 'foo'), " CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"),
1132
        );
1133
    }
1134
1135
    /**
1136
     * @dataProvider getGeneratesIdentifierNamesInCreateTableSQL
1137
     * @group DBAL-830
1138
     */
1139
    public function testGeneratesIdentifierNamesInCreateTableSQL($table, $expectedSql)
1140
    {
1141
        self::assertSame($expectedSql, $this->_platform->getCreateTableSQL($table));
1142
    }
1143
1144
    public function getGeneratesIdentifierNamesInCreateTableSQL()
1145
    {
1146
        return array(
1147
            // Unquoted identifiers non-reserved keywords.
1148
            array(
1149
                new Table('mytable', array(new Column('mycolumn', Type::getType('string'), array('default' => 'foo')))),
1150
                array(
1151
                    'CREATE TABLE mytable (mycolumn NVARCHAR(255) NOT NULL)',
1152
                    "ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR mycolumn"
1153
                )
1154
            ),
1155
            // Quoted identifiers reserved keywords.
1156
            array(
1157
                new Table('`mytable`', array(new Column('`mycolumn`', Type::getType('string'), array('default' => 'foo')))),
1158
                array(
1159
                    'CREATE TABLE [mytable] ([mycolumn] NVARCHAR(255) NOT NULL)',
1160
                    "ALTER TABLE [mytable] ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR [mycolumn]"
1161
                )
1162
            ),
1163
            // Unquoted identifiers reserved keywords.
1164
            array(
1165
                new Table('table', array(new Column('select', Type::getType('string'), array('default' => 'foo')))),
1166
                array(
1167
                    'CREATE TABLE [table] ([select] NVARCHAR(255) NOT NULL)',
1168
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"
1169
                )
1170
            ),
1171
            // Quoted identifiers reserved keywords.
1172
            array(
1173
                new Table('`table`', array(new Column('`select`', Type::getType('string'), array('default' => 'foo')))),
1174
                array(
1175
                    'CREATE TABLE [table] ([select] NVARCHAR(255) NOT NULL)',
1176
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"
1177
                )
1178
            ),
1179
        );
1180
    }
1181
1182
    /**
1183
     * @dataProvider getGeneratesIdentifierNamesInAlterTableSQL
1184
     * @group DBAL-830
1185
     */
1186
    public function testGeneratesIdentifierNamesInAlterTableSQL($tableDiff, $expectedSql)
1187
    {
1188
        self::assertSame($expectedSql, $this->_platform->getAlterTableSQL($tableDiff));
1189
    }
1190
1191
    public function getGeneratesIdentifierNamesInAlterTableSQL()
1192
    {
1193
        return array(
1194
            // Unquoted identifiers non-reserved keywords.
1195
            array(
1196
                new TableDiff(
1197
                    'mytable',
1198
                    array(new Column('addcolumn', Type::getType('string'), array('default' => 'foo'))),
1199
                    array(
1200
                        'mycolumn' => new ColumnDiff(
1201
                            'mycolumn',
1202
                            new Column('mycolumn', Type::getType('string'), array('default' => 'bar')),
1203
                            array('default'),
1204
                            new Column('mycolumn', Type::getType('string'), array('default' => 'foo'))
1205
                        )
1206
                    ),
1207
                    array(new Column('removecolumn', Type::getType('string'), array('default' => 'foo')))
1208
                ),
1209
                array(
1210
                    'ALTER TABLE mytable ADD addcolumn NVARCHAR(255) NOT NULL',
1211
                    "ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_4AD86123 DEFAULT 'foo' FOR addcolumn",
1212
                    'ALTER TABLE mytable DROP COLUMN removecolumn',
1213
                    'ALTER TABLE mytable DROP CONSTRAINT DF_6B2BD609_9BADD926',
1214
                    'ALTER TABLE mytable ALTER COLUMN mycolumn NVARCHAR(255) NOT NULL',
1215
                    "ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'bar' FOR mycolumn"
1216
                )
1217
            ),
1218
            // Quoted identifiers non-reserved keywords.
1219
            array(
1220
                new TableDiff(
1221
                    '`mytable`',
1222
                    array(new Column('`addcolumn`', Type::getType('string'), array('default' => 'foo'))),
1223
                    array(
1224
                        'mycolumn' => new ColumnDiff(
1225
                            '`mycolumn`',
1226
                            new Column('`mycolumn`', Type::getType('string'), array('default' => 'bar')),
1227
                            array('default'),
1228
                            new Column('`mycolumn`', Type::getType('string'), array('default' => 'foo'))
1229
                        )
1230
                    ),
1231
                    array(new Column('`removecolumn`', Type::getType('string'), array('default' => 'foo')))
1232
                ),
1233
                array(
1234
                    'ALTER TABLE [mytable] ADD [addcolumn] NVARCHAR(255) NOT NULL',
1235
                    "ALTER TABLE [mytable] ADD CONSTRAINT DF_6B2BD609_4AD86123 DEFAULT 'foo' FOR [addcolumn]",
1236
                    'ALTER TABLE [mytable] DROP COLUMN [removecolumn]',
1237
                    'ALTER TABLE [mytable] DROP CONSTRAINT DF_6B2BD609_9BADD926',
1238
                    'ALTER TABLE [mytable] ALTER COLUMN [mycolumn] NVARCHAR(255) NOT NULL',
1239
                    "ALTER TABLE [mytable] ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'bar' FOR [mycolumn]"
1240
                )
1241
            ),
1242
            // Unquoted identifiers reserved keywords.
1243
            array(
1244
                new TableDiff(
1245
                    'table',
1246
                    array(new Column('add', Type::getType('string'), array('default' => 'foo'))),
1247
                    array(
1248
                        'select' => new ColumnDiff(
1249
                            'select',
1250
                            new Column('select', Type::getType('string'), array('default' => 'bar')),
1251
                            array('default'),
1252
                            new Column('select', Type::getType('string'), array('default' => 'foo'))
1253
                        )
1254
                    ),
1255
                    array(new Column('drop', Type::getType('string'), array('default' => 'foo')))
1256
                ),
1257
                array(
1258
                    'ALTER TABLE [table] ADD [add] NVARCHAR(255) NOT NULL',
1259
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_FD1A73E7 DEFAULT 'foo' FOR [add]",
1260
                    'ALTER TABLE [table] DROP COLUMN [drop]',
1261
                    'ALTER TABLE [table] DROP CONSTRAINT DF_F6298F46_4BF2EAC0',
1262
                    'ALTER TABLE [table] ALTER COLUMN [select] NVARCHAR(255) NOT NULL',
1263
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'bar' FOR [select]"
1264
                )
1265
            ),
1266
            // Quoted identifiers reserved keywords.
1267
            array(
1268
                new TableDiff(
1269
                    '`table`',
1270
                    array(new Column('`add`', Type::getType('string'), array('default' => 'foo'))),
1271
                    array(
1272
                        'select' => new ColumnDiff(
1273
                            '`select`',
1274
                            new Column('`select`', Type::getType('string'), array('default' => 'bar')),
1275
                            array('default'),
1276
                            new Column('`select`', Type::getType('string'), array('default' => 'foo'))
1277
                        )
1278
                    ),
1279
                    array(new Column('`drop`', Type::getType('string'), array('default' => 'foo')))
1280
                ),
1281
                array(
1282
                    'ALTER TABLE [table] ADD [add] NVARCHAR(255) NOT NULL',
1283
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_FD1A73E7 DEFAULT 'foo' FOR [add]",
1284
                    'ALTER TABLE [table] DROP COLUMN [drop]',
1285
                    'ALTER TABLE [table] DROP CONSTRAINT DF_F6298F46_4BF2EAC0',
1286
                    'ALTER TABLE [table] ALTER COLUMN [select] NVARCHAR(255) NOT NULL',
1287
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'bar' FOR [select]"
1288
                )
1289
            ),
1290
        );
1291
    }
1292
1293
    /**
1294
     * @group DBAL-423
1295
     */
1296
    public function testReturnsGuidTypeDeclarationSQL()
1297
    {
1298
        self::assertSame('UNIQUEIDENTIFIER', $this->_platform->getGuidTypeDeclarationSQL(array()));
1299
    }
1300
1301
    /**
1302
     * {@inheritdoc}
1303
     */
1304
    public function getAlterTableRenameColumnSQL()
1305
    {
1306
        return array(
1307
            "sp_RENAME 'foo.bar', 'baz', 'COLUMN'",
1308
            'ALTER TABLE foo DROP CONSTRAINT DF_8C736521_76FF8CAA',
1309
            'ALTER TABLE foo ADD CONSTRAINT DF_8C736521_78240498 DEFAULT 666 FOR baz',
1310
        );
1311
    }
1312
1313
    /**
1314
     * {@inheritdoc}
1315
     */
1316
    protected function getQuotesTableIdentifiersInAlterTableSQL()
1317
    {
1318
        return array(
1319
            'ALTER TABLE [foo] DROP CONSTRAINT fk1',
1320
            'ALTER TABLE [foo] DROP CONSTRAINT fk2',
1321
            "sp_RENAME '[foo].id', 'war', 'COLUMN'",
1322
            'ALTER TABLE [foo] ADD bloo INT NOT NULL',
1323
            'ALTER TABLE [foo] DROP COLUMN baz',
1324
            'ALTER TABLE [foo] ALTER COLUMN bar INT',
1325
            "sp_RENAME '[foo]', 'table'",
1326
            "DECLARE @sql NVARCHAR(MAX) = N''; " .
1327
            "SELECT @sql += N'EXEC sp_rename N''' + dc.name + ''', N''' + REPLACE(dc.name, '8C736521', 'F6298F46') + ''', " .
1328
            "''OBJECT'';' FROM sys.default_constraints dc JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id " .
1329
            "WHERE tbl.name = 'table';EXEC sp_executesql @sql",
1330
            'ALTER TABLE [table] ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
1331
            'ALTER TABLE [table] ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
1332
        );
1333
    }
1334
1335
    /**
1336
     * {@inheritdoc}
1337
     */
1338
    protected function getCommentOnColumnSQL()
1339
    {
1340
        return array(
1341
            "COMMENT ON COLUMN foo.bar IS 'comment'",
1342
            "COMMENT ON COLUMN [Foo].[BAR] IS 'comment'",
1343
            "COMMENT ON COLUMN [select].[from] IS 'comment'",
1344
        );
1345
    }
1346
1347
    /**
1348
     * {@inheritdoc}
1349
     */
1350
    public function getReturnsForeignKeyReferentialActionSQL()
1351
    {
1352
        return array(
1353
            array('CASCADE', 'CASCADE'),
1354
            array('SET NULL', 'SET NULL'),
1355
            array('NO ACTION', 'NO ACTION'),
1356
            array('RESTRICT', 'NO ACTION'),
1357
            array('SET DEFAULT', 'SET DEFAULT'),
1358
            array('CaScAdE', 'CASCADE'),
1359
        );
1360
    }
1361
1362
    /**
1363
     * {@inheritdoc}
1364
     */
1365
    protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
1366
    {
1367
        return 'CONSTRAINT [select] UNIQUE (foo) WHERE foo IS NOT NULL';
1368
    }
1369
1370
    /**
1371
     * {@inheritdoc}
1372
     */
1373
    protected function getQuotesReservedKeywordInIndexDeclarationSQL()
1374
    {
1375
        return 'INDEX [select] (foo)';
1376
    }
1377
1378
    /**
1379
     * {@inheritdoc}
1380
     */
1381
    protected function getQuotesReservedKeywordInTruncateTableSQL()
1382
    {
1383
        return 'TRUNCATE TABLE [select]';
1384
    }
1385
1386
    /**
1387
     * {@inheritdoc}
1388
     */
1389
    protected function getAlterStringToFixedStringSQL()
1390
    {
1391
        return array(
1392
            'ALTER TABLE mytable ALTER COLUMN name NCHAR(2) NOT NULL',
1393
        );
1394
    }
1395
1396
    /**
1397
     * {@inheritdoc}
1398
     */
1399
    protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
1400
    {
1401
        return array(
1402
            "EXEC sp_RENAME N'mytable.idx_foo', N'idx_foo_renamed', N'INDEX'",
1403
        );
1404
    }
1405
1406
    public function testModifyLimitQueryWithTopNSubQueryWithOrderBy()
1407
    {
1408
        $querySql = 'SELECT * FROM test t WHERE t.id = (SELECT TOP 1 t2.id FROM test t2 ORDER BY t2.data DESC)';
1409
        $alteredSql = 'SELECT TOP 10 * FROM test t WHERE t.id = (SELECT TOP 1 t2.id FROM test t2 ORDER BY t2.data DESC)';
1410
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
1411
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
1412
1413
        $querySql = 'SELECT * FROM test t WHERE t.id = (SELECT TOP 1 t2.id FROM test t2 ORDER BY t2.data DESC) ORDER BY t.data2 DESC';
1414
        $alteredSql = 'SELECT TOP 10 * FROM test t WHERE t.id = (SELECT TOP 1 t2.id FROM test t2 ORDER BY t2.data DESC) ORDER BY t.data2 DESC';
1415
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
1416
        $this->expectCteWithMaxRowNum($alteredSql, 10, $sql);
1417
    }
1418
1419
    /**
1420
     * @group DBAL-2436
1421
     */
1422
    public function testQuotesTableNameInListTableColumnsSQL()
1423
    {
1424
        self::assertContains("'Foo''Bar\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
1425
    }
1426
1427
    /**
1428
     * @group DBAL-2436
1429
     */
1430
    public function testQuotesSchemaNameInListTableColumnsSQL()
1431
    {
1432
        self::assertContains(
1433
            "'Foo''Bar\\'",
1434
            $this->_platform->getListTableColumnsSQL("Foo'Bar\\.baz_table"),
1435
            '',
1436
            true
1437
        );
1438
    }
1439
1440
    /**
1441
     * @group DBAL-2436
1442
     */
1443
    public function testQuotesTableNameInListTableForeignKeysSQL()
1444
    {
1445
        self::assertContains("'Foo''Bar\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
1446
    }
1447
1448
    /**
1449
     * @group DBAL-2436
1450
     */
1451
    public function testQuotesSchemaNameInListTableForeignKeysSQL()
1452
    {
1453
        self::assertContains(
1454
            "'Foo''Bar\\'",
1455
            $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\.baz_table"),
1456
            '',
1457
            true
1458
        );
1459
    }
1460
1461
    /**
1462
     * @group DBAL-2436
1463
     */
1464
    public function testQuotesTableNameInListTableIndexesSQL()
1465
    {
1466
        self::assertContains("'Foo''Bar\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
1467
    }
1468
1469
    /**
1470
     * @group DBAL-2436
1471
     */
1472
    public function testQuotesSchemaNameInListTableIndexesSQL()
1473
    {
1474
        self::assertContains(
1475
            "'Foo''Bar\\'",
1476
            $this->_platform->getListTableIndexesSQL("Foo'Bar\\.baz_table"),
1477
            '',
1478
            true
1479
        );
1480
    }
1481
1482
    /**
1483
     * @group 2859
1484
     */
1485
    public function testGetDefaultValueDeclarationSQLForDateType() : void
1486
    {
1487
        $currentDateSql = $this->_platform->getCurrentDateSQL();
1488
        foreach (['date', 'date_immutable'] as $type) {
1489
            $field = [
1490
                'type' => Type::getType($type),
1491
                'default' => $currentDateSql,
1492
            ];
1493
1494
            self::assertSame(
1495
                " DEFAULT '" . $currentDateSql . "'",
1496
                $this->_platform->getDefaultValueDeclarationSQL($field)
1497
            );
1498
        }
1499
    }
1500
1501
    public function testSupportsColumnCollation() : void
1502
    {
1503
        self::assertTrue($this->_platform->supportsColumnCollation());
1504
    }
1505
1506
    public function testColumnCollationDeclarationSQL() : void
1507
    {
1508
        self::assertSame(
1509
            'COLLATE Latin1_General_CS_AS_KS_WS',
1510
            $this->_platform->getColumnCollationDeclarationSQL('Latin1_General_CS_AS_KS_WS')
1511
        );
1512
    }
1513
1514
    public function testGetCreateTableSQLWithColumnCollation() : void
1515
    {
1516
        $table = new Table('foo');
1517
        $table->addColumn('no_collation', 'string');
1518
        $table->addColumn('column_collation', 'string')->setPlatformOption('collation', 'Latin1_General_CS_AS_KS_WS');
1519
1520
        self::assertSame(
1521
            ['CREATE TABLE foo (no_collation NVARCHAR(255) NOT NULL, column_collation NVARCHAR(255) COLLATE Latin1_General_CS_AS_KS_WS NOT NULL)'],
1522
            $this->_platform->getCreateTableSQL($table),
1523
            'Column "no_collation" will use the default collation from the table/database and "column_collation" overwrites the collation on this column'
1524
        );
1525
    }
1526
1527
    private function expectCteWithMaxRowNum(string $expectedSql, int $expectedMax, string $sql) : void
1528
    {
1529
        $pattern = 'WITH dctrn_cte AS (%s) SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM dctrn_cte) AS doctrine_tbl WHERE doctrine_rownum <= %d ORDER BY doctrine_rownum ASC';
1530
        self::assertEquals(sprintf($pattern, $expectedSql, $expectedMax), $sql);
1531
    }
1532
1533
    private function expectCteWithMinAndMaxRowNums(string $expectedSql, int $expectedMin, int $expectedMax, string $sql) : void
1534
    {
1535
        $pattern = 'WITH dctrn_cte AS (%s) SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM dctrn_cte) AS doctrine_tbl WHERE doctrine_rownum >= %d AND doctrine_rownum <= %d ORDER BY doctrine_rownum ASC';
1536
        self::assertEquals(sprintf($pattern, $expectedSql, $expectedMin, $expectedMax), $sql);
1537
    }
1538
}
1539