db2 driver updates

This commit is contained in:
Mark Gregory
2026-11-11 13:18:47 +00:00
parent 76037ff72b
commit e85ab1da7b
46 changed files with 100 additions and 6663 deletions
+38 -35
View File
@@ -2,9 +2,9 @@
namespace App\Database\Db2;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Grammars\Grammar;
class Db2QueryGrammar extends Grammar
{
@@ -24,7 +24,9 @@ class Db2QueryGrammar extends Grammar
];
private array $db2ColumnCache = [];
public const NULL_STRING_SENTINEL = '__DB2_NULL_STRING__';
/**
* Compile a Db2 MERGE statement for Laravel upserts.
*/
@@ -68,32 +70,32 @@ class Db2QueryGrammar extends Grammar
$updates[] = "target.{$wrapped} = source.{$wrapped}";
} else {
$updates[] = "target."
. $this->wrap($key)
. " = "
. $this->parameter($value);
$updates[] = 'target.'
.$this->wrap($key)
.' = '
.$this->parameter($value);
}
}
$insertColumns = $this->columnize($columns);
$insertValues = implode(', ', array_map(
fn ($column) => 'source.' . $this->wrap($column),
fn ($column) => 'source.'.$this->wrap($column),
$columns
));
$sql = "MERGE INTO {$this->wrapTable($query->from)} AS target "
. "USING ({$source}) AS source "
. "ON (" . implode(' AND ', $matches) . ") ";
."USING ({$source}) AS source "
.'ON ('.implode(' AND ', $matches).') ';
if ($updates !== []) {
$sql .= "WHEN MATCHED THEN UPDATE SET "
. implode(', ', $updates) . " ";
$sql .= 'WHEN MATCHED THEN UPDATE SET '
.implode(', ', $updates).' ';
}
return $sql
. "WHEN NOT MATCHED THEN INSERT ({$insertColumns}) "
. "VALUES ({$insertValues})";
."WHEN NOT MATCHED THEN INSERT ({$insertColumns}) "
."VALUES ({$insertValues})";
}
/**
@@ -151,8 +153,8 @@ class Db2QueryGrammar extends Grammar
if ($conditions !== []) {
$uniqueConditions[] = '('
. implode(' AND ', $conditions)
. ')';
.implode(' AND ', $conditions)
.')';
}
}
@@ -165,15 +167,15 @@ class Db2QueryGrammar extends Grammar
$insertColumns = $this->columnize($columns);
$insertValues = implode(', ', array_map(
fn ($column) => 'source.' . $this->wrap($column),
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})";
."USING ({$source}) AS source "
.'ON ('.implode(' OR ', $uniqueConditions).') '
."WHEN NOT MATCHED THEN INSERT ({$insertColumns}) "
."VALUES ({$insertValues})";
}
/**
@@ -214,13 +216,13 @@ class Db2QueryGrammar extends Grammar
foreach ($columns as $column) {
$parts[] = $this->parameter($row[$column])
. ' AS '
. $this->wrap($column);
.' AS '
.$this->wrap($column);
}
$selects[] = 'SELECT '
. implode(', ', $parts)
. ' FROM SYSIBM.SYSDUMMY1';
.implode(', ', $parts)
.' FROM SYSIBM.SYSDUMMY1';
}
return implode(' UNION ALL ', $selects);
@@ -231,8 +233,8 @@ class Db2QueryGrammar extends Grammar
*/
public function compileSavepoint($name)
{
return 'SAVEPOINT ' . $name
. ' ON ROLLBACK RETAIN CURSORS';
return 'SAVEPOINT '.$name
.' ON ROLLBACK RETAIN CURSORS';
}
/**
@@ -240,7 +242,7 @@ class Db2QueryGrammar extends Grammar
*/
public function compileSavepointRollBack($name)
{
return 'ROLLBACK TO SAVEPOINT ' . $name;
return 'ROLLBACK TO SAVEPOINT '.$name;
}
protected function compileAggregate($query, $aggregate)
@@ -287,7 +289,7 @@ class Db2QueryGrammar extends Grammar
}
public function compileExists(
\Illuminate\Database\Query\Builder $query
Builder $query
) {
$select = $this->compileSelect($query);
@@ -443,6 +445,7 @@ class Db2QueryGrammar extends Grammar
if (in_array($type, ['CLOB', 'BLOB', 'DBCLOB'], true)) {
$lobs[] = $selected;
continue;
}
@@ -466,8 +469,8 @@ class Db2QueryGrammar extends Grammar
}
$expression = 'COALESCE('
. $wrapped
. ", CAST('' AS VARCHAR({$length})))";
.$wrapped
.", CAST('' AS VARCHAR({$length})))";
} else {
$formatted = $type === 'TIMESTAMP'
? "VARCHAR_FORMAT({$wrapped}, 'YYYY-MM-DD HH24:MI:SS.FF6')"
@@ -477,24 +480,24 @@ class Db2QueryGrammar extends Grammar
}
$ordinary[] = new Expression(
$expression . ' AS ' . $alias
$expression.' AS '.$alias
);
// Character indicators avoid the PDO_ODBC numeric
// aggregate/binary conversion issue.
$flagAlias = '__db2_null_flag_' . $indicatorIndex++;
$flagAlias = '__db2_null_flag_'.$indicatorIndex++;
// Ensure a generated alias cannot collide with any
// requested output column.
while (isset($aliases[strtolower($flagAlias)])) {
$flagAlias = '__db2_null_flag_' . $indicatorIndex++;
$flagAlias = '__db2_null_flag_'.$indicatorIndex++;
}
$ordinary[] = new Expression(
'CASE WHEN '
. $wrapped
. " IS NULL THEN '1' ELSE '0' END AS "
. $this->wrap($flagAlias)
.$wrapped
." IS NULL THEN '1' ELSE '0' END AS "
.$this->wrap($flagAlias)
);
$hasNullIndicators = true;