|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
5 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
6 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
7 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
8 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
9 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
10 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
11 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
12 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
13 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
14 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
15 |
* |
|
|
16 |
* This software consists of voluntary contributions made by many individuals |
|
|
17 |
* and is licensed under the LGPL. For more information, see |
|
|
18 |
* <http://www.doctrine-project.org>. |
|
|
19 |
*/ |
|
|
20 |
|
|
|
21 |
namespace Doctrine\DBAL\Platforms; |
|
|
22 |
|
|
|
23 |
use Doctrine\DBAL\Schema\TableDiff; |
|
|
24 |
use Doctrine\DBAL\DBALException; |
|
|
25 |
use Doctrine\DBAL\Schema\Index, |
|
|
26 |
Doctrine\DBAL\Schema\Table; |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* The MsSqlPlatform provides the behavior, features and SQL dialect of the |
|
|
30 |
* MySQL database platform. |
|
|
31 |
* |
|
|
32 |
* @since 2.0 |
|
|
33 |
* @author Roman Borschel <roman@code-factory.org> |
|
|
34 |
* @author Jonathan H. Wage <jonwage@gmail.com> |
|
|
35 |
* @author Benjamin Eberlei <kontakt@beberlei.de> |
|
|
36 |
* @todo Rename: MsSQLPlatform |
|
|
37 |
*/ |
|
|
38 |
class MsSqlPlatform extends AbstractPlatform |
|
|
39 |
{ |
|
|
40 |
|
|
|
41 |
/** |
|
|
42 |
* Whether the platform prefers identity columns for ID generation. |
|
|
43 |
* MsSql prefers "autoincrement" identity columns since sequences can only |
|
|
44 |
* be emulated with a table. |
|
|
45 |
* |
|
|
46 |
* @return boolean |
|
|
47 |
* @override |
|
|
48 |
*/ |
|
|
49 |
public function prefersIdentityColumns() |
|
|
50 |
{ |
|
|
51 |
return true; |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
/** |
|
|
55 |
* Whether the platform supports identity columns. |
|
|
56 |
* MsSql supports this through AUTO_INCREMENT columns. |
|
|
57 |
* |
|
|
58 |
* @return boolean |
|
|
59 |
* @override |
|
|
60 |
*/ |
|
|
61 |
public function supportsIdentityColumns() |
|
|
62 |
{ |
|
|
63 |
return true; |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
/** |
|
|
67 |
* Whether the platform supports releasing savepoints. |
|
|
68 |
* |
|
|
69 |
* @return boolean |
|
|
70 |
*/ |
|
|
71 |
public function supportsReleaseSavepoints() |
|
|
72 |
{ |
|
|
73 |
return false; |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
/** |
|
|
77 |
* create a new database |
|
|
78 |
* |
|
|
79 |
* @param string $name name of the database that should be created |
|
|
80 |
* @return string |
|
|
81 |
* @override |
|
|
82 |
*/ |
|
|
83 |
public function getCreateDatabaseSQL($name) |
|
|
84 |
{ |
|
|
85 |
return 'CREATE DATABASE ' . $name; |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
/** |
|
|
89 |
* drop an existing database |
|
|
90 |
* |
|
|
91 |
* @param string $name name of the database that should be dropped |
|
|
92 |
* @return string |
|
|
93 |
* @override |
|
|
94 |
*/ |
|
|
95 |
public function getDropDatabaseSQL($name) |
|
|
96 |
{ |
|
|
97 |
return 'DROP DATABASE ' . $name; |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
/** |
|
|
101 |
* @override |
|
|
102 |
*/ |
|
|
103 |
public function supportsCreateDropDatabase() |
|
|
104 |
{ |
|
|
105 |
return false; |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
/** |
|
|
109 |
* @override |
|
|
110 |
*/ |
|
|
111 |
public function getDropForeignKeySQL($foreignKey, $table) |
|
|
112 |
{ |
|
|
113 |
if ($foreignKey instanceof \Doctrine\DBAL\Schema\ForeignKeyConstraint) { |
|
|
114 |
$foreignKey = $foreignKey->getQuotedName($this); |
|
|
115 |
} |
|
|
116 |
|
|
|
117 |
if ($table instanceof \Doctrine\DBAL\Schema\Table) { |
|
|
118 |
$table = $table->getQuotedName($this); |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey; |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
/** |
|
|
125 |
* @override |
|
|
126 |
*/ |
|
|
127 |
public function getDropIndexSQL($index, $table=null) |
|
|
128 |
{ |
|
|
129 |
if ($index instanceof \Doctrine\DBAL\Schema\Index) { |
|
|
130 |
$index_ = $index; |
|
|
131 |
$index = $index->getQuotedName($this); |
|
|
132 |
} else if (!is_string($index)) { |
|
|
133 |
throw new \InvalidArgumentException('AbstractPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.'); |
|
|
134 |
} |
|
|
135 |
|
|
|
136 |
if (!isset($table)) { |
|
|
137 |
return 'DROP INDEX ' . $index; |
|
|
138 |
} else { |
|
|
139 |
if ($table instanceof \Doctrine\DBAL\Schema\Table) { |
|
|
140 |
$table = $table->getQuotedName($this); |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
return "IF EXISTS (SELECT * FROM sysobjects WHERE name = '$index') |
|
|
144 |
ALTER TABLE " . $table . " DROP CONSTRAINT " . $index . " |
|
|
145 |
ELSE |
|
|
146 |
DROP INDEX " . $index . " ON " . $table; |
|
|
147 |
} |
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
/** |
|
|
151 |
* @override |
|
|
152 |
*/ |
|
|
153 |
protected function _getCreateTableSQL($tableName, array $columns, array $options = array()) |
|
|
154 |
{ |
|
|
155 |
// @todo does other code breaks because of this? |
|
|
156 |
// foce primary keys to be not null |
|
|
157 |
foreach ($columns as &$column) { |
|
|
158 |
if (isset($column['primary']) && $column['primary']) { |
|
|
159 |
$column['notnull'] = true; |
|
|
160 |
} |
|
|
161 |
} |
|
|
162 |
|
|
|
163 |
$columnListSql = $this->getColumnDeclarationListSQL($columns); |
|
|
164 |
|
|
|
165 |
if (isset($options['uniqueConstraints']) && !empty($options['uniqueConstraints'])) { |
|
|
166 |
foreach ($options['uniqueConstraints'] as $name => $definition) { |
|
|
167 |
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($name, $definition); |
|
|
168 |
} |
|
|
169 |
} |
|
|
170 |
|
|
|
171 |
if (isset($options['primary']) && !empty($options['primary'])) { |
|
|
172 |
$columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')'; |
|
|
173 |
} |
|
|
174 |
|
|
|
175 |
$query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql; |
|
|
176 |
|
|
|
177 |
$check = $this->getCheckDeclarationSQL($columns); |
|
|
178 |
if (!empty($check)) { |
|
|
179 |
$query .= ', ' . $check; |
|
|
180 |
} |
|
|
181 |
$query .= ')'; |
|
|
182 |
|
|
|
183 |
$sql[] = $query; |
|
|
184 |
|
|
|
185 |
if (isset($options['indexes']) && !empty($options['indexes'])) { |
|
|
186 |
foreach ($options['indexes'] AS $index) { |
|
|
187 |
$sql[] = $this->getCreateIndexSQL($index, $tableName); |
|
|
188 |
} |
|
|
189 |
} |
|
|
190 |
|
|
|
191 |
if (isset($options['foreignKeys'])) { |
|
|
192 |
foreach ((array) $options['foreignKeys'] AS $definition) { |
|
|
193 |
$sql[] = $this->getCreateForeignKeySQL($definition, $tableName); |
|
|
194 |
} |
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
return $sql; |
|
|
198 |
} |
|
|
199 |
|
|
|
200 |
/** |
|
|
201 |
* @override |
|
|
202 |
*/ |
|
|
203 |
public function getUniqueConstraintDeclarationSQL($name, Index $index) |
|
|
204 |
{ |
|
|
205 |
$constraint = parent::getUniqueConstraintDeclarationSQL($name, $index); |
|
|
206 |
|
|
|
207 |
$constraint = $this->_appendUniqueConstraintDefinition($constraint, $index); |
|
|
208 |
|
|
|
209 |
return $constraint; |
|
|
210 |
} |
|
|
211 |
|
|
|
212 |
/** |
|
|
213 |
* @override |
|
|
214 |
*/ |
|
|
215 |
public function getCreateIndexSQL(Index $index, $table) |
|
|
216 |
{ |
|
|
217 |
$constraint = parent::getCreateIndexSQL($index, $table); |
|
|
218 |
|
|
|
219 |
if ($index->isUnique()) { |
|
|
220 |
$constraint = $this->_appendUniqueConstraintDefinition($constraint, $index); |
|
|
221 |
} |
|
|
222 |
|
|
|
223 |
return $constraint; |
|
|
224 |
} |
|
|
225 |
|
|
|
226 |
/** |
|
|
227 |
* Extend unique key constraint with required filters |
|
|
228 |
* |
|
|
229 |
* @param string $sql |
|
|
230 |
* @param Index $index |
|
|
231 |
* @return string |
|
|
232 |
*/ |
|
|
233 |
private function _appendUniqueConstraintDefinition($sql, Index $index) |
|
|
234 |
{ |
|
|
235 |
$fields = array(); |
|
|
236 |
foreach ($index->getColumns() as $field => $definition) { |
|
|
237 |
if (!is_array($definition)) { |
|
|
238 |
$field = $definition; |
|
|
239 |
} |
|
|
240 |
|
|
|
241 |
$fields[] = $field . ' IS NOT NULL'; |
|
|
242 |
} |
|
|
243 |
|
|
|
244 |
return $sql . ' WHERE ' . implode(' AND ', $fields); |
|
|
245 |
} |
|
|
246 |
|
|
|
247 |
/** |
|
|
248 |
* @override |
|
|
249 |
*/ |
|
|
250 |
public function getAlterTableSQL(TableDiff $diff) |
|
|
251 |
{ |
|
|
252 |
$queryParts = array(); |
|
|
253 |
if ($diff->newName !== false) { |
|
|
254 |
$queryParts[] = 'RENAME TO ' . $diff->newName; |
|
|
255 |
} |
|
|
256 |
|
|
|
257 |
foreach ($diff->addedColumns AS $fieldName => $column) { |
|
|
258 |
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
|
|
259 |
} |
|
|
260 |
|
|
|
261 |
foreach ($diff->removedColumns AS $column) { |
|
|
262 |
$queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this); |
|
|
263 |
} |
|
|
264 |
|
|
|
265 |
foreach ($diff->changedColumns AS $columnDiff) { |
|
|
266 |
/* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */ |
|
|
267 |
$column = $columnDiff->column; |
|
|
268 |
$queryParts[] = 'CHANGE ' . ($columnDiff->oldColumnName) . ' ' |
|
|
269 |
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
|
|
270 |
} |
|
|
271 |
|
|
|
272 |
foreach ($diff->renamedColumns AS $oldColumnName => $column) { |
|
|
273 |
$queryParts[] = 'CHANGE ' . $oldColumnName . ' ' |
|
|
274 |
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
|
|
275 |
} |
|
|
276 |
|
|
|
277 |
$sql = array(); |
|
|
278 |
|
|
|
279 |
foreach ($queryParts as $query) { |
|
|
280 |
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; |
|
|
281 |
} |
|
|
282 |
|
|
|
283 |
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff)); |
|
|
284 |
|
|
|
285 |
return $sql; |
|
|
286 |
} |
|
|
287 |
|
|
|
288 |
/** |
|
|
289 |
* @override |
|
|
290 |
*/ |
|
|
291 |
public function getEmptyIdentityInsertSQL($quotedTableName, $quotedIdentifierColumnName) |
|
|
292 |
{ |
|
|
293 |
return 'INSERT INTO ' . $quotedTableName . ' DEFAULT VALUES'; |
|
|
294 |
} |
|
|
295 |
|
|
|
296 |
/** |
|
|
297 |
* @override |
|
|
298 |
*/ |
|
|
299 |
public function getShowDatabasesSQL() |
|
|
300 |
{ |
|
|
301 |
return 'SHOW DATABASES'; |
|
|
302 |
} |
|
|
303 |
|
|
|
304 |
/** |
|
|
305 |
* @override |
|
|
306 |
*/ |
|
|
307 |
public function getListTablesSQL() |
|
|
308 |
{ |
|
|
309 |
return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
|
|
310 |
} |
|
|
311 |
|
|
|
312 |
/** |
|
|
313 |
* @override |
|
|
314 |
*/ |
|
|
315 |
public function getListTableColumnsSQL($table, $database = null) |
|
|
316 |
{ |
|
|
317 |
return 'exec sp_columns @table_name = ' . $table; |
|
|
318 |
} |
|
|
319 |
|
|
|
320 |
/** |
|
|
321 |
* @override |
|
|
322 |
*/ |
|
|
323 |
public function getListTableForeignKeysSQL($table, $database = null) |
|
|
324 |
{ |
|
|
325 |
return "SELECT f.name AS ForeignKey, |
|
|
326 |
SCHEMA_NAME (f.SCHEMA_ID) AS SchemaName, |
|
|
327 |
OBJECT_NAME (f.parent_object_id) AS TableName, |
|
|
328 |
COL_NAME (fc.parent_object_id,fc.parent_column_id) AS ColumnName, |
|
|
329 |
SCHEMA_NAME (o.SCHEMA_ID) ReferenceSchemaName, |
|
|
330 |
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName, |
|
|
331 |
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName, |
|
|
332 |
f.delete_referential_action_desc, |
|
|
333 |
f.update_referential_action_desc |
|
|
334 |
FROM sys.foreign_keys AS f |
|
|
335 |
INNER JOIN sys.foreign_key_columns AS fc |
|
|
336 |
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id |
|
|
337 |
ON f.OBJECT_ID = fc.constraint_object_id |
|
|
338 |
WHERE OBJECT_NAME (f.parent_object_id) = '" . $table . "'"; |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
/** |
|
|
342 |
* @override |
|
|
343 |
*/ |
|
|
344 |
public function getListTableIndexesSQL($table, $currentDatabase = null) |
|
|
345 |
{ |
|
|
346 |
return "exec sp_helpindex '" . $table . "'"; |
|
|
347 |
} |
|
|
348 |
|
|
|
349 |
/** |
|
|
350 |
* @override |
|
|
351 |
*/ |
|
|
352 |
public function getCreateViewSQL($name, $sql) |
|
|
353 |
{ |
|
|
354 |
return 'CREATE VIEW ' . $name . ' AS ' . $sql; |
|
|
355 |
} |
|
|
356 |
|
|
|
357 |
/** |
|
|
358 |
* @override |
|
|
359 |
*/ |
|
|
360 |
public function getListViewsSQL($database) |
|
|
361 |
{ |
|
|
362 |
return "SELECT name FROM sysobjects WHERE type = 'V' ORDER BY name"; |
|
|
363 |
} |
|
|
364 |
|
|
|
365 |
/** |
|
|
366 |
* @override |
|
|
367 |
*/ |
|
|
368 |
public function getDropViewSQL($name) |
|
|
369 |
{ |
|
|
370 |
return 'DROP VIEW ' . $name; |
|
|
371 |
} |
|
|
372 |
|
|
|
373 |
/** |
|
|
374 |
* Returns the regular expression operator. |
|
|
375 |
* |
|
|
376 |
* @return string |
|
|
377 |
* @override |
|
|
378 |
*/ |
|
|
379 |
public function getRegexpExpression() |
|
|
380 |
{ |
|
|
381 |
return 'RLIKE'; |
|
|
382 |
} |
|
|
383 |
|
|
|
384 |
/** |
|
|
385 |
* Returns global unique identifier |
|
|
386 |
* |
|
|
387 |
* @return string to get global unique identifier |
|
|
388 |
* @override |
|
|
389 |
*/ |
|
|
390 |
public function getGuidExpression() |
|
|
391 |
{ |
|
|
392 |
return 'UUID()'; |
|
|
393 |
} |
|
|
394 |
|
|
|
395 |
/** |
|
|
396 |
* @override |
|
|
397 |
*/ |
|
|
398 |
public function getLocateExpression($str, $substr, $startPos = false) |
|
|
399 |
{ |
|
|
400 |
if ($startPos == false) { |
|
|
401 |
return 'CHARINDEX(' . $substr . ', ' . $str . ')'; |
|
|
402 |
} else { |
|
|
403 |
return 'CHARINDEX(' . $substr . ', ' . $str . ', ' . $startPos . ')'; |
|
|
404 |
} |
|
|
405 |
} |
|
|
406 |
|
|
|
407 |
/** |
|
|
408 |
* @override |
|
|
409 |
*/ |
|
|
410 |
public function getModExpression($expression1, $expression2) |
|
|
411 |
{ |
|
|
412 |
return $expression1 . ' % ' . $expression2; |
|
|
413 |
} |
|
|
414 |
|
|
|
415 |
/** |
|
|
416 |
* @override |
|
|
417 |
*/ |
|
|
418 |
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false) |
|
|
419 |
{ |
|
|
420 |
$trimFn = ''; |
|
|
421 |
|
|
|
422 |
if (!$char) { |
|
|
423 |
if ($pos == self::TRIM_LEADING) { |
|
|
424 |
$trimFn = 'LTRIM'; |
|
|
425 |
} else if ($pos == self::TRIM_TRAILING) { |
|
|
426 |
$trimFn = 'RTRIM'; |
|
|
427 |
} else { |
|
|
428 |
return 'LTRIM(RTRIM(' . $str . '))'; |
|
|
429 |
} |
|
|
430 |
|
|
|
431 |
return $trimFn . '(' . $str . ')'; |
|
|
432 |
} else { |
|
|
433 |
/** Original query used to get those expressions |
|
|
434 |
declare @c varchar(100) = 'xxxBarxxx', @trim_char char(1) = 'x'; |
|
|
435 |
declare @pat varchar(10) = '%[^' + @trim_char + ']%'; |
|
|
436 |
select @c as string |
|
|
437 |
, @trim_char as trim_char |
|
|
438 |
, stuff(@c, 1, patindex(@pat, @c) - 1, null) as trim_leading |
|
|
439 |
, reverse(stuff(reverse(@c), 1, patindex(@pat, reverse(@c)) - 1, null)) as trim_trailing |
|
|
440 |
, reverse(stuff(reverse(stuff(@c, 1, patindex(@pat, @c) - 1, null)), 1, patindex(@pat, reverse(stuff(@c, 1, patindex(@pat, @c) - 1, null))) - 1, null)) as trim_both; |
|
|
441 |
*/ |
|
|
442 |
$pattern = "'%[^' + $char + ']%'"; |
|
|
443 |
|
|
|
444 |
if ($pos == self::TRIM_LEADING) { |
|
|
445 |
return 'stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)'; |
|
|
446 |
} else if ($pos == self::TRIM_TRAILING) { |
|
|
447 |
return 'reverse(stuff(reverse(' . $str . '), 1, patindex(' . $pattern . ', reverse(' . $str . ')) - 1, null))'; |
|
|
448 |
} else { |
|
|
449 |
return 'reverse(stuff(reverse(stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null)), 1, patindex(' . $pattern . ', reverse(stuff(' . $str . ', 1, patindex(' . $pattern . ', ' . $str . ') - 1, null))) - 1, null))'; |
|
|
450 |
} |
|
|
451 |
} |
|
|
452 |
} |
|
|
453 |
|
|
|
454 |
/** |
|
|
455 |
* @override |
|
|
456 |
*/ |
|
|
457 |
public function getConcatExpression() |
|
|
458 |
{ |
|
|
459 |
$args = func_get_args(); |
|
|
460 |
return '(' . implode(' + ', $args) . ')'; |
|
|
461 |
} |
|
|
462 |
|
|
|
463 |
public function getListDatabasesSQL() |
|
|
464 |
{ |
|
|
465 |
return 'SELECT * FROM SYS.DATABASES'; |
|
|
466 |
} |
|
|
467 |
|
|
|
468 |
/** |
|
|
469 |
* @override |
|
|
470 |
*/ |
|
|
471 |
public function getSubstringExpression($value, $from, $len = null) |
|
|
472 |
{ |
|
|
473 |
if (!is_null($len)) { |
|
|
474 |
return 'SUBSTRING(' . $value . ', ' . $from . ', ' . $len . ')'; |
|
|
475 |
} |
|
|
476 |
return 'SUBSTRING(' . $value . ', ' . $from . ', LEN(' . $value . ') - ' . $from . ' + 1)'; |
|
|
477 |
} |
|
|
478 |
|
|
|
479 |
/** |
|
|
480 |
* @override |
|
|
481 |
*/ |
|
|
482 |
public function getLengthExpression($column) |
|
|
483 |
{ |
|
|
484 |
return 'LEN(' . $column . ')'; |
|
|
485 |
} |
|
|
486 |
|
|
|
487 |
/** |
|
|
488 |
* @override |
|
|
489 |
*/ |
|
|
490 |
public function getSetTransactionIsolationSQL($level) |
|
|
491 |
{ |
|
|
492 |
return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); |
|
|
493 |
} |
|
|
494 |
|
|
|
495 |
/** |
|
|
496 |
* @override |
|
|
497 |
*/ |
|
|
498 |
public function getIntegerTypeDeclarationSQL(array $field) |
|
|
499 |
{ |
|
|
500 |
return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
|
|
501 |
} |
|
|
502 |
|
|
|
503 |
/** |
|
|
504 |
* @override |
|
|
505 |
*/ |
|
|
506 |
public function getBigIntTypeDeclarationSQL(array $field) |
|
|
507 |
{ |
|
|
508 |
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
|
|
509 |
} |
|
|
510 |
|
|
|
511 |
/** |
|
|
512 |
* @override |
|
|
513 |
*/ |
|
|
514 |
public function getSmallIntTypeDeclarationSQL(array $field) |
|
|
515 |
{ |
|
|
516 |
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field); |
|
|
517 |
} |
|
|
518 |
|
|
|
519 |
/** @override */ |
|
|
520 |
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) |
|
|
521 |
{ |
|
|
522 |
return $fixed ? ($length ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NVARCHAR(255)'); |
|
|
523 |
} |
|
|
524 |
|
|
|
525 |
/** @override */ |
|
|
526 |
public function getClobTypeDeclarationSQL(array $field) |
|
|
527 |
{ |
|
|
528 |
return 'TEXT'; |
|
|
529 |
} |
|
|
530 |
|
|
|
531 |
/** |
|
|
532 |
* @override |
|
|
533 |
*/ |
|
|
534 |
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
|
|
535 |
{ |
|
|
536 |
$autoinc = ''; |
|
|
537 |
if (!empty($columnDef['autoincrement'])) { |
|
|
538 |
$autoinc = ' IDENTITY'; |
|
|
539 |
} |
|
|
540 |
$unsigned = (isset($columnDef['unsigned']) && $columnDef['unsigned']) ? ' UNSIGNED' : ''; |
|
|
541 |
|
|
|
542 |
return $unsigned . $autoinc; |
|
|
543 |
} |
|
|
544 |
|
|
|
545 |
/** |
|
|
546 |
* @override |
|
|
547 |
*/ |
|
|
548 |
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) |
|
|
549 |
{ |
|
|
550 |
// 6 - microseconds precision length |
|
|
551 |
return 'DATETIME2(6)'; |
|
|
552 |
} |
|
|
553 |
|
|
|
554 |
/** |
|
|
555 |
* @override |
|
|
556 |
*/ |
|
|
557 |
public function getDateTypeDeclarationSQL(array $fieldDeclaration) |
|
|
558 |
{ |
|
|
559 |
return 'DATE'; |
|
|
560 |
} |
|
|
561 |
|
|
|
562 |
/** |
|
|
563 |
* @override |
|
|
564 |
*/ |
|
|
565 |
public function getTimeTypeDeclarationSQL(array $fieldDeclaration) |
|
|
566 |
{ |
|
|
567 |
return 'TIME(0)'; |
|
|
568 |
} |
|
|
569 |
|
|
|
570 |
/** |
|
|
571 |
* @override |
|
|
572 |
*/ |
|
|
573 |
public function getBooleanTypeDeclarationSQL(array $field) |
|
|
574 |
{ |
|
|
575 |
return 'BIT'; |
|
|
576 |
} |
|
|
577 |
|
|
|
578 |
/** |
|
|
579 |
* Adds an adapter-specific LIMIT clause to the SELECT statement. |
|
|
580 |
* |
|
|
581 |
* @param string $query |
|
|
582 |
* @param mixed $limit |
|
|
583 |
* @param mixed $offset |
|
|
584 |
* @link http://lists.bestpractical.com/pipermail/rt-devel/2005-June/007339.html |
|
|
585 |
* @return string |
|
|
586 |
*/ |
|
|
587 |
protected function doModifyLimitQuery($query, $limit, $offset = null) |
|
|
588 |
{ |
|
|
589 |
if ($limit > 0) { |
|
|
590 |
$count = intval($limit); |
|
|
591 |
$offset = intval($offset); |
|
|
592 |
|
|
|
593 |
if ($offset < 0) { |
|
|
594 |
throw new DBALException("LIMIT argument offset=$offset is not valid"); |
|
|
595 |
} |
|
|
596 |
|
|
|
597 |
if ($offset == 0) { |
|
|
598 |
$query = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . $count . ' ', $query); |
|
|
599 |
} else { |
|
|
600 |
$orderby = stristr($query, 'ORDER BY'); |
|
|
601 |
|
|
|
602 |
if (!$orderby) { |
|
|
603 |
$over = 'ORDER BY (SELECT 0)'; |
|
|
604 |
} else { |
|
|
605 |
$over = preg_replace('/\"[^,]*\".\"([^,]*)\"/i', '"inner_tbl"."$1"', $orderby); |
|
|
606 |
} |
|
|
607 |
|
|
|
608 |
// Remove ORDER BY clause from $query |
|
|
609 |
$query = preg_replace('/\s+ORDER BY(.*)/', '', $query); |
|
|
610 |
$query = preg_replace('/^SELECT\s/', '', $query); |
|
|
611 |
|
|
|
612 |
$start = $offset + 1; |
|
|
613 |
$end = $offset + $count; |
|
|
614 |
|
|
|
615 |
$query = "SELECT * FROM (SELECT ROW_NUMBER() OVER ($over) AS \"doctrine_rownum\", $query) AS doctrine_tbl WHERE \"doctrine_rownum\" BETWEEN $start AND $end"; |
|
|
616 |
} |
|
|
617 |
} |
|
|
618 |
|
|
|
619 |
return $query; |
|
|
620 |
} |
|
|
621 |
|
|
|
622 |
/** |
|
|
623 |
* @override |
|
|
624 |
*/ |
|
|
625 |
public function convertBooleans($item) |
|
|
626 |
{ |
|
|
627 |
if (is_array($item)) { |
|
|
628 |
foreach ($item as $key => $value) { |
|
|
629 |
if (is_bool($value) || is_numeric($item)) { |
|
|
630 |
$item[$key] = ($value) ? 1 : 0; |
|
|
631 |
} |
|
|
632 |
} |
|
|
633 |
} else { |
|
|
634 |
if (is_bool($item) || is_numeric($item)) { |
|
|
635 |
$item = ($item) ? 1 : 0; |
|
|
636 |
} |
|
|
637 |
} |
|
|
638 |
return $item; |
|
|
639 |
} |
|
|
640 |
|
|
|
641 |
/** |
|
|
642 |
* @override |
|
|
643 |
*/ |
|
|
644 |
public function getCreateTemporaryTableSnippetSQL() |
|
|
645 |
{ |
|
|
646 |
return "CREATE TABLE"; |
|
|
647 |
} |
|
|
648 |
|
|
|
649 |
/** |
|
|
650 |
* @override |
|
|
651 |
*/ |
|
|
652 |
public function getTemporaryTableName($tableName) |
|
|
653 |
{ |
|
|
654 |
return '#' . $tableName; |
|
|
655 |
} |
|
|
656 |
|
|
|
657 |
/** |
|
|
658 |
* @override |
|
|
659 |
*/ |
|
|
660 |
public function getDateTimeFormatString() |
|
|
661 |
{ |
|
|
662 |
return 'Y-m-d H:i:s.u'; |
|
|
663 |
} |
|
|
664 |
|
|
|
665 |
/** |
|
|
666 |
* @override |
|
|
667 |
*/ |
|
|
668 |
public function getDateTimeTzFormatString() |
|
|
669 |
{ |
|
|
670 |
return $this->getDateTimeFormatString(); |
|
|
671 |
} |
|
|
672 |
|
|
|
673 |
/** |
|
|
674 |
* Get the platform name for this instance |
|
|
675 |
* |
|
|
676 |
* @return string |
|
|
677 |
*/ |
|
|
678 |
public function getName() |
|
|
679 |
{ |
|
|
680 |
return 'mssql'; |
|
|
681 |
} |
|
|
682 |
|
|
|
683 |
/** |
|
|
684 |
* @override |
|
|
685 |
*/ |
|
|
686 |
protected function initializeDoctrineTypeMappings() |
|
|
687 |
{ |
|
|
688 |
$this->doctrineTypeMapping = array( |
|
|
689 |
'bigint' => 'bigint', |
|
|
690 |
'numeric' => 'decimal', |
|
|
691 |
'bit' => 'boolean', |
|
|
692 |
'smallint' => 'smallint', |
|
|
693 |
'decimal' => 'decimal', |
|
|
694 |
'smallmoney' => 'integer', |
|
|
695 |
'int' => 'integer', |
|
|
696 |
'tinyint' => 'smallint', |
|
|
697 |
'money' => 'integer', |
|
|
698 |
'float' => 'float', |
|
|
699 |
'real' => 'float', |
|
|
700 |
'double' => 'float', |
|
|
701 |
'double precision' => 'float', |
|
|
702 |
'date' => 'date', |
|
|
703 |
'datetimeoffset' => 'datetimetz', |
|
|
704 |
'datetime2' => 'datetime', |
|
|
705 |
'smalldatetime' => 'datetime', |
|
|
706 |
'datetime' => 'datetime', |
|
|
707 |
'time' => 'time', |
|
|
708 |
'char' => 'string', |
|
|
709 |
'varchar' => 'string', |
|
|
710 |
'text' => 'text', |
|
|
711 |
'nchar' => 'string', |
|
|
712 |
'nvarchar' => 'string', |
|
|
713 |
'ntext' => 'text', |
|
|
714 |
'binary' => 'text', |
|
|
715 |
'varbinary' => 'text', |
|
|
716 |
'image' => 'text', |
|
|
717 |
); |
|
|
718 |
} |
|
|
719 |
|
|
|
720 |
/** |
|
|
721 |
* Generate SQL to create a new savepoint |
|
|
722 |
* |
|
|
723 |
* @param string $savepoint |
|
|
724 |
* @return string |
|
|
725 |
*/ |
|
|
726 |
public function createSavePoint($savepoint) |
|
|
727 |
{ |
|
|
728 |
return 'SAVE TRANSACTION ' . $savepoint; |
|
|
729 |
} |
|
|
730 |
|
|
|
731 |
/** |
|
|
732 |
* Generate SQL to release a savepoint |
|
|
733 |
* |
|
|
734 |
* @param string $savepoint |
|
|
735 |
* @return string |
|
|
736 |
*/ |
|
|
737 |
public function releaseSavePoint($savepoint) |
|
|
738 |
{ |
|
|
739 |
return ''; |
|
|
740 |
} |
|
|
741 |
|
|
|
742 |
/** |
|
|
743 |
* Generate SQL to rollback a savepoint |
|
|
744 |
* |
|
|
745 |
* @param string $savepoint |
|
|
746 |
* @return string |
|
|
747 |
*/ |
|
|
748 |
public function rollbackSavePoint($savepoint) |
|
|
749 |
{ |
|
|
750 |
return 'ROLLBACK TRANSACTION ' . $savepoint; |
|
|
751 |
} |
|
|
752 |
|
|
|
753 |
/** |
|
|
754 |
* @override |
|
|
755 |
*/ |
|
|
756 |
public function appendLockHint($fromClause, $lockMode) |
|
|
757 |
{ |
|
|
758 |
// @todo coorect |
|
|
759 |
if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_READ) { |
|
|
760 |
return $fromClause . ' WITH (tablockx)'; |
|
|
761 |
} else if ($lockMode == \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE) { |
|
|
762 |
return $fromClause . ' WITH (tablockx)'; |
|
|
763 |
} else { |
|
|
764 |
return $fromClause; |
|
|
765 |
} |
|
|
766 |
} |
|
|
767 |
|
|
|
768 |
/** |
|
|
769 |
* @override |
|
|
770 |
*/ |
|
|
771 |
public function getForUpdateSQL() |
|
|
772 |
{ |
|
|
773 |
return ' '; |
|
|
774 |
} |
|
|
775 |
|
|
|
776 |
protected function getReservedKeywordsClass() |
|
|
777 |
{ |
|
|
778 |
return 'Doctrine\DBAL\Platforms\Keywords\MsSQLKeywords'; |
|
|
779 |
} |
|
|
780 |
|
|
|
781 |
/** |
|
|
782 |
* Quotes a string so that it can be safely used as a table or column name, |
|
|
783 |
* even if it is a reserved word of the platform. |
|
|
784 |
* |
|
|
785 |
* NOTE: Just because you CAN use quoted identifiers doesn't mean |
|
|
786 |
* you SHOULD use them. In general, they end up causing way more |
|
|
787 |
* problems than they solve. |
|
|
788 |
* |
|
|
789 |
* @param string $str identifier name to be quoted |
|
|
790 |
* @return string quoted identifier string |
|
|
791 |
*/ |
|
|
792 |
public function quoteIdentifier($str) |
|
|
793 |
{ |
|
|
794 |
return "[" . $str . "]"; |
|
|
795 |
} |
|
|
796 |
} |