123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841 |
- <?php
-
- namespace Illuminate\Database\Schema\Grammars;
-
- use RuntimeException;
- use Illuminate\Support\Fluent;
- use Illuminate\Database\Schema\Blueprint;
-
- class PostgresGrammar extends Grammar
- {
- /**
- * If this Grammar supports schema changes wrapped in a transaction.
- *
- * @var bool
- */
- protected $transactions = true;
-
- /**
- * The possible column modifiers.
- *
- * @var array
- */
- protected $modifiers = ['Increment', 'Nullable', 'Default'];
-
- /**
- * The columns available as serials.
- *
- * @var array
- */
- protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger'];
-
- /**
- * The commands to be executed outside of create or alter command.
- *
- * @var array
- */
- protected $fluentCommands = ['Comment'];
-
- /**
- * Compile the query to determine if a table exists.
- *
- * @return string
- */
- public function compileTableExists()
- {
- return 'select * from information_schema.tables where table_schema = ? and table_name = ?';
- }
-
- /**
- * Compile the query to determine the list of columns.
- *
- * @return string
- */
- public function compileColumnListing()
- {
- return 'select column_name from information_schema.columns where table_schema = ? and table_name = ?';
- }
-
- /**
- * Compile a create table command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileCreate(Blueprint $blueprint, Fluent $command)
- {
- return sprintf('%s table %s (%s)',
- $blueprint->temporary ? 'create temporary' : 'create',
- $this->wrapTable($blueprint),
- implode(', ', $this->getColumns($blueprint))
- );
- }
-
- /**
- * Compile a column addition command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileAdd(Blueprint $blueprint, Fluent $command)
- {
- return sprintf('alter table %s %s',
- $this->wrapTable($blueprint),
- implode(', ', $this->prefixArray('add column', $this->getColumns($blueprint)))
- );
- }
-
- /**
- * 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)
- {
- $columns = $this->columnize($command->columns);
-
- return 'alter table '.$this->wrapTable($blueprint)." add primary key ({$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 sprintf('alter table %s add constraint %s unique (%s)',
- $this->wrapTable($blueprint),
- $this->wrap($command->index),
- $this->columnize($command->columns)
- );
- }
-
- /**
- * 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 sprintf('create index %s on %s%s (%s)',
- $this->wrap($command->index),
- $this->wrapTable($blueprint),
- $command->algorithm ? ' using '.$command->algorithm : '',
- $this->columnize($command->columns)
- );
- }
-
- /**
- * 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)
- {
- $command->algorithm = 'gist';
-
- return $this->compileIndex($blueprint, $command);
- }
-
- /**
- * Compile a foreign key command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileForeign(Blueprint $blueprint, Fluent $command)
- {
- $sql = parent::compileForeign($blueprint, $command);
-
- if (! is_null($command->deferrable)) {
- $sql .= $command->deferrable ? ' deferrable' : ' not deferrable';
- }
-
- if ($command->deferrable && ! is_null($command->initiallyImmediate)) {
- $sql .= $command->initiallyImmediate ? ' initially immediate' : ' initially deferred';
- }
-
- return $sql;
- }
-
- /**
- * 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 the SQL needed to drop all tables.
- *
- * @param string $tables
- * @return string
- */
- public function compileDropAllTables($tables)
- {
- return 'drop table "'.implode('","', $tables).'" cascade';
- }
-
- /**
- * Compile the SQL needed to retrieve all table names.
- *
- * @param string $schema
- * @return string
- */
- public function compileGetAllTables($schema)
- {
- return "select tablename from pg_catalog.pg_tables where schemaname = '{$schema}'";
- }
-
- /**
- * 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 column', $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)
- {
- $index = $this->wrap("{$blueprint->getTable()}_pkey");
-
- return 'alter table '.$this->wrapTable($blueprint)." drop constraint {$index}";
- }
-
- /**
- * 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 constraint {$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)
- {
- return "drop index {$this->wrap($command->index)}";
- }
-
- /**
- * 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 constraint {$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 "alter table {$from} rename 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 index %s rename to %s',
- $this->wrap($command->from),
- $this->wrap($command->to)
- );
- }
-
- /**
- * Compile the command to enable foreign key constraints.
- *
- * @return string
- */
- public function compileEnableForeignKeyConstraints()
- {
- return 'SET CONSTRAINTS ALL IMMEDIATE;';
- }
-
- /**
- * Compile the command to disable foreign key constraints.
- *
- * @return string
- */
- public function compileDisableForeignKeyConstraints()
- {
- return 'SET CONSTRAINTS ALL DEFERRED;';
- }
-
- /**
- * Compile a comment command.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param \Illuminate\Support\Fluent $command
- * @return string
- */
- public function compileComment(Blueprint $blueprint, Fluent $command)
- {
- return sprintf('comment on column %s.%s is %s',
- $this->wrapTable($blueprint),
- $this->wrap($command->column->name),
- "'".str_replace("'", "''", $command->value)."'"
- );
- }
-
- /**
- * 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 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 'text';
- }
-
- /**
- * Create the column definition for a long text type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeLongText(Fluent $column)
- {
- return 'text';
- }
-
- /**
- * Create the column definition for an integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeInteger(Fluent $column)
- {
- return $column->autoIncrement ? 'serial' : 'integer';
- }
-
- /**
- * Create the column definition for a big integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeBigInteger(Fluent $column)
- {
- return $column->autoIncrement ? 'bigserial' : 'bigint';
- }
-
- /**
- * Create the column definition for a medium integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeMediumInteger(Fluent $column)
- {
- return $column->autoIncrement ? 'serial' : 'integer';
- }
-
- /**
- * Create the column definition for a tiny integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTinyInteger(Fluent $column)
- {
- return $column->autoIncrement ? 'smallserial' : 'smallint';
- }
-
- /**
- * Create the column definition for a small integer type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeSmallInteger(Fluent $column)
- {
- return $column->autoIncrement ? 'smallserial' : 'smallint';
- }
-
- /**
- * Create the column definition for a float type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeFloat(Fluent $column)
- {
- return $this->typeDouble($column);
- }
-
- /**
- * Create the column definition for a double type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeDouble(Fluent $column)
- {
- return 'double precision';
- }
-
- /**
- * Create the column definition for a real type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeReal(Fluent $column)
- {
- return 'real';
- }
-
- /**
- * 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 'boolean';
- }
-
- /**
- * Create the column definition for an enumeration type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeEnum(Fluent $column)
- {
- return sprintf(
- 'varchar(255) check ("%s" in (%s))',
- $column->name,
- $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 'jsonb';
- }
-
- /**
- * 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)
- {
- return "timestamp($column->precision) without time zone";
- }
-
- /**
- * 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 "timestamp($column->precision) with time zone";
- }
-
- /**
- * Create the column definition for a time type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTime(Fluent $column)
- {
- return "time($column->precision) without time zone";
- }
-
- /**
- * Create the column definition for a time (with time zone) type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTimeTz(Fluent $column)
- {
- return "time($column->precision) with time zone";
- }
-
- /**
- * Create the column definition for a timestamp type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTimestamp(Fluent $column)
- {
- $columnType = "timestamp($column->precision) without time zone";
-
- return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
- }
-
- /**
- * Create the column definition for a timestamp (with time zone) type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeTimestampTz(Fluent $column)
- {
- $columnType = "timestamp($column->precision) with time zone";
-
- return $column->useCurrent ? "$columnType default CURRENT_TIMESTAMP" : $columnType;
- }
-
- /**
- * Create the column definition for a year type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeYear(Fluent $column)
- {
- return $this->typeInteger($column);
- }
-
- /**
- * Create the column definition for a binary type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeBinary(Fluent $column)
- {
- return 'bytea';
- }
-
- /**
- * Create the column definition for a uuid type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeUuid(Fluent $column)
- {
- return 'uuid';
- }
-
- /**
- * Create the column definition for an IP address type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeIpAddress(Fluent $column)
- {
- return 'inet';
- }
-
- /**
- * Create the column definition for a MAC address type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeMacAddress(Fluent $column)
- {
- return 'macaddr';
- }
-
- /**
- * Create the column definition for a spatial Geometry type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @throws \RuntimeException
- */
- protected function typeGeometry(Fluent $column)
- {
- throw new RuntimeException('The database driver in use does not support the Geometry spatial column type.');
- }
-
- /**
- * Create the column definition for a spatial Point type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typePoint(Fluent $column)
- {
- return $this->formatPostGisType('point');
- }
-
- /**
- * Create the column definition for a spatial LineString type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeLineString(Fluent $column)
- {
- return $this->formatPostGisType('linestring');
- }
-
- /**
- * Create the column definition for a spatial Polygon type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typePolygon(Fluent $column)
- {
- return $this->formatPostGisType('polygon');
- }
-
- /**
- * Create the column definition for a spatial GeometryCollection type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeGeometryCollection(Fluent $column)
- {
- return $this->formatPostGisType('geometrycollection');
- }
-
- /**
- * Create the column definition for a spatial MultiPoint type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeMultiPoint(Fluent $column)
- {
- return $this->formatPostGisType('multipoint');
- }
-
- /**
- * Create the column definition for a spatial MultiLineString type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- public function typeMultiLineString(Fluent $column)
- {
- return $this->formatPostGisType('multilinestring');
- }
-
- /**
- * Create the column definition for a spatial MultiPolygon type.
- *
- * @param \Illuminate\Support\Fluent $column
- * @return string
- */
- protected function typeMultiPolygon(Fluent $column)
- {
- return $this->formatPostGisType('multipolygon');
- }
-
- /**
- * Format the column definition for a PostGIS spatial type.
- *
- * @param string $type
- * @return string
- */
- private function formatPostGisType(string $type)
- {
- return "geography($type, 4326)";
- }
-
- /**
- * 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)
- {
- return $column->nullable ? ' null' : ' not null';
- }
-
- /**
- * 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 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 ' primary key';
- }
- }
- }
|