db2 driver updates
This commit is contained in:
@@ -8,8 +8,241 @@ use Illuminate\Database\Query\Expression;
|
||||
|
||||
class Db2QueryGrammar extends Grammar
|
||||
{
|
||||
protected $selectComponents = [
|
||||
'aggregate',
|
||||
'columns',
|
||||
'from',
|
||||
'indexHint',
|
||||
'joins',
|
||||
'wheres',
|
||||
'groups',
|
||||
'havings',
|
||||
'orders',
|
||||
'offset',
|
||||
'limit',
|
||||
'lock',
|
||||
];
|
||||
|
||||
private array $db2ColumnCache = [];
|
||||
public const NULL_STRING_SENTINEL = '__DB2_NULL_STRING__';
|
||||
/**
|
||||
* Compile a Db2 MERGE statement for Laravel upserts.
|
||||
*/
|
||||
public function compileUpsert(
|
||||
Builder $query,
|
||||
array $values,
|
||||
array $uniqueBy,
|
||||
array $update
|
||||
) {
|
||||
$columns = $this->db2MergeColumns($values);
|
||||
|
||||
foreach ($uniqueBy as $column) {
|
||||
if (! in_array($column, $columns, true)) {
|
||||
throw new \InvalidArgumentException(
|
||||
"Upsert key is missing from inserted values: {$column}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$source = $this->db2MergeSource($values, $columns);
|
||||
|
||||
$matches = [];
|
||||
|
||||
foreach ($uniqueBy as $column) {
|
||||
$wrapped = $this->wrap($column);
|
||||
|
||||
$matches[] = "target.{$wrapped} = source.{$wrapped}";
|
||||
}
|
||||
|
||||
$updates = [];
|
||||
|
||||
foreach ($update as $key => $value) {
|
||||
if (is_int($key)) {
|
||||
if (! in_array($value, $columns, true)) {
|
||||
throw new \InvalidArgumentException(
|
||||
"Unknown upsert update column: {$value}"
|
||||
);
|
||||
}
|
||||
|
||||
$wrapped = $this->wrap($value);
|
||||
|
||||
$updates[] = "target.{$wrapped} = source.{$wrapped}";
|
||||
} else {
|
||||
$updates[] = "target."
|
||||
. $this->wrap($key)
|
||||
. " = "
|
||||
. $this->parameter($value);
|
||||
}
|
||||
}
|
||||
|
||||
$insertColumns = $this->columnize($columns);
|
||||
|
||||
$insertValues = implode(', ', array_map(
|
||||
fn ($column) => 'source.' . $this->wrap($column),
|
||||
$columns
|
||||
));
|
||||
|
||||
$sql = "MERGE INTO {$this->wrapTable($query->from)} AS target "
|
||||
. "USING ({$source}) AS source "
|
||||
. "ON (" . implode(' AND ', $matches) . ") ";
|
||||
|
||||
if ($updates !== []) {
|
||||
$sql .= "WHEN MATCHED THEN UPDATE SET "
|
||||
. implode(', ', $updates) . " ";
|
||||
}
|
||||
|
||||
return $sql
|
||||
. "WHEN NOT MATCHED THEN INSERT ({$insertColumns}) "
|
||||
. "VALUES ({$insertValues})";
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert rows without replacing existing rows.
|
||||
*
|
||||
* Db2 MERGE ignores rows that match the selected key.
|
||||
*/
|
||||
public function compileInsertOrIgnore(
|
||||
Builder $query,
|
||||
array $values
|
||||
) {
|
||||
$columns = $this->db2MergeColumns($values);
|
||||
$source = $this->db2MergeSource($values, $columns);
|
||||
|
||||
$indexes = $this->connection
|
||||
->getSchemaBuilder()
|
||||
->getIndexes($query->from);
|
||||
|
||||
$uniqueConditions = [];
|
||||
|
||||
foreach ($indexes as $index) {
|
||||
if (! ($index['unique'] ?? false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$indexColumns = $index['columns'] ?? [];
|
||||
|
||||
if ($indexColumns === []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// A unique key can only be compared if every
|
||||
// participating column exists in the source.
|
||||
$conditions = [];
|
||||
|
||||
foreach ($indexColumns as $column) {
|
||||
$matchedColumn = null;
|
||||
|
||||
foreach ($columns as $candidate) {
|
||||
if (strcasecmp($candidate, $column) === 0) {
|
||||
$matchedColumn = $candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($matchedColumn === null) {
|
||||
$conditions = [];
|
||||
break;
|
||||
}
|
||||
|
||||
$wrapped = $this->wrap($matchedColumn);
|
||||
|
||||
$conditions[] = "target.{$wrapped} = source.{$wrapped}";
|
||||
}
|
||||
|
||||
if ($conditions !== []) {
|
||||
$uniqueConditions[] = '('
|
||||
. implode(' AND ', $conditions)
|
||||
. ')';
|
||||
}
|
||||
}
|
||||
|
||||
if ($uniqueConditions === []) {
|
||||
throw new \RuntimeException(
|
||||
'Db2 insertOrIgnore requires an applicable unique index.'
|
||||
);
|
||||
}
|
||||
|
||||
$insertColumns = $this->columnize($columns);
|
||||
|
||||
$insertValues = implode(', ', array_map(
|
||||
fn ($column) => 'source.' . $this->wrap($column),
|
||||
$columns
|
||||
));
|
||||
|
||||
return "MERGE INTO {$this->wrapTable($query->from)} AS target "
|
||||
. "USING ({$source}) AS source "
|
||||
. "ON (" . implode(' OR ', $uniqueConditions) . ") "
|
||||
. "WHEN NOT MATCHED THEN INSERT ({$insertColumns}) "
|
||||
. "VALUES ({$insertValues})";
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the column layout of a multi-row MERGE.
|
||||
*/
|
||||
private function db2MergeColumns(array $values): array
|
||||
{
|
||||
if ($values === []) {
|
||||
throw new \InvalidArgumentException(
|
||||
'MERGE requires at least one row.'
|
||||
);
|
||||
}
|
||||
|
||||
$columns = array_keys(reset($values));
|
||||
|
||||
foreach ($values as $row) {
|
||||
if (array_keys($row) !== $columns) {
|
||||
throw new \InvalidArgumentException(
|
||||
'All MERGE rows must contain the same columns.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a parameterized Db2 MERGE source.
|
||||
*/
|
||||
private function db2MergeSource(
|
||||
array $values,
|
||||
array $columns
|
||||
): string {
|
||||
$selects = [];
|
||||
|
||||
foreach ($values as $row) {
|
||||
$parts = [];
|
||||
|
||||
foreach ($columns as $column) {
|
||||
$parts[] = $this->parameter($row[$column])
|
||||
. ' AS '
|
||||
. $this->wrap($column);
|
||||
}
|
||||
|
||||
$selects[] = 'SELECT '
|
||||
. implode(', ', $parts)
|
||||
. ' FROM SYSIBM.SYSDUMMY1';
|
||||
}
|
||||
|
||||
return implode(' UNION ALL ', $selects);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile a Db2 LUW savepoint statement.
|
||||
*/
|
||||
public function compileSavepoint($name)
|
||||
{
|
||||
return 'SAVEPOINT ' . $name
|
||||
. ' ON ROLLBACK RETAIN CURSORS';
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile a Db2 LUW rollback-to-savepoint statement.
|
||||
*/
|
||||
public function compileSavepointRollBack($name)
|
||||
{
|
||||
return 'ROLLBACK TO SAVEPOINT ' . $name;
|
||||
}
|
||||
|
||||
protected function compileAggregate($query, $aggregate)
|
||||
{
|
||||
$column = $this->columnize($aggregate['columns']);
|
||||
@@ -35,12 +268,23 @@ class Db2QueryGrammar extends Grammar
|
||||
$expression = "CAST(COALESCE({$expression}, 0) AS VARCHAR(20))";
|
||||
}
|
||||
|
||||
return 'select '.$expression.' as aggregate';
|
||||
return 'select '.$expression.' as "aggregate"';
|
||||
}
|
||||
|
||||
protected function compileLimit($query, $limit)
|
||||
{
|
||||
return 'fetch first '.(int) $limit.' rows only';
|
||||
$limit = (int) $limit;
|
||||
|
||||
if ((int) ($query->offset ?? 0) > 0) {
|
||||
return 'fetch next '.$limit.' rows only';
|
||||
}
|
||||
|
||||
return 'fetch first '.$limit.' rows only';
|
||||
}
|
||||
|
||||
protected function compileOffset($query, $offset)
|
||||
{
|
||||
return 'offset '.(int) $offset.' rows';
|
||||
}
|
||||
|
||||
public function compileExists(
|
||||
@@ -153,6 +397,7 @@ class Db2QueryGrammar extends Grammar
|
||||
$lobs = [];
|
||||
$changed = false;
|
||||
$indicatorIndex = 0;
|
||||
$hasNullIndicators = false;
|
||||
|
||||
foreach ($parsed as $entry) {
|
||||
$info = $entry['info'];
|
||||
@@ -191,18 +436,12 @@ class Db2QueryGrammar extends Grammar
|
||||
. $wrapped
|
||||
. ", CAST('' AS VARCHAR({$length})))";
|
||||
} else {
|
||||
$format = match ($type) {
|
||||
'DATE' => 'YYYY-MM-DD',
|
||||
'TIME' => 'HH24:MI:SS',
|
||||
default => 'YYYY-MM-DD HH24:MI:SS.FF6',
|
||||
};
|
||||
|
||||
$expression = 'CASE WHEN '
|
||||
. $wrapped
|
||||
. " IS NULL THEN CAST('' AS VARCHAR(32))"
|
||||
. ' ELSE VARCHAR_FORMAT('
|
||||
. ' ELSE CAST('
|
||||
. $wrapped
|
||||
. ", '{$format}') END";
|
||||
. ' AS VARCHAR(32)) END';
|
||||
}
|
||||
|
||||
$ordinary[] = new Expression(
|
||||
@@ -226,6 +465,7 @@ class Db2QueryGrammar extends Grammar
|
||||
. $this->wrap($flagAlias)
|
||||
);
|
||||
|
||||
$hasNullIndicators = true;
|
||||
$changed = true;
|
||||
} else {
|
||||
$ordinary[] = $selected;
|
||||
@@ -243,6 +483,14 @@ class Db2QueryGrammar extends Grammar
|
||||
$modified = clone $query;
|
||||
$modified->columns = array_merge($ordinary, $lobs);
|
||||
|
||||
return parent::compileSelect($modified);
|
||||
$sql = parent::compileSelect($modified);
|
||||
|
||||
// Only mark SELECT statements that actually contain
|
||||
// generated NULL indicator columns.
|
||||
if ($hasNullIndicators) {
|
||||
$sql .= ' /* DB2_NULL_FLAGS_V1 */';
|
||||
}
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user