1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DBALException; |
6
|
|
|
use Doctrine\DBAL\Driver\DriverException; |
7
|
|
|
use Doctrine\DBAL\Platforms\SQLServer2012Platform; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
use PDOException; |
10
|
|
|
use Throwable; |
11
|
|
|
use function assert; |
12
|
|
|
use function count; |
13
|
|
|
use function is_string; |
14
|
|
|
use function preg_match; |
15
|
|
|
use function sprintf; |
16
|
|
|
use function str_replace; |
17
|
|
|
use function strpos; |
18
|
|
|
use function strtok; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* SQL Server Schema Manager. |
22
|
|
|
*/ |
23
|
|
|
class SQLServerSchemaManager extends AbstractSchemaManager |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function dropDatabase($database) |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
|
|
parent::dropDatabase($database); |
32
|
|
|
} catch (DBALException $exception) { |
33
|
|
|
$exception = $exception->getPrevious(); |
34
|
|
|
assert($exception instanceof Throwable); |
35
|
|
|
|
36
|
|
|
if (! $exception instanceof DriverException) { |
37
|
|
|
throw $exception; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// If we have a error code 3702, the drop database operation failed |
41
|
|
|
// because of active connections on the database. |
42
|
|
|
// To force dropping the database, we first have to close all active connections |
43
|
|
|
// on that database and issue the drop database operation again. |
44
|
|
|
if ($exception->getErrorCode() !== 3702) { |
45
|
|
|
throw $exception; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->closeActiveDatabaseConnections($database); |
49
|
|
|
|
50
|
|
|
parent::dropDatabase($database); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function _getPortableSequenceDefinition($sequence) |
58
|
|
|
{ |
59
|
|
|
return new Sequence($sequence['name'], (int) $sequence['increment'], (int) $sequence['start_value']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
23 |
|
protected function _getPortableTableColumnDefinition($tableColumn) |
66
|
|
|
{ |
67
|
23 |
|
$dbType = strtok($tableColumn['type'], '(), '); |
68
|
23 |
|
assert(is_string($dbType)); |
69
|
|
|
|
70
|
23 |
|
$fixed = null; |
71
|
23 |
|
$length = (int) $tableColumn['length']; |
72
|
23 |
|
$default = $tableColumn['default']; |
73
|
|
|
|
74
|
23 |
|
if (! isset($tableColumn['name'])) { |
75
|
23 |
|
$tableColumn['name'] = ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
23 |
|
if ($default !== null) { |
79
|
|
|
$default = $this->parseDefaultExpression($default); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
switch ($dbType) { |
83
|
23 |
|
case 'nchar': |
84
|
23 |
|
case 'nvarchar': |
85
|
23 |
|
case 'ntext': |
86
|
|
|
// Unicode data requires 2 bytes per character |
87
|
|
|
$length /= 2; |
88
|
|
|
break; |
89
|
23 |
|
case 'varchar': |
90
|
|
|
// TEXT type is returned as VARCHAR(MAX) with a length of -1 |
91
|
|
|
if ($length === -1) { |
92
|
|
|
$dbType = 'text'; |
93
|
|
|
} |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
|
97
|
23 |
|
if ($dbType === 'char' || $dbType === 'nchar' || $dbType === 'binary') { |
98
|
|
|
$fixed = true; |
99
|
|
|
} |
100
|
|
|
|
101
|
23 |
|
$type = $this->_platform->getDoctrineTypeMapping($dbType); |
102
|
23 |
|
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
103
|
23 |
|
$tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
104
|
|
|
|
105
|
|
|
$options = [ |
106
|
23 |
|
'unsigned' => false, |
107
|
23 |
|
'fixed' => (bool) $fixed, |
108
|
23 |
|
'default' => $default, |
109
|
23 |
|
'notnull' => (bool) $tableColumn['notnull'], |
110
|
23 |
|
'scale' => $tableColumn['scale'], |
111
|
23 |
|
'precision' => $tableColumn['precision'], |
112
|
23 |
|
'autoincrement' => (bool) $tableColumn['autoincrement'], |
113
|
23 |
|
'comment' => $tableColumn['comment'] !== '' ? $tableColumn['comment'] : null, |
114
|
|
|
]; |
115
|
|
|
|
116
|
23 |
|
if ($length !== 0 && ($type === 'text' || $type === 'string')) { |
117
|
|
|
$options['length'] = $length; |
118
|
|
|
} |
119
|
|
|
|
120
|
23 |
|
$column = new Column($tableColumn['name'], Type::getType($type), $options); |
121
|
|
|
|
122
|
23 |
|
if (isset($tableColumn['collation']) && $tableColumn['collation'] !== 'NULL') { |
123
|
23 |
|
$column->setPlatformOption('collation', $tableColumn['collation']); |
124
|
|
|
} |
125
|
|
|
|
126
|
23 |
|
return $column; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function parseDefaultExpression(string $value) : ?string |
130
|
|
|
{ |
131
|
|
|
while (preg_match('/^\((.*)\)$/s', $value, $matches)) { |
132
|
|
|
$value = $matches[1]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($value === 'NULL') { |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (preg_match('/^\'(.*)\'$/s', $value, $matches) === 1) { |
140
|
|
|
$value = str_replace("''", "'", $matches[1]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($value === 'getdate()') { |
144
|
|
|
return $this->_platform->getCurrentTimestampSQL(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $value; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
protected function _getPortableTableForeignKeysList($tableForeignKeys) |
154
|
|
|
{ |
155
|
|
|
$foreignKeys = []; |
156
|
|
|
|
157
|
|
|
foreach ($tableForeignKeys as $tableForeignKey) { |
158
|
|
|
if (! isset($foreignKeys[$tableForeignKey['ForeignKey']])) { |
159
|
|
|
$foreignKeys[$tableForeignKey['ForeignKey']] = [ |
160
|
|
|
'local_columns' => [$tableForeignKey['ColumnName']], |
161
|
|
|
'foreign_table' => $tableForeignKey['ReferenceTableName'], |
162
|
|
|
'foreign_columns' => [$tableForeignKey['ReferenceColumnName']], |
163
|
|
|
'name' => $tableForeignKey['ForeignKey'], |
164
|
|
|
'options' => [ |
165
|
|
|
'onUpdate' => str_replace('_', ' ', $tableForeignKey['update_referential_action_desc']), |
166
|
|
|
'onDelete' => str_replace('_', ' ', $tableForeignKey['delete_referential_action_desc']), |
167
|
|
|
], |
168
|
|
|
]; |
169
|
|
|
} else { |
170
|
|
|
$foreignKeys[$tableForeignKey['ForeignKey']]['local_columns'][] = $tableForeignKey['ColumnName']; |
171
|
|
|
$foreignKeys[$tableForeignKey['ForeignKey']]['foreign_columns'][] = $tableForeignKey['ReferenceColumnName']; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return parent::_getPortableTableForeignKeysList($foreignKeys); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null) |
182
|
|
|
{ |
183
|
|
|
foreach ($tableIndexRows as &$tableIndex) { |
184
|
|
|
$tableIndex['non_unique'] = (bool) $tableIndex['non_unique']; |
185
|
|
|
$tableIndex['primary'] = (bool) $tableIndex['primary']; |
186
|
|
|
$tableIndex['flags'] = $tableIndex['flags'] ? [$tableIndex['flags']] : null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return parent::_getPortableTableIndexesList($tableIndexRows, $tableName); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
|
|
protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
196
|
|
|
{ |
197
|
|
|
return new ForeignKeyConstraint( |
198
|
|
|
$tableForeignKey['local_columns'], |
199
|
|
|
$tableForeignKey['foreign_table'], |
200
|
|
|
$tableForeignKey['foreign_columns'], |
201
|
|
|
$tableForeignKey['name'], |
202
|
|
|
$tableForeignKey['options'] |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritdoc} |
208
|
|
|
*/ |
209
|
|
|
protected function _getPortableTableDefinition($table) |
210
|
|
|
{ |
211
|
|
|
if (isset($table['schema_name']) && $table['schema_name'] !== 'dbo') { |
212
|
|
|
return $table['schema_name'] . '.' . $table['name']; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $table['name']; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritdoc} |
220
|
|
|
*/ |
221
|
|
|
protected function _getPortableDatabaseDefinition($database) |
222
|
|
|
{ |
223
|
|
|
return $database['name']; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* {@inheritdoc} |
228
|
|
|
*/ |
229
|
|
|
protected function getPortableNamespaceDefinition(array $namespace) |
230
|
|
|
{ |
231
|
|
|
return $namespace['name']; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* {@inheritdoc} |
236
|
|
|
*/ |
237
|
|
|
protected function _getPortableViewDefinition($view) |
238
|
|
|
{ |
239
|
|
|
// @todo |
240
|
|
|
return new View($view['name'], ''); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
|
|
public function listTableIndexes($table) |
247
|
|
|
{ |
248
|
|
|
$sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase()); |
249
|
|
|
|
250
|
|
|
try { |
251
|
|
|
$tableIndexes = $this->_conn->fetchAll($sql); |
252
|
|
|
} catch (PDOException $e) { |
253
|
|
|
if ($e->getCode() === 'IMSSP') { |
254
|
|
|
return []; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
throw $e; |
258
|
|
|
} catch (DBALException $e) { |
259
|
|
|
if (strpos($e->getMessage(), 'SQLSTATE [01000, 15472]') === 0) { |
260
|
|
|
return []; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
throw $e; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $this->_getPortableTableIndexesList($tableIndexes, $table); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* {@inheritdoc} |
271
|
|
|
*/ |
272
|
|
|
public function alterTable(TableDiff $tableDiff) |
273
|
|
|
{ |
274
|
|
|
if (count($tableDiff->removedColumns) > 0) { |
275
|
|
|
foreach ($tableDiff->removedColumns as $col) { |
276
|
|
|
$columnConstraintSql = $this->getColumnConstraintSQL($tableDiff->name, $col->getName()); |
277
|
|
|
foreach ($this->_conn->fetchAll($columnConstraintSql) as $constraint) { |
278
|
|
|
$this->_conn->exec( |
279
|
|
|
sprintf( |
280
|
|
|
'ALTER TABLE %s DROP CONSTRAINT %s', |
281
|
|
|
$tableDiff->name, |
282
|
|
|
$constraint['Name'] |
283
|
|
|
) |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
parent::alterTable($tableDiff); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Returns the SQL to retrieve the constraints for a given column. |
294
|
|
|
* |
295
|
|
|
* @param string $table |
296
|
|
|
* @param string $column |
297
|
|
|
* |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
private function getColumnConstraintSQL($table, $column) |
301
|
|
|
{ |
302
|
|
|
return "SELECT SysObjects.[Name] |
303
|
|
|
FROM SysObjects INNER JOIN (SELECT [Name],[ID] FROM SysObjects WHERE XType = 'U') AS Tab |
304
|
|
|
ON Tab.[ID] = Sysobjects.[Parent_Obj] |
305
|
|
|
INNER JOIN sys.default_constraints DefCons ON DefCons.[object_id] = Sysobjects.[ID] |
306
|
|
|
INNER JOIN SysColumns Col ON Col.[ColID] = DefCons.[parent_column_id] AND Col.[ID] = Tab.[ID] |
307
|
|
|
WHERE Col.[Name] = " . $this->_conn->quote($column) . ' AND Tab.[Name] = ' . $this->_conn->quote($table) . ' |
308
|
|
|
ORDER BY Col.[Name]'; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Closes currently active connections on the given database. |
313
|
|
|
* |
314
|
|
|
* This is useful to force DROP DATABASE operations which could fail because of active connections. |
315
|
|
|
* |
316
|
|
|
* @param string $database The name of the database to close currently active connections for. |
317
|
|
|
* |
318
|
|
|
* @return void |
319
|
|
|
*/ |
320
|
|
|
private function closeActiveDatabaseConnections($database) |
321
|
|
|
{ |
322
|
|
|
$database = new Identifier($database); |
323
|
|
|
|
324
|
|
|
$this->_execSql( |
325
|
|
|
sprintf( |
326
|
|
|
'ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE', |
327
|
|
|
$database->getQuotedName($this->_platform) |
328
|
|
|
) |
329
|
|
|
); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param string $tableName |
334
|
|
|
*/ |
335
|
|
|
public function listTableDetails($tableName) : Table |
336
|
|
|
{ |
337
|
|
|
$table = parent::listTableDetails($tableName); |
338
|
|
|
|
339
|
|
|
/** @var SQLServer2012Platform $platform */ |
340
|
|
|
$platform = $this->_platform; |
341
|
|
|
$sql = $platform->getListTableMetadataSQL($tableName); |
342
|
|
|
|
343
|
|
|
$tableOptions = $this->_conn->fetchAssoc($sql); |
344
|
|
|
|
345
|
|
|
if ($tableOptions !== false) { |
346
|
|
|
$table->addOption('comment', $tableOptions['table_comment']); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
return $table; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|