99 lines
2.4 KiB
PHP
99 lines
2.4 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
|
|
);
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
}
|