Completed
Pull Request — develop (#3524)
by Jonathan
95:41 queued 92:25
created

_getPortableTableForeignKeysList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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