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
PostgresGrammar.php
971 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Schema\Grammars; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\Connection; |
| 6 | use IAWPSCOPED\Illuminate\Database\Schema\Blueprint; |
| 7 | use IAWPSCOPED\Illuminate\Support\Fluent; |
| 8 | /** @internal */ |
| 9 | class PostgresGrammar extends Grammar |
| 10 | { |
| 11 | /** |
| 12 | * If this Grammar supports schema changes wrapped in a transaction. |
| 13 | * |
| 14 | * @var bool |
| 15 | */ |
| 16 | protected $transactions = \true; |
| 17 | /** |
| 18 | * The possible column modifiers. |
| 19 | * |
| 20 | * @var string[] |
| 21 | */ |
| 22 | protected $modifiers = ['Collate', 'Increment', 'Nullable', 'Default', 'VirtualAs', 'StoredAs']; |
| 23 | /** |
| 24 | * The columns available as serials. |
| 25 | * |
| 26 | * @var string[] |
| 27 | */ |
| 28 | protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger']; |
| 29 | /** |
| 30 | * The commands to be executed outside of create or alter command. |
| 31 | * |
| 32 | * @var string[] |
| 33 | */ |
| 34 | protected $fluentCommands = ['Comment']; |
| 35 | /** |
| 36 | * Compile a create database command. |
| 37 | * |
| 38 | * @param string $name |
| 39 | * @param \Illuminate\Database\Connection $connection |
| 40 | * @return string |
| 41 | */ |
| 42 | public function compileCreateDatabase($name, $connection) |
| 43 | { |
| 44 | return \sprintf('create database %s encoding %s', $this->wrapValue($name), $this->wrapValue($connection->getConfig('charset'))); |
| 45 | } |
| 46 | /** |
| 47 | * Compile a drop database if exists command. |
| 48 | * |
| 49 | * @param string $name |
| 50 | * @return string |
| 51 | */ |
| 52 | public function compileDropDatabaseIfExists($name) |
| 53 | { |
| 54 | return \sprintf('drop database if exists %s', $this->wrapValue($name)); |
| 55 | } |
| 56 | /** |
| 57 | * Compile the query to determine if a table exists. |
| 58 | * |
| 59 | * @return string |
| 60 | */ |
| 61 | public function compileTableExists() |
| 62 | { |
| 63 | return "select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'"; |
| 64 | } |
| 65 | /** |
| 66 | * Compile the query to determine the list of columns. |
| 67 | * |
| 68 | * @return string |
| 69 | */ |
| 70 | public function compileColumnListing() |
| 71 | { |
| 72 | return 'select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?'; |
| 73 | } |
| 74 | /** |
| 75 | * Compile a create table command. |
| 76 | * |
| 77 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 78 | * @param \Illuminate\Support\Fluent $command |
| 79 | * @return array |
| 80 | */ |
| 81 | public function compileCreate(Blueprint $blueprint, Fluent $command) |
| 82 | { |
| 83 | return \array_values(\array_filter(\array_merge([\sprintf('%s table %s (%s)', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), \implode(', ', $this->getColumns($blueprint)))], $this->compileAutoIncrementStartingValues($blueprint)))); |
| 84 | } |
| 85 | /** |
| 86 | * Compile a column addition command. |
| 87 | * |
| 88 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 89 | * @param \Illuminate\Support\Fluent $command |
| 90 | * @return string |
| 91 | */ |
| 92 | public function compileAdd(Blueprint $blueprint, Fluent $command) |
| 93 | { |
| 94 | return \array_values(\array_filter(\array_merge([\sprintf('alter table %s %s', $this->wrapTable($blueprint), \implode(', ', $this->prefixArray('add column', $this->getColumns($blueprint))))], $this->compileAutoIncrementStartingValues($blueprint)))); |
| 95 | } |
| 96 | /** |
| 97 | * Compile the auto-incrementing column starting values. |
| 98 | * |
| 99 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 100 | * @return array |
| 101 | */ |
| 102 | public function compileAutoIncrementStartingValues(Blueprint $blueprint) |
| 103 | { |
| 104 | return \IAWPSCOPED\collect($blueprint->autoIncrementingStartingValues())->map(function ($value, $column) use($blueprint) { |
| 105 | return 'alter sequence ' . $blueprint->getTable() . '_' . $column . '_seq restart with ' . $value; |
| 106 | })->all(); |
| 107 | } |
| 108 | /** |
| 109 | * Compile a rename column command. |
| 110 | * |
| 111 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 112 | * @param \Illuminate\Support\Fluent $command |
| 113 | * @param \Illuminate\Database\Connection $connection |
| 114 | * @return array|string |
| 115 | */ |
| 116 | public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) |
| 117 | { |
| 118 | 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); |
| 119 | } |
| 120 | /** |
| 121 | * Compile a primary key command. |
| 122 | * |
| 123 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 124 | * @param \Illuminate\Support\Fluent $command |
| 125 | * @return string |
| 126 | */ |
| 127 | public function compilePrimary(Blueprint $blueprint, Fluent $command) |
| 128 | { |
| 129 | $columns = $this->columnize($command->columns); |
| 130 | return 'alter table ' . $this->wrapTable($blueprint) . " add primary key ({$columns})"; |
| 131 | } |
| 132 | /** |
| 133 | * Compile a unique key command. |
| 134 | * |
| 135 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 136 | * @param \Illuminate\Support\Fluent $command |
| 137 | * @return string |
| 138 | */ |
| 139 | public function compileUnique(Blueprint $blueprint, Fluent $command) |
| 140 | { |
| 141 | $sql = \sprintf('alter table %s add constraint %s unique (%s)', $this->wrapTable($blueprint), $this->wrap($command->index), $this->columnize($command->columns)); |
| 142 | if (!\is_null($command->deferrable)) { |
| 143 | $sql .= $command->deferrable ? ' deferrable' : ' not deferrable'; |
| 144 | } |
| 145 | if ($command->deferrable && !\is_null($command->initiallyImmediate)) { |
| 146 | $sql .= $command->initiallyImmediate ? ' initially immediate' : ' initially deferred'; |
| 147 | } |
| 148 | return $sql; |
| 149 | } |
| 150 | /** |
| 151 | * Compile a plain index key command. |
| 152 | * |
| 153 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 154 | * @param \Illuminate\Support\Fluent $command |
| 155 | * @return string |
| 156 | */ |
| 157 | public function compileIndex(Blueprint $blueprint, Fluent $command) |
| 158 | { |
| 159 | return \sprintf('create index %s on %s%s (%s)', $this->wrap($command->index), $this->wrapTable($blueprint), $command->algorithm ? ' using ' . $command->algorithm : '', $this->columnize($command->columns)); |
| 160 | } |
| 161 | /** |
| 162 | * Compile a fulltext index key command. |
| 163 | * |
| 164 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 165 | * @param \Illuminate\Support\Fluent $command |
| 166 | * @return string |
| 167 | * |
| 168 | * @throws \RuntimeException |
| 169 | */ |
| 170 | public function compileFulltext(Blueprint $blueprint, Fluent $command) |
| 171 | { |
| 172 | $language = $command->language ?: 'english'; |
| 173 | $columns = \array_map(function ($column) use($language) { |
| 174 | return "to_tsvector({$this->quoteString($language)}, {$this->wrap($column)})"; |
| 175 | }, $command->columns); |
| 176 | return \sprintf('create index %s on %s using gin ((%s))', $this->wrap($command->index), $this->wrapTable($blueprint), \implode(' || ', $columns)); |
| 177 | } |
| 178 | /** |
| 179 | * Compile a spatial index key command. |
| 180 | * |
| 181 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 182 | * @param \Illuminate\Support\Fluent $command |
| 183 | * @return string |
| 184 | */ |
| 185 | public function compileSpatialIndex(Blueprint $blueprint, Fluent $command) |
| 186 | { |
| 187 | $command->algorithm = 'gist'; |
| 188 | return $this->compileIndex($blueprint, $command); |
| 189 | } |
| 190 | /** |
| 191 | * Compile a foreign key command. |
| 192 | * |
| 193 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 194 | * @param \Illuminate\Support\Fluent $command |
| 195 | * @return string |
| 196 | */ |
| 197 | public function compileForeign(Blueprint $blueprint, Fluent $command) |
| 198 | { |
| 199 | $sql = parent::compileForeign($blueprint, $command); |
| 200 | if (!\is_null($command->deferrable)) { |
| 201 | $sql .= $command->deferrable ? ' deferrable' : ' not deferrable'; |
| 202 | } |
| 203 | if ($command->deferrable && !\is_null($command->initiallyImmediate)) { |
| 204 | $sql .= $command->initiallyImmediate ? ' initially immediate' : ' initially deferred'; |
| 205 | } |
| 206 | if (!\is_null($command->notValid)) { |
| 207 | $sql .= ' not valid'; |
| 208 | } |
| 209 | return $sql; |
| 210 | } |
| 211 | /** |
| 212 | * Compile a drop table command. |
| 213 | * |
| 214 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 215 | * @param \Illuminate\Support\Fluent $command |
| 216 | * @return string |
| 217 | */ |
| 218 | public function compileDrop(Blueprint $blueprint, Fluent $command) |
| 219 | { |
| 220 | return 'drop table ' . $this->wrapTable($blueprint); |
| 221 | } |
| 222 | /** |
| 223 | * Compile a drop table (if exists) command. |
| 224 | * |
| 225 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 226 | * @param \Illuminate\Support\Fluent $command |
| 227 | * @return string |
| 228 | */ |
| 229 | public function compileDropIfExists(Blueprint $blueprint, Fluent $command) |
| 230 | { |
| 231 | return 'drop table if exists ' . $this->wrapTable($blueprint); |
| 232 | } |
| 233 | /** |
| 234 | * Compile the SQL needed to drop all tables. |
| 235 | * |
| 236 | * @param array $tables |
| 237 | * @return string |
| 238 | */ |
| 239 | public function compileDropAllTables($tables) |
| 240 | { |
| 241 | return 'drop table ' . \implode(',', $this->escapeNames($tables)) . ' cascade'; |
| 242 | } |
| 243 | /** |
| 244 | * Compile the SQL needed to drop all views. |
| 245 | * |
| 246 | * @param array $views |
| 247 | * @return string |
| 248 | */ |
| 249 | public function compileDropAllViews($views) |
| 250 | { |
| 251 | return 'drop view ' . \implode(',', $this->escapeNames($views)) . ' cascade'; |
| 252 | } |
| 253 | /** |
| 254 | * Compile the SQL needed to drop all types. |
| 255 | * |
| 256 | * @param array $types |
| 257 | * @return string |
| 258 | */ |
| 259 | public function compileDropAllTypes($types) |
| 260 | { |
| 261 | return 'drop type ' . \implode(',', $this->escapeNames($types)) . ' cascade'; |
| 262 | } |
| 263 | /** |
| 264 | * Compile the SQL needed to retrieve all table names. |
| 265 | * |
| 266 | * @param string|array $searchPath |
| 267 | * @return string |
| 268 | */ |
| 269 | public function compileGetAllTables($searchPath) |
| 270 | { |
| 271 | return "select tablename, concat('\"', schemaname, '\".\"', tablename, '\"') as qualifiedname from pg_catalog.pg_tables where schemaname in ('" . \implode("','", (array) $searchPath) . "')"; |
| 272 | } |
| 273 | /** |
| 274 | * Compile the SQL needed to retrieve all view names. |
| 275 | * |
| 276 | * @param string|array $searchPath |
| 277 | * @return string |
| 278 | */ |
| 279 | public function compileGetAllViews($searchPath) |
| 280 | { |
| 281 | return "select viewname, concat('\"', schemaname, '\".\"', viewname, '\"') as qualifiedname from pg_catalog.pg_views where schemaname in ('" . \implode("','", (array) $searchPath) . "')"; |
| 282 | } |
| 283 | /** |
| 284 | * Compile the SQL needed to retrieve all type names. |
| 285 | * |
| 286 | * @return string |
| 287 | */ |
| 288 | public function compileGetAllTypes() |
| 289 | { |
| 290 | return 'select distinct pg_type.typname from pg_type inner join pg_enum on pg_enum.enumtypid = pg_type.oid'; |
| 291 | } |
| 292 | /** |
| 293 | * Compile a drop column command. |
| 294 | * |
| 295 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 296 | * @param \Illuminate\Support\Fluent $command |
| 297 | * @return string |
| 298 | */ |
| 299 | public function compileDropColumn(Blueprint $blueprint, Fluent $command) |
| 300 | { |
| 301 | $columns = $this->prefixArray('drop column', $this->wrapArray($command->columns)); |
| 302 | return 'alter table ' . $this->wrapTable($blueprint) . ' ' . \implode(', ', $columns); |
| 303 | } |
| 304 | /** |
| 305 | * Compile a drop primary key command. |
| 306 | * |
| 307 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 308 | * @param \Illuminate\Support\Fluent $command |
| 309 | * @return string |
| 310 | */ |
| 311 | public function compileDropPrimary(Blueprint $blueprint, Fluent $command) |
| 312 | { |
| 313 | $index = $this->wrap("{$blueprint->getTable()}_pkey"); |
| 314 | return 'alter table ' . $this->wrapTable($blueprint) . " drop constraint {$index}"; |
| 315 | } |
| 316 | /** |
| 317 | * Compile a drop unique key command. |
| 318 | * |
| 319 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 320 | * @param \Illuminate\Support\Fluent $command |
| 321 | * @return string |
| 322 | */ |
| 323 | public function compileDropUnique(Blueprint $blueprint, Fluent $command) |
| 324 | { |
| 325 | $index = $this->wrap($command->index); |
| 326 | return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}"; |
| 327 | } |
| 328 | /** |
| 329 | * Compile a drop index command. |
| 330 | * |
| 331 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 332 | * @param \Illuminate\Support\Fluent $command |
| 333 | * @return string |
| 334 | */ |
| 335 | public function compileDropIndex(Blueprint $blueprint, Fluent $command) |
| 336 | { |
| 337 | return "drop index {$this->wrap($command->index)}"; |
| 338 | } |
| 339 | /** |
| 340 | * Compile a drop fulltext index command. |
| 341 | * |
| 342 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 343 | * @param \Illuminate\Support\Fluent $command |
| 344 | * @return string |
| 345 | */ |
| 346 | public function compileDropFullText(Blueprint $blueprint, Fluent $command) |
| 347 | { |
| 348 | return $this->compileDropIndex($blueprint, $command); |
| 349 | } |
| 350 | /** |
| 351 | * Compile a drop spatial index command. |
| 352 | * |
| 353 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 354 | * @param \Illuminate\Support\Fluent $command |
| 355 | * @return string |
| 356 | */ |
| 357 | public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command) |
| 358 | { |
| 359 | return $this->compileDropIndex($blueprint, $command); |
| 360 | } |
| 361 | /** |
| 362 | * Compile a drop foreign key command. |
| 363 | * |
| 364 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 365 | * @param \Illuminate\Support\Fluent $command |
| 366 | * @return string |
| 367 | */ |
| 368 | public function compileDropForeign(Blueprint $blueprint, Fluent $command) |
| 369 | { |
| 370 | $index = $this->wrap($command->index); |
| 371 | return "alter table {$this->wrapTable($blueprint)} drop constraint {$index}"; |
| 372 | } |
| 373 | /** |
| 374 | * Compile a rename table command. |
| 375 | * |
| 376 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 377 | * @param \Illuminate\Support\Fluent $command |
| 378 | * @return string |
| 379 | */ |
| 380 | public function compileRename(Blueprint $blueprint, Fluent $command) |
| 381 | { |
| 382 | $from = $this->wrapTable($blueprint); |
| 383 | return "alter table {$from} rename to " . $this->wrapTable($command->to); |
| 384 | } |
| 385 | /** |
| 386 | * Compile a rename index command. |
| 387 | * |
| 388 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 389 | * @param \Illuminate\Support\Fluent $command |
| 390 | * @return string |
| 391 | */ |
| 392 | public function compileRenameIndex(Blueprint $blueprint, Fluent $command) |
| 393 | { |
| 394 | return \sprintf('alter index %s rename to %s', $this->wrap($command->from), $this->wrap($command->to)); |
| 395 | } |
| 396 | /** |
| 397 | * Compile the command to enable foreign key constraints. |
| 398 | * |
| 399 | * @return string |
| 400 | */ |
| 401 | public function compileEnableForeignKeyConstraints() |
| 402 | { |
| 403 | return 'SET CONSTRAINTS ALL IMMEDIATE;'; |
| 404 | } |
| 405 | /** |
| 406 | * Compile the command to disable foreign key constraints. |
| 407 | * |
| 408 | * @return string |
| 409 | */ |
| 410 | public function compileDisableForeignKeyConstraints() |
| 411 | { |
| 412 | return 'SET CONSTRAINTS ALL DEFERRED;'; |
| 413 | } |
| 414 | /** |
| 415 | * Compile a comment command. |
| 416 | * |
| 417 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 418 | * @param \Illuminate\Support\Fluent $command |
| 419 | * @return string |
| 420 | */ |
| 421 | public function compileComment(Blueprint $blueprint, Fluent $command) |
| 422 | { |
| 423 | return \sprintf('comment on column %s.%s is %s', $this->wrapTable($blueprint), $this->wrap($command->column->name), "'" . \str_replace("'", "''", $command->value) . "'"); |
| 424 | } |
| 425 | /** |
| 426 | * Compile a table comment command. |
| 427 | * |
| 428 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 429 | * @param \Illuminate\Support\Fluent $command |
| 430 | * @return string |
| 431 | */ |
| 432 | public function compileTableComment(Blueprint $blueprint, Fluent $command) |
| 433 | { |
| 434 | return \sprintf('comment on table %s is %s', $this->wrapTable($blueprint), "'" . \str_replace("'", "''", $command->comment) . "'"); |
| 435 | } |
| 436 | /** |
| 437 | * Quote-escape the given tables, views, or types. |
| 438 | * |
| 439 | * @param array $names |
| 440 | * @return array |
| 441 | */ |
| 442 | public function escapeNames($names) |
| 443 | { |
| 444 | return \array_map(static function ($name) { |
| 445 | return '"' . \IAWPSCOPED\collect(\explode('.', $name))->map(fn($segment) => \trim($segment, '\'"'))->implode('"."') . '"'; |
| 446 | }, $names); |
| 447 | } |
| 448 | /** |
| 449 | * Create the column definition for a char type. |
| 450 | * |
| 451 | * @param \Illuminate\Support\Fluent $column |
| 452 | * @return string |
| 453 | */ |
| 454 | protected function typeChar(Fluent $column) |
| 455 | { |
| 456 | if ($column->length) { |
| 457 | return "char({$column->length})"; |
| 458 | } |
| 459 | return 'char'; |
| 460 | } |
| 461 | /** |
| 462 | * Create the column definition for a string type. |
| 463 | * |
| 464 | * @param \Illuminate\Support\Fluent $column |
| 465 | * @return string |
| 466 | */ |
| 467 | protected function typeString(Fluent $column) |
| 468 | { |
| 469 | if ($column->length) { |
| 470 | return "varchar({$column->length})"; |
| 471 | } |
| 472 | return 'varchar'; |
| 473 | } |
| 474 | /** |
| 475 | * Create the column definition for a tiny text type. |
| 476 | * |
| 477 | * @param \Illuminate\Support\Fluent $column |
| 478 | * @return string |
| 479 | */ |
| 480 | protected function typeTinyText(Fluent $column) |
| 481 | { |
| 482 | return 'varchar(255)'; |
| 483 | } |
| 484 | /** |
| 485 | * Create the column definition for a text type. |
| 486 | * |
| 487 | * @param \Illuminate\Support\Fluent $column |
| 488 | * @return string |
| 489 | */ |
| 490 | protected function typeText(Fluent $column) |
| 491 | { |
| 492 | return 'text'; |
| 493 | } |
| 494 | /** |
| 495 | * Create the column definition for a medium text type. |
| 496 | * |
| 497 | * @param \Illuminate\Support\Fluent $column |
| 498 | * @return string |
| 499 | */ |
| 500 | protected function typeMediumText(Fluent $column) |
| 501 | { |
| 502 | return 'text'; |
| 503 | } |
| 504 | /** |
| 505 | * Create the column definition for a long text type. |
| 506 | * |
| 507 | * @param \Illuminate\Support\Fluent $column |
| 508 | * @return string |
| 509 | */ |
| 510 | protected function typeLongText(Fluent $column) |
| 511 | { |
| 512 | return 'text'; |
| 513 | } |
| 514 | /** |
| 515 | * Create the column definition for an integer type. |
| 516 | * |
| 517 | * @param \Illuminate\Support\Fluent $column |
| 518 | * @return string |
| 519 | */ |
| 520 | protected function typeInteger(Fluent $column) |
| 521 | { |
| 522 | return $this->generatableColumn('integer', $column); |
| 523 | } |
| 524 | /** |
| 525 | * Create the column definition for a big integer type. |
| 526 | * |
| 527 | * @param \Illuminate\Support\Fluent $column |
| 528 | * @return string |
| 529 | */ |
| 530 | protected function typeBigInteger(Fluent $column) |
| 531 | { |
| 532 | return $this->generatableColumn('bigint', $column); |
| 533 | } |
| 534 | /** |
| 535 | * Create the column definition for a medium integer type. |
| 536 | * |
| 537 | * @param \Illuminate\Support\Fluent $column |
| 538 | * @return string |
| 539 | */ |
| 540 | protected function typeMediumInteger(Fluent $column) |
| 541 | { |
| 542 | return $this->generatableColumn('integer', $column); |
| 543 | } |
| 544 | /** |
| 545 | * Create the column definition for a tiny integer type. |
| 546 | * |
| 547 | * @param \Illuminate\Support\Fluent $column |
| 548 | * @return string |
| 549 | */ |
| 550 | protected function typeTinyInteger(Fluent $column) |
| 551 | { |
| 552 | return $this->generatableColumn('smallint', $column); |
| 553 | } |
| 554 | /** |
| 555 | * Create the column definition for a small integer type. |
| 556 | * |
| 557 | * @param \Illuminate\Support\Fluent $column |
| 558 | * @return string |
| 559 | */ |
| 560 | protected function typeSmallInteger(Fluent $column) |
| 561 | { |
| 562 | return $this->generatableColumn('smallint', $column); |
| 563 | } |
| 564 | /** |
| 565 | * Create the column definition for a generatable column. |
| 566 | * |
| 567 | * @param string $type |
| 568 | * @param \Illuminate\Support\Fluent $column |
| 569 | * @return string |
| 570 | */ |
| 571 | protected function generatableColumn($type, Fluent $column) |
| 572 | { |
| 573 | if (!$column->autoIncrement && \is_null($column->generatedAs)) { |
| 574 | return $type; |
| 575 | } |
| 576 | if ($column->autoIncrement && \is_null($column->generatedAs)) { |
| 577 | return with(['integer' => 'serial', 'bigint' => 'bigserial', 'smallint' => 'smallserial'])[$type]; |
| 578 | } |
| 579 | $options = ''; |
| 580 | if (!\is_bool($column->generatedAs) && !empty($column->generatedAs)) { |
| 581 | $options = \sprintf(' (%s)', $column->generatedAs); |
| 582 | } |
| 583 | return \sprintf('%s generated %s as identity%s', $type, $column->always ? 'always' : 'by default', $options); |
| 584 | } |
| 585 | /** |
| 586 | * Create the column definition for a float type. |
| 587 | * |
| 588 | * @param \Illuminate\Support\Fluent $column |
| 589 | * @return string |
| 590 | */ |
| 591 | protected function typeFloat(Fluent $column) |
| 592 | { |
| 593 | return $this->typeDouble($column); |
| 594 | } |
| 595 | /** |
| 596 | * Create the column definition for a double type. |
| 597 | * |
| 598 | * @param \Illuminate\Support\Fluent $column |
| 599 | * @return string |
| 600 | */ |
| 601 | protected function typeDouble(Fluent $column) |
| 602 | { |
| 603 | return 'double precision'; |
| 604 | } |
| 605 | /** |
| 606 | * Create the column definition for a real type. |
| 607 | * |
| 608 | * @param \Illuminate\Support\Fluent $column |
| 609 | * @return string |
| 610 | */ |
| 611 | protected function typeReal(Fluent $column) |
| 612 | { |
| 613 | return 'real'; |
| 614 | } |
| 615 | /** |
| 616 | * Create the column definition for a decimal type. |
| 617 | * |
| 618 | * @param \Illuminate\Support\Fluent $column |
| 619 | * @return string |
| 620 | */ |
| 621 | protected function typeDecimal(Fluent $column) |
| 622 | { |
| 623 | return "decimal({$column->total}, {$column->places})"; |
| 624 | } |
| 625 | /** |
| 626 | * Create the column definition for a boolean type. |
| 627 | * |
| 628 | * @param \Illuminate\Support\Fluent $column |
| 629 | * @return string |
| 630 | */ |
| 631 | protected function typeBoolean(Fluent $column) |
| 632 | { |
| 633 | return 'boolean'; |
| 634 | } |
| 635 | /** |
| 636 | * Create the column definition for an enumeration type. |
| 637 | * |
| 638 | * @param \Illuminate\Support\Fluent $column |
| 639 | * @return string |
| 640 | */ |
| 641 | protected function typeEnum(Fluent $column) |
| 642 | { |
| 643 | return \sprintf('varchar(255) check ("%s" in (%s))', $column->name, $this->quoteString($column->allowed)); |
| 644 | } |
| 645 | /** |
| 646 | * Create the column definition for a json type. |
| 647 | * |
| 648 | * @param \Illuminate\Support\Fluent $column |
| 649 | * @return string |
| 650 | */ |
| 651 | protected function typeJson(Fluent $column) |
| 652 | { |
| 653 | return 'json'; |
| 654 | } |
| 655 | /** |
| 656 | * Create the column definition for a jsonb type. |
| 657 | * |
| 658 | * @param \Illuminate\Support\Fluent $column |
| 659 | * @return string |
| 660 | */ |
| 661 | protected function typeJsonb(Fluent $column) |
| 662 | { |
| 663 | return 'jsonb'; |
| 664 | } |
| 665 | /** |
| 666 | * Create the column definition for a date type. |
| 667 | * |
| 668 | * @param \Illuminate\Support\Fluent $column |
| 669 | * @return string |
| 670 | */ |
| 671 | protected function typeDate(Fluent $column) |
| 672 | { |
| 673 | return 'date'; |
| 674 | } |
| 675 | /** |
| 676 | * Create the column definition for a date-time type. |
| 677 | * |
| 678 | * @param \Illuminate\Support\Fluent $column |
| 679 | * @return string |
| 680 | */ |
| 681 | protected function typeDateTime(Fluent $column) |
| 682 | { |
| 683 | return $this->typeTimestamp($column); |
| 684 | } |
| 685 | /** |
| 686 | * Create the column definition for a date-time (with time zone) type. |
| 687 | * |
| 688 | * @param \Illuminate\Support\Fluent $column |
| 689 | * @return string |
| 690 | */ |
| 691 | protected function typeDateTimeTz(Fluent $column) |
| 692 | { |
| 693 | return $this->typeTimestampTz($column); |
| 694 | } |
| 695 | /** |
| 696 | * Create the column definition for a time type. |
| 697 | * |
| 698 | * @param \Illuminate\Support\Fluent $column |
| 699 | * @return string |
| 700 | */ |
| 701 | protected function typeTime(Fluent $column) |
| 702 | { |
| 703 | return 'time' . (\is_null($column->precision) ? '' : "({$column->precision})") . ' without time zone'; |
| 704 | } |
| 705 | /** |
| 706 | * Create the column definition for a time (with time zone) type. |
| 707 | * |
| 708 | * @param \Illuminate\Support\Fluent $column |
| 709 | * @return string |
| 710 | */ |
| 711 | protected function typeTimeTz(Fluent $column) |
| 712 | { |
| 713 | return 'time' . (\is_null($column->precision) ? '' : "({$column->precision})") . ' with time zone'; |
| 714 | } |
| 715 | /** |
| 716 | * Create the column definition for a timestamp type. |
| 717 | * |
| 718 | * @param \Illuminate\Support\Fluent $column |
| 719 | * @return string |
| 720 | */ |
| 721 | protected function typeTimestamp(Fluent $column) |
| 722 | { |
| 723 | $columnType = 'timestamp' . (\is_null($column->precision) ? '' : "({$column->precision})") . ' without time zone'; |
| 724 | return $column->useCurrent ? "{$columnType} default CURRENT_TIMESTAMP" : $columnType; |
| 725 | } |
| 726 | /** |
| 727 | * Create the column definition for a timestamp (with time zone) type. |
| 728 | * |
| 729 | * @param \Illuminate\Support\Fluent $column |
| 730 | * @return string |
| 731 | */ |
| 732 | protected function typeTimestampTz(Fluent $column) |
| 733 | { |
| 734 | $columnType = 'timestamp' . (\is_null($column->precision) ? '' : "({$column->precision})") . ' with time zone'; |
| 735 | return $column->useCurrent ? "{$columnType} default CURRENT_TIMESTAMP" : $columnType; |
| 736 | } |
| 737 | /** |
| 738 | * Create the column definition for a year type. |
| 739 | * |
| 740 | * @param \Illuminate\Support\Fluent $column |
| 741 | * @return string |
| 742 | */ |
| 743 | protected function typeYear(Fluent $column) |
| 744 | { |
| 745 | return $this->typeInteger($column); |
| 746 | } |
| 747 | /** |
| 748 | * Create the column definition for a binary type. |
| 749 | * |
| 750 | * @param \Illuminate\Support\Fluent $column |
| 751 | * @return string |
| 752 | */ |
| 753 | protected function typeBinary(Fluent $column) |
| 754 | { |
| 755 | return 'bytea'; |
| 756 | } |
| 757 | /** |
| 758 | * Create the column definition for a uuid type. |
| 759 | * |
| 760 | * @param \Illuminate\Support\Fluent $column |
| 761 | * @return string |
| 762 | */ |
| 763 | protected function typeUuid(Fluent $column) |
| 764 | { |
| 765 | return 'uuid'; |
| 766 | } |
| 767 | /** |
| 768 | * Create the column definition for an IP address type. |
| 769 | * |
| 770 | * @param \Illuminate\Support\Fluent $column |
| 771 | * @return string |
| 772 | */ |
| 773 | protected function typeIpAddress(Fluent $column) |
| 774 | { |
| 775 | return 'inet'; |
| 776 | } |
| 777 | /** |
| 778 | * Create the column definition for a MAC address type. |
| 779 | * |
| 780 | * @param \Illuminate\Support\Fluent $column |
| 781 | * @return string |
| 782 | */ |
| 783 | protected function typeMacAddress(Fluent $column) |
| 784 | { |
| 785 | return 'macaddr'; |
| 786 | } |
| 787 | /** |
| 788 | * Create the column definition for a spatial Geometry type. |
| 789 | * |
| 790 | * @param \Illuminate\Support\Fluent $column |
| 791 | * @return string |
| 792 | */ |
| 793 | protected function typeGeometry(Fluent $column) |
| 794 | { |
| 795 | return $this->formatPostGisType('geometry', $column); |
| 796 | } |
| 797 | /** |
| 798 | * Create the column definition for a spatial Point type. |
| 799 | * |
| 800 | * @param \Illuminate\Support\Fluent $column |
| 801 | * @return string |
| 802 | */ |
| 803 | protected function typePoint(Fluent $column) |
| 804 | { |
| 805 | return $this->formatPostGisType('point', $column); |
| 806 | } |
| 807 | /** |
| 808 | * Create the column definition for a spatial LineString type. |
| 809 | * |
| 810 | * @param \Illuminate\Support\Fluent $column |
| 811 | * @return string |
| 812 | */ |
| 813 | protected function typeLineString(Fluent $column) |
| 814 | { |
| 815 | return $this->formatPostGisType('linestring', $column); |
| 816 | } |
| 817 | /** |
| 818 | * Create the column definition for a spatial Polygon type. |
| 819 | * |
| 820 | * @param \Illuminate\Support\Fluent $column |
| 821 | * @return string |
| 822 | */ |
| 823 | protected function typePolygon(Fluent $column) |
| 824 | { |
| 825 | return $this->formatPostGisType('polygon', $column); |
| 826 | } |
| 827 | /** |
| 828 | * Create the column definition for a spatial GeometryCollection type. |
| 829 | * |
| 830 | * @param \Illuminate\Support\Fluent $column |
| 831 | * @return string |
| 832 | */ |
| 833 | protected function typeGeometryCollection(Fluent $column) |
| 834 | { |
| 835 | return $this->formatPostGisType('geometrycollection', $column); |
| 836 | } |
| 837 | /** |
| 838 | * Create the column definition for a spatial MultiPoint type. |
| 839 | * |
| 840 | * @param \Illuminate\Support\Fluent $column |
| 841 | * @return string |
| 842 | */ |
| 843 | protected function typeMultiPoint(Fluent $column) |
| 844 | { |
| 845 | return $this->formatPostGisType('multipoint', $column); |
| 846 | } |
| 847 | /** |
| 848 | * Create the column definition for a spatial MultiLineString type. |
| 849 | * |
| 850 | * @param \Illuminate\Support\Fluent $column |
| 851 | * @return string |
| 852 | */ |
| 853 | public function typeMultiLineString(Fluent $column) |
| 854 | { |
| 855 | return $this->formatPostGisType('multilinestring', $column); |
| 856 | } |
| 857 | /** |
| 858 | * Create the column definition for a spatial MultiPolygon type. |
| 859 | * |
| 860 | * @param \Illuminate\Support\Fluent $column |
| 861 | * @return string |
| 862 | */ |
| 863 | protected function typeMultiPolygon(Fluent $column) |
| 864 | { |
| 865 | return $this->formatPostGisType('multipolygon', $column); |
| 866 | } |
| 867 | /** |
| 868 | * Create the column definition for a spatial MultiPolygonZ type. |
| 869 | * |
| 870 | * @param \Illuminate\Support\Fluent $column |
| 871 | * @return string |
| 872 | */ |
| 873 | protected function typeMultiPolygonZ(Fluent $column) |
| 874 | { |
| 875 | return $this->formatPostGisType('multipolygonz', $column); |
| 876 | } |
| 877 | /** |
| 878 | * Format the column definition for a PostGIS spatial type. |
| 879 | * |
| 880 | * @param string $type |
| 881 | * @param \Illuminate\Support\Fluent $column |
| 882 | * @return string |
| 883 | */ |
| 884 | private function formatPostGisType($type, Fluent $column) |
| 885 | { |
| 886 | if ($column->isGeometry === null) { |
| 887 | return \sprintf('geography(%s, %s)', $type, $column->projection ?? '4326'); |
| 888 | } |
| 889 | if ($column->projection !== null) { |
| 890 | return \sprintf('geometry(%s, %s)', $type, $column->projection); |
| 891 | } |
| 892 | return "geometry({$type})"; |
| 893 | } |
| 894 | /** |
| 895 | * Get the SQL for a collation column modifier. |
| 896 | * |
| 897 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 898 | * @param \Illuminate\Support\Fluent $column |
| 899 | * @return string|null |
| 900 | */ |
| 901 | protected function modifyCollate(Blueprint $blueprint, Fluent $column) |
| 902 | { |
| 903 | if (!\is_null($column->collation)) { |
| 904 | return ' collate ' . $this->wrapValue($column->collation); |
| 905 | } |
| 906 | } |
| 907 | /** |
| 908 | * Get the SQL for a nullable column modifier. |
| 909 | * |
| 910 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 911 | * @param \Illuminate\Support\Fluent $column |
| 912 | * @return string|null |
| 913 | */ |
| 914 | protected function modifyNullable(Blueprint $blueprint, Fluent $column) |
| 915 | { |
| 916 | return $column->nullable ? ' null' : ' not null'; |
| 917 | } |
| 918 | /** |
| 919 | * Get the SQL for a default column modifier. |
| 920 | * |
| 921 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 922 | * @param \Illuminate\Support\Fluent $column |
| 923 | * @return string|null |
| 924 | */ |
| 925 | protected function modifyDefault(Blueprint $blueprint, Fluent $column) |
| 926 | { |
| 927 | if (!\is_null($column->default)) { |
| 928 | return ' default ' . $this->getDefaultValue($column->default); |
| 929 | } |
| 930 | } |
| 931 | /** |
| 932 | * Get the SQL for an auto-increment column modifier. |
| 933 | * |
| 934 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 935 | * @param \Illuminate\Support\Fluent $column |
| 936 | * @return string|null |
| 937 | */ |
| 938 | protected function modifyIncrement(Blueprint $blueprint, Fluent $column) |
| 939 | { |
| 940 | if ((\in_array($column->type, $this->serials) || $column->generatedAs !== null) && $column->autoIncrement) { |
| 941 | return ' primary key'; |
| 942 | } |
| 943 | } |
| 944 | /** |
| 945 | * Get the SQL for a generated virtual column modifier. |
| 946 | * |
| 947 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 948 | * @param \Illuminate\Support\Fluent $column |
| 949 | * @return string|null |
| 950 | */ |
| 951 | protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) |
| 952 | { |
| 953 | if ($column->virtualAs !== null) { |
| 954 | return " generated always as ({$column->virtualAs})"; |
| 955 | } |
| 956 | } |
| 957 | /** |
| 958 | * Get the SQL for a generated stored column modifier. |
| 959 | * |
| 960 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 961 | * @param \Illuminate\Support\Fluent $column |
| 962 | * @return string|null |
| 963 | */ |
| 964 | protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) |
| 965 | { |
| 966 | if ($column->storedAs !== null) { |
| 967 | return " generated always as ({$column->storedAs}) stored"; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 |