initial Commit
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Db2;
|
||||
|
||||
use Illuminate\Database\Query\Grammars\Grammar;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Database\Query\Expression;
|
||||
|
||||
class Db2QueryGrammar extends Grammar
|
||||
{
|
||||
private array $db2ColumnCache = [];
|
||||
public const NULL_STRING_SENTINEL = '__DB2_NULL_STRING__';
|
||||
protected function compileAggregate($query, $aggregate)
|
||||
{
|
||||
$column = $this->columnize($aggregate['columns']);
|
||||
|
||||
if (is_array($query->distinct)) {
|
||||
$column = 'distinct '.$this->columnize($query->distinct);
|
||||
} elseif ($query->distinct && $column !== '*') {
|
||||
$column = 'distinct '.$column;
|
||||
}
|
||||
|
||||
$function = strtolower($aggregate['function']);
|
||||
|
||||
$expression = "{$function}({$column})";
|
||||
|
||||
// PDO_ODBC may return Db2 integer aggregates as binary data.
|
||||
// Cast only the migration batch aggregate to VARCHAR.
|
||||
if (
|
||||
$function === 'max' &&
|
||||
count($aggregate['columns']) === 1 &&
|
||||
$aggregate['columns'][0] === 'batch' &&
|
||||
$query->from === 'migrations'
|
||||
) {
|
||||
$expression = "CAST(COALESCE({$expression}, 0) AS VARCHAR(20))";
|
||||
}
|
||||
|
||||
return 'select '.$expression.' as aggregate';
|
||||
}
|
||||
|
||||
protected function compileLimit($query, $limit)
|
||||
{
|
||||
return 'fetch first '.(int) $limit.' rows only';
|
||||
}
|
||||
|
||||
public function compileExists(
|
||||
\Illuminate\Database\Query\Builder $query
|
||||
) {
|
||||
$select = $this->compileSelect($query);
|
||||
|
||||
return sprintf(
|
||||
'SELECT CASE WHEN EXISTS (%s) THEN 1 ELSE 0 END AS "exists" FROM SYSIBM.SYSDUMMY1',
|
||||
$select
|
||||
);
|
||||
}
|
||||
|
||||
/** A non-NULL value used only for nullable TIMESTAMP result columns. */
|
||||
public const NULL_TIMESTAMP_SENTINEL = '__DB2_NULL_TIMESTAMP__';
|
||||
|
||||
public function compileSelect(\Illuminate\Database\Query\Builder $query)
|
||||
{
|
||||
if (
|
||||
! is_string($query->from) ||
|
||||
! preg_match('/^(?:"[^"]+"|[A-Za-z_][A-Za-z0-9_]*)(?:\.(?:"[^"]+"|[A-Za-z_][A-Za-z0-9_]*))?$/', $query->from) ||
|
||||
! empty($query->joins) || ! empty($query->unions) ||
|
||||
! empty($query->groups) || ! empty($query->havings) ||
|
||||
$query->aggregate !== null || $query->distinct
|
||||
) {
|
||||
return parent::compileSelect($query);
|
||||
}
|
||||
|
||||
$table = $query->from;
|
||||
if (! isset($this->db2ColumnCache[$table])) {
|
||||
$this->db2ColumnCache[$table] = $this->connection
|
||||
->getSchemaBuilder()->getColumns($table);
|
||||
}
|
||||
|
||||
$metadata = [];
|
||||
foreach ($this->db2ColumnCache[$table] as $column) {
|
||||
$metadata[strtolower($column['name'])] = $column;
|
||||
}
|
||||
|
||||
$columns = $query->columns ?? ['*'];
|
||||
if ($columns === ['*']) {
|
||||
$columns = array_column($this->db2ColumnCache[$table], 'name');
|
||||
}
|
||||
|
||||
$ordinary = [];
|
||||
$lobs = [];
|
||||
$changed = false;
|
||||
|
||||
foreach ($columns as $column) {
|
||||
// Expressions and partial wildcards cannot be safely rearranged.
|
||||
if (! is_string($column) || ! preg_match(
|
||||
'/^((?:"[^"]+"|[A-Za-z_][A-Za-z0-9_]*)(?:\.(?:"[^"]+"|[A-Za-z_][A-Za-z0-9_]*))?)(?:\s+as\s+("[^"]+"|[A-Za-z_][A-Za-z0-9_]*))?$/i',
|
||||
$column, $matches
|
||||
)) {
|
||||
return parent::compileSelect($query);
|
||||
}
|
||||
|
||||
$identifier = $matches[1];
|
||||
$parts = explode('.', $identifier);
|
||||
$name = trim(end($parts), '"');
|
||||
$info = $metadata[strtolower($name)] ?? null;
|
||||
if ($info === null) {
|
||||
return parent::compileSelect($query);
|
||||
}
|
||||
|
||||
$type = strtoupper($info['type_name']);
|
||||
$selected = $column;
|
||||
|
||||
// Work around PDO_ODBC attempting a 4 GiB allocation
|
||||
// when fetching NULL Fortify VARCHAR(4096) columns.
|
||||
if (
|
||||
($info['nullable'] ?? false)
|
||||
&& $type === 'VARCHAR'
|
||||
&& in_array($name, [
|
||||
'two_factor_secret',
|
||||
'two_factor_recovery_codes',
|
||||
], true)
|
||||
) {
|
||||
$alias = isset($matches[2])
|
||||
? trim($matches[2], '"')
|
||||
: $name;
|
||||
|
||||
$wrapped = $this->wrap($identifier);
|
||||
|
||||
$selected = new Expression(
|
||||
'COALESCE('
|
||||
. $wrapped
|
||||
. ", CAST('__DB2_NULL_STRING__' AS VARCHAR(4096))) AS "
|
||||
. $this->wrap($alias)
|
||||
);
|
||||
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if (($info['nullable'] ?? false) && in_array($type, ['TIMESTAMP', 'DATE', 'TIME'], true)) {
|
||||
// Never return a SQL NULL date/time to the broken PDO_ODBC
|
||||
// retrieval path. Use a non-NULL character expression instead.
|
||||
$format = match ($type) {
|
||||
'DATE' => 'YYYY-MM-DD',
|
||||
'TIME' => 'HH24:MI:SS',
|
||||
default => 'YYYY-MM-DD HH24:MI:SS.FF6',
|
||||
};
|
||||
$alias = isset($matches[2]) ? trim($matches[2], '"') : $name;
|
||||
$wrapped = $this->wrap($identifier);
|
||||
$selected = new Expression(
|
||||
'CASE WHEN '.$wrapped.' IS NULL THEN '.
|
||||
"'".self::NULL_TIMESTAMP_SENTINEL."'".
|
||||
' ELSE VARCHAR_FORMAT('.$wrapped.", '".$format."') END AS ".
|
||||
$this->wrap($alias)
|
||||
);
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if (in_array($type, ['CLOB', 'BLOB', 'DBCLOB'], true)) {
|
||||
$lobs[] = $selected;
|
||||
} else {
|
||||
$ordinary[] = $selected;
|
||||
}
|
||||
}
|
||||
|
||||
if ($lobs !== [] && $ordinary !== [] && $columns !== array_merge(
|
||||
array_filter($ordinary, 'is_string'),
|
||||
array_filter($lobs, 'is_string')
|
||||
)) {
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if (! $changed && $lobs === []) {
|
||||
return parent::compileSelect($query);
|
||||
}
|
||||
|
||||
$modified = clone $query;
|
||||
$modified->columns = array_merge($ordinary, $lobs);
|
||||
return parent::compileSelect($modified);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user