ChangeColumn.php
1 month ago
Grammar.php
1 month ago
MySqlGrammar.php
1 month ago
PostgresGrammar.php
1 month ago
RenameColumn.php
2 years ago
SQLiteGrammar.php
1 month ago
SqlServerGrammar.php
1 month ago
SQLiteGrammar.php
865 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Schema\Grammars; |
| 4 | |
| 5 | use IAWPSCOPED\Doctrine\DBAL\Schema\Index; |
| 6 | use IAWPSCOPED\Illuminate\Database\Connection; |
| 7 | use IAWPSCOPED\Illuminate\Database\Schema\Blueprint; |
| 8 | use IAWPSCOPED\Illuminate\Support\Arr; |
| 9 | use IAWPSCOPED\Illuminate\Support\Fluent; |
| 10 | use RuntimeException; |
| 11 | /** @internal */ |
| 12 | class SQLiteGrammar extends Grammar |
| 13 | { |
| 14 | /** |
| 15 | * The possible column modifiers. |
| 16 | * |
| 17 | * @var string[] |
| 18 | */ |
| 19 | protected $modifiers = ['VirtualAs', 'StoredAs', 'Nullable', 'Default', 'Increment']; |
| 20 | /** |
| 21 | * The columns available as serials. |
| 22 | * |
| 23 | * @var string[] |
| 24 | */ |
| 25 | protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger']; |
| 26 | /** |
| 27 | * Compile the query to determine if a table exists. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | public function compileTableExists() |
| 32 | { |
| 33 | return "select * from sqlite_master where type = 'table' and name = ?"; |
| 34 | } |
| 35 | /** |
| 36 | * Compile the query to determine the list of columns. |
| 37 | * |
| 38 | * @param string $table |
| 39 | * @return string |
| 40 | */ |
| 41 | public function compileColumnListing($table) |
| 42 | { |
| 43 | return 'pragma table_info(' . $this->wrap(\str_replace('.', '__', $table)) . ')'; |
| 44 | } |
| 45 | /** |
| 46 | * Compile a create table command. |
| 47 | * |
| 48 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 49 | * @param \Illuminate\Support\Fluent $command |
| 50 | * @return string |
| 51 | */ |
| 52 | public function compileCreate(Blueprint $blueprint, Fluent $command) |
| 53 | { |
| 54 | return \sprintf('%s table %s (%s%s%s)', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), \implode(', ', $this->getColumns($blueprint)), (string) $this->addForeignKeys($blueprint), (string) $this->addPrimaryKeys($blueprint)); |
| 55 | } |
| 56 | /** |
| 57 | * Get the foreign key syntax for a table creation statement. |
| 58 | * |
| 59 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 60 | * @return string|null |
| 61 | */ |
| 62 | protected function addForeignKeys(Blueprint $blueprint) |
| 63 | { |
| 64 | $foreigns = $this->getCommandsByName($blueprint, 'foreign'); |
| 65 | return \IAWPSCOPED\collect($foreigns)->reduce(function ($sql, $foreign) { |
| 66 | // Once we have all the foreign key commands for the table creation statement |
| 67 | // we'll loop through each of them and add them to the create table SQL we |
| 68 | // are building, since SQLite needs foreign keys on the tables creation. |
| 69 | $sql .= $this->getForeignKey($foreign); |
| 70 | if (!\is_null($foreign->onDelete)) { |
| 71 | $sql .= " on delete {$foreign->onDelete}"; |
| 72 | } |
| 73 | // If this foreign key specifies the action to be taken on update we will add |
| 74 | // that to the statement here. We'll append it to this SQL and then return |
| 75 | // the SQL so we can keep adding any other foreign constraints onto this. |
| 76 | if (!\is_null($foreign->onUpdate)) { |
| 77 | $sql .= " on update {$foreign->onUpdate}"; |
| 78 | } |
| 79 | return $sql; |
| 80 | }, ''); |
| 81 | } |
| 82 | /** |
| 83 | * Get the SQL for the foreign key. |
| 84 | * |
| 85 | * @param \Illuminate\Support\Fluent $foreign |
| 86 | * @return string |
| 87 | */ |
| 88 | protected function getForeignKey($foreign) |
| 89 | { |
| 90 | // We need to columnize the columns that the foreign key is being defined for |
| 91 | // so that it is a properly formatted list. Once we have done this, we can |
| 92 | // return the foreign key SQL declaration to the calling method for use. |
| 93 | return \sprintf(', foreign key(%s) references %s(%s)', $this->columnize($foreign->columns), $this->wrapTable($foreign->on), $this->columnize((array) $foreign->references)); |
| 94 | } |
| 95 | /** |
| 96 | * Get the primary key syntax for a table creation statement. |
| 97 | * |
| 98 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 99 | * @return string|null |
| 100 | */ |
| 101 | protected function addPrimaryKeys(Blueprint $blueprint) |
| 102 | { |
| 103 | if (!\is_null($primary = $this->getCommandByName($blueprint, 'primary'))) { |
| 104 | return ", primary key ({$this->columnize($primary->columns)})"; |
| 105 | } |
| 106 | } |
| 107 | /** |
| 108 | * Compile alter table commands for adding columns. |
| 109 | * |
| 110 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 111 | * @param \Illuminate\Support\Fluent $command |
| 112 | * @return array |
| 113 | */ |
| 114 | public function compileAdd(Blueprint $blueprint, Fluent $command) |
| 115 | { |
| 116 | $columns = $this->prefixArray('add column', $this->getColumns($blueprint)); |
| 117 | return \IAWPSCOPED\collect($columns)->reject(function ($column) { |
| 118 | return \preg_match('/as \\(.*\\) stored/', $column) > 0; |
| 119 | })->map(function ($column) use($blueprint) { |
| 120 | return 'alter table ' . $this->wrapTable($blueprint) . ' ' . $column; |
| 121 | })->all(); |
| 122 | } |
| 123 | /** |
| 124 | * Compile a rename column command. |
| 125 | * |
| 126 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 127 | * @param \Illuminate\Support\Fluent $command |
| 128 | * @param \Illuminate\Database\Connection $connection |
| 129 | * @return array|string |
| 130 | */ |
| 131 | public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) |
| 132 | { |
| 133 | return $connection->usingNativeSchemaOperations() ? \sprintf('alter table %s rename column %s to %s', $this->wrapTable($blueprint), $this->wrap($command->from), $this->wrap($command->to)) : parent::compileRenameColumn($blueprint, $command, $connection); |
| 134 | } |
| 135 | /** |
| 136 | * Compile a unique key command. |
| 137 | * |
| 138 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 139 | * @param \Illuminate\Support\Fluent $command |
| 140 | * @return string |
| 141 | */ |
| 142 | public function compileUnique(Blueprint $blueprint, Fluent $command) |
| 143 | { |
| 144 | return \sprintf('create unique index %s on %s (%s)', $this->wrap($command->index), $this->wrapTable($blueprint), $this->columnize($command->columns)); |
| 145 | } |
| 146 | /** |
| 147 | * Compile a plain index key command. |
| 148 | * |
| 149 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 150 | * @param \Illuminate\Support\Fluent $command |
| 151 | * @return string |
| 152 | */ |
| 153 | public function compileIndex(Blueprint $blueprint, Fluent $command) |
| 154 | { |
| 155 | return \sprintf('create index %s on %s (%s)', $this->wrap($command->index), $this->wrapTable($blueprint), $this->columnize($command->columns)); |
| 156 | } |
| 157 | /** |
| 158 | * Compile a spatial index key command. |
| 159 | * |
| 160 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 161 | * @param \Illuminate\Support\Fluent $command |
| 162 | * @return void |
| 163 | * |
| 164 | * @throws \RuntimeException |
| 165 | */ |
| 166 | public function compileSpatialIndex(Blueprint $blueprint, Fluent $command) |
| 167 | { |
| 168 | throw new RuntimeException('The database driver in use does not support spatial indexes.'); |
| 169 | } |
| 170 | /** |
| 171 | * Compile a foreign key command. |
| 172 | * |
| 173 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 174 | * @param \Illuminate\Support\Fluent $command |
| 175 | * @return string |
| 176 | */ |
| 177 | public function compileForeign(Blueprint $blueprint, Fluent $command) |
| 178 | { |
| 179 | // Handled on table creation... |
| 180 | } |
| 181 | /** |
| 182 | * Compile a drop table command. |
| 183 | * |
| 184 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 185 | * @param \Illuminate\Support\Fluent $command |
| 186 | * @return string |
| 187 | */ |
| 188 | public function compileDrop(Blueprint $blueprint, Fluent $command) |
| 189 | { |
| 190 | return 'drop table ' . $this->wrapTable($blueprint); |
| 191 | } |
| 192 | /** |
| 193 | * Compile a drop table (if exists) command. |
| 194 | * |
| 195 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 196 | * @param \Illuminate\Support\Fluent $command |
| 197 | * @return string |
| 198 | */ |
| 199 | public function compileDropIfExists(Blueprint $blueprint, Fluent $command) |
| 200 | { |
| 201 | return 'drop table if exists ' . $this->wrapTable($blueprint); |
| 202 | } |
| 203 | /** |
| 204 | * Compile the SQL needed to drop all tables. |
| 205 | * |
| 206 | * @return string |
| 207 | */ |
| 208 | public function compileDropAllTables() |
| 209 | { |
| 210 | return "delete from sqlite_master where type in ('table', 'index', 'trigger')"; |
| 211 | } |
| 212 | /** |
| 213 | * Compile the SQL needed to drop all views. |
| 214 | * |
| 215 | * @return string |
| 216 | */ |
| 217 | public function compileDropAllViews() |
| 218 | { |
| 219 | return "delete from sqlite_master where type in ('view')"; |
| 220 | } |
| 221 | /** |
| 222 | * Compile the SQL needed to retrieve all table names. |
| 223 | * |
| 224 | * @return string |
| 225 | */ |
| 226 | public function compileGetAllTables() |
| 227 | { |
| 228 | return 'select type, name from sqlite_master where type = \'table\' and name not like \'sqlite_%\''; |
| 229 | } |
| 230 | /** |
| 231 | * Compile the SQL needed to retrieve all view names. |
| 232 | * |
| 233 | * @return string |
| 234 | */ |
| 235 | public function compileGetAllViews() |
| 236 | { |
| 237 | return 'select type, name from sqlite_master where type = \'view\''; |
| 238 | } |
| 239 | /** |
| 240 | * Compile the SQL needed to rebuild the database. |
| 241 | * |
| 242 | * @return string |
| 243 | */ |
| 244 | public function compileRebuild() |
| 245 | { |
| 246 | return 'vacuum'; |
| 247 | } |
| 248 | /** |
| 249 | * Compile a drop column command. |
| 250 | * |
| 251 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 252 | * @param \Illuminate\Support\Fluent $command |
| 253 | * @param \Illuminate\Database\Connection $connection |
| 254 | * @return array |
| 255 | */ |
| 256 | public function compileDropColumn(Blueprint $blueprint, Fluent $command, Connection $connection) |
| 257 | { |
| 258 | if ($connection->usingNativeSchemaOperations()) { |
| 259 | $table = $this->wrapTable($blueprint); |
| 260 | $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns)); |
| 261 | return \IAWPSCOPED\collect($columns)->map(fn($column) => 'alter table ' . $table . ' ' . $column)->all(); |
| 262 | } else { |
| 263 | $tableDiff = $this->getDoctrineTableDiff($blueprint, $schema = $connection->getDoctrineSchemaManager()); |
| 264 | foreach ($command->columns as $name) { |
| 265 | $tableDiff->removedColumns[$name] = $connection->getDoctrineColumn($this->getTablePrefix() . $blueprint->getTable(), $name); |
| 266 | } |
| 267 | return (array) $schema->getDatabasePlatform()->getAlterTableSQL($tableDiff); |
| 268 | } |
| 269 | } |
| 270 | /** |
| 271 | * Compile a drop unique key command. |
| 272 | * |
| 273 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 274 | * @param \Illuminate\Support\Fluent $command |
| 275 | * @return string |
| 276 | */ |
| 277 | public function compileDropUnique(Blueprint $blueprint, Fluent $command) |
| 278 | { |
| 279 | $index = $this->wrap($command->index); |
| 280 | return "drop index {$index}"; |
| 281 | } |
| 282 | /** |
| 283 | * Compile a drop index command. |
| 284 | * |
| 285 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 286 | * @param \Illuminate\Support\Fluent $command |
| 287 | * @return string |
| 288 | */ |
| 289 | public function compileDropIndex(Blueprint $blueprint, Fluent $command) |
| 290 | { |
| 291 | $index = $this->wrap($command->index); |
| 292 | return "drop index {$index}"; |
| 293 | } |
| 294 | /** |
| 295 | * Compile a drop spatial index command. |
| 296 | * |
| 297 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 298 | * @param \Illuminate\Support\Fluent $command |
| 299 | * @return void |
| 300 | * |
| 301 | * @throws \RuntimeException |
| 302 | */ |
| 303 | public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command) |
| 304 | { |
| 305 | throw new RuntimeException('The database driver in use does not support spatial indexes.'); |
| 306 | } |
| 307 | /** |
| 308 | * Compile a rename table command. |
| 309 | * |
| 310 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 311 | * @param \Illuminate\Support\Fluent $command |
| 312 | * @return string |
| 313 | */ |
| 314 | public function compileRename(Blueprint $blueprint, Fluent $command) |
| 315 | { |
| 316 | $from = $this->wrapTable($blueprint); |
| 317 | return "alter table {$from} rename to " . $this->wrapTable($command->to); |
| 318 | } |
| 319 | /** |
| 320 | * Compile a rename index command. |
| 321 | * |
| 322 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 323 | * @param \Illuminate\Support\Fluent $command |
| 324 | * @param \Illuminate\Database\Connection $connection |
| 325 | * @return array |
| 326 | * |
| 327 | * @throws \RuntimeException |
| 328 | */ |
| 329 | public function compileRenameIndex(Blueprint $blueprint, Fluent $command, Connection $connection) |
| 330 | { |
| 331 | $schemaManager = $connection->getDoctrineSchemaManager(); |
| 332 | $indexes = $schemaManager->listTableIndexes($this->getTablePrefix() . $blueprint->getTable()); |
| 333 | $index = Arr::get($indexes, $command->from); |
| 334 | if (!$index) { |
| 335 | throw new RuntimeException("Index [{$command->from}] does not exist."); |
| 336 | } |
| 337 | $newIndex = new Index($command->to, $index->getColumns(), $index->isUnique(), $index->isPrimary(), $index->getFlags(), $index->getOptions()); |
| 338 | $platform = $schemaManager->getDatabasePlatform(); |
| 339 | return [$platform->getDropIndexSQL($command->from, $this->getTablePrefix() . $blueprint->getTable()), $platform->getCreateIndexSQL($newIndex, $this->getTablePrefix() . $blueprint->getTable())]; |
| 340 | } |
| 341 | /** |
| 342 | * Compile the command to enable foreign key constraints. |
| 343 | * |
| 344 | * @return string |
| 345 | */ |
| 346 | public function compileEnableForeignKeyConstraints() |
| 347 | { |
| 348 | return 'PRAGMA foreign_keys = ON;'; |
| 349 | } |
| 350 | /** |
| 351 | * Compile the command to disable foreign key constraints. |
| 352 | * |
| 353 | * @return string |
| 354 | */ |
| 355 | public function compileDisableForeignKeyConstraints() |
| 356 | { |
| 357 | return 'PRAGMA foreign_keys = OFF;'; |
| 358 | } |
| 359 | /** |
| 360 | * Compile the SQL needed to enable a writable schema. |
| 361 | * |
| 362 | * @return string |
| 363 | */ |
| 364 | public function compileEnableWriteableSchema() |
| 365 | { |
| 366 | return 'PRAGMA writable_schema = 1;'; |
| 367 | } |
| 368 | /** |
| 369 | * Compile the SQL needed to disable a writable schema. |
| 370 | * |
| 371 | * @return string |
| 372 | */ |
| 373 | public function compileDisableWriteableSchema() |
| 374 | { |
| 375 | return 'PRAGMA writable_schema = 0;'; |
| 376 | } |
| 377 | /** |
| 378 | * Create the column definition for a char type. |
| 379 | * |
| 380 | * @param \Illuminate\Support\Fluent $column |
| 381 | * @return string |
| 382 | */ |
| 383 | protected function typeChar(Fluent $column) |
| 384 | { |
| 385 | return 'varchar'; |
| 386 | } |
| 387 | /** |
| 388 | * Create the column definition for a string type. |
| 389 | * |
| 390 | * @param \Illuminate\Support\Fluent $column |
| 391 | * @return string |
| 392 | */ |
| 393 | protected function typeString(Fluent $column) |
| 394 | { |
| 395 | return 'varchar'; |
| 396 | } |
| 397 | /** |
| 398 | * Create the column definition for a tiny text type. |
| 399 | * |
| 400 | * @param \Illuminate\Support\Fluent $column |
| 401 | * @return string |
| 402 | */ |
| 403 | protected function typeTinyText(Fluent $column) |
| 404 | { |
| 405 | return 'text'; |
| 406 | } |
| 407 | /** |
| 408 | * Create the column definition for a text type. |
| 409 | * |
| 410 | * @param \Illuminate\Support\Fluent $column |
| 411 | * @return string |
| 412 | */ |
| 413 | protected function typeText(Fluent $column) |
| 414 | { |
| 415 | return 'text'; |
| 416 | } |
| 417 | /** |
| 418 | * Create the column definition for a medium text type. |
| 419 | * |
| 420 | * @param \Illuminate\Support\Fluent $column |
| 421 | * @return string |
| 422 | */ |
| 423 | protected function typeMediumText(Fluent $column) |
| 424 | { |
| 425 | return 'text'; |
| 426 | } |
| 427 | /** |
| 428 | * Create the column definition for a long text type. |
| 429 | * |
| 430 | * @param \Illuminate\Support\Fluent $column |
| 431 | * @return string |
| 432 | */ |
| 433 | protected function typeLongText(Fluent $column) |
| 434 | { |
| 435 | return 'text'; |
| 436 | } |
| 437 | /** |
| 438 | * Create the column definition for an integer type. |
| 439 | * |
| 440 | * @param \Illuminate\Support\Fluent $column |
| 441 | * @return string |
| 442 | */ |
| 443 | protected function typeInteger(Fluent $column) |
| 444 | { |
| 445 | return 'integer'; |
| 446 | } |
| 447 | /** |
| 448 | * Create the column definition for a big integer type. |
| 449 | * |
| 450 | * @param \Illuminate\Support\Fluent $column |
| 451 | * @return string |
| 452 | */ |
| 453 | protected function typeBigInteger(Fluent $column) |
| 454 | { |
| 455 | return 'integer'; |
| 456 | } |
| 457 | /** |
| 458 | * Create the column definition for a medium integer type. |
| 459 | * |
| 460 | * @param \Illuminate\Support\Fluent $column |
| 461 | * @return string |
| 462 | */ |
| 463 | protected function typeMediumInteger(Fluent $column) |
| 464 | { |
| 465 | return 'integer'; |
| 466 | } |
| 467 | /** |
| 468 | * Create the column definition for a tiny integer type. |
| 469 | * |
| 470 | * @param \Illuminate\Support\Fluent $column |
| 471 | * @return string |
| 472 | */ |
| 473 | protected function typeTinyInteger(Fluent $column) |
| 474 | { |
| 475 | return 'integer'; |
| 476 | } |
| 477 | /** |
| 478 | * Create the column definition for a small integer type. |
| 479 | * |
| 480 | * @param \Illuminate\Support\Fluent $column |
| 481 | * @return string |
| 482 | */ |
| 483 | protected function typeSmallInteger(Fluent $column) |
| 484 | { |
| 485 | return 'integer'; |
| 486 | } |
| 487 | /** |
| 488 | * Create the column definition for a float type. |
| 489 | * |
| 490 | * @param \Illuminate\Support\Fluent $column |
| 491 | * @return string |
| 492 | */ |
| 493 | protected function typeFloat(Fluent $column) |
| 494 | { |
| 495 | return 'float'; |
| 496 | } |
| 497 | /** |
| 498 | * Create the column definition for a double type. |
| 499 | * |
| 500 | * @param \Illuminate\Support\Fluent $column |
| 501 | * @return string |
| 502 | */ |
| 503 | protected function typeDouble(Fluent $column) |
| 504 | { |
| 505 | return 'float'; |
| 506 | } |
| 507 | /** |
| 508 | * Create the column definition for a decimal type. |
| 509 | * |
| 510 | * @param \Illuminate\Support\Fluent $column |
| 511 | * @return string |
| 512 | */ |
| 513 | protected function typeDecimal(Fluent $column) |
| 514 | { |
| 515 | return 'numeric'; |
| 516 | } |
| 517 | /** |
| 518 | * Create the column definition for a boolean type. |
| 519 | * |
| 520 | * @param \Illuminate\Support\Fluent $column |
| 521 | * @return string |
| 522 | */ |
| 523 | protected function typeBoolean(Fluent $column) |
| 524 | { |
| 525 | return 'tinyint(1)'; |
| 526 | } |
| 527 | /** |
| 528 | * Create the column definition for an enumeration type. |
| 529 | * |
| 530 | * @param \Illuminate\Support\Fluent $column |
| 531 | * @return string |
| 532 | */ |
| 533 | protected function typeEnum(Fluent $column) |
| 534 | { |
| 535 | return \sprintf('varchar check ("%s" in (%s))', $column->name, $this->quoteString($column->allowed)); |
| 536 | } |
| 537 | /** |
| 538 | * Create the column definition for a json type. |
| 539 | * |
| 540 | * @param \Illuminate\Support\Fluent $column |
| 541 | * @return string |
| 542 | */ |
| 543 | protected function typeJson(Fluent $column) |
| 544 | { |
| 545 | return 'text'; |
| 546 | } |
| 547 | /** |
| 548 | * Create the column definition for a jsonb type. |
| 549 | * |
| 550 | * @param \Illuminate\Support\Fluent $column |
| 551 | * @return string |
| 552 | */ |
| 553 | protected function typeJsonb(Fluent $column) |
| 554 | { |
| 555 | return 'text'; |
| 556 | } |
| 557 | /** |
| 558 | * Create the column definition for a date type. |
| 559 | * |
| 560 | * @param \Illuminate\Support\Fluent $column |
| 561 | * @return string |
| 562 | */ |
| 563 | protected function typeDate(Fluent $column) |
| 564 | { |
| 565 | return 'date'; |
| 566 | } |
| 567 | /** |
| 568 | * Create the column definition for a date-time type. |
| 569 | * |
| 570 | * @param \Illuminate\Support\Fluent $column |
| 571 | * @return string |
| 572 | */ |
| 573 | protected function typeDateTime(Fluent $column) |
| 574 | { |
| 575 | return $this->typeTimestamp($column); |
| 576 | } |
| 577 | /** |
| 578 | * Create the column definition for a date-time (with time zone) type. |
| 579 | * |
| 580 | * Note: "SQLite does not have a storage class set aside for storing dates and/or times." |
| 581 | * |
| 582 | * @link https://www.sqlite.org/datatype3.html |
| 583 | * |
| 584 | * @param \Illuminate\Support\Fluent $column |
| 585 | * @return string |
| 586 | */ |
| 587 | protected function typeDateTimeTz(Fluent $column) |
| 588 | { |
| 589 | return $this->typeDateTime($column); |
| 590 | } |
| 591 | /** |
| 592 | * Create the column definition for a time type. |
| 593 | * |
| 594 | * @param \Illuminate\Support\Fluent $column |
| 595 | * @return string |
| 596 | */ |
| 597 | protected function typeTime(Fluent $column) |
| 598 | { |
| 599 | return 'time'; |
| 600 | } |
| 601 | /** |
| 602 | * Create the column definition for a time (with time zone) type. |
| 603 | * |
| 604 | * @param \Illuminate\Support\Fluent $column |
| 605 | * @return string |
| 606 | */ |
| 607 | protected function typeTimeTz(Fluent $column) |
| 608 | { |
| 609 | return $this->typeTime($column); |
| 610 | } |
| 611 | /** |
| 612 | * Create the column definition for a timestamp type. |
| 613 | * |
| 614 | * @param \Illuminate\Support\Fluent $column |
| 615 | * @return string |
| 616 | */ |
| 617 | protected function typeTimestamp(Fluent $column) |
| 618 | { |
| 619 | return $column->useCurrent ? 'datetime default CURRENT_TIMESTAMP' : 'datetime'; |
| 620 | } |
| 621 | /** |
| 622 | * Create the column definition for a timestamp (with time zone) type. |
| 623 | * |
| 624 | * @param \Illuminate\Support\Fluent $column |
| 625 | * @return string |
| 626 | */ |
| 627 | protected function typeTimestampTz(Fluent $column) |
| 628 | { |
| 629 | return $this->typeTimestamp($column); |
| 630 | } |
| 631 | /** |
| 632 | * Create the column definition for a year type. |
| 633 | * |
| 634 | * @param \Illuminate\Support\Fluent $column |
| 635 | * @return string |
| 636 | */ |
| 637 | protected function typeYear(Fluent $column) |
| 638 | { |
| 639 | return $this->typeInteger($column); |
| 640 | } |
| 641 | /** |
| 642 | * Create the column definition for a binary type. |
| 643 | * |
| 644 | * @param \Illuminate\Support\Fluent $column |
| 645 | * @return string |
| 646 | */ |
| 647 | protected function typeBinary(Fluent $column) |
| 648 | { |
| 649 | return 'blob'; |
| 650 | } |
| 651 | /** |
| 652 | * Create the column definition for a uuid type. |
| 653 | * |
| 654 | * @param \Illuminate\Support\Fluent $column |
| 655 | * @return string |
| 656 | */ |
| 657 | protected function typeUuid(Fluent $column) |
| 658 | { |
| 659 | return 'varchar'; |
| 660 | } |
| 661 | /** |
| 662 | * Create the column definition for an IP address type. |
| 663 | * |
| 664 | * @param \Illuminate\Support\Fluent $column |
| 665 | * @return string |
| 666 | */ |
| 667 | protected function typeIpAddress(Fluent $column) |
| 668 | { |
| 669 | return 'varchar'; |
| 670 | } |
| 671 | /** |
| 672 | * Create the column definition for a MAC address type. |
| 673 | * |
| 674 | * @param \Illuminate\Support\Fluent $column |
| 675 | * @return string |
| 676 | */ |
| 677 | protected function typeMacAddress(Fluent $column) |
| 678 | { |
| 679 | return 'varchar'; |
| 680 | } |
| 681 | /** |
| 682 | * Create the column definition for a spatial Geometry type. |
| 683 | * |
| 684 | * @param \Illuminate\Support\Fluent $column |
| 685 | * @return string |
| 686 | */ |
| 687 | public function typeGeometry(Fluent $column) |
| 688 | { |
| 689 | return 'geometry'; |
| 690 | } |
| 691 | /** |
| 692 | * Create the column definition for a spatial Point type. |
| 693 | * |
| 694 | * @param \Illuminate\Support\Fluent $column |
| 695 | * @return string |
| 696 | */ |
| 697 | public function typePoint(Fluent $column) |
| 698 | { |
| 699 | return 'point'; |
| 700 | } |
| 701 | /** |
| 702 | * Create the column definition for a spatial LineString type. |
| 703 | * |
| 704 | * @param \Illuminate\Support\Fluent $column |
| 705 | * @return string |
| 706 | */ |
| 707 | public function typeLineString(Fluent $column) |
| 708 | { |
| 709 | return 'linestring'; |
| 710 | } |
| 711 | /** |
| 712 | * Create the column definition for a spatial Polygon type. |
| 713 | * |
| 714 | * @param \Illuminate\Support\Fluent $column |
| 715 | * @return string |
| 716 | */ |
| 717 | public function typePolygon(Fluent $column) |
| 718 | { |
| 719 | return 'polygon'; |
| 720 | } |
| 721 | /** |
| 722 | * Create the column definition for a spatial GeometryCollection type. |
| 723 | * |
| 724 | * @param \Illuminate\Support\Fluent $column |
| 725 | * @return string |
| 726 | */ |
| 727 | public function typeGeometryCollection(Fluent $column) |
| 728 | { |
| 729 | return 'geometrycollection'; |
| 730 | } |
| 731 | /** |
| 732 | * Create the column definition for a spatial MultiPoint type. |
| 733 | * |
| 734 | * @param \Illuminate\Support\Fluent $column |
| 735 | * @return string |
| 736 | */ |
| 737 | public function typeMultiPoint(Fluent $column) |
| 738 | { |
| 739 | return 'multipoint'; |
| 740 | } |
| 741 | /** |
| 742 | * Create the column definition for a spatial MultiLineString type. |
| 743 | * |
| 744 | * @param \Illuminate\Support\Fluent $column |
| 745 | * @return string |
| 746 | */ |
| 747 | public function typeMultiLineString(Fluent $column) |
| 748 | { |
| 749 | return 'multilinestring'; |
| 750 | } |
| 751 | /** |
| 752 | * Create the column definition for a spatial MultiPolygon type. |
| 753 | * |
| 754 | * @param \Illuminate\Support\Fluent $column |
| 755 | * @return string |
| 756 | */ |
| 757 | public function typeMultiPolygon(Fluent $column) |
| 758 | { |
| 759 | return 'multipolygon'; |
| 760 | } |
| 761 | /** |
| 762 | * Create the column definition for a generated, computed column type. |
| 763 | * |
| 764 | * @param \Illuminate\Support\Fluent $column |
| 765 | * @return void |
| 766 | * |
| 767 | * @throws \RuntimeException |
| 768 | */ |
| 769 | protected function typeComputed(Fluent $column) |
| 770 | { |
| 771 | throw new RuntimeException('This database driver requires a type, see the virtualAs / storedAs modifiers.'); |
| 772 | } |
| 773 | /** |
| 774 | * Get the SQL for a generated virtual column modifier. |
| 775 | * |
| 776 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 777 | * @param \Illuminate\Support\Fluent $column |
| 778 | * @return string|null |
| 779 | */ |
| 780 | protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) |
| 781 | { |
| 782 | if (!\is_null($virtualAs = $column->virtualAsJson)) { |
| 783 | if ($this->isJsonSelector($virtualAs)) { |
| 784 | $virtualAs = $this->wrapJsonSelector($virtualAs); |
| 785 | } |
| 786 | return " as ({$virtualAs})"; |
| 787 | } |
| 788 | if (!\is_null($virtualAs = $column->virtualAs)) { |
| 789 | return " as ({$virtualAs})"; |
| 790 | } |
| 791 | } |
| 792 | /** |
| 793 | * Get the SQL for a generated stored column modifier. |
| 794 | * |
| 795 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 796 | * @param \Illuminate\Support\Fluent $column |
| 797 | * @return string|null |
| 798 | */ |
| 799 | protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) |
| 800 | { |
| 801 | if (!\is_null($storedAs = $column->storedAsJson)) { |
| 802 | if ($this->isJsonSelector($storedAs)) { |
| 803 | $storedAs = $this->wrapJsonSelector($storedAs); |
| 804 | } |
| 805 | return " as ({$storedAs}) stored"; |
| 806 | } |
| 807 | if (!\is_null($storedAs = $column->storedAs)) { |
| 808 | return " as ({$column->storedAs}) stored"; |
| 809 | } |
| 810 | } |
| 811 | /** |
| 812 | * Get the SQL for a nullable column modifier. |
| 813 | * |
| 814 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 815 | * @param \Illuminate\Support\Fluent $column |
| 816 | * @return string|null |
| 817 | */ |
| 818 | protected function modifyNullable(Blueprint $blueprint, Fluent $column) |
| 819 | { |
| 820 | if (\is_null($column->virtualAs) && \is_null($column->virtualAsJson) && \is_null($column->storedAs) && \is_null($column->storedAsJson)) { |
| 821 | return $column->nullable ? '' : ' not null'; |
| 822 | } |
| 823 | if ($column->nullable === \false) { |
| 824 | return ' not null'; |
| 825 | } |
| 826 | } |
| 827 | /** |
| 828 | * Get the SQL for a default column modifier. |
| 829 | * |
| 830 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 831 | * @param \Illuminate\Support\Fluent $column |
| 832 | * @return string|null |
| 833 | */ |
| 834 | protected function modifyDefault(Blueprint $blueprint, Fluent $column) |
| 835 | { |
| 836 | if (!\is_null($column->default) && \is_null($column->virtualAs) && \is_null($column->virtualAsJson) && \is_null($column->storedAs)) { |
| 837 | return ' default ' . $this->getDefaultValue($column->default); |
| 838 | } |
| 839 | } |
| 840 | /** |
| 841 | * Get the SQL for an auto-increment column modifier. |
| 842 | * |
| 843 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 844 | * @param \Illuminate\Support\Fluent $column |
| 845 | * @return string|null |
| 846 | */ |
| 847 | protected function modifyIncrement(Blueprint $blueprint, Fluent $column) |
| 848 | { |
| 849 | if (\in_array($column->type, $this->serials) && $column->autoIncrement) { |
| 850 | return ' primary key autoincrement'; |
| 851 | } |
| 852 | } |
| 853 | /** |
| 854 | * Wrap the given JSON selector. |
| 855 | * |
| 856 | * @param string $value |
| 857 | * @return string |
| 858 | */ |
| 859 | protected function wrapJsonSelector($value) |
| 860 | { |
| 861 | [$field, $path] = $this->wrapJsonFieldAndPath($value); |
| 862 | return 'json_extract(' . $field . $path . ')'; |
| 863 | } |
| 864 | } |
| 865 |