Total Complexity | 53 |
Total Lines | 327 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SQLServerSchemaManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SQLServerSchemaManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class SQLServerSchemaManager extends AbstractSchemaManager |
||
24 | { |
||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | public function dropDatabase($database) |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | protected function _getPortableSequenceDefinition($sequence) |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | protected function _getPortableTableColumnDefinition($tableColumn) |
||
66 | { |
||
67 | $dbType = strtok($tableColumn['type'], '(), '); |
||
68 | assert(is_string($dbType)); |
||
69 | |||
70 | $fixed = null; |
||
71 | $length = (int) $tableColumn['length']; |
||
72 | $default = $tableColumn['default']; |
||
73 | |||
74 | if (! isset($tableColumn['name'])) { |
||
75 | $tableColumn['name'] = ''; |
||
76 | } |
||
77 | |||
78 | if ($default !== null) { |
||
79 | $default = $this->parseDefaultExpression($default); |
||
80 | } |
||
81 | |||
82 | switch ($dbType) { |
||
83 | case 'nchar': |
||
84 | case 'nvarchar': |
||
85 | case 'ntext': |
||
86 | // Unicode data requires 2 bytes per character |
||
87 | $length /= 2; |
||
88 | break; |
||
89 | 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 | if ($dbType === 'char' || $dbType === 'nchar' || $dbType === 'binary') { |
||
98 | $fixed = true; |
||
99 | } |
||
100 | |||
101 | $type = $this->_platform->getDoctrineTypeMapping($dbType); |
||
102 | $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
||
103 | $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
||
104 | |||
105 | $options = [ |
||
106 | 'unsigned' => false, |
||
107 | 'fixed' => (bool) $fixed, |
||
108 | 'default' => $default, |
||
109 | 'notnull' => (bool) $tableColumn['notnull'], |
||
110 | 'scale' => $tableColumn['scale'], |
||
111 | 'precision' => $tableColumn['precision'], |
||
112 | 'autoincrement' => (bool) $tableColumn['autoincrement'], |
||
113 | 'comment' => $tableColumn['comment'] !== '' ? $tableColumn['comment'] : null, |
||
114 | ]; |
||
115 | |||
116 | if ($length !== 0 && ($type === 'text' || $type === 'string')) { |
||
117 | $options['length'] = $length; |
||
118 | } |
||
119 | |||
120 | $column = new Column($tableColumn['name'], Type::getType($type), $options); |
||
121 | |||
122 | if (isset($tableColumn['collation']) && $tableColumn['collation'] !== 'NULL') { |
||
123 | $column->setPlatformOption('collation', $tableColumn['collation']); |
||
124 | } |
||
125 | |||
126 | 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) |
||
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) |
||
328 | ) |
||
329 | ); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param string $tableName |
||
334 | */ |
||
335 | public function listTableDetails($tableName) : Table |
||
350 | } |
||
351 | } |
||
352 |