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