_getPortableTableColumnDefinition()   F
last analyzed

Complexity

Conditions 19
Paths 1536

Size

Total Lines 65
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 19

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 65
ccs 40
cts 40
cp 1
rs 0.3499
c 0
b 0
f 0
cc 19
nc 1536
nop 1
crap 19

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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