| Total Complexity | 44 |
| Total Lines | 297 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractDDLQueryBuilder 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 AbstractDDLQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | abstract class AbstractDDLQueryBuilder implements DDLQueryBuilderInterface |
||
| 26 | { |
||
| 27 | public function __construct( |
||
| 28 | protected QueryBuilderInterface $queryBuilder, |
||
| 29 | protected QuoterInterface $quoter, |
||
| 30 | protected SchemaInterface $schema |
||
| 31 | ) { |
||
| 32 | } |
||
| 33 | |||
| 34 | public function addCheck(string $table, string $name, string $expression): string |
||
| 35 | { |
||
| 36 | return 'ALTER TABLE ' |
||
| 37 | . $this->quoter->quoteTableName($table) |
||
| 38 | . ' ADD CONSTRAINT ' |
||
| 39 | . $this->quoter->quoteColumnName($name) |
||
| 40 | . ' CHECK (' . $this->quoter->quoteSql($expression) . ')'; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function addColumn(string $table, string $column, string $type): string |
||
| 44 | { |
||
| 45 | return 'ALTER TABLE ' |
||
| 46 | . $this->quoter->quoteTableName($table) |
||
| 47 | . ' ADD ' |
||
| 48 | . $this->quoter->quoteColumnName($column) |
||
| 49 | . ' ' |
||
| 50 | . $this->queryBuilder->getColumnType($type); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function addCommentOnColumn(string $table, string $column, string $comment): string |
||
| 54 | { |
||
| 55 | return 'COMMENT ON COLUMN ' |
||
| 56 | . $this->quoter->quoteTableName($table) |
||
| 57 | . '.' |
||
| 58 | . $this->quoter->quoteColumnName($column) |
||
| 59 | . ' IS ' |
||
| 60 | . (string) $this->quoter->quoteValue($comment); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function addCommentOnTable(string $table, string $comment): string |
||
| 64 | { |
||
| 65 | return 'COMMENT ON TABLE ' |
||
| 66 | . $this->quoter->quoteTableName($table) |
||
| 67 | . ' IS ' |
||
| 68 | . (string) $this->quoter->quoteValue($comment); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function addDefaultValue(string $table, string $name, string $column, mixed $value): string |
||
| 72 | { |
||
| 73 | throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.'); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function addForeignKey( |
||
| 77 | string $table, |
||
| 78 | string $name, |
||
| 79 | array|string $columns, |
||
| 80 | string $referenceTable, |
||
| 81 | array|string $referenceColumns, |
||
| 82 | string|null $delete = null, |
||
| 83 | string|null $update = null |
||
| 84 | ): string { |
||
| 85 | $sql = 'ALTER TABLE ' |
||
| 86 | . $this->quoter->quoteTableName($table) |
||
| 87 | . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) |
||
| 88 | . ' FOREIGN KEY (' . $this->queryBuilder->buildColumns($columns) . ')' |
||
| 89 | . ' REFERENCES ' . $this->quoter->quoteTableName($referenceTable) |
||
| 90 | . ' (' . $this->queryBuilder->buildColumns($referenceColumns) . ')'; |
||
| 91 | |||
| 92 | if ($delete !== null) { |
||
| 93 | $sql .= ' ON DELETE ' . $delete; |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($update !== null) { |
||
| 97 | $sql .= ' ON UPDATE ' . $update; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $sql; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function addPrimaryKey(string $table, string $name, array|string $columns): string |
||
| 104 | { |
||
| 105 | if (is_string($columns)) { |
||
|
|
|||
| 106 | $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** @psalm-var string[] $columns */ |
||
| 110 | foreach ($columns as $i => $col) { |
||
| 111 | $columns[$i] = $this->quoter->quoteColumnName($col); |
||
| 112 | } |
||
| 113 | |||
| 114 | return 'ALTER TABLE ' |
||
| 115 | . $this->quoter->quoteTableName($table) |
||
| 116 | . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) |
||
| 117 | . ' PRIMARY KEY (' . implode(', ', $columns) . ')'; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function addUnique(string $table, string $name, array|string $columns): string |
||
| 121 | { |
||
| 122 | if (is_string($columns)) { |
||
| 123 | $columns = preg_split('/\s*,\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** @psalm-var string[] $columns */ |
||
| 127 | foreach ($columns as $i => $col) { |
||
| 128 | $columns[$i] = $this->quoter->quoteColumnName($col); |
||
| 129 | } |
||
| 130 | |||
| 131 | return 'ALTER TABLE ' |
||
| 132 | . $this->quoter->quoteTableName($table) |
||
| 133 | . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) |
||
| 134 | . ' UNIQUE (' . implode(', ', $columns) . ')'; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function alterColumn( |
||
| 138 | string $table, |
||
| 139 | string $column, |
||
| 140 | ColumnInterface|string $type |
||
| 141 | ): string { |
||
| 142 | return 'ALTER TABLE ' |
||
| 143 | . $this->quoter->quoteTableName($table) |
||
| 144 | . ' CHANGE ' |
||
| 145 | . $this->quoter->quoteColumnName($column) |
||
| 146 | . ' ' |
||
| 147 | . $this->quoter->quoteColumnName($column) . ' ' |
||
| 148 | . $this->queryBuilder->getColumnType($type); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
||
| 152 | { |
||
| 153 | throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.'); |
||
| 154 | } |
||
| 155 | |||
| 156 | public function createIndex( |
||
| 157 | string $table, |
||
| 158 | string $name, |
||
| 159 | array|string $columns, |
||
| 160 | string $indexType = null, |
||
| 161 | string $indexMethod = null |
||
| 162 | ): string { |
||
| 163 | return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX ' |
||
| 164 | . $this->quoter->quoteTableName($name) |
||
| 165 | . ' ON ' . $this->quoter->quoteTableName($table) |
||
| 166 | . ' (' . $this->queryBuilder->buildColumns($columns) . ')'; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function createTable(string $table, array $columns, string $options = null): string |
||
| 170 | { |
||
| 171 | $cols = []; |
||
| 172 | |||
| 173 | /** @psalm-var string[] $columns */ |
||
| 174 | foreach ($columns as $name => $type) { |
||
| 175 | if (is_string($name)) { |
||
| 176 | $cols[] = "\t" |
||
| 177 | . $this->quoter->quoteColumnName($name) |
||
| 178 | . ' ' |
||
| 179 | . $this->queryBuilder->getColumnType($type); |
||
| 180 | } else { |
||
| 181 | $cols[] = "\t" . $type; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | $sql = 'CREATE TABLE ' |
||
| 186 | . $this->quoter->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)"; |
||
| 187 | |||
| 188 | return $options === null ? $sql : $sql . ' ' . $options; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function createView(string $viewName, QueryInterface|string $subQuery): string |
||
| 192 | { |
||
| 193 | if ($subQuery instanceof QueryInterface) { |
||
| 194 | [$rawQuery, $params] = $this->queryBuilder->build($subQuery); |
||
| 195 | |||
| 196 | /** @psalm-var mixed $value */ |
||
| 197 | foreach ($params as $key => $value) { |
||
| 198 | /** @psalm-var mixed */ |
||
| 199 | $params[$key] = $this->quoter->quoteValue($value); |
||
| 200 | } |
||
| 201 | |||
| 202 | $subQuery = strtr($rawQuery, $params); |
||
| 203 | } |
||
| 204 | |||
| 205 | return 'CREATE VIEW ' . $this->quoter->quoteTableName($viewName) . ' AS ' . $subQuery; |
||
| 206 | } |
||
| 207 | |||
| 208 | public function dropCheck(string $table, string $name): string |
||
| 209 | { |
||
| 210 | return 'ALTER TABLE ' |
||
| 211 | . $this->quoter->quoteTableName($table) |
||
| 212 | . ' DROP CONSTRAINT ' |
||
| 213 | . $this->quoter->quoteColumnName($name); |
||
| 214 | } |
||
| 215 | |||
| 216 | public function dropColumn(string $table, string $column): string |
||
| 217 | { |
||
| 218 | return 'ALTER TABLE ' |
||
| 219 | . $this->quoter->quoteTableName($table) |
||
| 220 | . ' DROP COLUMN ' |
||
| 221 | . $this->quoter->quoteColumnName($column); |
||
| 222 | } |
||
| 223 | |||
| 224 | public function dropCommentFromColumn(string $table, string $column): string |
||
| 225 | { |
||
| 226 | return 'COMMENT ON COLUMN ' |
||
| 227 | . $this->quoter->quoteTableName($table) |
||
| 228 | . '.' |
||
| 229 | . $this->quoter->quoteColumnName($column) |
||
| 230 | . ' IS NULL'; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function dropCommentFromTable(string $table): string |
||
| 234 | { |
||
| 235 | return 'COMMENT ON TABLE ' |
||
| 236 | . $this->quoter->quoteTableName($table) |
||
| 237 | . ' IS NULL'; |
||
| 238 | } |
||
| 239 | |||
| 240 | public function dropDefaultValue(string $table, string $name): string |
||
| 243 | } |
||
| 244 | |||
| 245 | public function dropForeignKey(string $table, string $name): string |
||
| 246 | { |
||
| 247 | return 'ALTER TABLE ' |
||
| 248 | . $this->quoter->quoteTableName($table) |
||
| 249 | . ' DROP CONSTRAINT ' |
||
| 250 | . $this->quoter->quoteColumnName($name); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function dropIndex(string $table, string $name): string |
||
| 254 | { |
||
| 255 | return 'DROP INDEX ' |
||
| 256 | . $this->quoter->quoteTableName($name) |
||
| 257 | . ' ON ' |
||
| 258 | . $this->quoter->quoteTableName($table); |
||
| 259 | } |
||
| 260 | |||
| 261 | public function dropPrimaryKey(string $table, string $name): string |
||
| 262 | { |
||
| 263 | return 'ALTER TABLE ' |
||
| 264 | . $this->quoter->quoteTableName($table) |
||
| 265 | . ' DROP CONSTRAINT ' |
||
| 266 | . $this->quoter->quoteColumnName($name); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function dropTable(string $table): string |
||
| 272 | } |
||
| 273 | |||
| 274 | public function dropUnique(string $table, string $name): string |
||
| 275 | { |
||
| 276 | return 'ALTER TABLE ' |
||
| 277 | . $this->quoter->quoteTableName($table) |
||
| 278 | . ' DROP CONSTRAINT ' |
||
| 279 | . $this->quoter->quoteColumnName($name); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function dropView(string $viewName): string |
||
| 283 | { |
||
| 284 | return 'DROP VIEW ' . $this->quoter->quoteTableName($viewName); |
||
| 285 | } |
||
| 286 | |||
| 287 | public function renameColumn(string $table, string $oldName, string $newName): string |
||
| 288 | { |
||
| 289 | return 'ALTER TABLE ' |
||
| 290 | . $this->quoter->quoteTableName($table) |
||
| 291 | . ' RENAME COLUMN ' . $this->quoter->quoteColumnName($oldName) |
||
| 292 | . ' TO ' . $this->quoter->quoteColumnName($newName); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function renameTable(string $oldName, string $newName): string |
||
| 300 | } |
||
| 301 | |||
| 302 | public function truncateTable(string $table): string |
||
| 305 | } |
||
| 306 | |||
| 307 | public function refreshMaterializedView(string $viewName, bool $concurrently = false, ?bool $withData = null): string |
||
| 308 | { |
||
| 309 | $sql = 'REFRESH MATERIALIZED VIEW '; |
||
| 310 | |||
| 311 | if ($concurrently) { |
||
| 312 | $sql .= 'CONCURRENTLY '; |
||
| 313 | } |
||
| 314 | |||
| 315 | $sql .= $this->quoter->quoteTableName($viewName); |
||
| 316 | |||
| 317 | if (is_bool($withData)) { |
||
| 318 | $sql .= ' WITH ' . ($withData ? 'DATA' : 'NO DATA'); |
||
| 322 | } |
||
| 323 | } |
||
| 324 |