mirror of
https://github.com/dg/dibi.git
synced 2025-02-22 18:02:25 +01:00
Connection, Result: added format 'native'
This commit is contained in:
parent
a9e90d0b22
commit
c464960239
@ -45,12 +45,14 @@ class Connection implements IConnection
|
|||||||
* Connection options: (see driver-specific options too)
|
* Connection options: (see driver-specific options too)
|
||||||
* - lazy (bool) => if true, connection will be established only when required
|
* - lazy (bool) => if true, connection will be established only when required
|
||||||
* - result (array) => result set options
|
* - result (array) => result set options
|
||||||
* - formatDateTime => date-time format (if empty, DateTime objects will be returned)
|
* - formatDateTime => date-time format
|
||||||
* - formatJson => json format (
|
* empty for decoding as Dibi\DateTime (default)
|
||||||
* "string" for leaving value as is,
|
* "..." formatted according to given format, see https://www.php.net/manual/en/datetime.format.php
|
||||||
* "object" for decoding json as \stdClass,
|
* "native" for leaving value as is
|
||||||
* "array" for decoding json as an array - default
|
* - formatJson => json format
|
||||||
* )
|
* "array" for decoding json as an array (default)
|
||||||
|
* "object" for decoding json as \stdClass
|
||||||
|
* "native" for leaving value as is
|
||||||
* - profiler (array)
|
* - profiler (array)
|
||||||
* - run (bool) => enable profiler?
|
* - run (bool) => enable profiler?
|
||||||
* - file => file to log
|
* - file => file to log
|
||||||
|
@ -454,8 +454,12 @@ class Result implements IDataSource
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$value = $row[$key];
|
$value = $row[$key];
|
||||||
|
$format = $this->formats[$type] ?? null;
|
||||||
|
|
||||||
if ($type === Type::TEXT) {
|
if ($type === null || $format === 'native') {
|
||||||
|
$row[$key] = $value;
|
||||||
|
|
||||||
|
} elseif ($type === Type::TEXT) {
|
||||||
$row[$key] = (string) $value;
|
$row[$key] = (string) $value;
|
||||||
|
|
||||||
} elseif ($type === Type::INTEGER) {
|
} elseif ($type === Type::INTEGER) {
|
||||||
@ -485,9 +489,7 @@ class Result implements IDataSource
|
|||||||
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
|
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
|
||||||
if ($value && substr((string) $value, 0, 3) !== '000') { // '', null, false, '0000-00-00', ...
|
if ($value && substr((string) $value, 0, 3) !== '000') { // '', null, false, '0000-00-00', ...
|
||||||
$value = new DateTime($value);
|
$value = new DateTime($value);
|
||||||
$row[$key] = empty($this->formats[$type])
|
$row[$key] = $format ? $value->format($format) : $value;
|
||||||
? $value
|
|
||||||
: $value->format($this->formats[$type]);
|
|
||||||
} else {
|
} else {
|
||||||
$row[$key] = null;
|
$row[$key] = null;
|
||||||
}
|
}
|
||||||
@ -503,15 +505,12 @@ class Result implements IDataSource
|
|||||||
: $value;
|
: $value;
|
||||||
|
|
||||||
} elseif ($type === Type::JSON) {
|
} elseif ($type === Type::JSON) {
|
||||||
if ($this->formats[$type] === 'string') {
|
if ($format === 'string') { // back compatibility with 'native'
|
||||||
$row[$key] = $value;
|
$row[$key] = $value;
|
||||||
} else {
|
} else {
|
||||||
$row[$key] = json_decode($value, $this->formats[$type] === 'array');
|
$row[$key] = json_decode($value, $format === 'array');
|
||||||
}
|
}
|
||||||
|
|
||||||
} elseif ($type === null) {
|
|
||||||
$row[$key] = $value;
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new \RuntimeException('Unexpected type ' . $type);
|
throw new \RuntimeException('Unexpected type ' . $type);
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,17 @@ class MockResult extends Dibi\Result
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
test('', function () {
|
||||||
|
$result = new MockResult;
|
||||||
|
$result->setType('col', Type::TEXT);
|
||||||
|
$result->setFormat(Type::TEXT, 'native');
|
||||||
|
|
||||||
|
Assert::same(['col' => null], $result->test(['col' => null]));
|
||||||
|
Assert::same(['col' => true], $result->test(['col' => true]));
|
||||||
|
Assert::same(['col' => false], $result->test(['col' => false]));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
test('', function () {
|
test('', function () {
|
||||||
$result = new MockResult;
|
$result = new MockResult;
|
||||||
$result->setType('col', Type::BOOL);
|
$result->setType('col', Type::BOOL);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user