summaryrefslogtreecommitdiff
path: root/src/Schema/Grammars/FirebirdGrammar.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Schema/Grammars/FirebirdGrammar.php')
-rwxr-xr-xsrc/Schema/Grammars/FirebirdGrammar.php631
1 files changed, 631 insertions, 0 deletions
diff --git a/src/Schema/Grammars/FirebirdGrammar.php b/src/Schema/Grammars/FirebirdGrammar.php
new file mode 100755
index 0000000..6c11385
--- /dev/null
+++ b/src/Schema/Grammars/FirebirdGrammar.php
@@ -0,0 +1,631 @@
+<?php
+
+namespace Xgrz\Firebird\Schema\Grammars;
+
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Schema\Grammars\Grammar;
+use Illuminate\Support\Fluent;
+
+class FirebirdGrammar extends Grammar
+{
+ /**
+ * The possible column modifiers.
+ *
+ * @var array
+ */
+ protected $modifiers = ['Charset', 'Collate', 'Increment', 'Nullable', 'Default'];
+
+ /**
+ * The columns available as serials.
+ *
+ * @var array
+ */
+ protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger'];
+
+ /**
+ * Compile the query to determine the tables.
+ *
+ * @return string
+ */
+ public function compileTables()
+ {
+ return 'select trim(trailing from rdb$relation_name) as "name" '
+ .'from rdb$relations '
+ .'where rdb$relation_type = 0 '
+ .'and (rdb$system_flag is null or rdb$system_flag = 0) '
+ .'order by rdb$relation_name';
+ }
+
+ /**
+ * Compile the query to determine if a table exists.
+ *
+ * @return string
+ */
+ public function compileTableExists()
+ {
+ return 'select rdb$relation_name from rdb$relations where rdb$relation_name = ?';
+ }
+
+ /**
+ * Compile the query to determine the views.
+ *
+ * @return string
+ */
+ public function compileViews()
+ {
+ return 'select trim(trailing from rdb$relation_name) as "name", '
+ .'rdb$view_source as "definition" '
+ .'from rdb$relations '
+ .'where rdb$view_blr is not null '
+ .'and (rdb$system_flag is null or rdb$system_flag = 0)';
+ }
+
+ /**
+ * Compile the query to determine the columns.
+ *
+ * @param string $table
+ * @return string
+ */
+ public function compileColumns($table)
+ {
+ return 'select trim(trailing from rdb$field_name) as "name" '
+ .'from rdb$relation_fields '
+ .'where rdb$relation_name = '.$this->quoteString($table).' '
+ .'order by rdb$relation_name';
+ }
+
+ /**
+ * Compile the query to determine the list of columns.
+ *
+ * @param string $table
+ * @return string
+ */
+ public function compileColumnListing($table)
+ {
+ return "select trim(rdb\$field_name) as \"column_name\" from rdb\$relation_fields where rdb\$relation_name = '$table'";
+ }
+
+ /**
+ * 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)
+ {
+ if ($blueprint->temporary) {
+ throw new \LogicException('This database driver does not support temporary tables.');
+ }
+
+ $columns = implode(', ', $this->getColumns($blueprint));
+
+ $sql = 'create table '.$this->wrapTable($blueprint)." ($columns)";
+
+ 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)
+ {
+ // Replace the double quotes with single quotes.
+ $table = str_replace('"', "'", $this->wrapTable($blueprint));
+
+ return sprintf(
+ "execute block as begin if (exists(%s)) then execute statement '%s'; end",
+ str_replace('?', $table, $this->compileTableExists()), // Replace the ? character with the table name.
+ $this->compileDrop($blueprint, $command)
+ );
+ }
+
+ /**
+ * 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)
+ {
+ $table = $this->wrapTable($blueprint);
+
+ $columns = $this->prefixArray('ADD', $this->getColumns($blueprint));
+
+ return 'ALTER TABLE '.$table.' '.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)
+ {
+ $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)
+ {
+ $table = $this->wrapTable($blueprint);
+
+ $index = $this->wrap(substr($command->index, 0, 31));
+
+ $columns = $this->columnize($command->columns);
+
+ return "ALTER TABLE {$table} ADD CONSTRAINT {$index} UNIQUE ({$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)
+ {
+ $columns = $this->columnize($command->columns);
+
+ $index = $this->wrap(substr($command->index, 0, 31));
+
+ $table = $this->wrapTable($blueprint);
+
+ return "CREATE INDEX {$index} ON {$table} ($columns)";
+ }
+
+ /**
+ * 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)
+ {
+ $table = $this->wrapTable($blueprint);
+
+ $on = $this->wrapTable($command->on);
+
+ // We need to prepare several of the elements of the foreign key definition
+ // before we can create the SQL, such as wrapping the tables and convert
+ // an array of columns to comma-delimited strings for the SQL queries.
+ $columns = $this->columnize($command->columns);
+
+ $onColumns = $this->columnize((array) $command->references);
+
+ $fkName = substr($command->index, 0, 31);
+
+ $sql = "ALTER TABLE {$table} ADD CONSTRAINT {$fkName} ";
+
+ $sql .= "FOREIGN KEY ({$columns}) REFERENCES {$on} ({$onColumns})";
+
+ // Once we have the basic foreign key creation statement constructed we can
+ // build out the syntax for what should happen on an update or delete of
+ // the affected columns, which will get something like "cascade", etc.
+ if (! is_null($command->onDelete)) {
+ $sql .= " ON DELETE {$command->onDelete}";
+ }
+
+ if (! is_null($command->onUpdate)) {
+ $sql .= " ON UPDATE {$command->onUpdate}";
+ }
+
+ return $sql;
+ }
+
+ /**
+ * 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)
+ {
+ $table = $this->wrapTable($blueprint);
+
+ return "ALTER TABLE {$table} DROP CONSTRAINT {$command->index}";
+ }
+
+ /**
+ * 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)
+ {
+ return $column->nullable ? '' : ' 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);
+ }
+ }
+
+ /**
+ * 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 'BLOB SUB_TYPE TEXT';
+ }
+
+ /**
+ * Create the column definition for a medium text type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeMediumText(Fluent $column)
+ {
+ return 'BLOB SUB_TYPE TEXT';
+ }
+
+ /**
+ * Create the column definition for a long text type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeLongText(Fluent $column)
+ {
+ return 'BLOB SUB_TYPE TEXT';
+ }
+
+ /**
+ * Create the column definition for a integer type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeInteger(Fluent $column)
+ {
+ return 'INTEGER';
+ }
+
+ /**
+ * 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 a medium integer type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeMediumInteger(Fluent $column)
+ {
+ return 'INTEGER';
+ }
+
+ /**
+ * Create the column definition for a tiny integer type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeTinyInteger(Fluent $column)
+ {
+ return 'SMALLINT';
+ }
+
+ /**
+ * 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)
+ {
+ return 'FLOAT';
+ }
+
+ /**
+ * 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 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 'CHAR(1)';
+ }
+
+ /**
+ * Create the column definition for an enum type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeEnum(Fluent $column)
+ {
+ $allowed = array_map(function ($a) {
+ return "'".$a."'";
+ }, $column->allowed);
+
+ return "VARCHAR(255) CHECK (\"{$column->name}\" IN (".implode(', ', $allowed).'))';
+ }
+
+ /**
+ * Create the column definition for a json type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeJson(Fluent $column)
+ {
+ return 'VARCHAR(8191)';
+ }
+
+ /**
+ * Create the column definition for a jsonb type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeJsonb(Fluent $column)
+ {
+ return 'VARCHAR(8191) CHARACTER SET OCTETS';
+ }
+
+ /**
+ * 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';
+ }
+
+ /**
+ * Create the column definition for a date-time type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeDateTimeTz(Fluent $column)
+ {
+ // No timezone support, default to plain date time
+ 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 'TIME';
+ }
+
+ /**
+ * Create the column definition for a time type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeTimeTz(Fluent $column)
+ {
+ // No timezone support, default to plain time
+ return $this->typeTime($column);
+ }
+
+ /**
+ * Create the column definition for a timestamp type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeTimestamp(Fluent $column)
+ {
+ if ($column->useCurrent) {
+ return 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP';
+ }
+
+ return 'TIMESTAMP';
+ }
+
+ /**
+ * Create the column definition for a timestamp type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeTimestampTz(Fluent $column)
+ {
+ // No timezone support, default to plain timestamp
+ return $this->typeTimestamp($column);
+ }
+
+ /**
+ * Create the column definition for a binary type.
+ *
+ * @param \Illuminate\Support\Fluent $column
+ * @return string
+ */
+ protected function typeBinary(Fluent $column)
+ {
+ return 'BLOB SUB_TYPE BINARY';
+ }
+
+ /**
+ * 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)';
+ }
+}