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
MySqlGrammar.php
1056 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 | use RuntimeException; |
| 9 | /** @internal */ |
| 10 | class MySqlGrammar extends Grammar |
| 11 | { |
| 12 | /** |
| 13 | * The possible column modifiers. |
| 14 | * |
| 15 | * @var string[] |
| 16 | */ |
| 17 | protected $modifiers = ['Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable', 'Invisible', 'Srid', 'Default', 'Increment', 'Comment', 'After', 'First']; |
| 18 | /** |
| 19 | * The possible column serials. |
| 20 | * |
| 21 | * @var string[] |
| 22 | */ |
| 23 | protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger']; |
| 24 | /** |
| 25 | * Compile a create database command. |
| 26 | * |
| 27 | * @param string $name |
| 28 | * @param \Illuminate\Database\Connection $connection |
| 29 | * @return string |
| 30 | */ |
| 31 | public function compileCreateDatabase($name, $connection) |
| 32 | { |
| 33 | return \sprintf('create database %s default character set %s default collate %s', $this->wrapValue($name), $this->wrapValue($connection->getConfig('charset')), $this->wrapValue($connection->getConfig('collation'))); |
| 34 | } |
| 35 | /** |
| 36 | * Compile a drop database if exists command. |
| 37 | * |
| 38 | * @param string $name |
| 39 | * @return string |
| 40 | */ |
| 41 | public function compileDropDatabaseIfExists($name) |
| 42 | { |
| 43 | return \sprintf('drop database if exists %s', $this->wrapValue($name)); |
| 44 | } |
| 45 | /** |
| 46 | * Compile the query to determine the list of tables. |
| 47 | * |
| 48 | * @return string |
| 49 | */ |
| 50 | public function compileTableExists() |
| 51 | { |
| 52 | return "select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'"; |
| 53 | } |
| 54 | /** |
| 55 | * Compile the query to determine the list of columns. |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public function compileColumnListing() |
| 60 | { |
| 61 | return 'select column_name as `column_name` from information_schema.columns where table_schema = ? and table_name = ?'; |
| 62 | } |
| 63 | /** |
| 64 | * Compile a create table command. |
| 65 | * |
| 66 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 67 | * @param \Illuminate\Support\Fluent $command |
| 68 | * @param \Illuminate\Database\Connection $connection |
| 69 | * @return array |
| 70 | */ |
| 71 | public function compileCreate(Blueprint $blueprint, Fluent $command, Connection $connection) |
| 72 | { |
| 73 | $sql = $this->compileCreateTable($blueprint, $command, $connection); |
| 74 | // Once we have the primary SQL, we can add the encoding option to the SQL for |
| 75 | // the table. Then, we can check if a storage engine has been supplied for |
| 76 | // the table. If so, we will add the engine declaration to the SQL query. |
| 77 | $sql = $this->compileCreateEncoding($sql, $connection, $blueprint); |
| 78 | // Finally, we will append the engine configuration onto this SQL statement as |
| 79 | // the final thing we do before returning this finished SQL. Once this gets |
| 80 | // added the query will be ready to execute against the real connections. |
| 81 | return \array_values(\array_filter(\array_merge([$this->compileCreateEngine($sql, $connection, $blueprint)], $this->compileAutoIncrementStartingValues($blueprint)))); |
| 82 | } |
| 83 | /** |
| 84 | * Create the main create table clause. |
| 85 | * |
| 86 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 87 | * @param \Illuminate\Support\Fluent $command |
| 88 | * @param \Illuminate\Database\Connection $connection |
| 89 | * @return array |
| 90 | */ |
| 91 | protected function compileCreateTable($blueprint, $command, $connection) |
| 92 | { |
| 93 | return \trim(\sprintf('%s table %s (%s)', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), \implode(', ', $this->getColumns($blueprint)))); |
| 94 | } |
| 95 | /** |
| 96 | * Append the character set specifications to a command. |
| 97 | * |
| 98 | * @param string $sql |
| 99 | * @param \Illuminate\Database\Connection $connection |
| 100 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 101 | * @return string |
| 102 | */ |
| 103 | protected function compileCreateEncoding($sql, Connection $connection, Blueprint $blueprint) |
| 104 | { |
| 105 | // First we will set the character set if one has been set on either the create |
| 106 | // blueprint itself or on the root configuration for the connection that the |
| 107 | // table is being created on. We will add these to the create table query. |
| 108 | if (isset($blueprint->charset)) { |
| 109 | $sql .= ' default character set ' . $blueprint->charset; |
| 110 | } elseif (!\is_null($charset = $connection->getConfig('charset'))) { |
| 111 | $sql .= ' default character set ' . $charset; |
| 112 | } |
| 113 | // Next we will add the collation to the create table statement if one has been |
| 114 | // added to either this create table blueprint or the configuration for this |
| 115 | // connection that the query is targeting. We'll add it to this SQL query. |
| 116 | if (isset($blueprint->collation)) { |
| 117 | $sql .= " collate '{$blueprint->collation}'"; |
| 118 | } elseif (!\is_null($collation = $connection->getConfig('collation'))) { |
| 119 | $sql .= " collate '{$collation}'"; |
| 120 | } |
| 121 | return $sql; |
| 122 | } |
| 123 | /** |
| 124 | * Append the engine specifications to a command. |
| 125 | * |
| 126 | * @param string $sql |
| 127 | * @param \Illuminate\Database\Connection $connection |
| 128 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 129 | * @return string |
| 130 | */ |
| 131 | protected function compileCreateEngine($sql, Connection $connection, Blueprint $blueprint) |
| 132 | { |
| 133 | if (isset($blueprint->engine)) { |
| 134 | return $sql . ' engine = ' . $blueprint->engine; |
| 135 | } elseif (!\is_null($engine = $connection->getConfig('engine'))) { |
| 136 | return $sql . ' engine = ' . $engine; |
| 137 | } |
| 138 | return $sql; |
| 139 | } |
| 140 | /** |
| 141 | * Compile an add column command. |
| 142 | * |
| 143 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 144 | * @param \Illuminate\Support\Fluent $command |
| 145 | * @return array |
| 146 | */ |
| 147 | public function compileAdd(Blueprint $blueprint, Fluent $command) |
| 148 | { |
| 149 | $columns = $this->prefixArray('add', $this->getColumns($blueprint)); |
| 150 | return \array_values(\array_merge(['alter table ' . $this->wrapTable($blueprint) . ' ' . \implode(', ', $columns)], $this->compileAutoIncrementStartingValues($blueprint))); |
| 151 | } |
| 152 | /** |
| 153 | * Compile the auto-incrementing column starting values. |
| 154 | * |
| 155 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 156 | * @return array |
| 157 | */ |
| 158 | public function compileAutoIncrementStartingValues(Blueprint $blueprint) |
| 159 | { |
| 160 | return \IAWPSCOPED\collect($blueprint->autoIncrementingStartingValues())->map(function ($value, $column) use($blueprint) { |
| 161 | return 'alter table ' . $this->wrapTable($blueprint->getTable()) . ' auto_increment = ' . $value; |
| 162 | })->all(); |
| 163 | } |
| 164 | /** |
| 165 | * Compile a rename column command. |
| 166 | * |
| 167 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 168 | * @param \Illuminate\Support\Fluent $command |
| 169 | * @param \Illuminate\Database\Connection $connection |
| 170 | * @return array|string |
| 171 | */ |
| 172 | public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) |
| 173 | { |
| 174 | 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); |
| 175 | } |
| 176 | /** |
| 177 | * Compile a primary key command. |
| 178 | * |
| 179 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 180 | * @param \Illuminate\Support\Fluent $command |
| 181 | * @return string |
| 182 | */ |
| 183 | public function compilePrimary(Blueprint $blueprint, Fluent $command) |
| 184 | { |
| 185 | return \sprintf('alter table %s add primary key %s(%s)', $this->wrapTable($blueprint), $command->algorithm ? 'using ' . $command->algorithm : '', $this->columnize($command->columns)); |
| 186 | } |
| 187 | /** |
| 188 | * Compile a unique key command. |
| 189 | * |
| 190 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 191 | * @param \Illuminate\Support\Fluent $command |
| 192 | * @return string |
| 193 | */ |
| 194 | public function compileUnique(Blueprint $blueprint, Fluent $command) |
| 195 | { |
| 196 | return $this->compileKey($blueprint, $command, 'unique'); |
| 197 | } |
| 198 | /** |
| 199 | * Compile a plain index key command. |
| 200 | * |
| 201 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 202 | * @param \Illuminate\Support\Fluent $command |
| 203 | * @return string |
| 204 | */ |
| 205 | public function compileIndex(Blueprint $blueprint, Fluent $command) |
| 206 | { |
| 207 | return $this->compileKey($blueprint, $command, 'index'); |
| 208 | } |
| 209 | /** |
| 210 | * Compile a fulltext index key command. |
| 211 | * |
| 212 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 213 | * @param \Illuminate\Support\Fluent $command |
| 214 | * @return string |
| 215 | */ |
| 216 | public function compileFullText(Blueprint $blueprint, Fluent $command) |
| 217 | { |
| 218 | return $this->compileKey($blueprint, $command, 'fulltext'); |
| 219 | } |
| 220 | /** |
| 221 | * Compile a spatial index key command. |
| 222 | * |
| 223 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 224 | * @param \Illuminate\Support\Fluent $command |
| 225 | * @return string |
| 226 | */ |
| 227 | public function compileSpatialIndex(Blueprint $blueprint, Fluent $command) |
| 228 | { |
| 229 | return $this->compileKey($blueprint, $command, 'spatial index'); |
| 230 | } |
| 231 | /** |
| 232 | * Compile an index creation command. |
| 233 | * |
| 234 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 235 | * @param \Illuminate\Support\Fluent $command |
| 236 | * @param string $type |
| 237 | * @return string |
| 238 | */ |
| 239 | protected function compileKey(Blueprint $blueprint, Fluent $command, $type) |
| 240 | { |
| 241 | return \sprintf('alter table %s add %s %s%s(%s)', $this->wrapTable($blueprint), $type, $this->wrap($command->index), $command->algorithm ? ' using ' . $command->algorithm : '', $this->columnize($command->columns)); |
| 242 | } |
| 243 | /** |
| 244 | * Compile a drop table command. |
| 245 | * |
| 246 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 247 | * @param \Illuminate\Support\Fluent $command |
| 248 | * @return string |
| 249 | */ |
| 250 | public function compileDrop(Blueprint $blueprint, Fluent $command) |
| 251 | { |
| 252 | return 'drop table ' . $this->wrapTable($blueprint); |
| 253 | } |
| 254 | /** |
| 255 | * Compile a drop table (if exists) command. |
| 256 | * |
| 257 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 258 | * @param \Illuminate\Support\Fluent $command |
| 259 | * @return string |
| 260 | */ |
| 261 | public function compileDropIfExists(Blueprint $blueprint, Fluent $command) |
| 262 | { |
| 263 | return 'drop table if exists ' . $this->wrapTable($blueprint); |
| 264 | } |
| 265 | /** |
| 266 | * Compile a drop column command. |
| 267 | * |
| 268 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 269 | * @param \Illuminate\Support\Fluent $command |
| 270 | * @return string |
| 271 | */ |
| 272 | public function compileDropColumn(Blueprint $blueprint, Fluent $command) |
| 273 | { |
| 274 | $columns = $this->prefixArray('drop', $this->wrapArray($command->columns)); |
| 275 | return 'alter table ' . $this->wrapTable($blueprint) . ' ' . \implode(', ', $columns); |
| 276 | } |
| 277 | /** |
| 278 | * Compile a drop primary key command. |
| 279 | * |
| 280 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 281 | * @param \Illuminate\Support\Fluent $command |
| 282 | * @return string |
| 283 | */ |
| 284 | public function compileDropPrimary(Blueprint $blueprint, Fluent $command) |
| 285 | { |
| 286 | return 'alter table ' . $this->wrapTable($blueprint) . ' drop primary key'; |
| 287 | } |
| 288 | /** |
| 289 | * Compile a drop unique key command. |
| 290 | * |
| 291 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 292 | * @param \Illuminate\Support\Fluent $command |
| 293 | * @return string |
| 294 | */ |
| 295 | public function compileDropUnique(Blueprint $blueprint, Fluent $command) |
| 296 | { |
| 297 | $index = $this->wrap($command->index); |
| 298 | return "alter table {$this->wrapTable($blueprint)} drop index {$index}"; |
| 299 | } |
| 300 | /** |
| 301 | * Compile a drop index command. |
| 302 | * |
| 303 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 304 | * @param \Illuminate\Support\Fluent $command |
| 305 | * @return string |
| 306 | */ |
| 307 | public function compileDropIndex(Blueprint $blueprint, Fluent $command) |
| 308 | { |
| 309 | $index = $this->wrap($command->index); |
| 310 | return "alter table {$this->wrapTable($blueprint)} drop index {$index}"; |
| 311 | } |
| 312 | /** |
| 313 | * Compile a drop fulltext index command. |
| 314 | * |
| 315 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 316 | * @param \Illuminate\Support\Fluent $command |
| 317 | * @return string |
| 318 | */ |
| 319 | public function compileDropFullText(Blueprint $blueprint, Fluent $command) |
| 320 | { |
| 321 | return $this->compileDropIndex($blueprint, $command); |
| 322 | } |
| 323 | /** |
| 324 | * Compile a drop spatial index command. |
| 325 | * |
| 326 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 327 | * @param \Illuminate\Support\Fluent $command |
| 328 | * @return string |
| 329 | */ |
| 330 | public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command) |
| 331 | { |
| 332 | return $this->compileDropIndex($blueprint, $command); |
| 333 | } |
| 334 | /** |
| 335 | * Compile a drop foreign key command. |
| 336 | * |
| 337 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 338 | * @param \Illuminate\Support\Fluent $command |
| 339 | * @return string |
| 340 | */ |
| 341 | public function compileDropForeign(Blueprint $blueprint, Fluent $command) |
| 342 | { |
| 343 | $index = $this->wrap($command->index); |
| 344 | return "alter table {$this->wrapTable($blueprint)} drop foreign key {$index}"; |
| 345 | } |
| 346 | /** |
| 347 | * Compile a rename table command. |
| 348 | * |
| 349 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 350 | * @param \Illuminate\Support\Fluent $command |
| 351 | * @return string |
| 352 | */ |
| 353 | public function compileRename(Blueprint $blueprint, Fluent $command) |
| 354 | { |
| 355 | $from = $this->wrapTable($blueprint); |
| 356 | return "rename table {$from} to " . $this->wrapTable($command->to); |
| 357 | } |
| 358 | /** |
| 359 | * Compile a rename index command. |
| 360 | * |
| 361 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 362 | * @param \Illuminate\Support\Fluent $command |
| 363 | * @return string |
| 364 | */ |
| 365 | public function compileRenameIndex(Blueprint $blueprint, Fluent $command) |
| 366 | { |
| 367 | return \sprintf('alter table %s rename index %s to %s', $this->wrapTable($blueprint), $this->wrap($command->from), $this->wrap($command->to)); |
| 368 | } |
| 369 | /** |
| 370 | * Compile the SQL needed to drop all tables. |
| 371 | * |
| 372 | * @param array $tables |
| 373 | * @return string |
| 374 | */ |
| 375 | public function compileDropAllTables($tables) |
| 376 | { |
| 377 | return 'drop table ' . \implode(',', $this->wrapArray($tables)); |
| 378 | } |
| 379 | /** |
| 380 | * Compile the SQL needed to drop all views. |
| 381 | * |
| 382 | * @param array $views |
| 383 | * @return string |
| 384 | */ |
| 385 | public function compileDropAllViews($views) |
| 386 | { |
| 387 | return 'drop view ' . \implode(',', $this->wrapArray($views)); |
| 388 | } |
| 389 | /** |
| 390 | * Compile the SQL needed to retrieve all table names. |
| 391 | * |
| 392 | * @return string |
| 393 | */ |
| 394 | public function compileGetAllTables() |
| 395 | { |
| 396 | return 'SHOW FULL TABLES WHERE table_type = \'BASE TABLE\''; |
| 397 | } |
| 398 | /** |
| 399 | * Compile the SQL needed to retrieve all view names. |
| 400 | * |
| 401 | * @return string |
| 402 | */ |
| 403 | public function compileGetAllViews() |
| 404 | { |
| 405 | return 'SHOW FULL TABLES WHERE table_type = \'VIEW\''; |
| 406 | } |
| 407 | /** |
| 408 | * Compile the command to enable foreign key constraints. |
| 409 | * |
| 410 | * @return string |
| 411 | */ |
| 412 | public function compileEnableForeignKeyConstraints() |
| 413 | { |
| 414 | return 'SET FOREIGN_KEY_CHECKS=1;'; |
| 415 | } |
| 416 | /** |
| 417 | * Compile the command to disable foreign key constraints. |
| 418 | * |
| 419 | * @return string |
| 420 | */ |
| 421 | public function compileDisableForeignKeyConstraints() |
| 422 | { |
| 423 | return 'SET FOREIGN_KEY_CHECKS=0;'; |
| 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('alter table %s comment = %s', $this->wrapTable($blueprint), "'" . \str_replace("'", "''", $command->comment) . "'"); |
| 435 | } |
| 436 | /** |
| 437 | * Create the column definition for a char type. |
| 438 | * |
| 439 | * @param \Illuminate\Support\Fluent $column |
| 440 | * @return string |
| 441 | */ |
| 442 | protected function typeChar(Fluent $column) |
| 443 | { |
| 444 | return "char({$column->length})"; |
| 445 | } |
| 446 | /** |
| 447 | * Create the column definition for a string type. |
| 448 | * |
| 449 | * @param \Illuminate\Support\Fluent $column |
| 450 | * @return string |
| 451 | */ |
| 452 | protected function typeString(Fluent $column) |
| 453 | { |
| 454 | return "varchar({$column->length})"; |
| 455 | } |
| 456 | /** |
| 457 | * Create the column definition for a tiny text type. |
| 458 | * |
| 459 | * @param \Illuminate\Support\Fluent $column |
| 460 | * @return string |
| 461 | */ |
| 462 | protected function typeTinyText(Fluent $column) |
| 463 | { |
| 464 | return 'tinytext'; |
| 465 | } |
| 466 | /** |
| 467 | * Create the column definition for a text type. |
| 468 | * |
| 469 | * @param \Illuminate\Support\Fluent $column |
| 470 | * @return string |
| 471 | */ |
| 472 | protected function typeText(Fluent $column) |
| 473 | { |
| 474 | return 'text'; |
| 475 | } |
| 476 | /** |
| 477 | * Create the column definition for a medium text type. |
| 478 | * |
| 479 | * @param \Illuminate\Support\Fluent $column |
| 480 | * @return string |
| 481 | */ |
| 482 | protected function typeMediumText(Fluent $column) |
| 483 | { |
| 484 | return 'mediumtext'; |
| 485 | } |
| 486 | /** |
| 487 | * Create the column definition for a long text type. |
| 488 | * |
| 489 | * @param \Illuminate\Support\Fluent $column |
| 490 | * @return string |
| 491 | */ |
| 492 | protected function typeLongText(Fluent $column) |
| 493 | { |
| 494 | return 'longtext'; |
| 495 | } |
| 496 | /** |
| 497 | * Create the column definition for a big integer type. |
| 498 | * |
| 499 | * @param \Illuminate\Support\Fluent $column |
| 500 | * @return string |
| 501 | */ |
| 502 | protected function typeBigInteger(Fluent $column) |
| 503 | { |
| 504 | return 'bigint'; |
| 505 | } |
| 506 | /** |
| 507 | * Create the column definition for an integer type. |
| 508 | * |
| 509 | * @param \Illuminate\Support\Fluent $column |
| 510 | * @return string |
| 511 | */ |
| 512 | protected function typeInteger(Fluent $column) |
| 513 | { |
| 514 | return 'int'; |
| 515 | } |
| 516 | /** |
| 517 | * Create the column definition for a medium integer type. |
| 518 | * |
| 519 | * @param \Illuminate\Support\Fluent $column |
| 520 | * @return string |
| 521 | */ |
| 522 | protected function typeMediumInteger(Fluent $column) |
| 523 | { |
| 524 | return 'mediumint'; |
| 525 | } |
| 526 | /** |
| 527 | * Create the column definition for a tiny integer type. |
| 528 | * |
| 529 | * @param \Illuminate\Support\Fluent $column |
| 530 | * @return string |
| 531 | */ |
| 532 | protected function typeTinyInteger(Fluent $column) |
| 533 | { |
| 534 | return 'tinyint'; |
| 535 | } |
| 536 | /** |
| 537 | * Create the column definition for a small integer type. |
| 538 | * |
| 539 | * @param \Illuminate\Support\Fluent $column |
| 540 | * @return string |
| 541 | */ |
| 542 | protected function typeSmallInteger(Fluent $column) |
| 543 | { |
| 544 | return 'smallint'; |
| 545 | } |
| 546 | /** |
| 547 | * Create the column definition for a float type. |
| 548 | * |
| 549 | * @param \Illuminate\Support\Fluent $column |
| 550 | * @return string |
| 551 | */ |
| 552 | protected function typeFloat(Fluent $column) |
| 553 | { |
| 554 | return $this->typeDouble($column); |
| 555 | } |
| 556 | /** |
| 557 | * Create the column definition for a double type. |
| 558 | * |
| 559 | * @param \Illuminate\Support\Fluent $column |
| 560 | * @return string |
| 561 | */ |
| 562 | protected function typeDouble(Fluent $column) |
| 563 | { |
| 564 | if ($column->total && $column->places) { |
| 565 | return "double({$column->total}, {$column->places})"; |
| 566 | } |
| 567 | return 'double'; |
| 568 | } |
| 569 | /** |
| 570 | * Create the column definition for a decimal type. |
| 571 | * |
| 572 | * @param \Illuminate\Support\Fluent $column |
| 573 | * @return string |
| 574 | */ |
| 575 | protected function typeDecimal(Fluent $column) |
| 576 | { |
| 577 | return "decimal({$column->total}, {$column->places})"; |
| 578 | } |
| 579 | /** |
| 580 | * Create the column definition for a boolean type. |
| 581 | * |
| 582 | * @param \Illuminate\Support\Fluent $column |
| 583 | * @return string |
| 584 | */ |
| 585 | protected function typeBoolean(Fluent $column) |
| 586 | { |
| 587 | return 'tinyint(1)'; |
| 588 | } |
| 589 | /** |
| 590 | * Create the column definition for an enumeration type. |
| 591 | * |
| 592 | * @param \Illuminate\Support\Fluent $column |
| 593 | * @return string |
| 594 | */ |
| 595 | protected function typeEnum(Fluent $column) |
| 596 | { |
| 597 | return \sprintf('enum(%s)', $this->quoteString($column->allowed)); |
| 598 | } |
| 599 | /** |
| 600 | * Create the column definition for a set enumeration type. |
| 601 | * |
| 602 | * @param \Illuminate\Support\Fluent $column |
| 603 | * @return string |
| 604 | */ |
| 605 | protected function typeSet(Fluent $column) |
| 606 | { |
| 607 | return \sprintf('set(%s)', $this->quoteString($column->allowed)); |
| 608 | } |
| 609 | /** |
| 610 | * Create the column definition for a json type. |
| 611 | * |
| 612 | * @param \Illuminate\Support\Fluent $column |
| 613 | * @return string |
| 614 | */ |
| 615 | protected function typeJson(Fluent $column) |
| 616 | { |
| 617 | return 'json'; |
| 618 | } |
| 619 | /** |
| 620 | * Create the column definition for a jsonb type. |
| 621 | * |
| 622 | * @param \Illuminate\Support\Fluent $column |
| 623 | * @return string |
| 624 | */ |
| 625 | protected function typeJsonb(Fluent $column) |
| 626 | { |
| 627 | return 'json'; |
| 628 | } |
| 629 | /** |
| 630 | * Create the column definition for a date type. |
| 631 | * |
| 632 | * @param \Illuminate\Support\Fluent $column |
| 633 | * @return string |
| 634 | */ |
| 635 | protected function typeDate(Fluent $column) |
| 636 | { |
| 637 | return 'date'; |
| 638 | } |
| 639 | /** |
| 640 | * Create the column definition for a date-time type. |
| 641 | * |
| 642 | * @param \Illuminate\Support\Fluent $column |
| 643 | * @return string |
| 644 | */ |
| 645 | protected function typeDateTime(Fluent $column) |
| 646 | { |
| 647 | $columnType = $column->precision ? "datetime({$column->precision})" : 'datetime'; |
| 648 | $current = $column->precision ? "CURRENT_TIMESTAMP({$column->precision})" : 'CURRENT_TIMESTAMP'; |
| 649 | $columnType = $column->useCurrent ? "{$columnType} default {$current}" : $columnType; |
| 650 | return $column->useCurrentOnUpdate ? "{$columnType} on update {$current}" : $columnType; |
| 651 | } |
| 652 | /** |
| 653 | * Create the column definition for a date-time (with time zone) type. |
| 654 | * |
| 655 | * @param \Illuminate\Support\Fluent $column |
| 656 | * @return string |
| 657 | */ |
| 658 | protected function typeDateTimeTz(Fluent $column) |
| 659 | { |
| 660 | return $this->typeDateTime($column); |
| 661 | } |
| 662 | /** |
| 663 | * Create the column definition for a time type. |
| 664 | * |
| 665 | * @param \Illuminate\Support\Fluent $column |
| 666 | * @return string |
| 667 | */ |
| 668 | protected function typeTime(Fluent $column) |
| 669 | { |
| 670 | return $column->precision ? "time({$column->precision})" : 'time'; |
| 671 | } |
| 672 | /** |
| 673 | * Create the column definition for a time (with time zone) type. |
| 674 | * |
| 675 | * @param \Illuminate\Support\Fluent $column |
| 676 | * @return string |
| 677 | */ |
| 678 | protected function typeTimeTz(Fluent $column) |
| 679 | { |
| 680 | return $this->typeTime($column); |
| 681 | } |
| 682 | /** |
| 683 | * Create the column definition for a timestamp type. |
| 684 | * |
| 685 | * @param \Illuminate\Support\Fluent $column |
| 686 | * @return string |
| 687 | */ |
| 688 | protected function typeTimestamp(Fluent $column) |
| 689 | { |
| 690 | $columnType = $column->precision ? "timestamp({$column->precision})" : 'timestamp'; |
| 691 | $current = $column->precision ? "CURRENT_TIMESTAMP({$column->precision})" : 'CURRENT_TIMESTAMP'; |
| 692 | $columnType = $column->useCurrent ? "{$columnType} default {$current}" : $columnType; |
| 693 | return $column->useCurrentOnUpdate ? "{$columnType} on update {$current}" : $columnType; |
| 694 | } |
| 695 | /** |
| 696 | * Create the column definition for a timestamp (with time zone) type. |
| 697 | * |
| 698 | * @param \Illuminate\Support\Fluent $column |
| 699 | * @return string |
| 700 | */ |
| 701 | protected function typeTimestampTz(Fluent $column) |
| 702 | { |
| 703 | return $this->typeTimestamp($column); |
| 704 | } |
| 705 | /** |
| 706 | * Create the column definition for a year type. |
| 707 | * |
| 708 | * @param \Illuminate\Support\Fluent $column |
| 709 | * @return string |
| 710 | */ |
| 711 | protected function typeYear(Fluent $column) |
| 712 | { |
| 713 | return 'year'; |
| 714 | } |
| 715 | /** |
| 716 | * Create the column definition for a binary type. |
| 717 | * |
| 718 | * @param \Illuminate\Support\Fluent $column |
| 719 | * @return string |
| 720 | */ |
| 721 | protected function typeBinary(Fluent $column) |
| 722 | { |
| 723 | return 'blob'; |
| 724 | } |
| 725 | /** |
| 726 | * Create the column definition for a uuid type. |
| 727 | * |
| 728 | * @param \Illuminate\Support\Fluent $column |
| 729 | * @return string |
| 730 | */ |
| 731 | protected function typeUuid(Fluent $column) |
| 732 | { |
| 733 | return 'char(36)'; |
| 734 | } |
| 735 | /** |
| 736 | * Create the column definition for an IP address type. |
| 737 | * |
| 738 | * @param \Illuminate\Support\Fluent $column |
| 739 | * @return string |
| 740 | */ |
| 741 | protected function typeIpAddress(Fluent $column) |
| 742 | { |
| 743 | return 'varchar(45)'; |
| 744 | } |
| 745 | /** |
| 746 | * Create the column definition for a MAC address type. |
| 747 | * |
| 748 | * @param \Illuminate\Support\Fluent $column |
| 749 | * @return string |
| 750 | */ |
| 751 | protected function typeMacAddress(Fluent $column) |
| 752 | { |
| 753 | return 'varchar(17)'; |
| 754 | } |
| 755 | /** |
| 756 | * Create the column definition for a spatial Geometry type. |
| 757 | * |
| 758 | * @param \Illuminate\Support\Fluent $column |
| 759 | * @return string |
| 760 | */ |
| 761 | public function typeGeometry(Fluent $column) |
| 762 | { |
| 763 | return 'geometry'; |
| 764 | } |
| 765 | /** |
| 766 | * Create the column definition for a spatial Point type. |
| 767 | * |
| 768 | * @param \Illuminate\Support\Fluent $column |
| 769 | * @return string |
| 770 | */ |
| 771 | public function typePoint(Fluent $column) |
| 772 | { |
| 773 | return 'point'; |
| 774 | } |
| 775 | /** |
| 776 | * Create the column definition for a spatial LineString type. |
| 777 | * |
| 778 | * @param \Illuminate\Support\Fluent $column |
| 779 | * @return string |
| 780 | */ |
| 781 | public function typeLineString(Fluent $column) |
| 782 | { |
| 783 | return 'linestring'; |
| 784 | } |
| 785 | /** |
| 786 | * Create the column definition for a spatial Polygon type. |
| 787 | * |
| 788 | * @param \Illuminate\Support\Fluent $column |
| 789 | * @return string |
| 790 | */ |
| 791 | public function typePolygon(Fluent $column) |
| 792 | { |
| 793 | return 'polygon'; |
| 794 | } |
| 795 | /** |
| 796 | * Create the column definition for a spatial GeometryCollection type. |
| 797 | * |
| 798 | * @param \Illuminate\Support\Fluent $column |
| 799 | * @return string |
| 800 | */ |
| 801 | public function typeGeometryCollection(Fluent $column) |
| 802 | { |
| 803 | return 'geometrycollection'; |
| 804 | } |
| 805 | /** |
| 806 | * Create the column definition for a spatial MultiPoint type. |
| 807 | * |
| 808 | * @param \Illuminate\Support\Fluent $column |
| 809 | * @return string |
| 810 | */ |
| 811 | public function typeMultiPoint(Fluent $column) |
| 812 | { |
| 813 | return 'multipoint'; |
| 814 | } |
| 815 | /** |
| 816 | * Create the column definition for a spatial MultiLineString type. |
| 817 | * |
| 818 | * @param \Illuminate\Support\Fluent $column |
| 819 | * @return string |
| 820 | */ |
| 821 | public function typeMultiLineString(Fluent $column) |
| 822 | { |
| 823 | return 'multilinestring'; |
| 824 | } |
| 825 | /** |
| 826 | * Create the column definition for a spatial MultiPolygon type. |
| 827 | * |
| 828 | * @param \Illuminate\Support\Fluent $column |
| 829 | * @return string |
| 830 | */ |
| 831 | public function typeMultiPolygon(Fluent $column) |
| 832 | { |
| 833 | return 'multipolygon'; |
| 834 | } |
| 835 | /** |
| 836 | * Create the column definition for a generated, computed column type. |
| 837 | * |
| 838 | * @param \Illuminate\Support\Fluent $column |
| 839 | * @return void |
| 840 | * |
| 841 | * @throws \RuntimeException |
| 842 | */ |
| 843 | protected function typeComputed(Fluent $column) |
| 844 | { |
| 845 | throw new RuntimeException('This database driver requires a type, see the virtualAs / storedAs modifiers.'); |
| 846 | } |
| 847 | /** |
| 848 | * Get the SQL for a generated virtual column modifier. |
| 849 | * |
| 850 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 851 | * @param \Illuminate\Support\Fluent $column |
| 852 | * @return string|null |
| 853 | */ |
| 854 | protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) |
| 855 | { |
| 856 | if (!\is_null($virtualAs = $column->virtualAsJson)) { |
| 857 | if ($this->isJsonSelector($virtualAs)) { |
| 858 | $virtualAs = $this->wrapJsonSelector($virtualAs); |
| 859 | } |
| 860 | return " as ({$virtualAs})"; |
| 861 | } |
| 862 | if (!\is_null($virtualAs = $column->virtualAs)) { |
| 863 | return " as ({$virtualAs})"; |
| 864 | } |
| 865 | } |
| 866 | /** |
| 867 | * Get the SQL for a generated stored column modifier. |
| 868 | * |
| 869 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 870 | * @param \Illuminate\Support\Fluent $column |
| 871 | * @return string|null |
| 872 | */ |
| 873 | protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) |
| 874 | { |
| 875 | if (!\is_null($storedAs = $column->storedAsJson)) { |
| 876 | if ($this->isJsonSelector($storedAs)) { |
| 877 | $storedAs = $this->wrapJsonSelector($storedAs); |
| 878 | } |
| 879 | return " as ({$storedAs}) stored"; |
| 880 | } |
| 881 | if (!\is_null($storedAs = $column->storedAs)) { |
| 882 | return " as ({$storedAs}) stored"; |
| 883 | } |
| 884 | } |
| 885 | /** |
| 886 | * Get the SQL for an unsigned column modifier. |
| 887 | * |
| 888 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 889 | * @param \Illuminate\Support\Fluent $column |
| 890 | * @return string|null |
| 891 | */ |
| 892 | protected function modifyUnsigned(Blueprint $blueprint, Fluent $column) |
| 893 | { |
| 894 | if ($column->unsigned) { |
| 895 | return ' unsigned'; |
| 896 | } |
| 897 | } |
| 898 | /** |
| 899 | * Get the SQL for a character set column modifier. |
| 900 | * |
| 901 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 902 | * @param \Illuminate\Support\Fluent $column |
| 903 | * @return string|null |
| 904 | */ |
| 905 | protected function modifyCharset(Blueprint $blueprint, Fluent $column) |
| 906 | { |
| 907 | if (!\is_null($column->charset)) { |
| 908 | return ' character set ' . $column->charset; |
| 909 | } |
| 910 | } |
| 911 | /** |
| 912 | * Get the SQL for a collation column modifier. |
| 913 | * |
| 914 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 915 | * @param \Illuminate\Support\Fluent $column |
| 916 | * @return string|null |
| 917 | */ |
| 918 | protected function modifyCollate(Blueprint $blueprint, Fluent $column) |
| 919 | { |
| 920 | if (!\is_null($column->collation)) { |
| 921 | return " collate '{$column->collation}'"; |
| 922 | } |
| 923 | } |
| 924 | /** |
| 925 | * Get the SQL for a nullable column modifier. |
| 926 | * |
| 927 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 928 | * @param \Illuminate\Support\Fluent $column |
| 929 | * @return string|null |
| 930 | */ |
| 931 | protected function modifyNullable(Blueprint $blueprint, Fluent $column) |
| 932 | { |
| 933 | if (\is_null($column->virtualAs) && \is_null($column->virtualAsJson) && \is_null($column->storedAs) && \is_null($column->storedAsJson)) { |
| 934 | return $column->nullable ? ' null' : ' not null'; |
| 935 | } |
| 936 | if ($column->nullable === \false) { |
| 937 | return ' not null'; |
| 938 | } |
| 939 | } |
| 940 | /** |
| 941 | * Get the SQL for an invisible column modifier. |
| 942 | * |
| 943 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 944 | * @param \Illuminate\Support\Fluent $column |
| 945 | * @return string|null |
| 946 | */ |
| 947 | protected function modifyInvisible(Blueprint $blueprint, Fluent $column) |
| 948 | { |
| 949 | if (!\is_null($column->invisible)) { |
| 950 | return ' invisible'; |
| 951 | } |
| 952 | } |
| 953 | /** |
| 954 | * Get the SQL for a default column modifier. |
| 955 | * |
| 956 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 957 | * @param \Illuminate\Support\Fluent $column |
| 958 | * @return string|null |
| 959 | */ |
| 960 | protected function modifyDefault(Blueprint $blueprint, Fluent $column) |
| 961 | { |
| 962 | if (!\is_null($column->default)) { |
| 963 | return ' default ' . $this->getDefaultValue($column->default); |
| 964 | } |
| 965 | } |
| 966 | /** |
| 967 | * Get the SQL for an auto-increment column modifier. |
| 968 | * |
| 969 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 970 | * @param \Illuminate\Support\Fluent $column |
| 971 | * @return string|null |
| 972 | */ |
| 973 | protected function modifyIncrement(Blueprint $blueprint, Fluent $column) |
| 974 | { |
| 975 | if (\in_array($column->type, $this->serials) && $column->autoIncrement) { |
| 976 | return ' auto_increment primary key'; |
| 977 | } |
| 978 | } |
| 979 | /** |
| 980 | * Get the SQL for a "first" column modifier. |
| 981 | * |
| 982 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 983 | * @param \Illuminate\Support\Fluent $column |
| 984 | * @return string|null |
| 985 | */ |
| 986 | protected function modifyFirst(Blueprint $blueprint, Fluent $column) |
| 987 | { |
| 988 | if (!\is_null($column->first)) { |
| 989 | return ' first'; |
| 990 | } |
| 991 | } |
| 992 | /** |
| 993 | * Get the SQL for an "after" column modifier. |
| 994 | * |
| 995 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 996 | * @param \Illuminate\Support\Fluent $column |
| 997 | * @return string|null |
| 998 | */ |
| 999 | protected function modifyAfter(Blueprint $blueprint, Fluent $column) |
| 1000 | { |
| 1001 | if (!\is_null($column->after)) { |
| 1002 | return ' after ' . $this->wrap($column->after); |
| 1003 | } |
| 1004 | } |
| 1005 | /** |
| 1006 | * Get the SQL for a "comment" column modifier. |
| 1007 | * |
| 1008 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 1009 | * @param \Illuminate\Support\Fluent $column |
| 1010 | * @return string|null |
| 1011 | */ |
| 1012 | protected function modifyComment(Blueprint $blueprint, Fluent $column) |
| 1013 | { |
| 1014 | if (!\is_null($column->comment)) { |
| 1015 | return " comment '" . \addslashes($column->comment) . "'"; |
| 1016 | } |
| 1017 | } |
| 1018 | /** |
| 1019 | * Get the SQL for a SRID column modifier. |
| 1020 | * |
| 1021 | * @param \Illuminate\Database\Schema\Blueprint $blueprint |
| 1022 | * @param \Illuminate\Support\Fluent $column |
| 1023 | * @return string|null |
| 1024 | */ |
| 1025 | protected function modifySrid(Blueprint $blueprint, Fluent $column) |
| 1026 | { |
| 1027 | if (\is_int($column->srid) && $column->srid > 0) { |
| 1028 | return ' srid ' . $column->srid; |
| 1029 | } |
| 1030 | } |
| 1031 | /** |
| 1032 | * Wrap a single string in keyword identifiers. |
| 1033 | * |
| 1034 | * @param string $value |
| 1035 | * @return string |
| 1036 | */ |
| 1037 | protected function wrapValue($value) |
| 1038 | { |
| 1039 | if ($value !== '*') { |
| 1040 | return '`' . \str_replace('`', '``', $value) . '`'; |
| 1041 | } |
| 1042 | return $value; |
| 1043 | } |
| 1044 | /** |
| 1045 | * Wrap the given JSON selector. |
| 1046 | * |
| 1047 | * @param string $value |
| 1048 | * @return string |
| 1049 | */ |
| 1050 | protected function wrapJsonSelector($value) |
| 1051 | { |
| 1052 | [$field, $path] = $this->wrapJsonFieldAndPath($value); |
| 1053 | return 'json_unquote(json_extract(' . $field . $path . '))'; |
| 1054 | } |
| 1055 | } |
| 1056 |