db2 driver updates
This commit is contained in:
@@ -16,27 +16,14 @@ class Db2SchemaGrammar extends Grammar
|
||||
$primaryKeys = [];
|
||||
|
||||
foreach ($blueprint->getAddedColumns() as $column) {
|
||||
$sql = $this->wrap($column) . ' ' . $this->getType($column);
|
||||
|
||||
$columns[] = $this->columnDefinition($column);
|
||||
if ($column->autoIncrement) {
|
||||
$sql .= ' GENERATED BY DEFAULT AS IDENTITY';
|
||||
|
||||
// Laravel's $table->id() does not explicitly
|
||||
// set the primary attribute.
|
||||
$primaryKeys[] = $column->name;
|
||||
}
|
||||
|
||||
$sql .= $column->nullable ? '' : ' NOT NULL';
|
||||
|
||||
if (!is_null($column->default)) {
|
||||
$sql .= ' DEFAULT ' . $this->getDefaultValue($column->default);
|
||||
}
|
||||
|
||||
$columns[] = $sql;
|
||||
}
|
||||
|
||||
// Db2 identity columns are not automatically primary keys.
|
||||
if (!empty($primaryKeys)) {
|
||||
$explicitPrimary = collect($blueprint->getCommands())->contains('name', 'primary');
|
||||
if ($primaryKeys !== [] && ! $explicitPrimary) {
|
||||
$columns[] = sprintf(
|
||||
'PRIMARY KEY (%s)',
|
||||
$this->columnize($primaryKeys)
|
||||
@@ -50,6 +37,121 @@ class Db2SchemaGrammar extends Grammar
|
||||
);
|
||||
}
|
||||
|
||||
public function compileAdd(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return 'ALTER TABLE '.$this->wrapTable($blueprint).' ADD COLUMN '.$this->columnDefinition($command->column);
|
||||
}
|
||||
|
||||
public function compileChange(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
$column = $command->column;
|
||||
if ($column->autoIncrement) {
|
||||
throw new \RuntimeException('Changing a Db2 identity column is unsupported.');
|
||||
}
|
||||
|
||||
$columns = array_column($this->connection->getSchemaBuilder()->getColumns($blueprint->getTable()), null, 'name');
|
||||
$current = $columns[$column->name] ?? null;
|
||||
if ($current === null || $current['auto_increment']) {
|
||||
throw new \RuntimeException('Db2 change requires an existing non-identity column.');
|
||||
}
|
||||
|
||||
$prefix = 'ALTER TABLE '.$this->wrapTable($blueprint).' ALTER COLUMN '.$this->wrap($column);
|
||||
$type = $this->getType($column);
|
||||
$statements = [];
|
||||
|
||||
if (strcasecmp($current['type'], $type) !== 0) {
|
||||
$statements[] = $prefix.' SET DATA TYPE '.$type;
|
||||
}
|
||||
if ($current['nullable'] !== (bool) $column->nullable) {
|
||||
$statements[] = $prefix.($column->nullable ? ' DROP NOT NULL' : ' SET NOT NULL');
|
||||
}
|
||||
if ($column->default !== null) {
|
||||
$statements[] = $prefix.' SET DEFAULT '.$this->getDefaultValue($column->default);
|
||||
} elseif ($current['default'] !== null) {
|
||||
$statements[] = $prefix.' DROP DEFAULT';
|
||||
}
|
||||
|
||||
return $statements;
|
||||
}
|
||||
|
||||
public function compileDropColumn(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
$columns = array_map(fn ($column) => 'DROP COLUMN '.$this->wrap($column), $command->columns);
|
||||
|
||||
return 'ALTER TABLE '.$this->wrapTable($blueprint).' '.implode(' ', $columns);
|
||||
}
|
||||
|
||||
public function compileDrop(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return 'DROP TABLE '.$this->wrapTable($blueprint);
|
||||
}
|
||||
|
||||
public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return 'DROP TABLE IF EXISTS '.$this->wrapTable($blueprint);
|
||||
}
|
||||
|
||||
public function compileRename(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
[$schema] = $this->connection->getSchemaBuilder()->parseSchemaAndTable($blueprint->getTable());
|
||||
[$targetSchema, $target] = $this->connection->getSchemaBuilder()->parseSchemaAndTable($command->to);
|
||||
if ($targetSchema !== null && $targetSchema !== ($schema ?? $this->connection->getConfig('schema'))) {
|
||||
throw new \RuntimeException('Db2 cannot rename a table across schemas.');
|
||||
}
|
||||
|
||||
return 'RENAME TABLE '.$this->wrapTable($blueprint).' TO '.$this->wrapTable($target);
|
||||
}
|
||||
|
||||
public function compileRenameColumn(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return 'ALTER TABLE '.$this->wrapTable($blueprint)
|
||||
.' RENAME COLUMN '.$this->wrap($command->from).' TO '.$this->wrap($command->to);
|
||||
}
|
||||
|
||||
public function compileDropForeign(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return 'ALTER TABLE '.$this->wrapTable($blueprint).' DROP FOREIGN KEY '.$this->wrap($command->index);
|
||||
}
|
||||
|
||||
public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return 'ALTER TABLE '.$this->wrapTable($blueprint).' DROP PRIMARY KEY';
|
||||
}
|
||||
|
||||
public function compileDropUnique(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return $this->compileDropIndex($blueprint, $command);
|
||||
}
|
||||
|
||||
public function compileDropIndex(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return 'DROP INDEX '.$this->wrapIndex($blueprint, $command->index);
|
||||
}
|
||||
|
||||
private function wrapIndex(Blueprint $blueprint, string $name): string
|
||||
{
|
||||
[$schema] = $this->connection->getSchemaBuilder()->parseSchemaAndTable($blueprint->getTable());
|
||||
|
||||
return $schema === null ? $this->wrapValue($name) : $this->wrapValue($schema).'.'.$this->wrapValue($name);
|
||||
}
|
||||
|
||||
private function columnDefinition(Fluent $column): string
|
||||
{
|
||||
$sql = $this->wrap($column).' '.$this->getType($column);
|
||||
|
||||
if ($column->autoIncrement) {
|
||||
$sql .= ' GENERATED BY DEFAULT AS IDENTITY';
|
||||
}
|
||||
if (! $column->nullable) {
|
||||
$sql .= ' NOT NULL';
|
||||
}
|
||||
if ($column->default !== null) {
|
||||
$sql .= ' DEFAULT '.$this->getDefaultValue($column->default);
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
protected function typeBigInteger(Fluent $column)
|
||||
{
|
||||
return 'BIGINT';
|
||||
@@ -117,8 +219,9 @@ class Db2SchemaGrammar extends Grammar
|
||||
public function compilePrimary(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return sprintf(
|
||||
'ALTER TABLE %s ADD PRIMARY KEY (%s)',
|
||||
'ALTER TABLE %s ADD CONSTRAINT %s PRIMARY KEY (%s)',
|
||||
$this->wrapTable($blueprint),
|
||||
$this->wrapValue($command->index),
|
||||
$this->columnize($command->columns)
|
||||
);
|
||||
}
|
||||
@@ -132,7 +235,7 @@ class Db2SchemaGrammar extends Grammar
|
||||
{
|
||||
return sprintf(
|
||||
'CREATE UNIQUE INDEX %s ON %s (%s)',
|
||||
$this->wrap($command->index),
|
||||
$this->wrapIndex($blueprint, $command->index),
|
||||
$this->wrapTable($blueprint),
|
||||
$this->columnize($command->columns)
|
||||
);
|
||||
@@ -142,7 +245,7 @@ class Db2SchemaGrammar extends Grammar
|
||||
{
|
||||
return sprintf(
|
||||
'CREATE INDEX %s ON %s (%s)',
|
||||
$this->wrap($command->index),
|
||||
$this->wrapIndex($blueprint, $command->index),
|
||||
$this->wrapTable($blueprint),
|
||||
$this->columnize($command->columns)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user