Passed
Pull Request — master (#2945)
by Dorian
13:10
created

getAlterTableRenameForeignKeySQL()   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\Types\Type;
10
11
abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCase
12
{
13
    protected static $selectFromCtePattern = "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 BETWEEN %d AND %d ORDER BY doctrine_rownum ASC";
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 View Code Duplication
    public function testGeneratesTransactionsCommands()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        self::assertEquals(
68
                'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
69
                $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED)
70
        );
71
        self::assertEquals(
72
                'SET TRANSACTION ISOLATION LEVEL READ COMMITTED',
73
                $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
74
        );
75
        self::assertEquals(
76
                'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ',
77
                $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
78
        );
79
        self::assertEquals(
80
                'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE',
81
                $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
82
        );
83
    }
84
85 View Code Duplication
    public function testGeneratesDDLSnippets()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testGeneratesTypeDeclarationForIntegers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testModifyLimitQuery()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        $querySql = 'SELECT * FROM user';
179
        $alteredSql = 'SELECT TOP 10 * FROM user';
180
        $sql = $this->_platform->modifyLimitQuery($querySql, 10, 0);
181
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
182
    }
183
184 View Code Duplication
    public function testModifyLimitQueryWithEmptyOffset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186
        $querySql = 'SELECT * FROM user';
187
        $alteredSql = 'SELECT TOP 10 * FROM user';
188
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
189
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
190
    }
191
192 View Code Duplication
    public function testModifyLimitQueryWithOffset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 6, 15), $sql);
203
    }
204
205 View Code Duplication
    public function testModifyLimitQueryWithAscOrderBy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
212
    }
213
214 View Code Duplication
    public function testModifyLimitQueryWithLowercaseOrderBy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
220
    }
221
222 View Code Duplication
    public function testModifyLimitQueryWithDescOrderBy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
228
    }
229
230 View Code Duplication
    public function testModifyLimitQueryWithMultipleOrderBy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
236
    }
237
238 View Code Duplication
    public function testModifyLimitQueryWithSubSelect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
244
    }
245
246 View Code Duplication
    public function testModifyLimitQueryWithSubSelectAndOrder()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 6, 15), $sql);
279
    }
280
281 View Code Duplication
    public function testModifyLimitQueryWithFromColumnNames()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
287
    }
288
289
    /**
290
     * @group DBAL-927
291
     */
292 View Code Duplication
    public function testModifyLimitQueryWithExtraLongQuery()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
306
    }
307
308
    /**
309
     * @group DDC-2470
310
     */
311 View Code Duplication
    public function testModifyLimitQueryWithOrderByClause()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 6, 15), $actual);
322
    }
323
324
    /**
325
     * @group DBAL-713
326
     */
327 View Code Duplication
    public function testModifyLimitQueryWithSubSelectInSelectList()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 6, 15), $sql);
375
    }
376
377
    /**
378
     * @group DBAL-834
379
     */
380 View Code Duplication
    public function testModifyLimitQueryWithAggregateFunctionInOrderByClause()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 1), $sql);
396
    }
397
398
    /**
399
     * @throws \Doctrine\DBAL\DBALException
400
     */
401 View Code Duplication
    public function testModifyLimitSubqueryWithJoinAndSubqueryOrderedByColumnFromBaseTable()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 5), $sql);
420
    }
421
422
423
    /**
424
     * @throws \Doctrine\DBAL\DBALException
425
     */
426 View Code Duplication
    public function testModifyLimitSubqueryWithJoinAndSubqueryOrderedByColumnFromJoinTable()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
427
    {
428
        $querySql = "SELECT DISTINCT id_0, name_1 "
429
            . "FROM ("
430
            . "SELECT t1.id AS id_0, t2.name AS name_1 "
431
            . "FROM table_parent t1 "
432
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id "
433
            . "ORDER BY t2.name ASC"
434
            . ") dctrn_result "
435
            . "ORDER BY name_1 ASC";
436
        $alteredSql = "SELECT DISTINCT TOP 5 id_0, name_1 "
437
            . "FROM ("
438
            . "SELECT t1.id AS id_0, t2.name AS name_1 "
439
            . "FROM table_parent t1 "
440
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id"
441
            . ") dctrn_result "
442
            . "ORDER BY name_1 ASC";
443
        $sql = $this->_platform->modifyLimitQuery($querySql, 5);
444
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 5), $sql);
445
    }
446
447
    /**
448
     * @throws \Doctrine\DBAL\DBALException
449
     */
450 View Code Duplication
    public function testModifyLimitSubqueryWithJoinAndSubqueryOrderedByColumnsFromBothTables()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
451
    {
452
        $querySql = "SELECT DISTINCT id_0, name_1, foo_2 "
453
            . "FROM ("
454
            . "SELECT t1.id AS id_0, t2.name AS name_1, t2.foo AS foo_2 "
455
            . "FROM table_parent t1 "
456
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id "
457
            . "ORDER BY t2.name ASC, t2.foo DESC"
458
            . ") dctrn_result "
459
            . "ORDER BY name_1 ASC, foo_2 DESC";
460
        $alteredSql = "SELECT DISTINCT TOP 5 id_0, name_1, foo_2 "
461
            . "FROM ("
462
            . "SELECT t1.id AS id_0, t2.name AS name_1, t2.foo AS foo_2 "
463
            . "FROM table_parent t1 "
464
            . "LEFT JOIN join_table t2 ON t1.id = t2.table_id"
465
            . ") dctrn_result "
466
            . "ORDER BY name_1 ASC, foo_2 DESC";
467
        $sql = $this->_platform->modifyLimitQuery($querySql, 5);
468
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 5), $sql);
469
    }
470
471
    public function testModifyLimitSubquerySimple()
472
    {
473
        $querySql = "SELECT DISTINCT id_0 FROM "
474
            . "(SELECT k0_.id AS id_0, k0_.field AS field_1 "
475
            . "FROM key_table k0_ WHERE (k0_.where_field IN (1))) dctrn_result";
476
        $alteredSql = "SELECT DISTINCT TOP 20 id_0 FROM (SELECT k0_.id AS id_0, k0_.field AS field_1 "
477
            . "FROM key_table k0_ WHERE (k0_.where_field IN (1))) dctrn_result";
478
        $sql = $this->_platform->modifyLimitQuery($querySql, 20);
479
        self::assertEquals(sprintf(self::$selectFromCtePattern, $alteredSql, 1, 20), $sql);
480
    }
481
482
    /**
483
     * @group DDC-1360
484
     */
485
    public function testQuoteIdentifier()
486
    {
487
        self::assertEquals('[fo][o]', $this->_platform->quoteIdentifier('fo]o'));
488
        self::assertEquals('[test]', $this->_platform->quoteIdentifier('test'));
489
        self::assertEquals('[test].[test]', $this->_platform->quoteIdentifier('test.test'));
490
    }
491
492
    /**
493
     * @group DDC-1360
494
     */
495
    public function testQuoteSingleIdentifier()
496
    {
497
        self::assertEquals('[fo][o]', $this->_platform->quoteSingleIdentifier('fo]o'));
498
        self::assertEquals('[test]', $this->_platform->quoteSingleIdentifier('test'));
499
        self::assertEquals('[test.test]', $this->_platform->quoteSingleIdentifier('test.test'));
500
    }
501
502
    /**
503
     * @group DBAL-220
504
     */
505
    public function testCreateClusteredIndex()
506
    {
507
        $idx = new \Doctrine\DBAL\Schema\Index('idx', array('id'));
508
        $idx->addFlag('clustered');
509
        self::assertEquals('CREATE CLUSTERED INDEX idx ON tbl (id)', $this->_platform->getCreateIndexSQL($idx, 'tbl'));
510
    }
511
512
    /**
513
     * @group DBAL-220
514
     */
515
    public function testCreateNonClusteredPrimaryKeyInTable()
516
    {
517
        $table = new \Doctrine\DBAL\Schema\Table("tbl");
518
        $table->addColumn("id", "integer");
519
        $table->setPrimaryKey(Array("id"));
520
        $table->getIndex('primary')->addFlag('nonclustered');
521
522
        self::assertEquals(array('CREATE TABLE tbl (id INT NOT NULL, PRIMARY KEY NONCLUSTERED (id))'), $this->_platform->getCreateTableSQL($table));
523
    }
524
525
    /**
526
     * @group DBAL-220
527
     */
528
    public function testCreateNonClusteredPrimaryKey()
529
    {
530
        $idx = new \Doctrine\DBAL\Schema\Index('idx', array('id'), false, true);
531
        $idx->addFlag('nonclustered');
532
        self::assertEquals('ALTER TABLE tbl ADD PRIMARY KEY NONCLUSTERED (id)', $this->_platform->getCreatePrimaryKeySQL($idx, 'tbl'));
533
    }
534
535
    public function testAlterAddPrimaryKey()
536
    {
537
        $idx = new \Doctrine\DBAL\Schema\Index('idx', array('id'), false, true);
538
        self::assertEquals('ALTER TABLE tbl ADD PRIMARY KEY (id)', $this->_platform->getCreateIndexSQL($idx, 'tbl'));
539
    }
540
541
    protected function getQuotedColumnInPrimaryKeySQL()
542
    {
543
        return array(
544
            'CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL, PRIMARY KEY ([create]))',
545
        );
546
    }
547
548
    protected function getQuotedColumnInIndexSQL()
549
    {
550
        return array(
551
            'CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL)',
552
            'CREATE INDEX IDX_22660D028FD6E0FB ON [quoted] ([create])',
553
        );
554
    }
555
556
    protected function getQuotedNameInIndexSQL()
557
    {
558
        return array(
559
            'CREATE TABLE test (column1 NVARCHAR(255) NOT NULL)',
560
            'CREATE INDEX [key] ON test (column1)',
561
        );
562
    }
563
564
    protected function getQuotedColumnInForeignKeySQL()
565
    {
566
        return array(
567
            'CREATE TABLE [quoted] ([create] NVARCHAR(255) NOT NULL, foo NVARCHAR(255) NOT NULL, [bar] NVARCHAR(255) NOT NULL)',
568
            'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_RESERVED_KEYWORD FOREIGN KEY ([create], foo, [bar]) REFERENCES [foreign] ([create], bar, [foo-bar])',
569
            'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_NON_RESERVED_KEYWORD FOREIGN KEY ([create], foo, [bar]) REFERENCES foo ([create], bar, [foo-bar])',
570
            'ALTER TABLE [quoted] ADD CONSTRAINT FK_WITH_INTENDED_QUOTATION FOREIGN KEY ([create], foo, [bar]) REFERENCES [foo-bar] ([create], bar, [foo-bar])',
571
        );
572
    }
573
574
    public function testGetCreateSchemaSQL()
575
    {
576
        $schemaName = 'schema';
577
        $sql = $this->_platform->getCreateSchemaSQL($schemaName);
578
        self::assertEquals('CREATE SCHEMA ' . $schemaName, $sql);
579
    }
580
581
    /**
582
     * @group DBAL-543
583
     */
584
    public function getCreateTableColumnCommentsSQL()
585
    {
586
        return array(
587
            "CREATE TABLE test (id INT NOT NULL, PRIMARY KEY (id))",
588
            "EXEC sp_addextendedproperty N'MS_Description', N'This is a comment', N'SCHEMA', dbo, N'TABLE', test, N'COLUMN', id",
589
        );
590
    }
591
592
    /**
593
     * @group DBAL-543
594
     */
595
    public function getAlterTableColumnCommentsSQL()
596
    {
597
        return array(
598
            "ALTER TABLE mytable ADD quota INT NOT NULL",
599
            "EXEC sp_addextendedproperty N'MS_Description', N'A comment', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', quota",
600
            // todo
601
            //"EXEC sp_addextendedproperty N'MS_Description', N'B comment', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', baz",
602
        );
603
    }
604
605
    /**
606
     * @group DBAL-543
607
     */
608
    public function getCreateTableColumnTypeCommentsSQL()
609
    {
610
        return array(
611
            "CREATE TABLE test (id INT NOT NULL, data VARCHAR(MAX) NOT NULL, PRIMARY KEY (id))",
612
            "EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:array)', N'SCHEMA', dbo, N'TABLE', test, N'COLUMN', data",
613
        );
614
    }
615
616
    /**
617
     * @group DBAL-543
618
     */
619
    public function testGeneratesCreateTableSQLWithColumnComments()
620
    {
621
        $table = new Table('mytable');
622
        $table->addColumn('id', 'integer', array('autoincrement' => true));
623
        $table->addColumn('comment_null', 'integer', array('comment' => null));
624
        $table->addColumn('comment_false', 'integer', array('comment' => false));
625
        $table->addColumn('comment_empty_string', 'integer', array('comment' => ''));
626
        $table->addColumn('comment_integer_0', 'integer', array('comment' => 0));
627
        $table->addColumn('comment_float_0', 'integer', array('comment' => 0.0));
628
        $table->addColumn('comment_string_0', 'integer', array('comment' => '0'));
629
        $table->addColumn('comment', 'integer', array('comment' => 'Doctrine 0wnz you!'));
630
        $table->addColumn('`comment_quoted`', 'integer', array('comment' => 'Doctrine 0wnz comments for explicitly quoted columns!'));
631
        $table->addColumn('create', 'integer', array('comment' => 'Doctrine 0wnz comments for reserved keyword columns!'));
632
        $table->addColumn('commented_type', 'object');
633
        $table->addColumn('commented_type_with_comment', 'array', array('comment' => 'Doctrine array type.'));
634
        $table->addColumn('comment_with_string_literal_char', 'string', array('comment' => "O'Reilly"));
635
        $table->setPrimaryKey(array('id'));
636
637
        self::assertEquals(
638
            array(
639
                "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))",
640
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment_integer_0",
641
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment_float_0",
642
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment_string_0",
643
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine 0wnz you!', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment",
644
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine 0wnz comments for explicitly quoted columns!', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', [comment_quoted]",
645
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine 0wnz comments for reserved keyword columns!', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', [create]",
646
                "EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:object)', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', commented_type",
647
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine array type.(DC2Type:array)', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', commented_type_with_comment",
648
                "EXEC sp_addextendedproperty N'MS_Description', N'O''Reilly', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment_with_string_literal_char",
649
            ),
650
            $this->_platform->getCreateTableSQL($table)
651
        );
652
    }
653
654
    /**
655
     * @group DBAL-543
656
     * @group DBAL-1011
657
     */
658
    public function testGeneratesAlterTableSQLWithColumnComments()
659
    {
660
        $table = new Table('mytable');
661
        $table->addColumn('id', 'integer', array('autoincrement' => true));
662
        $table->addColumn('comment_null', 'integer', array('comment' => null));
663
        $table->addColumn('comment_false', 'integer', array('comment' => false));
664
        $table->addColumn('comment_empty_string', 'integer', array('comment' => ''));
665
        $table->addColumn('comment_integer_0', 'integer', array('comment' => 0));
666
        $table->addColumn('comment_float_0', 'integer', array('comment' => 0.0));
667
        $table->addColumn('comment_string_0', 'integer', array('comment' => '0'));
668
        $table->addColumn('comment', 'integer', array('comment' => 'Doctrine 0wnz you!'));
669
        $table->addColumn('`comment_quoted`', 'integer', array('comment' => 'Doctrine 0wnz comments for explicitly quoted columns!'));
670
        $table->addColumn('create', 'integer', array('comment' => 'Doctrine 0wnz comments for reserved keyword columns!'));
671
        $table->addColumn('commented_type', 'object');
672
        $table->addColumn('commented_type_with_comment', 'array', array('comment' => 'Doctrine array type.'));
673
        $table->addColumn('comment_with_string_literal_quote_char', 'array', array('comment' => "O'Reilly"));
674
        $table->setPrimaryKey(array('id'));
675
676
        $tableDiff = new TableDiff('mytable');
677
        $tableDiff->fromTable = $table;
678
        $tableDiff->addedColumns['added_comment_none'] = new Column('added_comment_none', Type::getType('integer'));
679
        $tableDiff->addedColumns['added_comment_null'] = new Column('added_comment_null', Type::getType('integer'), array('comment' => null));
680
        $tableDiff->addedColumns['added_comment_false'] = new Column('added_comment_false', Type::getType('integer'), array('comment' => false));
681
        $tableDiff->addedColumns['added_comment_empty_string'] = new Column('added_comment_empty_string', Type::getType('integer'), array('comment' => ''));
682
        $tableDiff->addedColumns['added_comment_integer_0'] = new Column('added_comment_integer_0', Type::getType('integer'), array('comment' => 0));
683
        $tableDiff->addedColumns['added_comment_float_0'] = new Column('added_comment_float_0', Type::getType('integer'), array('comment' => 0.0));
684
        $tableDiff->addedColumns['added_comment_string_0'] = new Column('added_comment_string_0', Type::getType('integer'), array('comment' => '0'));
685
        $tableDiff->addedColumns['added_comment'] = new Column('added_comment', Type::getType('integer'), array('comment' => 'Doctrine'));
686
        $tableDiff->addedColumns['`added_comment_quoted`'] = new Column('`added_comment_quoted`', Type::getType('integer'), array('comment' => 'rulez'));
687
        $tableDiff->addedColumns['select'] = new Column('select', Type::getType('integer'), array('comment' => '666'));
688
        $tableDiff->addedColumns['added_commented_type'] = new Column('added_commented_type', Type::getType('object'));
689
        $tableDiff->addedColumns['added_commented_type_with_comment'] = new Column('added_commented_type_with_comment', Type::getType('array'), array('comment' => '666'));
690
        $tableDiff->addedColumns['added_comment_with_string_literal_char'] = new Column('added_comment_with_string_literal_char', Type::getType('string'), array('comment' => "''"));
691
692
        $tableDiff->renamedColumns['comment_float_0'] = new Column('comment_double_0', Type::getType('decimal'), array('comment' => 'Double for real!'));
693
694
        // Add comment to non-commented column.
695
        $tableDiff->changedColumns['id'] = new ColumnDiff(
696
            'id',
697
            new Column('id', Type::getType('integer'), array('autoincrement' => true, 'comment' => 'primary')),
698
            array('comment'),
699
            new Column('id', Type::getType('integer'), array('autoincrement' => true))
700
        );
701
702
        // Remove comment from null-commented column.
703
        $tableDiff->changedColumns['comment_null'] = new ColumnDiff(
704
            'comment_null',
705
            new Column('comment_null', Type::getType('string')),
706
            array('type'),
707
            new Column('comment_null', Type::getType('integer'), array('comment' => null))
708
        );
709
710
        // Add comment to false-commented column.
711
        $tableDiff->changedColumns['comment_false'] = new ColumnDiff(
712
            'comment_false',
713
            new Column('comment_false', Type::getType('integer'), array('comment' => 'false')),
714
            array('comment'),
715
            new Column('comment_false', Type::getType('integer'), array('comment' => false))
716
        );
717
718
        // Change type to custom type from empty string commented column.
719
        $tableDiff->changedColumns['comment_empty_string'] = new ColumnDiff(
720
            'comment_empty_string',
721
            new Column('comment_empty_string', Type::getType('object')),
722
            array('type'),
723
            new Column('comment_empty_string', Type::getType('integer'), array('comment' => ''))
724
        );
725
726
        // Change comment to false-comment from zero-string commented column.
727
        $tableDiff->changedColumns['comment_string_0'] = new ColumnDiff(
728
            'comment_string_0',
729
            new Column('comment_string_0', Type::getType('integer'), array('comment' => false)),
730
            array('comment'),
731
            new Column('comment_string_0', Type::getType('integer'), array('comment' => '0'))
732
        );
733
734
        // Remove comment from regular commented column.
735
        $tableDiff->changedColumns['comment'] = new ColumnDiff(
736
            'comment',
737
            new Column('comment', Type::getType('integer')),
738
            array('comment'),
739
            new Column('comment', Type::getType('integer'), array('comment' => 'Doctrine 0wnz you!'))
740
        );
741
742
        // Change comment and change type to custom type from regular commented column.
743
        $tableDiff->changedColumns['`comment_quoted`'] = new ColumnDiff(
744
            '`comment_quoted`',
745
            new Column('`comment_quoted`', Type::getType('array'), array('comment' => 'Doctrine array.')),
746
            array('comment', 'type'),
747
            new Column('`comment_quoted`', Type::getType('integer'), array('comment' => 'Doctrine 0wnz you!'))
748
        );
749
750
        // Remove comment and change type to custom type from regular commented column.
751
        $tableDiff->changedColumns['create'] = new ColumnDiff(
752
            'create',
753
            new Column('create', Type::getType('object')),
754
            array('comment', 'type'),
755
            new Column('create', Type::getType('integer'), array('comment' => 'Doctrine 0wnz comments for reserved keyword columns!'))
756
        );
757
758
        // Add comment and change custom type to regular type from non-commented column.
759
        $tableDiff->changedColumns['commented_type'] = new ColumnDiff(
760
            'commented_type',
761
            new Column('commented_type', Type::getType('integer'), array('comment' => 'foo')),
762
            array('comment', 'type'),
763
            new Column('commented_type', Type::getType('object'))
764
        );
765
766
        // Remove comment from commented custom type column.
767
        $tableDiff->changedColumns['commented_type_with_comment'] = new ColumnDiff(
768
            'commented_type_with_comment',
769
            new Column('commented_type_with_comment', Type::getType('array')),
770
            array('comment'),
771
            new Column('commented_type_with_comment', Type::getType('array'), array('comment' => 'Doctrine array type.'))
772
        );
773
774
        // Change comment from comment with string literal char column.
775
        $tableDiff->changedColumns['comment_with_string_literal_char'] = new ColumnDiff(
776
            'comment_with_string_literal_char',
777
            new Column('comment_with_string_literal_char', Type::getType('string'), array('comment' => "'")),
778
            array('comment'),
779
            new Column('comment_with_string_literal_char', Type::getType('array'), array('comment' => "O'Reilly"))
780
        );
781
782
        $tableDiff->removedColumns['comment_integer_0'] = new Column('comment_integer_0', Type::getType('integer'), array('comment' => 0));
783
784
        self::assertEquals(
785
            array(
786
                // Renamed columns.
787
                "sp_RENAME 'mytable.comment_float_0', 'comment_double_0', 'COLUMN'",
788
789
                // Added columns.
790
                "ALTER TABLE mytable ADD added_comment_none INT NOT NULL",
791
                "ALTER TABLE mytable ADD added_comment_null INT NOT NULL",
792
                "ALTER TABLE mytable ADD added_comment_false INT NOT NULL",
793
                "ALTER TABLE mytable ADD added_comment_empty_string INT NOT NULL",
794
                "ALTER TABLE mytable ADD added_comment_integer_0 INT NOT NULL",
795
                "ALTER TABLE mytable ADD added_comment_float_0 INT NOT NULL",
796
                "ALTER TABLE mytable ADD added_comment_string_0 INT NOT NULL",
797
                "ALTER TABLE mytable ADD added_comment INT NOT NULL",
798
                "ALTER TABLE mytable ADD [added_comment_quoted] INT NOT NULL",
799
                "ALTER TABLE mytable ADD [select] INT NOT NULL",
800
                "ALTER TABLE mytable ADD added_commented_type VARCHAR(MAX) NOT NULL",
801
                "ALTER TABLE mytable ADD added_commented_type_with_comment VARCHAR(MAX) NOT NULL",
802
                "ALTER TABLE mytable ADD added_comment_with_string_literal_char NVARCHAR(255) NOT NULL",
803
                "ALTER TABLE mytable DROP COLUMN comment_integer_0",
804
                "ALTER TABLE mytable ALTER COLUMN comment_null NVARCHAR(255) NOT NULL",
805
                "ALTER TABLE mytable ALTER COLUMN comment_empty_string VARCHAR(MAX) NOT NULL",
806
                "ALTER TABLE mytable ALTER COLUMN [comment_quoted] VARCHAR(MAX) NOT NULL",
807
                "ALTER TABLE mytable ALTER COLUMN [create] VARCHAR(MAX) NOT NULL",
808
                "ALTER TABLE mytable ALTER COLUMN commented_type INT NOT NULL",
809
810
                // Added columns.
811
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', added_comment_integer_0",
812
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', added_comment_float_0",
813
                "EXEC sp_addextendedproperty N'MS_Description', N'0', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', added_comment_string_0",
814
                "EXEC sp_addextendedproperty N'MS_Description', N'Doctrine', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', added_comment",
815
                "EXEC sp_addextendedproperty N'MS_Description', N'rulez', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', [added_comment_quoted]",
816
                "EXEC sp_addextendedproperty N'MS_Description', N'666', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', [select]",
817
                "EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:object)', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', added_commented_type",
818
                "EXEC sp_addextendedproperty N'MS_Description', N'666(DC2Type:array)', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', added_commented_type_with_comment",
819
                "EXEC sp_addextendedproperty N'MS_Description', N'''''', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', added_comment_with_string_literal_char",
820
821
                // Changed columns.
822
                "EXEC sp_addextendedproperty N'MS_Description', N'primary', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', id",
823
                "EXEC sp_addextendedproperty N'MS_Description', N'false', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment_false",
824
                "EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:object)', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment_empty_string",
825
                "EXEC sp_dropextendedproperty N'MS_Description', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment_string_0",
826
                "EXEC sp_dropextendedproperty N'MS_Description', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment",
827
                "EXEC sp_updateextendedproperty N'MS_Description', N'Doctrine array.(DC2Type:array)', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', [comment_quoted]",
828
                "EXEC sp_updateextendedproperty N'MS_Description', N'(DC2Type:object)', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', [create]",
829
                "EXEC sp_updateextendedproperty N'MS_Description', N'foo', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', commented_type",
830
                "EXEC sp_updateextendedproperty N'MS_Description', N'(DC2Type:array)', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', commented_type_with_comment",
831
                "EXEC sp_updateextendedproperty N'MS_Description', N'''', N'SCHEMA', dbo, N'TABLE', mytable, N'COLUMN', comment_with_string_literal_char",
832
            ),
833
            $this->_platform->getAlterTableSQL($tableDiff)
834
        );
835
    }
836
837
    /**
838
     * @group DBAL-122
839
     */
840
    public function testInitializesDoctrineTypeMappings()
841
    {
842
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('bigint'));
843
        self::assertSame('bigint', $this->_platform->getDoctrineTypeMapping('bigint'));
844
845
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('numeric'));
846
        self::assertSame('decimal', $this->_platform->getDoctrineTypeMapping('numeric'));
847
848
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('bit'));
849
        self::assertSame('boolean', $this->_platform->getDoctrineTypeMapping('bit'));
850
851
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('smallint'));
852
        self::assertSame('smallint', $this->_platform->getDoctrineTypeMapping('smallint'));
853
854
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('decimal'));
855
        self::assertSame('decimal', $this->_platform->getDoctrineTypeMapping('decimal'));
856
857
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('smallmoney'));
858
        self::assertSame('integer', $this->_platform->getDoctrineTypeMapping('smallmoney'));
859
860
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('int'));
861
        self::assertSame('integer', $this->_platform->getDoctrineTypeMapping('int'));
862
863
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('tinyint'));
864
        self::assertSame('smallint', $this->_platform->getDoctrineTypeMapping('tinyint'));
865
866
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('money'));
867
        self::assertSame('integer', $this->_platform->getDoctrineTypeMapping('money'));
868
869
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('float'));
870
        self::assertSame('float', $this->_platform->getDoctrineTypeMapping('float'));
871
872
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('real'));
873
        self::assertSame('float', $this->_platform->getDoctrineTypeMapping('real'));
874
875
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('double'));
876
        self::assertSame('float', $this->_platform->getDoctrineTypeMapping('double'));
877
878
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('double precision'));
879
        self::assertSame('float', $this->_platform->getDoctrineTypeMapping('double precision'));
880
881
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('smalldatetime'));
882
        self::assertSame('datetime', $this->_platform->getDoctrineTypeMapping('smalldatetime'));
883
884
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('datetime'));
885
        self::assertSame('datetime', $this->_platform->getDoctrineTypeMapping('datetime'));
886
887
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('char'));
888
        self::assertSame('string', $this->_platform->getDoctrineTypeMapping('char'));
889
890
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('varchar'));
891
        self::assertSame('string', $this->_platform->getDoctrineTypeMapping('varchar'));
892
893
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('text'));
894
        self::assertSame('text', $this->_platform->getDoctrineTypeMapping('text'));
895
896
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('nchar'));
897
        self::assertSame('string', $this->_platform->getDoctrineTypeMapping('nchar'));
898
899
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('nvarchar'));
900
        self::assertSame('string', $this->_platform->getDoctrineTypeMapping('nvarchar'));
901
902
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('ntext'));
903
        self::assertSame('text', $this->_platform->getDoctrineTypeMapping('ntext'));
904
905
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('binary'));
906
        self::assertSame('binary', $this->_platform->getDoctrineTypeMapping('binary'));
907
908
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('varbinary'));
909
        self::assertSame('binary', $this->_platform->getDoctrineTypeMapping('varbinary'));
910
911
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('image'));
912
        self::assertSame('blob', $this->_platform->getDoctrineTypeMapping('image'));
913
914
        self::assertTrue($this->_platform->hasDoctrineTypeMappingFor('uniqueidentifier'));
915
        self::assertSame('guid', $this->_platform->getDoctrineTypeMapping('uniqueidentifier'));
916
    }
917
918
    protected function getBinaryMaxLength()
919
    {
920
        return 8000;
921
    }
922
923 View Code Duplication
    public function testReturnsBinaryTypeDeclarationSQL()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
924
    {
925
        self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array()));
926
        self::assertSame('VARBINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 0)));
927
        self::assertSame('VARBINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 8000)));
928
        self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(array('length' => 8001)));
929
930
        self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true)));
931
        self::assertSame('BINARY(255)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 0)));
932
        self::assertSame('BINARY(8000)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 8000)));
933
        self::assertSame('VARBINARY(MAX)', $this->_platform->getBinaryTypeDeclarationSQL(array('fixed' => true, 'length' => 8001)));
934
    }
935
936
    /**
937
     * @group DBAL-234
938
     */
939
    protected function getAlterTableRenameForeignKeySQL(): array
940
    {
941
        return [
942
            'ALTER TABLE mytable DROP CONSTRAINT fk1',
943
            'ALTER TABLE mytable ADD CONSTRAINT fk2 FOREIGN KEY (fk) REFERENCES fk_table (id)',
944
        ];
945
    }
946
947
    /**
948
     * @group DBAL-234
949
     */
950
    protected function getAlterTableRenameIndexSQL()
951
    {
952
        return array(
953
            "EXEC sp_RENAME N'mytable.idx_foo', N'idx_bar', N'INDEX'",
954
        );
955
    }
956
957
    /**
958
     * @group DBAL-234
959
     */
960
    protected function getQuotedAlterTableRenameIndexSQL()
961
    {
962
        return array(
963
            "EXEC sp_RENAME N'[table].[create]', N'[select]', N'INDEX'",
964
            "EXEC sp_RENAME N'[table].[foo]', N'[bar]', N'INDEX'",
965
        );
966
    }
967
968
    /**
969
     * @group DBAL-825
970
     */
971
    public function testChangeColumnsTypeWithDefaultValue()
972
    {
973
        $tableName = 'column_def_change_type';
974
        $table     = new Table($tableName);
975
976
        $table->addColumn('col_int', 'smallint', array('default' => 666));
977
        $table->addColumn('col_string', 'string', array('default' => 'foo'));
978
979
        $tableDiff = new TableDiff($tableName);
980
        $tableDiff->fromTable = $table;
981
        $tableDiff->changedColumns['col_int'] = new ColumnDiff(
982
            'col_int',
983
            new Column('col_int', Type::getType('integer'), array('default' => 666)),
984
            array('type'),
985
            new Column('col_int', Type::getType('smallint'), array('default' => 666))
986
        );
987
988
        $tableDiff->changedColumns['col_string'] = new ColumnDiff(
989
            'col_string',
990
            new Column('col_string', Type::getType('string'), array('default' => 666, 'fixed' => true)),
991
            array('fixed'),
992
            new Column('col_string', Type::getType('string'), array('default' => 666))
993
        );
994
995
        $expected = $this->_platform->getAlterTableSQL($tableDiff);
996
997
        self::assertSame(
998
            $expected,
999
            array(
1000
                'ALTER TABLE column_def_change_type DROP CONSTRAINT DF_829302E0_FA2CB292',
1001
                'ALTER TABLE column_def_change_type ALTER COLUMN col_int INT NOT NULL',
1002
                'ALTER TABLE column_def_change_type ADD CONSTRAINT DF_829302E0_FA2CB292 DEFAULT 666 FOR col_int',
1003
                'ALTER TABLE column_def_change_type DROP CONSTRAINT DF_829302E0_2725A6D0',
1004
                'ALTER TABLE column_def_change_type ALTER COLUMN col_string NCHAR(255) NOT NULL',
1005
                "ALTER TABLE column_def_change_type ADD CONSTRAINT DF_829302E0_2725A6D0 DEFAULT '666' FOR col_string",
1006
            )
1007
        );
1008
    }
1009
1010
    /**
1011
     * {@inheritdoc}
1012
     */
1013
    protected function getQuotedAlterTableRenameColumnSQL()
1014
    {
1015
        return array(
1016
            "sp_RENAME 'mytable.unquoted1', 'unquoted', 'COLUMN'",
1017
            "sp_RENAME 'mytable.unquoted2', '[where]', 'COLUMN'",
1018
            "sp_RENAME 'mytable.unquoted3', '[foo]', 'COLUMN'",
1019
            "sp_RENAME 'mytable.[create]', 'reserved_keyword', 'COLUMN'",
1020
            "sp_RENAME 'mytable.[table]', '[from]', 'COLUMN'",
1021
            "sp_RENAME 'mytable.[select]', '[bar]', 'COLUMN'",
1022
            "sp_RENAME 'mytable.quoted1', 'quoted', 'COLUMN'",
1023
            "sp_RENAME 'mytable.quoted2', '[and]', 'COLUMN'",
1024
            "sp_RENAME 'mytable.quoted3', '[baz]', 'COLUMN'",
1025
        );
1026
    }
1027
1028
    /**
1029
     * {@inheritdoc}
1030
     */
1031
    protected function getQuotedAlterTableChangeColumnLengthSQL()
1032
    {
1033
        $this->markTestIncomplete('Not implemented yet');
1034
    }
1035
1036
    /**
1037
     * @group DBAL-807
1038
     */
1039
    protected function getAlterTableRenameIndexInSchemaSQL()
1040
    {
1041
        return array(
1042
            "EXEC sp_RENAME N'myschema.mytable.idx_foo', N'idx_bar', N'INDEX'",
1043
        );
1044
    }
1045
1046
    /**
1047
     * @group DBAL-807
1048
     */
1049
    protected function getQuotedAlterTableRenameIndexInSchemaSQL()
1050
    {
1051
        return array(
1052
            "EXEC sp_RENAME N'[schema].[table].[create]', N'[select]', N'INDEX'",
1053
            "EXEC sp_RENAME N'[schema].[table].[foo]', N'[bar]', N'INDEX'",
1054
        );
1055
    }
1056
1057
    protected function getQuotesDropForeignKeySQL()
1058
    {
1059
        return 'ALTER TABLE [table] DROP CONSTRAINT [select]';
1060
    }
1061
1062
    protected function getQuotesDropConstraintSQL()
1063
    {
1064
        return 'ALTER TABLE [table] DROP CONSTRAINT [select]';
1065
    }
1066
1067
    /**
1068
     * @dataProvider getGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL
1069
     * @group DBAL-830
1070
     */
1071
    public function testGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL($table, $column, $expectedSql)
1072
    {
1073
        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

1073
        self::assertSame($expectedSql, $this->_platform->/** @scrutinizer ignore-call */ getDefaultConstraintDeclarationSQL($table, $column));
Loading history...
1074
    }
1075
1076
    public function getGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL()
1077
    {
1078
        return array(
1079
            // Unquoted identifiers non-reserved keywords.
1080
            array('mytable', array('name' => 'mycolumn', 'default' => 'foo'), " CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR mycolumn"),
1081
            // Quoted identifiers non-reserved keywords.
1082
            array('`mytable`', array('name' => '`mycolumn`', 'default' => 'foo'), " CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR [mycolumn]"),
1083
            // Unquoted identifiers reserved keywords.
1084
            array('table', array('name' => 'select', 'default' => 'foo'), " CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"),
1085
            // Quoted identifiers reserved keywords.
1086
            array('`table`', array('name' => '`select`', 'default' => 'foo'), " CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"),
1087
        );
1088
    }
1089
1090
    /**
1091
     * @dataProvider getGeneratesIdentifierNamesInCreateTableSQL
1092
     * @group DBAL-830
1093
     */
1094
    public function testGeneratesIdentifierNamesInCreateTableSQL($table, $expectedSql)
1095
    {
1096
        self::assertSame($expectedSql, $this->_platform->getCreateTableSQL($table));
1097
    }
1098
1099
    public function getGeneratesIdentifierNamesInCreateTableSQL()
1100
    {
1101
        return array(
1102
            // Unquoted identifiers non-reserved keywords.
1103
            array(
1104
                new Table('mytable', array(new Column('mycolumn', Type::getType('string'), array('default' => 'foo')))),
1105
                array(
1106
                    'CREATE TABLE mytable (mycolumn NVARCHAR(255) NOT NULL)',
1107
                    "ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR mycolumn"
1108
                )
1109
            ),
1110
            // Quoted identifiers reserved keywords.
1111
            array(
1112
                new Table('`mytable`', array(new Column('`mycolumn`', Type::getType('string'), array('default' => 'foo')))),
1113
                array(
1114
                    'CREATE TABLE [mytable] ([mycolumn] NVARCHAR(255) NOT NULL)',
1115
                    "ALTER TABLE [mytable] ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR [mycolumn]"
1116
                )
1117
            ),
1118
            // Unquoted identifiers reserved keywords.
1119
            array(
1120
                new Table('table', array(new Column('select', Type::getType('string'), array('default' => 'foo')))),
1121
                array(
1122
                    'CREATE TABLE [table] ([select] NVARCHAR(255) NOT NULL)',
1123
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"
1124
                )
1125
            ),
1126
            // Quoted identifiers reserved keywords.
1127
            array(
1128
                new Table('`table`', array(new Column('`select`', Type::getType('string'), array('default' => 'foo')))),
1129
                array(
1130
                    'CREATE TABLE [table] ([select] NVARCHAR(255) NOT NULL)',
1131
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"
1132
                )
1133
            ),
1134
        );
1135
    }
1136
1137
    /**
1138
     * @dataProvider getGeneratesIdentifierNamesInAlterTableSQL
1139
     * @group DBAL-830
1140
     */
1141
    public function testGeneratesIdentifierNamesInAlterTableSQL($tableDiff, $expectedSql)
1142
    {
1143
        self::assertSame($expectedSql, $this->_platform->getAlterTableSQL($tableDiff));
1144
    }
1145
1146
    public function getGeneratesIdentifierNamesInAlterTableSQL()
1147
    {
1148
        return array(
1149
            // Unquoted identifiers non-reserved keywords.
1150
            array(
1151
                new TableDiff(
1152
                    'mytable',
1153
                    array(new Column('addcolumn', Type::getType('string'), array('default' => 'foo'))),
1154
                    array(
1155
                        'mycolumn' => new ColumnDiff(
1156
                            'mycolumn',
1157
                            new Column('mycolumn', Type::getType('string'), array('default' => 'bar')),
1158
                            array('default'),
1159
                            new Column('mycolumn', Type::getType('string'), array('default' => 'foo'))
1160
                        )
1161
                    ),
1162
                    array(new Column('removecolumn', Type::getType('string'), array('default' => 'foo')))
1163
                ),
1164
                array(
1165
                    'ALTER TABLE mytable ADD addcolumn NVARCHAR(255) NOT NULL',
1166
                    "ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_4AD86123 DEFAULT 'foo' FOR addcolumn",
1167
                    'ALTER TABLE mytable DROP COLUMN removecolumn',
1168
                    'ALTER TABLE mytable DROP CONSTRAINT DF_6B2BD609_9BADD926',
1169
                    'ALTER TABLE mytable ALTER COLUMN mycolumn NVARCHAR(255) NOT NULL',
1170
                    "ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'bar' FOR mycolumn"
1171
                )
1172
            ),
1173
            // Quoted identifiers non-reserved keywords.
1174
            array(
1175
                new TableDiff(
1176
                    '`mytable`',
1177
                    array(new Column('`addcolumn`', Type::getType('string'), array('default' => 'foo'))),
1178
                    array(
1179
                        'mycolumn' => new ColumnDiff(
1180
                            '`mycolumn`',
1181
                            new Column('`mycolumn`', Type::getType('string'), array('default' => 'bar')),
1182
                            array('default'),
1183
                            new Column('`mycolumn`', Type::getType('string'), array('default' => 'foo'))
1184
                        )
1185
                    ),
1186
                    array(new Column('`removecolumn`', Type::getType('string'), array('default' => 'foo')))
1187
                ),
1188
                array(
1189
                    'ALTER TABLE [mytable] ADD [addcolumn] NVARCHAR(255) NOT NULL',
1190
                    "ALTER TABLE [mytable] ADD CONSTRAINT DF_6B2BD609_4AD86123 DEFAULT 'foo' FOR [addcolumn]",
1191
                    'ALTER TABLE [mytable] DROP COLUMN [removecolumn]',
1192
                    'ALTER TABLE [mytable] DROP CONSTRAINT DF_6B2BD609_9BADD926',
1193
                    'ALTER TABLE [mytable] ALTER COLUMN [mycolumn] NVARCHAR(255) NOT NULL',
1194
                    "ALTER TABLE [mytable] ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'bar' FOR [mycolumn]"
1195
                )
1196
            ),
1197
            // Unquoted identifiers reserved keywords.
1198
            array(
1199
                new TableDiff(
1200
                    'table',
1201
                    array(new Column('add', Type::getType('string'), array('default' => 'foo'))),
1202
                    array(
1203
                        'select' => new ColumnDiff(
1204
                            'select',
1205
                            new Column('select', Type::getType('string'), array('default' => 'bar')),
1206
                            array('default'),
1207
                            new Column('select', Type::getType('string'), array('default' => 'foo'))
1208
                        )
1209
                    ),
1210
                    array(new Column('drop', Type::getType('string'), array('default' => 'foo')))
1211
                ),
1212
                array(
1213
                    'ALTER TABLE [table] ADD [add] NVARCHAR(255) NOT NULL',
1214
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_FD1A73E7 DEFAULT 'foo' FOR [add]",
1215
                    'ALTER TABLE [table] DROP COLUMN [drop]',
1216
                    'ALTER TABLE [table] DROP CONSTRAINT DF_F6298F46_4BF2EAC0',
1217
                    'ALTER TABLE [table] ALTER COLUMN [select] NVARCHAR(255) NOT NULL',
1218
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'bar' FOR [select]"
1219
                )
1220
            ),
1221
            // Quoted identifiers reserved keywords.
1222
            array(
1223
                new TableDiff(
1224
                    '`table`',
1225
                    array(new Column('`add`', Type::getType('string'), array('default' => 'foo'))),
1226
                    array(
1227
                        'select' => new ColumnDiff(
1228
                            '`select`',
1229
                            new Column('`select`', Type::getType('string'), array('default' => 'bar')),
1230
                            array('default'),
1231
                            new Column('`select`', Type::getType('string'), array('default' => 'foo'))
1232
                        )
1233
                    ),
1234
                    array(new Column('`drop`', Type::getType('string'), array('default' => 'foo')))
1235
                ),
1236
                array(
1237
                    'ALTER TABLE [table] ADD [add] NVARCHAR(255) NOT NULL',
1238
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_FD1A73E7 DEFAULT 'foo' FOR [add]",
1239
                    'ALTER TABLE [table] DROP COLUMN [drop]',
1240
                    'ALTER TABLE [table] DROP CONSTRAINT DF_F6298F46_4BF2EAC0',
1241
                    'ALTER TABLE [table] ALTER COLUMN [select] NVARCHAR(255) NOT NULL',
1242
                    "ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'bar' FOR [select]"
1243
                )
1244
            ),
1245
        );
1246
    }
1247
1248
    /**
1249
     * @group DBAL-423
1250
     */
1251
    public function testReturnsGuidTypeDeclarationSQL()
1252
    {
1253
        self::assertSame('UNIQUEIDENTIFIER', $this->_platform->getGuidTypeDeclarationSQL(array()));
1254
    }
1255
1256
    /**
1257
     * {@inheritdoc}
1258
     */
1259
    public function getAlterTableRenameColumnSQL()
1260
    {
1261
        return array(
1262
            "sp_RENAME 'foo.bar', 'baz', 'COLUMN'",
1263
            'ALTER TABLE foo DROP CONSTRAINT DF_8C736521_76FF8CAA',
1264
            'ALTER TABLE foo ADD CONSTRAINT DF_8C736521_78240498 DEFAULT 666 FOR baz',
1265
        );
1266
    }
1267
1268
    /**
1269
     * {@inheritdoc}
1270
     */
1271
    protected function getQuotesTableIdentifiersInAlterTableSQL()
1272
    {
1273
        return array(
1274
            'ALTER TABLE [foo] DROP CONSTRAINT fk1',
1275
            'ALTER TABLE [foo] DROP CONSTRAINT fk2',
1276
            "sp_RENAME '[foo].id', 'war', 'COLUMN'",
1277
            'ALTER TABLE [foo] ADD bloo INT NOT NULL',
1278
            'ALTER TABLE [foo] DROP COLUMN baz',
1279
            'ALTER TABLE [foo] ALTER COLUMN bar INT',
1280
            "sp_RENAME '[foo]', 'table'",
1281
            "DECLARE @sql NVARCHAR(MAX) = N''; " .
1282
            "SELECT @sql += N'EXEC sp_rename N''' + dc.name + ''', N''' + REPLACE(dc.name, '8C736521', 'F6298F46') + ''', " .
1283
            "''OBJECT'';' FROM sys.default_constraints dc JOIN sys.tables tbl ON dc.parent_object_id = tbl.object_id " .
1284
            "WHERE tbl.name = 'table';EXEC sp_executesql @sql",
1285
            'ALTER TABLE [table] ADD CONSTRAINT fk_add FOREIGN KEY (fk3) REFERENCES fk_table (id)',
1286
            'ALTER TABLE [table] ADD CONSTRAINT fk2 FOREIGN KEY (fk2) REFERENCES fk_table2 (id)',
1287
        );
1288
    }
1289
1290
    /**
1291
     * {@inheritdoc}
1292
     */
1293
    protected function getCommentOnColumnSQL()
1294
    {
1295
        return array(
1296
            "COMMENT ON COLUMN foo.bar IS 'comment'",
1297
            "COMMENT ON COLUMN [Foo].[BAR] IS 'comment'",
1298
            "COMMENT ON COLUMN [select].[from] IS 'comment'",
1299
        );
1300
    }
1301
1302
    /**
1303
     * {@inheritdoc}
1304
     */
1305 View Code Duplication
    public function getReturnsForeignKeyReferentialActionSQL()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1306
    {
1307
        return array(
1308
            array('CASCADE', 'CASCADE'),
1309
            array('SET NULL', 'SET NULL'),
1310
            array('NO ACTION', 'NO ACTION'),
1311
            array('RESTRICT', 'NO ACTION'),
1312
            array('SET DEFAULT', 'SET DEFAULT'),
1313
            array('CaScAdE', 'CASCADE'),
1314
        );
1315
    }
1316
1317
    /**
1318
     * {@inheritdoc}
1319
     */
1320
    protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
1321
    {
1322
        return 'CONSTRAINT [select] UNIQUE (foo) WHERE foo IS NOT NULL';
1323
    }
1324
1325
    /**
1326
     * {@inheritdoc}
1327
     */
1328
    protected function getQuotesReservedKeywordInIndexDeclarationSQL()
1329
    {
1330
        return 'INDEX [select] (foo)';
1331
    }
1332
1333
    /**
1334
     * {@inheritdoc}
1335
     */
1336
    protected function getQuotesReservedKeywordInTruncateTableSQL()
1337
    {
1338
        return 'TRUNCATE TABLE [select]';
1339
    }
1340
1341
    /**
1342
     * {@inheritdoc}
1343
     */
1344
    protected function getAlterStringToFixedStringSQL()
1345
    {
1346
        return array(
1347
            'ALTER TABLE mytable ALTER COLUMN name NCHAR(2) NOT NULL',
1348
        );
1349
    }
1350
1351
    /**
1352
     * {@inheritdoc}
1353
     */
1354
    protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL()
1355
    {
1356
        return array(
1357
            "EXEC sp_RENAME N'mytable.idx_foo', N'idx_foo_renamed', N'INDEX'",
1358
        );
1359
    }
1360
1361 View Code Duplication
    public function testModifyLimitQueryWithTopNSubQueryWithOrderBy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1362
    {
1363
        $querySql = 'SELECT * FROM test t WHERE t.id = (SELECT TOP 1 t2.id FROM test t2 ORDER BY t2.data DESC)';
1364
        $alteredSql = 'SELECT TOP 10 * FROM test t WHERE t.id = (SELECT TOP 1 t2.id FROM test t2 ORDER BY t2.data DESC)';
1365
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
1366
        self::assertEquals(sprintf(static::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
1367
1368
        $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';
1369
        $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';
1370
        $sql = $this->_platform->modifyLimitQuery($querySql, 10);
1371
        self::assertEquals(sprintf(static::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
1372
    }
1373
1374
    /**
1375
     * @group DBAL-2436
1376
     */
1377
    public function testQuotesTableNameInListTableColumnsSQL()
1378
    {
1379
        self::assertContains("'Foo''Bar\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
1380
    }
1381
1382
    /**
1383
     * @group DBAL-2436
1384
     */
1385
    public function testQuotesSchemaNameInListTableColumnsSQL()
1386
    {
1387
        self::assertContains(
1388
            "'Foo''Bar\\'",
1389
            $this->_platform->getListTableColumnsSQL("Foo'Bar\\.baz_table"),
1390
            '',
1391
            true
1392
        );
1393
    }
1394
1395
    /**
1396
     * @group DBAL-2436
1397
     */
1398
    public function testQuotesTableNameInListTableForeignKeysSQL()
1399
    {
1400
        self::assertContains("'Foo''Bar\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
1401
    }
1402
1403
    /**
1404
     * @group DBAL-2436
1405
     */
1406
    public function testQuotesSchemaNameInListTableForeignKeysSQL()
1407
    {
1408
        self::assertContains(
1409
            "'Foo''Bar\\'",
1410
            $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\.baz_table"),
1411
            '',
1412
            true
1413
        );
1414
    }
1415
1416
    /**
1417
     * @group DBAL-2436
1418
     */
1419
    public function testQuotesTableNameInListTableIndexesSQL()
1420
    {
1421
        self::assertContains("'Foo''Bar\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
1422
    }
1423
1424
    /**
1425
     * @group DBAL-2436
1426
     */
1427
    public function testQuotesSchemaNameInListTableIndexesSQL()
1428
    {
1429
        self::assertContains(
1430
            "'Foo''Bar\\'",
1431
            $this->_platform->getListTableIndexesSQL("Foo'Bar\\.baz_table"),
1432
            '',
1433
            true
1434
        );
1435
    }
1436
1437
    /**
1438
     * @group 2859
1439
     */
1440 View Code Duplication
    public function testGetDefaultValueDeclarationSQLForDateType() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1441
    {
1442
        $currentDateSql = $this->_platform->getCurrentDateSQL();
1443
        foreach (['date', 'date_immutable'] as $type) {
1444
            $field = [
1445
                'type'    => Type::getType($type),
1446
                'default' => $currentDateSql,
1447
            ];
1448
1449
            self::assertSame(
1450
                " DEFAULT '" . $currentDateSql . "'",
1451
                $this->_platform->getDefaultValueDeclarationSQL($field)
1452
            );
1453
        }
1454
    }
1455
}
1456