initial Commit
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Db2;
|
||||
|
||||
use Illuminate\Database\Schema\Grammars\Grammar;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Fluent;
|
||||
|
||||
class Db2SchemaGrammar extends Grammar
|
||||
{
|
||||
protected $modifiers = ['Nullable', 'Default'];
|
||||
|
||||
public function compileCreate(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
$columns = [];
|
||||
$primaryKeys = [];
|
||||
|
||||
foreach ($blueprint->getAddedColumns() as $column) {
|
||||
$sql = $this->wrap($column) . ' ' . $this->getType($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)) {
|
||||
$columns[] = sprintf(
|
||||
'PRIMARY KEY (%s)',
|
||||
$this->columnize($primaryKeys)
|
||||
);
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'CREATE TABLE %s (%s)',
|
||||
$this->wrapTable($blueprint),
|
||||
implode(', ', $columns)
|
||||
);
|
||||
}
|
||||
|
||||
protected function typeBigInteger(Fluent $column)
|
||||
{
|
||||
return 'BIGINT';
|
||||
}
|
||||
|
||||
protected function typeInteger(Fluent $column)
|
||||
{
|
||||
return 'INTEGER';
|
||||
}
|
||||
|
||||
protected function typeSmallInteger(Fluent $column)
|
||||
{
|
||||
return 'SMALLINT';
|
||||
}
|
||||
|
||||
protected function typeString(Fluent $column)
|
||||
{
|
||||
return 'VARCHAR(' . ($column->length ?: 255) . ')';
|
||||
}
|
||||
|
||||
protected function typeText(Fluent $column)
|
||||
{
|
||||
return 'CLOB(1M)';
|
||||
}
|
||||
|
||||
protected function typeLongText(Fluent $column)
|
||||
{
|
||||
return 'CLOB(2M)';
|
||||
}
|
||||
|
||||
protected function typeBoolean(Fluent $column)
|
||||
{
|
||||
return 'SMALLINT';
|
||||
}
|
||||
|
||||
protected function typeTimestamp(Fluent $column)
|
||||
{
|
||||
return 'TIMESTAMP';
|
||||
}
|
||||
|
||||
protected function typeDateTime(Fluent $column)
|
||||
{
|
||||
return 'TIMESTAMP';
|
||||
}
|
||||
|
||||
protected function typeDate(Fluent $column)
|
||||
{
|
||||
return 'DATE';
|
||||
}
|
||||
|
||||
protected function typeBinary(Fluent $column)
|
||||
{
|
||||
return 'BLOB(1M)';
|
||||
}
|
||||
|
||||
protected function typeDecimal(Fluent $column)
|
||||
{
|
||||
return sprintf(
|
||||
'DECIMAL(%d,%d)',
|
||||
$column->total ?? 8,
|
||||
$column->places ?? 2
|
||||
);
|
||||
}
|
||||
|
||||
public function compilePrimary(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return sprintf(
|
||||
'ALTER TABLE %s ADD PRIMARY KEY (%s)',
|
||||
$this->wrapTable($blueprint),
|
||||
$this->columnize($command->columns)
|
||||
);
|
||||
}
|
||||
|
||||
protected function typeMediumText(Fluent $column)
|
||||
{
|
||||
return 'CLOB(16M)';
|
||||
}
|
||||
|
||||
public function compileUnique(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return sprintf(
|
||||
'CREATE UNIQUE INDEX %s ON %s (%s)',
|
||||
$this->wrap($command->index),
|
||||
$this->wrapTable($blueprint),
|
||||
$this->columnize($command->columns)
|
||||
);
|
||||
}
|
||||
|
||||
public function compileIndex(Blueprint $blueprint, Fluent $command)
|
||||
{
|
||||
return sprintf(
|
||||
'CREATE INDEX %s ON %s (%s)',
|
||||
$this->wrap($command->index),
|
||||
$this->wrapTable($blueprint),
|
||||
$this->columnize($command->columns)
|
||||
);
|
||||
}
|
||||
|
||||
protected function typeJson(\Illuminate\Database\Schema\ColumnDefinition $column)
|
||||
{
|
||||
return 'CLOB(1M)';
|
||||
}
|
||||
|
||||
protected function typeJsonb(\Illuminate\Database\Schema\ColumnDefinition $column)
|
||||
{
|
||||
return 'CLOB(1M)';
|
||||
}
|
||||
|
||||
protected function typeTinyText(\Illuminate\Database\Schema\ColumnDefinition $column)
|
||||
{
|
||||
return 'VARCHAR(255)';
|
||||
}
|
||||
|
||||
public function compileInsertOrIgnore(
|
||||
\Illuminate\Database\Query\Builder $query,
|
||||
array $values
|
||||
) {
|
||||
if (empty($values)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!is_array(reset($values))) {
|
||||
$values = [$values];
|
||||
}
|
||||
|
||||
$columns = array_keys(reset($values));
|
||||
|
||||
$table = $this->wrapTable($query->from);
|
||||
|
||||
$sourceColumns = implode(
|
||||
', ',
|
||||
array_map(fn ($column) => $this->wrap($column), $columns)
|
||||
);
|
||||
|
||||
$rows = [];
|
||||
|
||||
foreach ($values as $row) {
|
||||
$rows[] = '(' . implode(
|
||||
', ',
|
||||
array_fill(0, count($columns), '?')
|
||||
) . ')';
|
||||
}
|
||||
|
||||
$source = implode(', ', $rows);
|
||||
|
||||
$conditions = [];
|
||||
|
||||
foreach ($columns as $column) {
|
||||
$wrapped = $this->wrap($column);
|
||||
|
||||
$conditions[] = "target.$wrapped = source.$wrapped";
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'MERGE INTO %s AS target
|
||||
USING (VALUES %s) AS source (%s)
|
||||
ON %s
|
||||
WHEN NOT MATCHED THEN
|
||||
INSERT (%s)
|
||||
VALUES (%s)',
|
||||
$table,
|
||||
$source,
|
||||
$sourceColumns,
|
||||
implode(' AND ', $conditions),
|
||||
$sourceColumns,
|
||||
implode(
|
||||
', ',
|
||||
array_map(
|
||||
fn ($column) => 'source.' . $this->wrap($column),
|
||||
$columns
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user