1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Schema; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\DBALException; |
23
|
|
|
use Doctrine\DBAL\Driver\DriverException; |
24
|
|
|
use Doctrine\DBAL\Types\Type; |
25
|
|
|
use function count; |
26
|
|
|
use function in_array; |
27
|
|
|
use function preg_replace; |
28
|
|
|
use function sprintf; |
29
|
|
|
use function str_replace; |
30
|
|
|
use function strpos; |
31
|
|
|
use function strtok; |
32
|
|
|
use function trim; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* SQL Server Schema Manager. |
36
|
|
|
* |
37
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
38
|
|
|
* @author Konsta Vesterinen <[email protected]> |
39
|
|
|
* @author Lukas Smith <[email protected]> (PEAR MDB2 library) |
40
|
|
|
* @author Juozas Kaziukenas <[email protected]> |
41
|
|
|
* @author Steve Müller <[email protected]> |
42
|
|
|
* @since 2.0 |
43
|
|
|
*/ |
44
|
|
|
class SQLServerSchemaManager extends AbstractSchemaManager |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
4 |
|
public function dropDatabase($database) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
4 |
|
parent::dropDatabase($database); |
53
|
4 |
|
} catch (DBALException $exception) { |
54
|
4 |
|
$exception = $exception->getPrevious(); |
55
|
|
|
|
56
|
4 |
|
if (! $exception instanceof DriverException) { |
57
|
|
|
throw $exception; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// If we have a error code 3702, the drop database operation failed |
61
|
|
|
// because of active connections on the database. |
62
|
|
|
// To force dropping the database, we first have to close all active connections |
63
|
|
|
// on that database and issue the drop database operation again. |
64
|
4 |
|
if ($exception->getErrorCode() !== 3702) { |
65
|
4 |
|
throw $exception; |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
$this->closeActiveDatabaseConnections($database); |
69
|
|
|
|
70
|
2 |
|
parent::dropDatabase($database); |
71
|
|
|
} |
72
|
2 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
10 |
|
protected function _getPortableSequenceDefinition($sequence) |
78
|
|
|
{ |
79
|
10 |
|
return new Sequence($sequence['name'], $sequence['increment'], $sequence['start_value']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
89 |
|
protected function _getPortableTableColumnDefinition($tableColumn) |
86
|
|
|
{ |
87
|
89 |
|
$dbType = strtok($tableColumn['type'], '(), '); |
88
|
89 |
|
$fixed = null; |
89
|
89 |
|
$length = (int) $tableColumn['length']; |
90
|
89 |
|
$default = $tableColumn['default']; |
91
|
|
|
|
92
|
89 |
|
if (!isset($tableColumn['name'])) { |
93
|
17 |
|
$tableColumn['name'] = ''; |
94
|
|
|
} |
95
|
|
|
|
96
|
89 |
|
while ($default != ($default2 = preg_replace("/^\((.*)\)$/", '$1', $default))) { |
97
|
16 |
|
$default = trim($default2, "'"); |
98
|
|
|
|
99
|
16 |
|
if ($default == 'getdate()') { |
100
|
2 |
|
$default = $this->_platform->getCurrentTimestampSQL(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
switch ($dbType) { |
105
|
89 |
|
case 'nchar': |
106
|
87 |
|
case 'nvarchar': |
107
|
85 |
|
case 'ntext': |
108
|
|
|
// Unicode data requires 2 bytes per character |
109
|
30 |
|
$length = $length / 2; |
110
|
30 |
|
break; |
111
|
85 |
|
case 'varchar': |
112
|
|
|
// TEXT type is returned as VARCHAR(MAX) with a length of -1 |
113
|
18 |
|
if ($length == -1) { |
114
|
18 |
|
$dbType = 'text'; |
115
|
|
|
} |
116
|
18 |
|
break; |
117
|
|
|
} |
118
|
|
|
|
119
|
89 |
|
if ('char' === $dbType || 'nchar' === $dbType || 'binary' === $dbType) { |
120
|
12 |
|
$fixed = true; |
121
|
|
|
} |
122
|
|
|
|
123
|
89 |
|
$type = $this->_platform->getDoctrineTypeMapping($dbType); |
124
|
89 |
|
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
125
|
89 |
|
$tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
126
|
|
|
|
127
|
|
|
$options = [ |
128
|
89 |
|
'length' => ($length == 0 || !in_array($type, ['text', 'string'])) ? null : $length, |
129
|
|
|
'unsigned' => false, |
130
|
89 |
|
'fixed' => (bool) $fixed, |
131
|
89 |
|
'default' => $default !== 'NULL' ? $default : null, |
132
|
89 |
|
'notnull' => (bool) $tableColumn['notnull'], |
133
|
89 |
|
'scale' => $tableColumn['scale'], |
134
|
89 |
|
'precision' => $tableColumn['precision'], |
135
|
89 |
|
'autoincrement' => (bool) $tableColumn['autoincrement'], |
136
|
89 |
|
'comment' => $tableColumn['comment'] !== '' ? $tableColumn['comment'] : null, |
137
|
|
|
]; |
138
|
|
|
|
139
|
89 |
|
$column = new Column($tableColumn['name'], Type::getType($type), $options); |
140
|
|
|
|
141
|
89 |
|
if (isset($tableColumn['collation']) && $tableColumn['collation'] !== 'NULL') { |
142
|
53 |
|
$column->setPlatformOption('collation', $tableColumn['collation']); |
143
|
|
|
} |
144
|
|
|
|
145
|
89 |
|
return $column; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
50 |
|
protected function _getPortableTableForeignKeysList($tableForeignKeys) |
152
|
|
|
{ |
153
|
50 |
|
$foreignKeys = []; |
154
|
|
|
|
155
|
50 |
|
foreach ($tableForeignKeys as $tableForeignKey) { |
156
|
16 |
|
if ( ! isset($foreignKeys[$tableForeignKey['ForeignKey']])) { |
157
|
16 |
|
$foreignKeys[$tableForeignKey['ForeignKey']] = [ |
158
|
16 |
|
'local_columns' => [$tableForeignKey['ColumnName']], |
159
|
16 |
|
'foreign_table' => $tableForeignKey['ReferenceTableName'], |
160
|
16 |
|
'foreign_columns' => [$tableForeignKey['ReferenceColumnName']], |
161
|
16 |
|
'name' => $tableForeignKey['ForeignKey'], |
162
|
|
|
'options' => [ |
163
|
16 |
|
'onUpdate' => str_replace('_', ' ', $tableForeignKey['update_referential_action_desc']), |
164
|
16 |
|
'onDelete' => str_replace('_', ' ', $tableForeignKey['delete_referential_action_desc']) |
165
|
|
|
] |
166
|
|
|
]; |
167
|
|
|
} else { |
168
|
2 |
|
$foreignKeys[$tableForeignKey['ForeignKey']]['local_columns'][] = $tableForeignKey['ColumnName']; |
169
|
16 |
|
$foreignKeys[$tableForeignKey['ForeignKey']]['foreign_columns'][] = $tableForeignKey['ReferenceColumnName']; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
50 |
|
return parent::_getPortableTableForeignKeysList($foreignKeys); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
56 |
|
protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null) |
180
|
|
|
{ |
181
|
56 |
|
foreach ($tableIndexRows as &$tableIndex) { |
182
|
34 |
|
$tableIndex['non_unique'] = (boolean) $tableIndex['non_unique']; |
183
|
34 |
|
$tableIndex['primary'] = (boolean) $tableIndex['primary']; |
184
|
34 |
|
$tableIndex['flags'] = $tableIndex['flags'] ? [$tableIndex['flags']] : null; |
185
|
|
|
} |
186
|
|
|
|
187
|
56 |
|
return parent::_getPortableTableIndexesList($tableIndexRows, $tableName); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
16 |
|
protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
194
|
|
|
{ |
195
|
16 |
|
return new ForeignKeyConstraint( |
196
|
16 |
|
$tableForeignKey['local_columns'], |
197
|
16 |
|
$tableForeignKey['foreign_table'], |
198
|
16 |
|
$tableForeignKey['foreign_columns'], |
199
|
16 |
|
$tableForeignKey['name'], |
200
|
16 |
|
$tableForeignKey['options'] |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
130 |
|
protected function _getPortableTableDefinition($table) |
208
|
|
|
{ |
209
|
130 |
|
if (isset($table['schema_name']) && $table['schema_name'] !== 'dbo') { |
210
|
2 |
|
return $table['schema_name'] . '.' . $table['name']; |
211
|
|
|
} |
212
|
|
|
|
213
|
130 |
|
return $table['name']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritdoc} |
218
|
|
|
*/ |
219
|
4 |
|
protected function _getPortableDatabaseDefinition($database) |
220
|
|
|
{ |
221
|
4 |
|
return $database['name']; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
4 |
|
protected function getPortableNamespaceDefinition(array $namespace) |
228
|
|
|
{ |
229
|
4 |
|
return $namespace['name']; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
2 |
|
protected function _getPortableViewDefinition($view) |
236
|
|
|
{ |
237
|
|
|
// @todo |
238
|
2 |
|
return new View($view['name'], null); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
56 |
|
public function listTableIndexes($table) |
245
|
|
|
{ |
246
|
56 |
|
$sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase()); |
247
|
|
|
|
248
|
|
|
try { |
249
|
56 |
|
$tableIndexes = $this->_conn->fetchAll($sql); |
250
|
|
|
} catch (\PDOException $e) { |
251
|
|
|
if ($e->getCode() == "IMSSP") { |
252
|
|
|
return []; |
253
|
|
|
} else { |
254
|
|
|
throw $e; |
255
|
|
|
} |
256
|
|
|
} catch (DBALException $e) { |
257
|
|
|
if (strpos($e->getMessage(), 'SQLSTATE [01000, 15472]') === 0) { |
258
|
|
|
return []; |
259
|
|
|
} else { |
260
|
|
|
throw $e; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
56 |
|
return $this->_getPortableTableIndexesList($tableIndexes, $table); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
36 |
|
public function alterTable(TableDiff $tableDiff) |
271
|
|
|
{ |
272
|
36 |
|
if (count($tableDiff->removedColumns) > 0) { |
273
|
8 |
|
foreach ($tableDiff->removedColumns as $col) { |
274
|
8 |
|
$columnConstraintSql = $this->getColumnConstraintSQL($tableDiff->name, $col->getName()); |
275
|
8 |
|
foreach ($this->_conn->fetchAll($columnConstraintSql) as $constraint) { |
276
|
8 |
|
$this->_conn->exec("ALTER TABLE $tableDiff->name DROP CONSTRAINT " . $constraint['Name']); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
36 |
|
parent::alterTable($tableDiff); |
282
|
36 |
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Returns the SQL to retrieve the constraints for a given column. |
286
|
|
|
* |
287
|
|
|
* @param string $table |
288
|
|
|
* @param string $column |
289
|
|
|
* |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
8 |
|
private function getColumnConstraintSQL($table, $column) |
293
|
|
|
{ |
294
|
|
|
return "SELECT SysObjects.[Name] |
295
|
|
|
FROM SysObjects INNER JOIN (SELECT [Name],[ID] FROM SysObjects WHERE XType = 'U') AS Tab |
296
|
|
|
ON Tab.[ID] = Sysobjects.[Parent_Obj] |
297
|
|
|
INNER JOIN sys.default_constraints DefCons ON DefCons.[object_id] = Sysobjects.[ID] |
298
|
|
|
INNER JOIN SysColumns Col ON Col.[ColID] = DefCons.[parent_column_id] AND Col.[ID] = Tab.[ID] |
299
|
8 |
|
WHERE Col.[Name] = " . $this->_conn->quote($column) ." AND Tab.[Name] = " . $this->_conn->quote($table) . " |
300
|
|
|
ORDER BY Col.[Name]"; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Closes currently active connections on the given database. |
305
|
|
|
* |
306
|
|
|
* This is useful to force DROP DATABASE operations which could fail because of active connections. |
307
|
|
|
* |
308
|
|
|
* @param string $database The name of the database to close currently active connections for. |
309
|
|
|
* |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
2 |
|
private function closeActiveDatabaseConnections($database) |
313
|
|
|
{ |
314
|
2 |
|
$database = new Identifier($database); |
315
|
|
|
|
316
|
2 |
|
$this->_execSql( |
317
|
2 |
|
sprintf( |
318
|
2 |
|
'ALTER DATABASE %s SET SINGLE_USER WITH ROLLBACK IMMEDIATE', |
319
|
2 |
|
$database->getQuotedName($this->_platform) |
320
|
|
|
) |
321
|
|
|
); |
322
|
2 |
|
} |
323
|
|
|
} |
324
|
|
|
|