| Total Complexity | 89 |
| Total Lines | 714 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Forge 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 Forge, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Forge extends AbstractForge |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Forge::$unsignedSupportColumnTypes |
||
| 28 | * |
||
| 29 | * UNSIGNED support |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $unsignedSupportColumnTypes = [ |
||
| 34 | 'TINYINT', |
||
| 35 | 'SMALLINT', |
||
| 36 | 'MEDIUMINT', |
||
| 37 | 'INT', |
||
| 38 | 'INTEGER', |
||
| 39 | 'BIGINT', |
||
| 40 | 'REAL', |
||
| 41 | 'DOUBLE', |
||
| 42 | 'DOUBLE PRECISION', |
||
| 43 | 'FLOAT', |
||
| 44 | 'DECIMAL', |
||
| 45 | 'NUMERIC', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Forge::$quotedTableOptions |
||
| 50 | * |
||
| 51 | * Table Options list which required to be quoted |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $quotedTableOptions = [ |
||
| 56 | 'COMMENT', |
||
| 57 | 'COMPRESSION', |
||
| 58 | 'CONNECTION', |
||
| 59 | 'DATA DIRECTORY', |
||
| 60 | 'INDEX DIRECTORY', |
||
| 61 | 'ENCRYPTION', |
||
| 62 | 'PASSWORD', |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Forge::$nullStatement |
||
| 67 | * |
||
| 68 | * NULL value representation in CREATE/ALTER TABLE statements |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $nullStatement = 'NULL'; |
||
| 73 | |||
| 74 | // ------------------------------------------------------------------------ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Forge::platformCreateDatabaseStatement |
||
| 78 | * |
||
| 79 | * @param string $database |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | public function platformCreateDatabaseStatement($database) |
||
| 84 | { |
||
| 85 | return 'CREATE DATABASE ' . $database; |
||
| 86 | } |
||
| 87 | |||
| 88 | // ------------------------------------------------------------------------ |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Forge::platformDropDatabaseStatement |
||
| 92 | * |
||
| 93 | * @param string $database |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function platformDropDatabaseStatement($database) |
||
| 100 | } |
||
| 101 | |||
| 102 | // ------------------------------------------------------------------------ |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Forge::platformBackupDatabaseStatement |
||
| 106 | * |
||
| 107 | * @param string $database |
||
| 108 | * @param string $backupFilePath |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function platformBackupDatabaseStatement($database, $backupFilePath) |
||
| 115 | } |
||
| 116 | |||
| 117 | // ------------------------------------------------------------------------ |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Forge::platformCreateTableStatement |
||
| 121 | * |
||
| 122 | * @param string $table |
||
| 123 | * @param array $columns |
||
| 124 | * @param bool $force |
||
| 125 | * @param array $attributes |
||
| 126 | * |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | public function platformCreateTableStatement($table, array $columns = [], $force = false, array $attributes = []) |
||
| 130 | { |
||
| 131 | $primaryKeys = $foreignKeys = $uniqueKeys = $indexesKeys = []; |
||
|
|
|||
| 132 | |||
| 133 | // Open Statement |
||
| 134 | $statementLines[] = 'CREATE TABLE ' . ($force === true ? ' IF NOT EXISTS ' : '') . $this->conn->escapeIdentifiers($table) . ' ('; |
||
| 135 | |||
| 136 | // Columns Statement |
||
| 137 | $columnStatements = []; |
||
| 138 | foreach ($columns as $columnName => $columnAttributes) { |
||
| 139 | if (isset($columnAttributes[ 'type' ])) { |
||
| 140 | if (isset($columnAttributes[ 'foreign_key' ])) { |
||
| 141 | $foreignKeys[ $columnName ] = $columnAttributes[ 'foreign_key' ]; |
||
| 142 | } |
||
| 143 | |||
| 144 | $columnStatementLine = []; |
||
| 145 | $columnName = $this->conn->escapeIdentifiers($columnName); |
||
| 146 | $columnAttributes[ 'type' ] = strtoupper($columnAttributes[ 'type' ]); |
||
| 147 | |||
| 148 | if (isset($columnAttributes[ 'primary_key' ])) { |
||
| 149 | if ($columnAttributes[ 'primary_key' ] === true) { |
||
| 150 | $primaryKeys[] = $columnName; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | if (isset($columnAttributes[ 'unique' ])) { |
||
| 155 | if ($columnAttributes[ 'unique' ] === true) { |
||
| 156 | $uniqueKeys[] = $columnName; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($columnAttributes[ 'type' ] === 'ENUM') { |
||
| 161 | if (empty($columnAttributes[ 'value' ])) { |
||
| 162 | continue; |
||
| 163 | } else { |
||
| 164 | if (is_string($columnAttributes[ 'value' ])) { |
||
| 165 | $columnAttributes[ 'value' ] = explode(',', $columnAttributes[ 'value' ]); |
||
| 166 | } |
||
| 167 | |||
| 168 | $columnAttributes[ 'value' ] = array_map(function ($value) { |
||
| 169 | return $this->conn->escape(str_replace('\'', '', trim($value))); |
||
| 170 | }, $columnAttributes[ 'value' ]); |
||
| 171 | |||
| 172 | $columnStatementLine[] = $columnName . |
||
| 173 | ' ' . $columnAttributes[ 'type' ] . '(' . |
||
| 174 | implode(',', $columnAttributes[ 'value' ]) |
||
| 175 | . ')'; |
||
| 176 | } |
||
| 177 | } elseif (isset($columnAttributes[ 'length' ])) { |
||
| 178 | $columnStatementLine[] = $columnName . ' ' . $columnAttributes[ 'type' ] . '(' . $columnAttributes[ 'length' ] . ')'; |
||
| 179 | } else { |
||
| 180 | $columnStatementLine[] = $columnName . ' ' . $columnAttributes[ 'type' ]; |
||
| 181 | } |
||
| 182 | |||
| 183 | if (isset($columnAttributes[ 'unsigned' ])) { |
||
| 184 | if ($columnAttributes[ 'unsigned' ] === true) { |
||
| 185 | if (in_array($columnAttributes[ 'type' ], $this->unsignedSupportColumnTypes)) { |
||
| 186 | $columnStatementLine[] = 'UNSIGNED'; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | if (isset($columnAttributes[ 'collate' ])) { |
||
| 192 | $columnStatementLine[] = 'COLLATE ' . $columnAttributes[ 'collate' ]; |
||
| 193 | } elseif (in_array($columnAttributes[ 'type' ], |
||
| 194 | ['CHAR', 'VARCHAR', 'TEXT', 'LONGTEXT', 'TINYTEXT', 'ENUM'])) { |
||
| 195 | $columnStatementLine[] = 'COLLATE ' . $this->conn->getConfig('collate'); |
||
| 196 | } |
||
| 197 | |||
| 198 | if (isset($columnAttributes[ 'not_null' ])) { |
||
| 199 | if ($columnAttributes[ 'not_null' ] === true) { |
||
| 200 | $columnStatementLine[] = 'NOT NULL'; |
||
| 201 | } |
||
| 202 | } elseif (isset($columnAttributes[ 'null' ])) { |
||
| 203 | if ($columnAttributes[ 'null' ] === false) { |
||
| 204 | $columnStatementLine[] = 'NOT NULL'; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | if (isset($columnAttributes[ 'timestamp' ])) { |
||
| 209 | if ($columnAttributes[ 'timestamp' ] === true) { |
||
| 210 | $columnStatementLine[] = 'ON UPDATE CURRENT_TIMESTAMP'; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | if (isset($columnAttributes[ 'default' ])) { |
||
| 215 | $columnStatementLine[] = 'DEFAULT ' . $this->conn->escape($columnAttributes[ 'default' ]); |
||
| 216 | } |
||
| 217 | |||
| 218 | if (isset($columnAttributes[ 'auto_increment' ])) { |
||
| 219 | if ($columnAttributes[ 'auto_increment' ] === true) { |
||
| 220 | $columnStatementLine[] = 'AUTO_INCREMENT'; |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | if (isset($columnAttributes[ 'comment' ])) { |
||
| 225 | $columnStatementLine[] = 'COMMENT ' . $columnAttributes[ 'comment' ]; |
||
| 226 | } |
||
| 227 | |||
| 228 | $columnStatements[] = "\t" . implode(' ', $columnStatementLine); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | // Keys Statement |
||
| 233 | $keyStatements = []; |
||
| 234 | $constraintStatements = []; |
||
| 235 | |||
| 236 | // Primary Key Statement |
||
| 237 | if (count($primaryKeys)) { |
||
| 238 | $keyStatements[] = 'PRIMARY KEY (' . implode(', ', |
||
| 239 | $primaryKeys) . ')' . (count($primaryKeys) >= 2 ? ' USING BTREE' : ''); |
||
| 240 | } |
||
| 241 | |||
| 242 | // Unique Key Statement |
||
| 243 | if (count($uniqueKeys)) { |
||
| 244 | if (count($uniqueKeys) == 1) { |
||
| 245 | $constraintStatements[] = 'UNIQUE (' . implode(',', $uniqueKeys) . ')'; |
||
| 246 | } else { |
||
| 247 | $uniqueName = 'idx_' . implode('_', $uniqueKeys); |
||
| 248 | $constraintStatements[] = 'CONSTRAINT ' . $uniqueName . ' UNIQUE (' . implode(',', $uniqueKeys) . ')'; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | // Foreign Keys Statement |
||
| 253 | if (count($foreignKeys)) { |
||
| 254 | foreach ($foreignKeys as $foreignKeyColumnName => $foreignKeyAttributes) { |
||
| 255 | if (empty($foreignKeyAttributes[ 'name' ])) { |
||
| 256 | $foreignKeyAttributes[ 'name' ] = 'fk_' . $foreignKeyColumnName; |
||
| 257 | } |
||
| 258 | |||
| 259 | if (isset($foreignKeyAttributes[ 'references' ])) { |
||
| 260 | $keyStatements[] = 'KEY ' . |
||
| 261 | $this->conn->escapeIdentifiers($foreignKeyAttributes[ 'name' ]) . |
||
| 262 | ' (' . $this->conn->escapeIdentifiers($foreignKeyColumnName) . ')'; |
||
| 263 | |||
| 264 | $referenceParts = array_map('trim', explode('.', $foreignKeyAttributes[ 'references' ])); |
||
| 265 | list($referenceTable, $referenceColumn) = $referenceParts; |
||
| 266 | |||
| 267 | $referenceOnDelete = 'NO ACTION'; |
||
| 268 | $referenceOnUpdate = 'NO ACTION'; |
||
| 269 | |||
| 270 | $validReferenceActions = [ |
||
| 271 | 'NO ACTION', |
||
| 272 | 'CASCADE', |
||
| 273 | 'RESTRICT', |
||
| 274 | 'SET NULL', |
||
| 275 | ]; |
||
| 276 | |||
| 277 | if (isset($foreignKeyAttributes[ 'on_delete' ])) { |
||
| 278 | if (in_array($foreignKeyAttributes[ 'on_delete' ], $validReferenceActions)) { |
||
| 279 | $referenceOnDelete = $foreignKeyAttributes[ 'on_delete' ]; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | if (isset($foreignKeyAttributes[ 'on_update' ])) { |
||
| 284 | if (in_array($foreignKeyAttributes[ 'on_update' ], $validReferenceActions)) { |
||
| 285 | $referenceOnUpdate = $foreignKeyAttributes[ 'on_update' ]; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | $constraintStatements[] = 'CONSTRAINT ' . |
||
| 290 | $this->conn->escapeIdentifiers($foreignKeyAttributes[ 'name' ]) . |
||
| 291 | ' FOREIGN KEY (' . $this->conn->escapeIdentifiers($foreignKeyColumnName) . ') REFERENCES ' . |
||
| 292 | $this->conn->escapeIdentifiers($referenceTable) . ' (' . $this->conn->escapeIdentifiers($referenceColumn) . |
||
| 293 | ') ON DELETE ' . $referenceOnDelete . ' ON UPDATE ' . $referenceOnUpdate; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | $statementLines[] = implode(',' . PHP_EOL, |
||
| 299 | array_merge($columnStatements, $keyStatements, $constraintStatements)); |
||
| 300 | |||
| 301 | if (empty($attributes)) { |
||
| 302 | $attributes[ 'engine' ] = 'InnoDB'; |
||
| 303 | } |
||
| 304 | |||
| 305 | if( ! array_key_exists('charset', $attributes) ) { |
||
| 306 | $attributes[ 'charset' ] = $this->conn->getConfig('charset'); |
||
| 307 | } |
||
| 308 | |||
| 309 | if( ! array_key_exists('collate', $attributes) ) { |
||
| 310 | $attributes[ 'collate' ] = $this->conn->getConfig('collate'); |
||
| 311 | } |
||
| 312 | |||
| 313 | $attributeStatements = []; |
||
| 314 | foreach ($attributes as $key => $value) { |
||
| 315 | if(is_string($key)) { |
||
| 316 | $key = strtoupper(dash($key)); |
||
| 317 | |||
| 318 | if ($key === 'CHARSET') { |
||
| 319 | $attributeStatements[] = 'DEFAULT CHARSET=' . $value; |
||
| 320 | } elseif(in_array($key, $this->quotedTableOptions)) { |
||
| 321 | $attributeStatements[] = $this->conn->escape($key) . '=' . $value; |
||
| 322 | } else { |
||
| 323 | $attributeStatements[] = $this->conn->escapeString($key) . '=' . $value; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | $statementLines[] = ') ' . implode(' ', $attributeStatements); |
||
| 329 | |||
| 330 | return implode(PHP_EOL, $statementLines) . ';'; |
||
| 331 | } |
||
| 332 | |||
| 333 | // ------------------------------------------------------------------------ |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Forge::platformDropTableStatement |
||
| 337 | * |
||
| 338 | * @param string $table |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function platformDropTableStatement($table) |
||
| 343 | { |
||
| 344 | return 'DROP TABLE ' . $this->conn->escapeIdentifiers($table); |
||
| 345 | } |
||
| 346 | |||
| 347 | // ------------------------------------------------------------------------ |
||
| 348 | |||
| 349 | /** |
||
| 350 | * AbstractForge::platformTruncateTableStatement |
||
| 351 | * |
||
| 352 | * @param string $table |
||
| 353 | * |
||
| 354 | * @return mixed |
||
| 355 | */ |
||
| 356 | protected function platformTruncateTableStatement($table) |
||
| 357 | { |
||
| 358 | return 'TRUNCATE TABLE ' . $this->conn->escapeIdentifiers($table); |
||
| 359 | } |
||
| 360 | |||
| 361 | // ------------------------------------------------------------------------ |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Forge::platformAlterTableStatement |
||
| 365 | * |
||
| 366 | * @param string $table |
||
| 367 | * @param string $column |
||
| 368 | * @param array $attributes |
||
| 369 | * @param string $action |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | public function platformAlterTableStatement($table, $column, array $attributes, $action) |
||
| 374 | { |
||
| 375 | $statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
||
| 376 | |||
| 377 | $alterTableStatementStrings[] = strtoupper($action); |
||
| 378 | $alterTableStatementStrings[] = $this->conn->escapeIdentifiers($column); |
||
| 379 | |||
| 380 | if (isset($attributes[ 'type' ])) { |
||
| 381 | if ($attributes[ 'type' ] === 'ENUM') { |
||
| 382 | if (empty($attributes[ 'value' ])) { |
||
| 383 | return false; |
||
| 384 | } else { |
||
| 385 | if (is_string($attributes[ 'value' ])) { |
||
| 386 | $attributes[ 'value' ] = explode(',', $attributes[ 'value' ]); |
||
| 387 | } |
||
| 388 | |||
| 389 | $attributes[ 'value' ] = array_map(function ($value) { |
||
| 390 | return $this->conn->escape(str_replace('\'', '', trim($value))); |
||
| 391 | }, $attributes[ 'value' ]); |
||
| 392 | |||
| 393 | $alterTableStatementStrings[] = $attributes[ 'type' ] . '(' . |
||
| 394 | implode(',', $attributes[ 'value' ]) |
||
| 395 | . ')'; |
||
| 396 | } |
||
| 397 | } elseif (isset($attributes[ 'length' ])) { |
||
| 398 | $alterTableStatementStrings[] = $attributes[ 'type' ] . '(' . $attributes[ 'length' ] . ')'; |
||
| 399 | } else { |
||
| 400 | $alterTableStatementStrings[] = $attributes[ 'type' ]; |
||
| 401 | } |
||
| 402 | |||
| 403 | if (isset($attributes[ 'unsigned' ])) { |
||
| 404 | if ($attributes[ 'unsigned' ] === true) { |
||
| 405 | if (in_array($attributes[ 'type' ], |
||
| 406 | ['INT', 'BIGINT', 'SMALLINT', 'TINYINT', 'FLOAT', 'DECIMAL', 'REAL'])) { |
||
| 407 | $alterTableStatementStrings[] = 'UNSIGNED'; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | if (isset($attributes[ 'collate' ])) { |
||
| 413 | $alterTableStatementStrings[] = 'COLLATE ' . $attributes[ 'collate' ]; |
||
| 414 | } elseif (in_array($attributes[ 'type' ], |
||
| 415 | ['CHAR', 'VARCHAR', 'TEXT', 'LONGTEXT', 'TINYTEXT', 'ENUM'])) { |
||
| 416 | $alterTableStatementStrings[] = 'COLLATE ' . $this->conn->getConfig('collate'); |
||
| 417 | } |
||
| 418 | |||
| 419 | if (isset($attributes[ 'not_null' ])) { |
||
| 420 | if ($attributes[ 'not_null' ] === true) { |
||
| 421 | $alterTableStatementStrings[] = 'NOT NULL'; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | if (isset($attributes[ 'default' ])) { |
||
| 426 | $alterTableStatementStrings[] = 'DEFAULT ' . $this->conn->escape($attributes[ 'default' ]); |
||
| 427 | } |
||
| 428 | |||
| 429 | if (isset($attributes[ 'auto_increment' ])) { |
||
| 430 | if ($attributes[ 'auto_increment' ] === true) { |
||
| 431 | $alterTableStatementStrings[] = 'AUTO_INCREMENT '; |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | if (isset($attributes[ 'comment' ])) { |
||
| 436 | $alterTableStatementStrings[] = 'COMMENT ' . $attributes[ 'comment' ]; |
||
| 437 | } |
||
| 438 | |||
| 439 | $statementLines[] = implode(' ', $alterTableStatementStrings) . ';'; |
||
| 440 | } |
||
| 441 | |||
| 442 | return implode(PHP_EOL, $statementLines); |
||
| 443 | } |
||
| 444 | |||
| 445 | // ------------------------------------------------------------------------ |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Forge::platformAlterTableDropColumnStatement |
||
| 449 | * |
||
| 450 | * @param string $table |
||
| 451 | * @param string $column |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | protected function platformAlterTableDropColumnStatement($table, $column) |
||
| 456 | { |
||
| 457 | $statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
||
| 458 | $statementLines[] = 'DROP COLUMN ' . $this->conn->escapeIdentifiers($column); |
||
| 459 | |||
| 460 | return implode(PHP_EOL, $statementLines); |
||
| 461 | } |
||
| 462 | |||
| 463 | // ------------------------------------------------------------------------ |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Forge::platformAlterTablePrimaryKeysStatement |
||
| 467 | * |
||
| 468 | * @param string $table |
||
| 469 | * @param array $columns |
||
| 470 | * |
||
| 471 | * @return mixed |
||
| 472 | */ |
||
| 473 | protected function platformAlterTablePrimaryKeysStatement($table, array $columns) |
||
| 474 | { |
||
| 475 | $statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
||
| 476 | |||
| 477 | $keys = array_map(function ($column) { |
||
| 478 | return $this->conn->escapeIdentifiers($column); |
||
| 479 | }, $columns); |
||
| 480 | |||
| 481 | if (count($columns) == 1) { |
||
| 482 | $statementLines[] = 'ADD PRIMARY KEY (' . implode(',', $keys) . ');'; |
||
| 483 | } else { |
||
| 484 | $statementLines[] = 'ADD CONSTRAINT ' . |
||
| 485 | $this->conn->escapeIdentifiers('pk_' . implode('_', $columns)) . |
||
| 486 | ' PRIMARY KEY (' . implode(',', $keys) . ');'; |
||
| 487 | } |
||
| 488 | |||
| 489 | return implode(PHP_EOL, $statementLines); |
||
| 490 | } |
||
| 491 | |||
| 492 | // ------------------------------------------------------------------------ |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Forge::platformAlterTableForeignKeyStatement |
||
| 496 | * |
||
| 497 | * @param string $table |
||
| 498 | * @param string $column |
||
| 499 | * @param string $references |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | protected function platformAlterTableForeignKeyStatement($table, $column, $references) |
||
| 504 | { |
||
| 505 | $statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
||
| 506 | $statementLines[] = 'ADD CONSTRAINT ' . $this->conn->escapeIdentifiers('fk_' . $column); |
||
| 507 | |||
| 508 | $referenceParts = array_map('trim', explode('.', $references)); |
||
| 509 | list($referenceTable, $referenceColumn) = $referenceParts; |
||
| 510 | |||
| 511 | $statementLines[] = 'FOREIGN KEY (' . $this->conn->escapeIdentifiers($column) . ') REFERENCES ' . |
||
| 512 | $this->conn->escapeIdentifiers($referenceTable) . '(' . |
||
| 513 | $this->conn->escapeIdentifiers($referenceColumn) . ')'; |
||
| 514 | |||
| 515 | return implode(PHP_EOL, $statementLines); |
||
| 516 | } |
||
| 517 | |||
| 518 | // ------------------------------------------------------------------------ |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Forge::platformDropTableForeignKeyStatement |
||
| 522 | * |
||
| 523 | * @param string $table |
||
| 524 | * @param string $column |
||
| 525 | * |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | protected function platformDropTableForeignKeyStatement($table, $column) |
||
| 529 | { |
||
| 530 | $statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
||
| 531 | $statementLines[] = 'DROP FOREIGN KEY ' . $this->conn->escapeIdentifiers('fk_' . $column); |
||
| 532 | |||
| 533 | return implode(PHP_EOL, $statementLines); |
||
| 534 | } |
||
| 535 | |||
| 536 | // ------------------------------------------------------------------------ |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Forge::platformAlterTableUniquesStatement |
||
| 540 | * |
||
| 541 | * @param string $table |
||
| 542 | * @param array $columns |
||
| 543 | * |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | protected function platformAlterTableUniquesStatement($table, array $columns) |
||
| 563 | } |
||
| 564 | |||
| 565 | // ------------------------------------------------------------------------ |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Forge::platformCreateTableIndexesStatement |
||
| 569 | * |
||
| 570 | * @param string $table |
||
| 571 | * @param array $columns |
||
| 572 | * @param bool $unique |
||
| 573 | * |
||
| 574 | * @return string |
||
| 575 | */ |
||
| 576 | protected function platformCreateTableIndexesStatement($table, array $columns, $unique = false) |
||
| 577 | { |
||
| 578 | $keys = array_map(function ($column) { |
||
| 579 | return $this->conn->escapeIdentifiers($column); |
||
| 580 | }, $columns); |
||
| 581 | |||
| 582 | $statementLines[] = 'CREATE' . ($unique === true ? ' UNIQUE ' : ' ') . |
||
| 583 | 'INDEX ' . $this->conn->escapeIdentifiers('idx_' . implode('_', $columns)); |
||
| 584 | $statementLines[] = 'ON ' . $this->conn->escapeIdentifiers($table) . ' (' . implode(', ', $keys) . ');'; |
||
| 585 | |||
| 586 | return implode(PHP_EOL, $statementLines); |
||
| 587 | } |
||
| 588 | |||
| 589 | // ------------------------------------------------------------------------ |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Forge::platformDropTableIndexesStatement |
||
| 593 | * |
||
| 594 | * @param string $table |
||
| 595 | * @param array $columns |
||
| 596 | * |
||
| 597 | * @return string |
||
| 598 | */ |
||
| 599 | protected function platformDropTableIndexesStatement($table, $columns) |
||
| 600 | { |
||
| 601 | $statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
||
| 602 | $statementLines[] = 'DROP INDEX ' . $this->conn->escapeIdentifiers('idx_' . implode('_', $columns)); |
||
| 603 | |||
| 604 | return implode(PHP_EOL, $statementLines); |
||
| 605 | } |
||
| 606 | |||
| 607 | // ------------------------------------------------------------------------ |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Forge::platformAlterTableSetColumnDefaultValueStatement |
||
| 611 | * |
||
| 612 | * @param string $table |
||
| 613 | * @param string $column |
||
| 614 | * @param mixed $value |
||
| 615 | * |
||
| 616 | * @return string |
||
| 617 | */ |
||
| 618 | protected function platformAlterTableSetColumnDefaultValueStatement($table, $column, $value) |
||
| 624 | } |
||
| 625 | |||
| 626 | // ------------------------------------------------------------------------ |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Forge::platformAlterTableDropColumnDefaultValue |
||
| 630 | * |
||
| 631 | * @param string $table |
||
| 632 | * @param string $column |
||
| 633 | * |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | protected function platformAlterTableDropColumnDefaultValueStatement($table, $column) |
||
| 642 | } |
||
| 643 | |||
| 644 | // ------------------------------------------------------------------------ |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Forge::platformAlterTableCheckStatement |
||
| 648 | * |
||
| 649 | * @param string $table |
||
| 650 | * @param array $conditions |
||
| 651 | * |
||
| 652 | * @return string |
||
| 653 | */ |
||
| 654 | protected function platformAlterTableCheckStatement($table, array $conditions) |
||
| 655 | { |
||
| 656 | $statementLines[] = 'ALTER TABLE ' . $this->conn->escapeIdentifiers($table); |
||
| 657 | |||
| 658 | $columns = array_keys($conditions); |
||
| 659 | |||
| 660 | if (count($conditions) == 1) { |
||
| 661 | $statementLines[] = 'ADD CHECK (' . $this->conn->escapeIdentifiers($columns[ 0 ]) . ')'; |
||
| 662 | } else { |
||
| 663 | $conditionStatementStrings = []; |
||
| 664 | |||
| 665 | foreach ($conditions as $column => $condition) { |
||
| 666 | if (preg_match('/\s*(?:<|>|!)?=\s*|\s*<>?\s*|\s*>\s*/i', $column, $match, PREG_OFFSET_CAPTURE)) { |
||
| 667 | $operator = trim($match[ 0 ]); |
||
| 668 | $column = trim(str_replace($operator, '', $column)); |
||
| 669 | |||
| 670 | $conditionStatementStrings[] = $this->conn->escapeIdentifiers($column) . $operator . $this->conn->escape($condition); |
||
| 671 | } else { |
||
| 672 | $conditionStatementStrings[] = $this->conn->escapeIdentifiers($column) . '=' . $this->conn->escape($condition); |
||
| 673 | } |
||
| 674 | } |
||
| 675 | |||
| 676 | $statementLines[] = 'ADD CONSTRAINT ' . |
||
| 677 | $this->conn->escapeIdentifiers('chk_' . implode('_', $columns)) . |
||
| 678 | ' (' . implode(' AND ', $conditionStatementStrings) . ');'; |
||
| 679 | } |
||
| 680 | |||
| 681 | return implode(PHP_EOL, $statementLines); |
||
| 682 | } |
||
| 683 | |||
| 684 | // ------------------------------------------------------------------------ |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Forge::platformAlterTableDropCheckStatement |
||
| 688 | * |
||
| 689 | * @param string $table |
||
| 690 | * @param array $columns |
||
| 691 | * |
||
| 692 | * @return mixed |
||
| 693 | */ |
||
| 694 | protected function platformAlterTableDropCheckStatement($table, array $columns) |
||
| 700 | } |
||
| 701 | |||
| 702 | // ------------------------------------------------------------------------ |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Forge::platformCreateViewStatement |
||
| 706 | * |
||
| 707 | * @param string $name |
||
| 708 | * @param string $query |
||
| 709 | * @param bool $force |
||
| 710 | * |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | protected function platformCreateViewStatement($name, $query, $force = false) |
||
| 724 | } |
||
| 725 | |||
| 726 | // ------------------------------------------------------------------------ |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Forge::platformDropViewStatement |
||
| 730 | * |
||
| 731 | * @param string $name |
||
| 732 | * |
||
| 733 | * @return string |
||
| 734 | */ |
||
| 735 | protected function platformDropViewStatement($name) |
||
| 738 | } |
||
| 739 | } |