Lines
98.46%
192 / 195
Methods
91.42%
32 / 35
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| wrap | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| type | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 17 | ||
| autoIncrement | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| compileDropIndex | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| compileDropColumn | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| compileSetColumnDefault | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| compileDropColumnDefault | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| compileModifyColumn | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 2 | ||
| compileAddForeignKey | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| compileDropForeignKey | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| compileAddCheck | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| assertValidIdentifier | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileCreate | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileTableBody | 100.00% | 22 / 22 | 100.00% | 1 / 1 | 5 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileCheckConstraint | 75.00% | 3 / 4 | 0.00% | 0 / 1 | 2.06 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileForeignKeyConstraint | 91.66% | 11 / 12 | 0.00% | 0 / 1 | 6.02 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileDeferrableClause | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileAddColumns | 93.75% | 15 / 16 | 0.00% | 0 / 1 | 5.01 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] withTemporaryDefault | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileDropColumns | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileDrop | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileRenameTable | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileRenameColumn | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileCopyTable | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileDropCheck | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileIndexes | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 4 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileNullsNotDistinctClause | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compilePartialIndexClause | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileAddColumn | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileColumnDefinition | 100.00% | 16 / 16 | 100.00% | 1 / 1 | 11 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileEnumCheckClause | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 4 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileDefault | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 4 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] autoIncrementBeforePrimaryKey | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] requireLength | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] suffix | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| 17 | final class MySqlSchemaGrammar extends SchemaGrammar | |
| 18 | { | |
| 19 | /** | |
| 20 | * Wrap an identifier in MySQL backticks. | |
| 21 | * | |
| 22 | * @param string $value | |
| 23 | * @return string | |
| 24 | */ | |
| 25 | protected function wrap(string $value): string | |
| 26 | { | |
| 27 | return '`' . str_replace('`', '``', $value) . '`'; | |
| 28 | } | |
| 29 | ||
| 30 | /** | |
| 31 | * Map a logical column type to MySQL's native type. | |
| 32 | * | |
| 33 | * Datetime types render fractional seconds when a precision is declared | |
| 34 | * (`datetime(3)`); MySQL 5.6.4+ stores the declared digits natively. | |
| 35 | * | |
| 36 | * @param ColumnType $type | |
| 37 | * @param int|null $length | |
| 38 | * @param int|null $precision | |
| 39 | * @param int|null $scale | |
| 40 | * @return string | |
| 41 | */ | |
| 42 | public function type(ColumnType $type, ?int $length = null, ?int $precision = null, ?int $scale = null): string | |
| 43 | { | |
| 44 | return match ($type) { | |
| 45 | ColumnType::String => 'varchar' . $this->suffix($this->requireLength($length)), | |
| 46 | ColumnType::Char => 'char' . $this->suffix($this->requireLength($length)), | |
| 47 | ColumnType::Text => 'text', | |
| 48 | ColumnType::BigInt => 'bigint', | |
| 49 | ColumnType::Int => 'int', | |
| 50 | ColumnType::Decimal => 'decimal' . $this->suffix($precision, $scale), | |
| 51 | ColumnType::Float => 'double', | |
| 52 | ColumnType::Boolean => 'tinyint(1)', | |
| 53 | ColumnType::Date => 'date', | |
| 54 | ColumnType::DateTime => 'datetime' . $this->suffix($precision), | |
| 55 | ColumnType::Timestamp => 'timestamp' . $this->suffix($precision), | |
| 56 | ColumnType::Json => 'json', | |
| 57 | ColumnType::Enum => 'varchar' . $this->suffix($this->requireLength($length)), | |
| 58 | ColumnType::Binary => $length === null ? 'blob' : 'varbinary' . $this->suffix($length), | |
| 59 | ColumnType::Uuid => 'char(36)', | |
| 60 | }; | |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 64 | * The MySQL auto-increment clause. | |
| 65 | * | |
| 66 | * @return string | |
| 67 | */ | |
| 68 | protected function autoIncrement(): string | |
| 69 | { | |
| 70 | return 'AUTO_INCREMENT'; | |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * MySQL has NO partial (filtered) indexes, NO NULLS NOT DISTINCT, and | |
| 75 | * NO DEFERRABLE foreign keys — the base clause hooks throw, and MySQL | |
| 76 | * overrides none of them, so declaring any of those options fails | |
| 77 | * fast at compile time (Postgres — and for partial indexes SQLite — | |
| 78 | * render them). | |
| 79 | */ | |
| 80 | ||
| 81 | /** | |
| 82 | * MySQL drops an index relative to its table: `ALTER TABLE … DROP INDEX`. | |
| 83 | * | |
| 84 | * @param string $name | |
| 85 | * @param string $table | |
| 86 | * @return string | |
| 87 | */ | |
| 88 | public function compileDropIndex(string $name, string $table): string | |
| 89 | { | |
| 90 | $this->assertValidIdentifier($name); | |
| 91 | ||
| 92 | return 'ALTER TABLE ' . $this->wrap($table) . ' DROP INDEX ' . $this->wrap($name); | |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Compile one column's `DROP COLUMN` clause. | |
| 97 | * | |
| 98 | * @param string $column | |
| 99 | * @return string | |
| 100 | */ | |
| 101 | protected function compileDropColumn(string $column): string | |
| 102 | { | |
| 103 | return $this->wrap($column); | |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Compile the statement that restores a column's declared default. | |
| 108 | * | |
| 109 | * @param string $table | |
| 110 | * @param string $column | |
| 111 | * @param mixed $default A scalar or an Expression. | |
| 112 | * @return string | |
| 113 | */ | |
| 114 | #[\Override] | |
| 115 | protected function compileSetColumnDefault(string $table, string $column, mixed $default): string | |
| 116 | { | |
| 117 | return 'ALTER TABLE ' . $this->wrap($table) . ' ALTER COLUMN ' . $this->wrap($column) | |
| 118 | . ' SET DEFAULT ' . $this->compileDefault($default); | |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Compile the statement that drops a column's temporary default. | |
| 123 | * | |
| 124 | * @param string $table | |
| 125 | * @param string $column | |
| 126 | * @return string | |
| 127 | */ | |
| 128 | #[\Override] | |
| 129 | protected function compileDropColumnDefault(string $table, string $column): string | |
| 130 | { | |
| 131 | return 'ALTER TABLE ' . $this->wrap($table) . ' ALTER COLUMN ' . $this->wrap($column) . ' DROP DEFAULT'; | |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Compile an `ALTER TABLE ... MODIFY COLUMN` statement — MySQL's | |
| 136 | * in-place content-drift form. | |
| 137 | * | |
| 138 | * @param Blueprint $blueprint | |
| 139 | * @return list<string> | |
| 140 | */ | |
| 141 | public function compileModifyColumn(Blueprint $blueprint): array | |
| 142 | { | |
| 143 | $table = $blueprint->getTable(); | |
| 144 | $columns = $blueprint->getColumns(); | |
| 145 | if ($columns === []) { | |
| 146 | throw new \InvalidArgumentException('Cannot modify columns with no columns defined.'); | |
| 147 | } | |
| 148 | ||
| 149 | return array_map( | |
| 150 | fn (array $column): string => 'ALTER TABLE ' . $this->wrap($table) | |
| 151 | . ' MODIFY ' . $this->compileColumnDefinition($column), | |
| 152 | $columns, | |
| 153 | ); | |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * Compile an `ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY` | |
| 158 | * statement — MySQL's in-place FK-add form. | |
| 159 | * | |
| 160 | * @param string $table | |
| 161 | * @param array{columns: list<string>, references: list<string>, onDelete: \BlueprintAU\Radiant\Database\Schema\Enums\ForeignKeyAction|null, onUpdate: \BlueprintAU\Radiant\Database\Schema\Enums\ForeignKeyAction|null, deferrable: bool, initiallyDeferred: bool} $foreignKey | |
| 162 | * @param string $name | |
| 163 | * @return string | |
| 164 | */ | |
| 165 | public function compileAddForeignKey(string $table, array $foreignKey, string $name): string | |
| 166 | { | |
| 167 | $this->assertValidIdentifier($name); | |
| 168 | ||
| 169 | return 'ALTER TABLE ' . $this->wrap($table) . ' ADD CONSTRAINT ' . $this->wrap($name) . ' ' | |
| 170 | . $this->compileForeignKeyConstraint($foreignKey); | |
| 171 | } | |
| 172 | ||
| 173 | /** | |
| 174 | * Compile an `ALTER TABLE ... DROP FOREIGN KEY` statement — MySQL's | |
| 175 | * in-place FK-drop form. | |
| 176 | * | |
| 177 | * @param string $table | |
| 178 | * @param string $name | |
| 179 | * @return string | |
| 180 | */ | |
| 181 | public function compileDropForeignKey(string $table, string $name): string | |
| 182 | { | |
| 183 | $this->assertValidIdentifier($name); | |
| 184 | ||
| 185 | return 'ALTER TABLE ' . $this->wrap($table) . ' DROP FOREIGN KEY ' . $this->wrap($name); | |
| 186 | } | |
| 187 | ||
| 188 | /** | |
| 189 | * Compile an `ALTER TABLE ... ADD CONSTRAINT ... CHECK` statement — | |
| 190 | * MySQL's in-place CHECK-add form. | |
| 191 | * | |
| 192 | * @param string $table | |
| 193 | * @param string $name | |
| 194 | * @param string $expression | |
| 195 | * @return string | |
| 196 | */ | |
| 197 | public function compileAddCheck(string $table, string $name, string $expression): string | |
| 198 | { | |
| 199 | $this->assertValidIdentifier($name); | |
| 200 | ||
| 201 | return 'ALTER TABLE ' . $this->wrap($table) . ' ADD CONSTRAINT ' . $this->wrap($name) | |
| 202 | . ' CHECK (' . $expression . ')'; | |
| 203 | } | |
| 204 | ||
| 205 | /** | |
| 206 | * MySQL caps identifiers at 64 characters — fail fast at compile | |
| 207 | * time, never silently truncate. | |
| 208 | * | |
| 209 | * @param string $name | |
| 210 | * @return void | |
| 211 | * @throws \InvalidArgumentException | |
| 212 | */ | |
| 213 | public function assertValidIdentifier(string $name): void | |
| 214 | { | |
| 215 | if (strlen($name) > 64) { | |
| 216 | throw new \InvalidArgumentException(sprintf( | |
| 217 | 'Identifier [%s] exceeds MySQL\'s 64-character limit (%d chars); ' | |
| 218 | . 'declare a shorter #[Unique(name: ...)] / #[Index(name: ...)].', | |
| 219 | $name, | |
| 220 | strlen($name), | |
| 221 | )); | |
| 222 | } | |
| 223 | } | |
| 224 | } |
Inherited from BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar
| 49 | final public function compileCreate(Blueprint $blueprint): string | |
| 50 | { | |
| 51 | return 'CREATE TABLE ' . $this->wrap($blueprint->getTable()) . ' (' . $this->compileTableBody($blueprint) . ')'; | |
| 52 | } |
| 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 | // A composite primary key is declared table-level, not per-column. | |
| 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 | } |
| 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 | } |
| 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 | // Statement assembly: the constraint body plus optional clauses — | |
| 127 | // the concatenate() join (absent clause = '' segment, dropped), | |
| 128 | // NOT a list join. Column lists inside the parens stay list-joins. | |
| 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 | } |
| 147 | protected function compileDeferrableClause(bool $initiallyDeferred): string | |
| 148 | { | |
| 149 | throw new UnsupportedFeatureException( | |
| 150 | 'This dialect does not support DEFERRABLE foreign keys.' | |
| 151 | ); | |
| 152 | } |
| 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 | // A NOT NULL column with a backfill is added with the backfill as | |
| 175 | // a TEMPORARY default (existing rows get it), then the default is | |
| 176 | // restored to the declared one (new rows get that) or dropped. | |
| 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 | } |
| 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 | } |
| 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 | } |
| 271 | final public function compileDrop(string $table): string | |
| 272 | { | |
| 273 | return 'DROP TABLE ' . $this->wrap($table); | |
| 274 | } |
| 283 | final public function compileRenameTable(string $from, string $to): string | |
| 284 | { | |
| 285 | return 'ALTER TABLE ' . $this->wrap($from) . ' RENAME TO ' . $this->wrap($to); | |
| 286 | } |
| 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 | } |
| 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 | } |
| 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 | } |
| 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 | // Names on the blueprint are FINAL (built at declaration | |
| 425 | // time — user-set names verbatim, derived names with the | |
| 426 | // table prefix and kind suffix). The grammar renders them | |
| 427 | // as-is; its only job is quoting + dialect validation. | |
| 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 | // The option clauses are optional segments of the one | |
| 436 | // statement — statement assembly, not a list join. The | |
| 437 | // NULLS clause rides EVERY unique index (the base carries | |
| 438 | // the default semantics implicitly as ''; Postgres pins | |
| 439 | // them explicitly); a plain index renders neither. | |
| 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 | } |
| 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 | } |
| 478 | protected function compilePartialIndexClause(string $predicate): string | |
| 479 | { | |
| 480 | throw new UnsupportedFeatureException( | |
| 481 | 'This dialect does not support partial (filtered) indexes.' | |
| 482 | ); | |
| 483 | } |
| 500 | protected function compileAddColumn(array $column): string | |
| 501 | { | |
| 502 | return $this->compileColumnDefinition($column); | |
| 503 | } |
| 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 | // A composite PK has no generated id in the ORM's contract (the | |
| 530 | // caller assigns every key part — insertGetId is a single-column | |
| 531 | // concept), so the auto-increment clause is suppressed on its | |
| 532 | // member columns. On SQLite it would be outright invalid: | |
| 533 | // AUTOINCREMENT is only legal on a single-column INTEGER PRIMARY | |
| 534 | // KEY. Auto-increment placement is otherwise dialect-dependent: | |
| 535 | // MySQL/Postgres render it before PRIMARY KEY (`AUTO_INCREMENT | |
| 536 | // PRIMARY KEY`, `GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY`), | |
| 537 | // while SQLite requires it after (`INTEGER PRIMARY KEY | |
| 538 | // AUTOINCREMENT`). | |
| 539 | $autoIncrement = $column['autoIncrement'] === true && !$composite; | |
| 540 | $beforeKey = $autoIncrement && $this->autoIncrementBeforePrimaryKey(); | |
| 541 | $afterKey = $autoIncrement && !$this->autoIncrementBeforePrimaryKey(); | |
| 542 | ||
| 543 | // Statement assembly: the optional clauses are concatenate() | |
| 544 | // segments — NOT NULL / DEFAULT / UNIQUE / the auto-increment and | |
| 545 | // PRIMARY KEY clauses each may be absent. No list items here. | |
| 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 | // A single-column PK is declared inline; a composite PK is | |
| 554 | // declared table-level (see compileCreate), so a column that is | |
| 555 | // part of a composite PK must not also get an inline PRIMARY KEY. | |
| 556 | $column['primaryKey'] === true && !$composite ? 'PRIMARY KEY' : '', | |
| 557 | $afterKey ? $this->autoIncrement() : '', | |
| 558 | $this->compileEnumCheckClause($column), | |
| 559 | ]); | |
| 560 | } |
| 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 | } |
| 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 | } |
| 617 | protected function autoIncrementBeforePrimaryKey(): bool | |
| 618 | { | |
| 619 | return true; | |
| 620 | } |
| 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 | } |
| 644 | protected function suffix(\Stringable|int|null ...$parts): string | |
| 645 | { | |
| 646 | // Drop the nulls, then stringify what's left. The filter is | |
| 647 | // explicit (`!== null`), not the default falsy filter: a numeric | |
| 648 | // part of 0 stringifies to '0', which is falsy and would be | |
| 649 | // silently dropped. | |
| 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 | } |