Files
Testbed/app/Database/Db2/Db2Connection.php
T
2026-11-11 11:03:23 +00:00

113 lines
2.8 KiB
PHP

<?php
namespace App\Database\Db2;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Grammars\Grammar as QueryGrammar;
use App\Database\Db2\Db2Processor;
class Db2Connection extends Connection
{
/**
* Restore SQL NULL values from Db2 workaround expressions
* before Eloquent processes the results.
*/
public function select(
$query,
$bindings = [],
$useReadPdo = true,
array $fetchUsing = []
) {
$rows = parent::select(
$query,
$bindings,
$useReadPdo,
$fetchUsing
);
// Never interpret ordinary raw-query columns as
// internal NULL indicators.
if (
! is_string($query) ||
! str_contains($query, '/* DB2_NULL_FLAGS_V1 */')
) {
return $rows;
}
foreach ($rows as &$row) {
if (! is_object($row) && ! is_array($row)) {
continue;
}
$values = (array) $row;
$result = [];
$previousColumn = null;
foreach ($values as $key => $value) {
if (
is_string($key) &&
preg_match('/^__db2_null_flag_[0-9]+$/i', $key)
) {
if ($previousColumn === null) {
throw new \RuntimeException(
'Db2 NULL indicator has no preceding column.'
);
}
if ((string) $value === '1') {
$result[$previousColumn] = null;
} elseif ((string) $value !== '0') {
throw new \RuntimeException(
'Unexpected Db2 NULL indicator value.'
);
}
$previousColumn = null;
continue;
}
$result[$key] = $value;
$previousColumn = $key;
}
$row = is_object($row)
? (object) $result
: $result;
}
unset($row);
return $rows;
}
public function query()
{
return new Db2QueryBuilder($this, $this->getQueryGrammar(), $this->getPostProcessor());
}
protected function getDefaultQueryGrammar()
{
return new Db2QueryGrammar($this);
}
protected function getDefaultSchemaGrammar()
{
return new Db2SchemaGrammar($this);
}
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
return new Db2SchemaBuilder($this);
}
protected function getDefaultPostProcessor()
{
return new Db2Processor;
}
}