| 19 | | abstract class SchemaGrammar |
| 20 | | { |
| 21 | | use QuotesLiterals; |
| 22 | | use ConcatenatesStatements; |
| 23 | | |
| 24 | | |
| 25 | | |
| 26 | | |
| 27 | | |
| 28 | | |
| 29 | | |
| 30 | | abstract protected function wrap(string $value): string; |
| 31 | | |
| 32 | | |
| 33 | | |
| 34 | | |
| 35 | | |
| 36 | | |
| 37 | | |
| 38 | | |
| 39 | | |
| 40 | | |
| 41 | | abstract public function type(ColumnType $type, ?int $length = null, ?int $precision = null, ?int $scale = null): string; |
| 42 | | |
| 43 | | |
| 44 | | |
| 45 | | |
| 46 | | |
| 47 | | |
| 48 | | |
| 49 | | final public function compileCreate(Blueprint $blueprint): string |
| 50 | | { |
| 51 | | return 'CREATE TABLE ' . $this->wrap($blueprint->getTable()) . ' (' . $this->compileTableBody($blueprint) . ')'; |
| 52 | | } |
| 53 | | |
| 54 | | |
| 55 | | |
| 56 | | |
| 57 | | |
| 58 | | |
| 59 | | |
| 60 | | final public function compileTableBody(Blueprint $blueprint): string |
| 61 | | { |
| 62 | | $columns = $blueprint->getColumns(); |
| 63 | | if ($columns === []) { |
| 64 | | throw new \InvalidArgumentException('Cannot create a table with no columns.'); |
| 65 | | } |
| 66 | | |
| 67 | | |
| 68 | | $primaryKeys = array_values(array_filter( |
| 69 | | $columns, |
| 70 | | fn (array $column) => $column['primaryKey'] === true, |
| 71 | | )); |
| 72 | | $composite = count($primaryKeys) > 1; |
| 73 | | |
| 74 | | $definitions = array_map( |
| 75 | | fn (array $column) => $this->compileColumnDefinition($column, $composite), |
| 76 | | $columns, |
| 77 | | ); |
| 78 | | |
| 79 | | if ($composite) { |
| 80 | | $definitions[] = 'PRIMARY KEY (' . implode(', ', array_map( |
| 81 | | fn (array $column) => $this->wrap($column['name']), |
| 82 | | $primaryKeys, |
| 83 | | )) . ')'; |
| 84 | | } |
| 85 | | |
| 86 | | foreach ($blueprint->getForeignKeys() as $foreignKey) { |
| 87 | | $definitions[] = $this->compileForeignKeyConstraint($foreignKey); |
| 88 | | } |
| 89 | | |
| 90 | | foreach ($blueprint->getChecks() as $check) { |
| 91 | | $definitions[] = $this->compileCheckConstraint($check); |
| 92 | | } |
| 93 | | |
| 94 | | return implode(', ', $definitions); |
| 95 | | } |
| 96 | | |
| 97 | | |
| 98 | | |
| 99 | | |
| 100 | | |
| 101 | | |
| 102 | | |
| 103 | | protected function compileCheckConstraint(array $check): string |
| 104 | | { |
| 105 | | if ($check['name'] !== null) { |
| 106 | | $this->assertValidIdentifier($check['name']); |
| 107 | | return 'CONSTRAINT ' . $this->wrap($check['name']) . ' CHECK (' . $check['expression'] . ')'; |
| 108 | | } |
| 109 | | |
| 110 | | return 'CHECK (' . $check['expression'] . ')'; |
| 111 | | } |
| 112 | | |
| 113 | | |
| 114 | | |
| 115 | | |
| 116 | | |
| 117 | | |
| 118 | | |
| 119 | | protected function compileForeignKeyConstraint(array $foreignKey): string |
| 120 | | { |
| 121 | | $table = array_shift($foreignKey['references']); |
| 122 | | if ($table === null) { |
| 123 | | throw new \InvalidArgumentException('A foreign key constraint requires a referenced table.'); |
| 124 | | } |
| 125 | | |
| 126 | | |
| 127 | | |
| 128 | | |
| 129 | | return $this->concatenate([ |
| 130 | | 'FOREIGN KEY (' . implode(', ', array_map(fn (string $column) => $this->wrap($column), $foreignKey['columns'])) . ')', |
| 131 | | 'REFERENCES ' . $this->wrap($table) . ' (' . implode(', ', array_map(fn (string $column) => $this->wrap($column), $foreignKey['references'])) . ')', |
| 132 | | $foreignKey['onDelete'] === null ? '' : 'ON DELETE ' . $foreignKey['onDelete']->value, |
| 133 | | $foreignKey['onUpdate'] === null ? '' : 'ON UPDATE ' . $foreignKey['onUpdate']->value, |
| 134 | | $foreignKey['initiallyDeferred'] || $foreignKey['deferrable'] |
| 135 | | ? $this->compileDeferrableClause($foreignKey['initiallyDeferred']) |
| 136 | | : '', |
| 137 | | ]); |
| 138 | | } |
| 139 | | |
| 140 | | |
| 141 | | |
| 142 | | |
| 143 | | |
| 144 | | |
| 145 | | |
| 146 | | |
| 147 | | protected function compileDeferrableClause(bool $initiallyDeferred): string |
| 148 | | { |
| 149 | | throw new UnsupportedFeatureException( |
| 150 | | 'This dialect does not support DEFERRABLE foreign keys.' |
| 151 | | ); |
| 152 | | } |
| 153 | | |
| 154 | | |
| 155 | | |
| 156 | | |
| 157 | | |
| 158 | | |
| 159 | | |
| 160 | | |
| 161 | | |
| 162 | | |
| 163 | | |
| 164 | | public function compileAddColumns(Blueprint $blueprint): array |
| 165 | | { |
| 166 | | $columns = $blueprint->getColumns(); |
| 167 | | if ($columns === []) { |
| 168 | | throw new \InvalidArgumentException('Cannot add columns with no columns defined.'); |
| 169 | | } |
| 170 | | |
| 171 | | $backfills = $blueprint->getBackfills(); |
| 172 | | $table = $blueprint->getTable(); |
| 173 | | |
| 174 | | |
| 175 | | |
| 176 | | |
| 177 | | $statements = ['ALTER TABLE ' . $this->wrap($table) . ' ADD COLUMN ' . implode(', ADD COLUMN ', array_map( |
| 178 | | fn (array $column) => $this->compileAddColumn($this->withTemporaryDefault($column, $backfills)), |
| 179 | | $columns, |
| 180 | | ))]; |
| 181 | | |
| 182 | | foreach ($columns as $column) { |
| 183 | | if (!array_key_exists($column['name'], $backfills)) { |
| 184 | | continue; |
| 185 | | } |
| 186 | | |
| 187 | | $statements[] = $column['default'] !== null |
| 188 | | ? $this->compileSetColumnDefault($table, $column['name'], $column['default']) |
| 189 | | : $this->compileDropColumnDefault($table, $column['name']); |
| 190 | | } |
| 191 | | |
| 192 | | return $statements; |
| 193 | | } |
| 194 | | |
| 195 | | |
| 196 | | |
| 197 | | |
| 198 | | |
| 199 | | |
| 200 | | |
| 201 | | |
| 202 | | |
| 203 | | protected function withTemporaryDefault(array $column, array $backfills): array |
| 204 | | { |
| 205 | | if (!array_key_exists($column['name'], $backfills)) { |
| 206 | | return $column; |
| 207 | | } |
| 208 | | |
| 209 | | return [...$column, 'default' => $backfills[$column['name']]]; |
| 210 | | } |
| 211 | | |
| 212 | | |
| 213 | | |
| 214 | | |
| 215 | | |
| 216 | | |
| 217 | | |
| 218 | | |
| 219 | | |
| 220 | | |
| 221 | | |
| 222 | | protected function compileSetColumnDefault(string $table, string $column, mixed $default): string |
| 223 | | { |
| 224 | | throw new UnsupportedFeatureException('This dialect does not support altering a column default.'); |
| 225 | | } |
| 226 | | |
| 227 | | |
| 228 | | |
| 229 | | |
| 230 | | |
| 231 | | |
| 232 | | |
| 233 | | |
| 234 | | |
| 235 | | |
| 236 | | protected function compileDropColumnDefault(string $table, string $column): string |
| 237 | | { |
| 238 | | throw new UnsupportedFeatureException('This dialect does not support altering a column default.'); |
| 239 | | } |
| 240 | | |
| 241 | | |
| 242 | | |
| 243 | | |
| 244 | | |
| 245 | | |
| 246 | | |
| 247 | | |
| 248 | | |
| 249 | | |
| 250 | | |
| 251 | | |
| 252 | | public function compileDropColumns(Blueprint $blueprint): array |
| 253 | | { |
| 254 | | $columns = $blueprint->getDropColumns(); |
| 255 | | if ($columns === []) { |
| 256 | | throw new \InvalidArgumentException('Cannot drop columns with no columns defined.'); |
| 257 | | } |
| 258 | | |
| 259 | | return ['ALTER TABLE ' . $this->wrap($blueprint->getTable()) . ' DROP COLUMN ' . implode(', DROP COLUMN ', array_map( |
| 260 | | fn (string $column) => $this->compileDropColumn($column), |
| 261 | | $columns, |
| 262 | | ))]; |
| 263 | | } |
| 264 | | |
| 265 | | |
| 266 | | |
| 267 | | |
| 268 | | |
| 269 | | |
| 270 | | |
| 271 | | final public function compileDrop(string $table): string |
| 272 | | { |
| 273 | | return 'DROP TABLE ' . $this->wrap($table); |
| 274 | | } |
| 275 | | |
| 276 | | |
| 277 | | |
| 278 | | |
| 279 | | |
| 280 | | |
| 281 | | |
| 282 | | |
| 283 | | final public function compileRenameTable(string $from, string $to): string |
| 284 | | { |
| 285 | | return 'ALTER TABLE ' . $this->wrap($from) . ' RENAME TO ' . $this->wrap($to); |
| 286 | | } |
| 287 | | |
| 288 | | |
| 289 | | |
| 290 | | |
| 291 | | |
| 292 | | |
| 293 | | |
| 294 | | |
| 295 | | |
| 296 | | final public function compileRenameColumn(string $table, string $from, string $to): string |
| 297 | | { |
| 298 | | return 'ALTER TABLE ' . $this->wrap($table) . ' RENAME COLUMN ' |
| 299 | | . $this->wrap($from) . ' TO ' . $this->wrap($to); |
| 300 | | } |
| 301 | | |
| 302 | | |
| 303 | | |
| 304 | | |
| 305 | | |
| 306 | | |
| 307 | | |
| 308 | | |
| 309 | | |
| 310 | | |
| 311 | | final public function compileCopyTable(string $from, string $to, array $columns): string |
| 312 | | { |
| 313 | | if ($columns === []) { |
| 314 | | throw new \InvalidArgumentException('A table copy requires at least one column.'); |
| 315 | | } |
| 316 | | |
| 317 | | $wrapped = implode(', ', array_map(fn (string $column) => $this->wrap($column), $columns)); |
| 318 | | |
| 319 | | return 'INSERT INTO ' . $this->wrap($to) . ' (' . $wrapped . ') SELECT ' . $wrapped . ' FROM ' . $this->wrap($from); |
| 320 | | } |
| 321 | | |
| 322 | | |
| 323 | | |
| 324 | | |
| 325 | | |
| 326 | | |
| 327 | | |
| 328 | | |
| 329 | | |
| 330 | | public function compileModifyColumn(Blueprint $blueprint): array |
| 331 | | { |
| 332 | | throw new UnsupportedFeatureException( |
| 333 | | 'This dialect does not support modifying columns in place; the change requires a table rebuild.' |
| 334 | | ); |
| 335 | | } |
| 336 | | |
| 337 | | |
| 338 | | |
| 339 | | |
| 340 | | |
| 341 | | |
| 342 | | |
| 343 | | |
| 344 | | |
| 345 | | |
| 346 | | |
| 347 | | public function compileAddForeignKey(string $table, array $foreignKey, string $name): string |
| 348 | | { |
| 349 | | throw new UnsupportedFeatureException( |
| 350 | | 'This dialect does not support adding a foreign key constraint in place; the change requires a table rebuild.' |
| 351 | | ); |
| 352 | | } |
| 353 | | |
| 354 | | |
| 355 | | |
| 356 | | |
| 357 | | |
| 358 | | |
| 359 | | |
| 360 | | |
| 361 | | |
| 362 | | |
| 363 | | public function compileDropForeignKey(string $table, string $name): string |
| 364 | | { |
| 365 | | throw new UnsupportedFeatureException( |
| 366 | | 'This dialect does not support dropping a foreign key constraint in place; the change requires a table rebuild.' |
| 367 | | ); |
| 368 | | } |
| 369 | | |
| 370 | | |
| 371 | | |
| 372 | | |
| 373 | | |
| 374 | | |
| 375 | | |
| 376 | | |
| 377 | | |
| 378 | | |
| 379 | | |
| 380 | | public function compileAddCheck(string $table, string $name, string $expression): string |
| 381 | | { |
| 382 | | throw new UnsupportedFeatureException( |
| 383 | | 'This dialect does not support adding a CHECK constraint in place; the change requires a table rebuild.' |
| 384 | | ); |
| 385 | | } |
| 386 | | |
| 387 | | |
| 388 | | |
| 389 | | |
| 390 | | |
| 391 | | |
| 392 | | |
| 393 | | |
| 394 | | |
| 395 | | |
| 396 | | public function compileDropCheck(string $table, string $name): string |
| 397 | | { |
| 398 | | throw new UnsupportedFeatureException( |
| 399 | | 'This dialect does not support dropping a CHECK constraint in place; the change requires a table rebuild.' |
| 400 | | ); |
| 401 | | } |
| 402 | | |
| 403 | | |
| 404 | | |
| 405 | | |
| 406 | | |
| 407 | | |
| 408 | | |
| 409 | | |
| 410 | | abstract public function compileDropIndex(string $name, string $table): string; |
| 411 | | |
| 412 | | |
| 413 | | |
| 414 | | |
| 415 | | |
| 416 | | |
| 417 | | |
| 418 | | final public function compileIndexes(Blueprint $blueprint): array |
| 419 | | { |
| 420 | | $table = $blueprint->getTable(); |
| 421 | | |
| 422 | | return array_map( |
| 423 | | function (array $index) use ($table): string { |
| 424 | | |
| 425 | | |
| 426 | | |
| 427 | | |
| 428 | | $this->assertValidIdentifier($index['name']); |
| 429 | | |
| 430 | | $sql = ($index['unique'] ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') |
| 431 | | . $this->wrap($index['name']) |
| 432 | | . ' ON ' . $this->wrap($table) |
| 433 | | . ' (' . implode(', ', array_map(fn (string $column) => $this->wrap($column), $index['columns'])) . ')'; |
| 434 | | |
| 435 | | |
| 436 | | |
| 437 | | |
| 438 | | |
| 439 | | |
| 440 | | return $this->concatenate([ |
| 441 | | $sql, |
| 442 | | $index['unique'] |
| 443 | | ? $this->compileNullsNotDistinctClause($index['nullsNotDistinct']) |
| 444 | | : '', |
| 445 | | $index['where'] === null ? '' : $this->compilePartialIndexClause($index['where']), |
| 446 | | ]); |
| 447 | | }, |
| 448 | | $blueprint->getIndexes(), |
| 449 | | ); |
| 450 | | } |
| 451 | | |
| 452 | | |
| 453 | | |
| 454 | | |
| 455 | | |
| 456 | | |
| 457 | | |
| 458 | | |
| 459 | | protected function compileNullsNotDistinctClause(bool $nullsNotDistinct): string |
| 460 | | { |
| 461 | | if ($nullsNotDistinct) { |
| 462 | | throw new UnsupportedFeatureException( |
| 463 | | 'This dialect does not support NULLS NOT DISTINCT on a unique index.' |
| 464 | | ); |
| 465 | | } |
| 466 | | |
| 467 | | return ''; |
| 468 | | } |
| 469 | | |
| 470 | | |
| 471 | | |
| 472 | | |
| 473 | | |
| 474 | | |
| 475 | | |
| 476 | | |
| 477 | | |
| 478 | | protected function compilePartialIndexClause(string $predicate): string |
| 479 | | { |
| 480 | | throw new UnsupportedFeatureException( |
| 481 | | 'This dialect does not support partial (filtered) indexes.' |
| 482 | | ); |
| 483 | | } |
| 484 | | |
| 485 | | |
| 486 | | |
| 487 | | |
| 488 | | |
| 489 | | |
| 490 | | |
| 491 | | |
| 492 | | abstract public function assertValidIdentifier(string $name): void; |
| 493 | | |
| 494 | | |
| 495 | | |
| 496 | | |
| 497 | | |
| 498 | | |
| 499 | | |
| 500 | | protected function compileAddColumn(array $column): string |
| 501 | | { |
| 502 | | return $this->compileColumnDefinition($column); |
| 503 | | } |
| 504 | | |
| 505 | | |
| 506 | | |
| 507 | | |
| 508 | | |
| 509 | | |
| 510 | | |
| 511 | | |
| 512 | | protected function compileDropColumn(string $column): string |
| 513 | | { |
| 514 | | throw new UnsupportedFeatureException('This dialect does not support dropping columns.'); |
| 515 | | } |
| 516 | | |
| 517 | | |
| 518 | | |
| 519 | | |
| 520 | | |
| 521 | | |
| 522 | | |
| 523 | | |
| 524 | | protected function compileColumnDefinition(array $column, bool $composite = false): string |
| 525 | | { |
| 526 | | $name = $this->wrap($column['name']); |
| 527 | | $type = $this->type($column['type'], $column['length'], $column['precision'], $column['scale']); |
| 528 | | |
| 529 | | |
| 530 | | |
| 531 | | |
| 532 | | |
| 533 | | |
| 534 | | |
| 535 | | |
| 536 | | |
| 537 | | |
| 538 | | |
| 539 | | $autoIncrement = $column['autoIncrement'] === true && !$composite; |
| 540 | | $beforeKey = $autoIncrement && $this->autoIncrementBeforePrimaryKey(); |
| 541 | | $afterKey = $autoIncrement && !$this->autoIncrementBeforePrimaryKey(); |
| 542 | | |
| 543 | | |
| 544 | | |
| 545 | | |
| 546 | | return $this->concatenate([ |
| 547 | | $name, |
| 548 | | $type, |
| 549 | | $column['nullable'] !== true ? 'NOT NULL' : '', |
| 550 | | $column['default'] !== null ? 'DEFAULT ' . $this->compileDefault($column['default']) : '', |
| 551 | | $column['unique'] === true ? 'UNIQUE' : '', |
| 552 | | $beforeKey ? $this->autoIncrement() : '', |
| 553 | | |
| 554 | | |
| 555 | | |
| 556 | | $column['primaryKey'] === true && !$composite ? 'PRIMARY KEY' : '', |
| 557 | | $afterKey ? $this->autoIncrement() : '', |
| 558 | | $this->compileEnumCheckClause($column), |
| 559 | | ]); |
| 560 | | } |
| 561 | | |
| 562 | | |
| 563 | | |
| 564 | | |
| 565 | | |
| 566 | | |
| 567 | | |
| 568 | | |
| 569 | | protected function compileEnumCheckClause(array $column): string |
| 570 | | { |
| 571 | | $values = $column['values'] ?? null; |
| 572 | | |
| 573 | | if ($column['type'] !== ColumnType::Enum || $values === null || $values === []) { |
| 574 | | return ''; |
| 575 | | } |
| 576 | | |
| 577 | | $literals = implode(', ', array_map( |
| 578 | | fn (string $value) => $this->quoteLiteral($value), |
| 579 | | $values, |
| 580 | | )); |
| 581 | | |
| 582 | | return 'CHECK (' . $this->wrap($column['name']) . ' IN (' . $literals . '))'; |
| 583 | | } |
| 584 | | |
| 585 | | |
| 586 | | |
| 587 | | |
| 588 | | |
| 589 | | |
| 590 | | |
| 591 | | |
| 592 | | protected function compileDefault(mixed $default): string |
| 593 | | { |
| 594 | | if ($default instanceof \BlueprintAU\Radiant\Database\Query\Expression) { |
| 595 | | return $default->value; |
| 596 | | } |
| 597 | | if (is_scalar($default) || $default === null) { |
| 598 | | return $this->quoteLiteral($default); |
| 599 | | } |
| 600 | | throw new \InvalidArgumentException( |
| 601 | | 'A column default must be a scalar or an Expression; got ' . get_debug_type($default) . '.' |
| 602 | | ); |
| 603 | | } |
| 604 | | |
| 605 | | |
| 606 | | |
| 607 | | |
| 608 | | |
| 609 | | |
| 610 | | abstract protected function autoIncrement(): string; |
| 611 | | |
| 612 | | |
| 613 | | |
| 614 | | |
| 615 | | |
| 616 | | |
| 617 | | protected function autoIncrementBeforePrimaryKey(): bool |
| 618 | | { |
| 619 | | return true; |
| 620 | | } |
| 621 | | |
| 622 | | |
| 623 | | |
| 624 | | |
| 625 | | |
| 626 | | |
| 627 | | |
| 628 | | |
| 629 | | protected function requireLength(?int $length): int |
| 630 | | { |
| 631 | | if ($length === null) { |
| 632 | | throw new \InvalidArgumentException('A string column requires a length.'); |
| 633 | | } |
| 634 | | return $length; |
| 635 | | } |
| 636 | | |
| 637 | | |
| 638 | | |
| 639 | | |
| 640 | | |
| 641 | | |
| 642 | | |
| 643 | | |
| 644 | | protected function suffix(\Stringable|int|null ...$parts): string |
| 645 | | { |
| 646 | | |
| 647 | | |
| 648 | | |
| 649 | | |
| 650 | | $rendered = array_map( |
| 651 | | fn (\Stringable|int $part): string => (string) $part, |
| 652 | | array_filter($parts, fn (\Stringable|int|null $part): bool => $part !== null), |
| 653 | | ); |
| 654 | | |
| 655 | | if ($rendered === []) { |
| 656 | | return ''; |
| 657 | | } |
| 658 | | |
| 659 | | return '(' . implode(',', $rendered) . ')'; |
| 660 | | } |
| 661 | | } |