| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342 |
- <?php
- namespace Illuminate\Database\Schema\Grammars;
- use Illuminate\Database\Connection;
- use Illuminate\Database\Query\Expression;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Schema\ColumnDefinition;
- use Illuminate\Support\Fluent;
- use RuntimeException;
- class MySqlGrammar extends Grammar
- {
- /**
- * The possible column modifiers.
- *
- * @var string[]
- */
- protected $modifiers = [
- 'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable',
- 'Default', 'OnUpdate', 'Invisible', 'Increment', 'Comment', 'After', 'First',
- ];
- /**
- * The possible column serials.
- *
- * @var string[]
- */
- protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger'];
- /**
- * The commands to be executed outside of create or alter command.
- *
- * @var string[]
- */
- protected $fluentCommands = ['AutoIncrementStartingValues'];
- /**
- * Compile a create database command.
- *
- * @param string $name
- * @param \Illuminate\Database\Connection $connection
- * @return string
- */
- public function compileCreateDatabase($name, $connection)
- {
- $charset = $connection->getConfig('charset');
- $collation = $connection->getConfig('collation');
- if (! $charset || ! $collation) {
- return sprintf(
- 'create database %s',
- $this->wrapValue($name),
- );
- }
- return sprintf(
- 'create database %s default character set %s default collate %s',
- $this->wrapValue($name),
- $this->wrapValue($charset),
- $this->wrapValue($collation),
- );
- }
- /**
- * Compile a drop database if exists command.
- *
- * @param string $name
- * @return string
- */
- public function compileDropDatabaseIfExists($name)
- {
- return sprintf(
- 'drop database if exists %s',
- $this->wrapValue($name)
- );
- }
- /**
- * Compile the query to determine the tables.
- *
- * @param string $database
- * @return string
- */
- public function compileTables($database)
- {
- return sprintf(
- 'select table_name as `name`, (data_length + index_length) as `size`, '
- .'table_comment as `comment`, engine as `engine`, table_collation as `collation` '
- ."from information_schema.tables where table_schema = %s and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') "
- .'order by table_name',
- $this->quoteString($database)
- );
- }
- /**
- * Compile the query to determine the views.
- *
- * @param string $database
- * @return string
- */
- public function compileViews($database)
- {
- return sprintf(
- 'select table_name as `name`, view_definition as `definition` '
- .'from information_schema.views where table_schema = %s '
- .'order by table_name',
- $this->quoteString($database)
- );
- }
- /**
- * Compile the query to determine the columns.
- *
- * @param string $database
- * @param string $table
- * @return string
- */
- public function compileColumns($database, $table)
- {
- return sprintf(
- 'select column_name as `name`, data_type as `type_name`, column_type as `type`, '
- .'collation_name as `collation`, is_nullable as `nullable`, '
- .'column_default as `default`, column_comment as `comment`, '
- .'generation_expression as `expression`, extra as `extra` '
- .'from information_schema.columns where table_schema = %s and table_name = %s '
- .'order by ordinal_position asc',
- $this->quoteString($database),
- $this->quoteString($table)
- );
- }
- /**
- * Compile the query to determine the indexes.
- *
- * @param string $database
- * @param string $table
- * @return string
- */
- public function compileIndexes($database, $table)
- {
- return sprintf(
- 'select index_name as `name`, group_concat(column_name order by seq_in_index) as `columns`, '
- .'index_type as `type`, not non_unique as `unique` '
- .'from information_schema.statistics where table_schema = %s and table_name = %s '
- .'group by index_name, index_type, non_unique',
- $this->quoteString($database),
- $this->quoteString($table)
- );
- }
- /**
- * Compile the query to determine the foreign keys.
- *
- * @param string $database
- * @param string $table
- * @return string
- */
- public function compileForeignKeys($database, $table)
- {
- return sprintf(
- 'select kc.constraint_name as `name`, '
- .'group_concat(kc.column_name order by kc.ordinal_position) as `columns`, '
- .'kc.referenced_table_schema as `foreign_schema`, '
- .'kc.referenced_table_name as `foreign_table`, '
- .'group_concat(kc.referenced_column_name order by kc.ordinal_position) as `foreign_columns`, '
- .'rc.update_rule as `on_update`, '
- .'rc.delete_rule as `on_delete` '
- .'from information_schema.key_column_usage kc join information_schema.referential_constraints rc '
- .'on kc.constraint_schema = rc.constraint_schema and kc.constraint_name = rc.constraint_name '
- .'where kc.table_schema = %s and kc.table_name = %s and kc.referenced_table_name is not null '
- .'group by kc.constraint_name, kc.referenced_table_schema, kc.referenced_table_name, rc.update_rule, rc.delete_rule',
- $this->quoteString($database),
- $this->quoteString($table)
- );
- }
- /**
- * Compile a create table command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @param \Illuminate\Database\Connection $connection
- * @return string
- */
- public function compileCreate(Blueprint $blueprint, Fluent $command, Connection $connection)
- {
- $sql = $this->compileCreateTable(
- $blueprint, $command, $connection
- );
- // Once we have the primary SQL, we can add the encoding option to the SQL for
- // the table. Then, we can check if a storage engine has been supplied for
- // the table. If so, we will add the engine declaration to the SQL query.
- $sql = $this->compileCreateEncoding(
- $sql, $connection, $blueprint
- );
- // Finally, we will append the engine configuration onto this SQL statement as
- // the final thing we do before returning this finished SQL. Once this gets
- // added the query will be ready to execute against the real connections.
- return $this->compileCreateEngine($sql, $connection, $blueprint);
- }
- /**
- * Create the main create table clause.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @param \Illuminate\Database\Connection $connection
- * @return string
- */
- protected function compileCreateTable($blueprint, $command, $connection)
- {
- $tableStructure = $this->getColumns($blueprint);
- if ($primaryKey = $this->getCommandByName($blueprint, 'primary')) {
- $tableStructure[] = sprintf(
- 'primary key %s(%s)',
- $primaryKey->algorithm ? 'using '.$primaryKey->algorithm : '',
- $this->columnize($primaryKey->columns)
- );
- $primaryKey->shouldBeSkipped = true;
- }
- return sprintf('%s table %s (%s)',
- $blueprint->temporary ? 'create temporary' : 'create',
- $this->wrapTable($blueprint),
- implode(', ', $tableStructure)
- );
- }
- /**
- * Append the character set specifications to a command.
- *
- * @param string $sql
- * @param \Illuminate\Database\Connection $connection
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @return string
- */
- protected function compileCreateEncoding($sql, Connection $connection, Blueprint $blueprint)
- {
- // First we will set the character set if one has been set on either the create
- // blueprint itself or on the root configuration for the connection that the
- // table is being created on. We will add these to the create table query.
- if (isset($blueprint->charset)) {
- $sql .= ' default character set '.$blueprint->charset;
- } elseif (! is_null($charset = $connection->getConfig('charset'))) {
- $sql .= ' default character set '.$charset;
- }
- // Next we will add the collation to the create table statement if one has been
- // added to either this create table blueprint or the configuration for this
- // connection that the query is targeting. We'll add it to this SQL query.
- if (isset($blueprint->collation)) {
- $sql .= " collate '{$blueprint->collation}'";
- } elseif (! is_null($collation = $connection->getConfig('collation'))) {
- $sql .= " collate '{$collation}'";
- }
- return $sql;
- }
- /**
- * Append the engine specifications to a command.
- *
- * @param string $sql
- * @param \Illuminate\Database\Connection $connection
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @return string
- */
- protected function compileCreateEngine($sql, Connection $connection, Blueprint $blueprint)
- {
- if (isset($blueprint->engine)) {
- return $sql.' engine = '.$blueprint->engine;
- } elseif (! is_null($engine = $connection->getConfig('engine'))) {
- return $sql.' engine = '.$engine;
- }
- return $sql;
- }
- /**
- * Compile an add column command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileAdd(Blueprint $blueprint, Fluent $command)
- {
- $columns = $this->prefixArray('add', $this->getColumns($blueprint));
- return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
- }
- /**
- * Compile the auto-incrementing column starting values.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileAutoIncrementStartingValues(Blueprint $blueprint, Fluent $command)
- {
- if ($command->column->autoIncrement
- && $value = $command->column->get('startingValue', $command->column->get('from'))) {
- return 'alter table '.$this->wrapTable($blueprint).' auto_increment = '.$value;
- }
- }
- /**
- * Compile a rename column command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @param \Illuminate\Database\Connection $connection
- * @return array|string
- */
- public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
- {
- $version = $connection->getServerVersion();
- if (($connection->isMaria() && version_compare($version, '10.5.2', '<')) ||
- (! $connection->isMaria() && version_compare($version, '8.0.3', '<'))) {
- return $this->compileLegacyRenameColumn($blueprint, $command, $connection);
- }
- return parent::compileRenameColumn($blueprint, $command, $connection);
- }
- /**
- * Compile a rename column command for legacy versions of MySQL.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @param \Illuminate\Database\Connection $connection
- * @return string
- */
- protected function compileLegacyRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
- {
- $column = collect($connection->getSchemaBuilder()->getColumns($blueprint->getTable()))
- ->firstWhere('name', $command->from);
- $modifiers = $this->addModifiers($column['type'], $blueprint, new ColumnDefinition([
- 'change' => true,
- 'type' => match ($column['type_name']) {
- 'bigint' => 'bigInteger',
- 'int' => 'integer',
- 'mediumint' => 'mediumInteger',
- 'smallint' => 'smallInteger',
- 'tinyint' => 'tinyInteger',
- default => $column['type_name'],
- },
- 'nullable' => $column['nullable'],
- 'default' => $column['default'] && (str_starts_with(strtolower($column['default']), 'current_timestamp') || $column['default'] === 'NULL')
- ? new Expression($column['default'])
- : $column['default'],
- 'autoIncrement' => $column['auto_increment'],
- 'collation' => $column['collation'],
- 'comment' => $column['comment'],
- 'virtualAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'virtual'
- ? $column['generation']['expression'] : null,
- 'storedAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'stored'
- ? $column['generation']['expression'] : null,
- ]));
- return sprintf('alter table %s change %s %s %s',
- $this->wrapTable($blueprint),
- $this->wrap($command->from),
- $this->wrap($command->to),
- $modifiers
- );
- }
- /**
- * Compile a change column command into a series of SQL statements.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @param \Illuminate\Database\Connection $connection
- * @return array|string
- *
- * @throws \RuntimeException
- */
- public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection)
- {
- $columns = [];
- foreach ($blueprint->getChangedColumns() as $column) {
- $sql = sprintf('%s %s%s %s',
- is_null($column->renameTo) ? 'modify' : 'change',
- $this->wrap($column),
- is_null($column->renameTo) ? '' : ' '.$this->wrap($column->renameTo),
- $this->getType($column)
- );
- $columns[] = $this->addModifiers($sql, $blueprint, $column);
- }
- return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
- }
- /**
- * Compile a primary key command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compilePrimary(Blueprint $blueprint, Fluent $command)
- {
- return sprintf('alter table %s add primary key %s(%s)',
- $this->wrapTable($blueprint),
- $command->algorithm ? 'using '.$command->algorithm : '',
- $this->columnize($command->columns)
- );
- }
- /**
- * Compile a unique key command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileUnique(Blueprint $blueprint, Fluent $command)
- {
- return $this->compileKey($blueprint, $command, 'unique');
- }
- /**
- * Compile a plain index key command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileIndex(Blueprint $blueprint, Fluent $command)
- {
- return $this->compileKey($blueprint, $command, 'index');
- }
- /**
- * Compile a fulltext index key command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileFullText(Blueprint $blueprint, Fluent $command)
- {
- return $this->compileKey($blueprint, $command, 'fulltext');
- }
- /**
- * Compile a spatial index key command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileSpatialIndex(Blueprint $blueprint, Fluent $command)
- {
- return $this->compileKey($blueprint, $command, 'spatial index');
- }
- /**
- * Compile an index creation command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @param string $type
- * @return string
- */
- protected function compileKey(Blueprint $blueprint, Fluent $command, $type)
- {
- 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)
- );
- }
- /**
- * Compile a drop table command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileDrop(Blueprint $blueprint, Fluent $command)
- {
- return 'drop table '.$this->wrapTable($blueprint);
- }
- /**
- * Compile a drop table (if exists) command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
- {
- return 'drop table if exists '.$this->wrapTable($blueprint);
- }
- /**
- * Compile a drop column command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileDropColumn(Blueprint $blueprint, Fluent $command)
- {
- $columns = $this->prefixArray('drop', $this->wrapArray($command->columns));
- return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
- }
- /**
- * Compile a drop primary key command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
- {
- return 'alter table '.$this->wrapTable($blueprint).' drop primary key';
- }
- /**
- * Compile a drop unique key command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileDropUnique(Blueprint $blueprint, Fluent $command)
- {
- $index = $this->wrap($command->index);
- return "alter table {$this->wrapTable($blueprint)} drop index {$index}";
- }
- /**
- * Compile a drop index command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileDropIndex(Blueprint $blueprint, Fluent $command)
- {
- $index = $this->wrap($command->index);
- return "alter table {$this->wrapTable($blueprint)} drop index {$index}";
- }
- /**
- * Compile a drop fulltext index command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileDropFullText(Blueprint $blueprint, Fluent $command)
- {
- return $this->compileDropIndex($blueprint, $command);
- }
- /**
- * Compile a drop spatial index command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileDropSpatialIndex(Blueprint $blueprint, Fluent $command)
- {
- return $this->compileDropIndex($blueprint, $command);
- }
- /**
- * Compile a drop foreign key command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileDropForeign(Blueprint $blueprint, Fluent $command)
- {
- $index = $this->wrap($command->index);
- return "alter table {$this->wrapTable($blueprint)} drop foreign key {$index}";
- }
- /**
- * Compile a rename table command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileRename(Blueprint $blueprint, Fluent $command)
- {
- $from = $this->wrapTable($blueprint);
- return "rename table {$from} to ".$this->wrapTable($command->to);
- }
- /**
- * Compile a rename index command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
- {
- return sprintf('alter table %s rename index %s to %s',
- $this->wrapTable($blueprint),
- $this->wrap($command->from),
- $this->wrap($command->to)
- );
- }
- /**
- * Compile the SQL needed to drop all tables.
- *
- * @param array $tables
- * @return string
- */
- public function compileDropAllTables($tables)
- {
- return 'drop table '.implode(',', $this->wrapArray($tables));
- }
- /**
- * Compile the SQL needed to drop all views.
- *
- * @param array $views
- * @return string
- */
- public function compileDropAllViews($views)
- {
- return 'drop view '.implode(',', $this->wrapArray($views));
- }
- /**
- * Compile the command to enable foreign key constraints.
- *
- * @return string
- */
- public function compileEnableForeignKeyConstraints()
- {
- return 'SET FOREIGN_KEY_CHECKS=1;';
- }
- /**
- * Compile the command to disable foreign key constraints.
- *
- * @return string
- */
- public function compileDisableForeignKeyConstraints()
- {
- return 'SET FOREIGN_KEY_CHECKS=0;';
- }
- /**
- * Compile a table comment command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileTableComment(Blueprint $blueprint, Fluent $command)
- {
- return sprintf('alter table %s comment = %s',
- $this->wrapTable($blueprint),
- "'".str_replace("'", "''", $command->comment)."'"
- );
- }
- /**
- * Create the column definition for a char type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeChar(Fluent $column)
- {
- return "char({$column->length})";
- }
- /**
- * Create the column definition for a string type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeString(Fluent $column)
- {
- return "varchar({$column->length})";
- }
- /**
- * Create the column definition for a tiny text type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTinyText(Fluent $column)
- {
- return 'tinytext';
- }
- /**
- * Create the column definition for a text type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeText(Fluent $column)
- {
- return 'text';
- }
- /**
- * Create the column definition for a medium text type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeMediumText(Fluent $column)
- {
- return 'mediumtext';
- }
- /**
- * Create the column definition for a long text type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeLongText(Fluent $column)
- {
- return 'longtext';
- }
- /**
- * Create the column definition for a big integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeBigInteger(Fluent $column)
- {
- return 'bigint';
- }
- /**
- * Create the column definition for an integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeInteger(Fluent $column)
- {
- return 'int';
- }
- /**
- * Create the column definition for a medium integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeMediumInteger(Fluent $column)
- {
- return 'mediumint';
- }
- /**
- * Create the column definition for a tiny integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTinyInteger(Fluent $column)
- {
- return 'tinyint';
- }
- /**
- * Create the column definition for a small integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeSmallInteger(Fluent $column)
- {
- return 'smallint';
- }
- /**
- * Create the column definition for a float type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeFloat(Fluent $column)
- {
- if ($column->precision) {
- return "float({$column->precision})";
- }
- return 'float';
- }
- /**
- * Create the column definition for a double type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeDouble(Fluent $column)
- {
- return 'double';
- }
- /**
- * Create the column definition for a decimal type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeDecimal(Fluent $column)
- {
- return "decimal({$column->total}, {$column->places})";
- }
- /**
- * Create the column definition for a boolean type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeBoolean(Fluent $column)
- {
- return 'tinyint(1)';
- }
- /**
- * Create the column definition for an enumeration type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeEnum(Fluent $column)
- {
- return sprintf('enum(%s)', $this->quoteString($column->allowed));
- }
- /**
- * Create the column definition for a set enumeration type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeSet(Fluent $column)
- {
- return sprintf('set(%s)', $this->quoteString($column->allowed));
- }
- /**
- * Create the column definition for a json type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeJson(Fluent $column)
- {
- return 'json';
- }
- /**
- * Create the column definition for a jsonb type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeJsonb(Fluent $column)
- {
- return 'json';
- }
- /**
- * Create the column definition for a date type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeDate(Fluent $column)
- {
- return 'date';
- }
- /**
- * Create the column definition for a date-time type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeDateTime(Fluent $column)
- {
- $current = $column->precision ? "CURRENT_TIMESTAMP($column->precision)" : 'CURRENT_TIMESTAMP';
- if ($column->useCurrent) {
- $column->default(new Expression($current));
- }
- if ($column->useCurrentOnUpdate) {
- $column->onUpdate(new Expression($current));
- }
- return $column->precision ? "datetime($column->precision)" : 'datetime';
- }
- /**
- * Create the column definition for a date-time (with time zone) type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeDateTimeTz(Fluent $column)
- {
- return $this->typeDateTime($column);
- }
- /**
- * Create the column definition for a time type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTime(Fluent $column)
- {
- return $column->precision ? "time($column->precision)" : 'time';
- }
- /**
- * Create the column definition for a time (with time zone) type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTimeTz(Fluent $column)
- {
- return $this->typeTime($column);
- }
- /**
- * Create the column definition for a timestamp type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTimestamp(Fluent $column)
- {
- $current = $column->precision ? "CURRENT_TIMESTAMP($column->precision)" : 'CURRENT_TIMESTAMP';
- if ($column->useCurrent) {
- $column->default(new Expression($current));
- }
- if ($column->useCurrentOnUpdate) {
- $column->onUpdate(new Expression($current));
- }
- return $column->precision ? "timestamp($column->precision)" : 'timestamp';
- }
- /**
- * Create the column definition for a timestamp (with time zone) type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTimestampTz(Fluent $column)
- {
- return $this->typeTimestamp($column);
- }
- /**
- * Create the column definition for a year type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeYear(Fluent $column)
- {
- return 'year';
- }
- /**
- * Create the column definition for a binary type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeBinary(Fluent $column)
- {
- if ($column->length) {
- return $column->fixed ? "binary({$column->length})" : "varbinary({$column->length})";
- }
- return 'blob';
- }
- /**
- * Create the column definition for a uuid type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeUuid(Fluent $column)
- {
- return 'char(36)';
- }
- /**
- * Create the column definition for an IP address type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeIpAddress(Fluent $column)
- {
- return 'varchar(45)';
- }
- /**
- * Create the column definition for a MAC address type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeMacAddress(Fluent $column)
- {
- return 'varchar(17)';
- }
- /**
- * Create the column definition for a spatial Geometry type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeGeometry(Fluent $column)
- {
- $subtype = $column->subtype ? strtolower($column->subtype) : null;
- if (! in_array($subtype, ['point', 'linestring', 'polygon', 'geometrycollection', 'multipoint', 'multilinestring', 'multipolygon'])) {
- $subtype = null;
- }
- return sprintf('%s%s',
- $subtype ?? 'geometry',
- match (true) {
- $column->srid && $this->connection?->isMaria() => ' ref_system_id='.$column->srid,
- (bool) $column->srid => ' srid '.$column->srid,
- default => '',
- }
- );
- }
- /**
- * Create the column definition for a spatial Geography type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeGeography(Fluent $column)
- {
- return $this->typeGeometry($column);
- }
- /**
- * Create the column definition for a generated, computed column type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return void
- *
- * @throws \RuntimeException
- */
- protected function typeComputed(Fluent $column)
- {
- throw new RuntimeException('This database driver requires a type, see the virtualAs / storedAs modifiers.');
- }
- /**
- * Get the SQL for a generated virtual column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($virtualAs = $column->virtualAsJson)) {
- if ($this->isJsonSelector($virtualAs)) {
- $virtualAs = $this->wrapJsonSelector($virtualAs);
- }
- return " as ({$virtualAs})";
- }
- if (! is_null($virtualAs = $column->virtualAs)) {
- return " as ({$this->getValue($virtualAs)})";
- }
- }
- /**
- * Get the SQL for a generated stored column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyStoredAs(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($storedAs = $column->storedAsJson)) {
- if ($this->isJsonSelector($storedAs)) {
- $storedAs = $this->wrapJsonSelector($storedAs);
- }
- return " as ({$storedAs}) stored";
- }
- if (! is_null($storedAs = $column->storedAs)) {
- return " as ({$this->getValue($storedAs)}) stored";
- }
- }
- /**
- * Get the SQL for an unsigned column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyUnsigned(Blueprint $blueprint, Fluent $column)
- {
- if ($column->unsigned) {
- return ' unsigned';
- }
- }
- /**
- * Get the SQL for a character set column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyCharset(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($column->charset)) {
- return ' character set '.$column->charset;
- }
- }
- /**
- * Get the SQL for a collation column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyCollate(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($column->collation)) {
- return " collate '{$column->collation}'";
- }
- }
- /**
- * Get the SQL for a nullable column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyNullable(Blueprint $blueprint, Fluent $column)
- {
- if (is_null($column->virtualAs) &&
- is_null($column->virtualAsJson) &&
- is_null($column->storedAs) &&
- is_null($column->storedAsJson)) {
- return $column->nullable ? ' null' : ' not null';
- }
- if ($column->nullable === false) {
- return ' not null';
- }
- }
- /**
- * Get the SQL for an invisible column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyInvisible(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($column->invisible)) {
- return ' invisible';
- }
- }
- /**
- * Get the SQL for a default column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyDefault(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($column->default)) {
- return ' default '.$this->getDefaultValue($column->default);
- }
- }
- /**
- * Get the SQL for an "on update" column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyOnUpdate(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($column->onUpdate)) {
- return ' on update '.$this->getValue($column->onUpdate);
- }
- }
- /**
- * Get the SQL for an auto-increment column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
- {
- if (in_array($column->type, $this->serials) && $column->autoIncrement) {
- return $this->hasCommand($blueprint, 'primary') || ($column->change && ! $column->primary)
- ? ' auto_increment'
- : ' auto_increment primary key';
- }
- }
- /**
- * Get the SQL for a "first" column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyFirst(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($column->first)) {
- return ' first';
- }
- }
- /**
- * Get the SQL for an "after" column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyAfter(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($column->after)) {
- return ' after '.$this->wrap($column->after);
- }
- }
- /**
- * Get the SQL for a "comment" column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $column
- * @return string|null
- */
- protected function modifyComment(Blueprint $blueprint, Fluent $column)
- {
- if (! is_null($column->comment)) {
- return " comment '".addslashes($column->comment)."'";
- }
- }
- /**
- * Wrap a single string in keyword identifiers.
- *
- * @param string $value
- * @return string
- */
- protected function wrapValue($value)
- {
- if ($value !== '*') {
- return '`'.str_replace('`', '``', $value).'`';
- }
- return $value;
- }
- /**
- * Wrap the given JSON selector.
- *
- * @param string $value
- * @return string
- */
- protected function wrapJsonSelector($value)
- {
- [$field, $path] = $this->wrapJsonFieldAndPath($value);
- return 'json_unquote(json_extract('.$field.$path.'))';
- }
- }
|