mirror of
https://github.com/dg/dibi.git
synced 2025-08-04 13:17:58 +02:00
type constants dibi::* moved to new class Type
This commit is contained in:
@@ -22,9 +22,9 @@ dibi::connect([
|
|||||||
// using manual hints
|
// using manual hints
|
||||||
$res = dibi::query('SELECT * FROM [customers]');
|
$res = dibi::query('SELECT * FROM [customers]');
|
||||||
|
|
||||||
$res->setType('customer_id', Dibi::INTEGER)
|
$res->setType('customer_id', DibiType::INTEGER)
|
||||||
->setType('added', Dibi::DATETIME)
|
->setType('added', DibiType::DATETIME)
|
||||||
->setFormat(dibi::DATETIME, 'Y-m-d H:i:s');
|
->setFormat(DibiType::DATETIME, 'Y-m-d H:i:s');
|
||||||
|
|
||||||
|
|
||||||
Tracy\Dumper::dump($res->fetch());
|
Tracy\Dumper::dump($res->fetch());
|
||||||
|
@@ -454,8 +454,8 @@ class DibiConnection extends DibiObject
|
|||||||
public function createResultSet(IDibiResultDriver $resultDriver)
|
public function createResultSet(IDibiResultDriver $resultDriver)
|
||||||
{
|
{
|
||||||
$res = new DibiResult($resultDriver);
|
$res = new DibiResult($resultDriver);
|
||||||
return $res->setFormat(dibi::DATE, $this->config['result']['formatDate'])
|
return $res->setFormat(DibiType::DATE, $this->config['result']['formatDate'])
|
||||||
->setFormat(dibi::DATETIME, $this->config['result']['formatDateTime']);
|
->setFormat(DibiType::DATETIME, $this->config['result']['formatDateTime']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -95,11 +95,11 @@ class DibiHelpers
|
|||||||
public static function escape($driver, $value, $type)
|
public static function escape($driver, $value, $type)
|
||||||
{
|
{
|
||||||
static $types = [
|
static $types = [
|
||||||
dibi::TEXT => 'text',
|
DibiType::TEXT => 'text',
|
||||||
dibi::BINARY => 'binary',
|
DibiType::BINARY => 'binary',
|
||||||
dibi::BOOL => 'bool',
|
DibiType::BOOL => 'bool',
|
||||||
dibi::DATE => 'date',
|
DibiType::DATE => 'date',
|
||||||
dibi::DATETIME => 'datetime',
|
DibiType::DATETIME => 'datetime',
|
||||||
dibi::IDENTIFIER => 'identifier',
|
dibi::IDENTIFIER => 'identifier',
|
||||||
];
|
];
|
||||||
if (isset($types[$type])) {
|
if (isset($types[$type])) {
|
||||||
|
@@ -171,15 +171,15 @@ class DibiColumnInfo extends DibiObject
|
|||||||
public static function detectType($type)
|
public static function detectType($type)
|
||||||
{
|
{
|
||||||
static $patterns = [
|
static $patterns = [
|
||||||
'^_' => dibi::TEXT, // PostgreSQL arrays
|
'^_' => DibiType::TEXT, // PostgreSQL arrays
|
||||||
'BYTEA|BLOB|BIN' => dibi::BINARY,
|
'BYTEA|BLOB|BIN' => DibiType::BINARY,
|
||||||
'TEXT|CHAR|POINT|INTERVAL' => dibi::TEXT,
|
'TEXT|CHAR|POINT|INTERVAL' => DibiType::TEXT,
|
||||||
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT' => dibi::INTEGER,
|
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT' => DibiType::INTEGER,
|
||||||
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => dibi::FLOAT,
|
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => DibiType::FLOAT,
|
||||||
'^TIME$' => dibi::TIME,
|
'^TIME$' => DibiType::TIME,
|
||||||
'TIME' => dibi::DATETIME, // DATETIME, TIMESTAMP
|
'TIME' => DibiType::DATETIME, // DATETIME, TIMESTAMP
|
||||||
'DATE' => dibi::DATE,
|
'DATE' => DibiType::DATE,
|
||||||
'BOOL' => dibi::BOOL,
|
'BOOL' => DibiType::BOOL,
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ($patterns as $s => $val) {
|
foreach ($patterns as $s => $val) {
|
||||||
@@ -187,7 +187,7 @@ class DibiColumnInfo extends DibiObject
|
|||||||
return $val;
|
return $val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dibi::TEXT;
|
return DibiType::TEXT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -488,24 +488,24 @@ class DibiResult extends DibiObject implements IDataSource
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$value = $row[$key];
|
$value = $row[$key];
|
||||||
if ($value === FALSE || $type === dibi::TEXT) {
|
if ($value === FALSE || $type === DibiType::TEXT) {
|
||||||
|
|
||||||
} elseif ($type === dibi::INTEGER) {
|
} elseif ($type === DibiType::INTEGER) {
|
||||||
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
|
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
|
||||||
|
|
||||||
} elseif ($type === dibi::FLOAT) {
|
} elseif ($type === DibiType::FLOAT) {
|
||||||
$row[$key] = str_replace(',', '.', ltrim((string) ($tmp = (float) $value), '0')) === ltrim(rtrim(rtrim($value, '0'), '.'), '0') ? $tmp : $value;
|
$row[$key] = str_replace(',', '.', ltrim((string) ($tmp = (float) $value), '0')) === ltrim(rtrim(rtrim($value, '0'), '.'), '0') ? $tmp : $value;
|
||||||
|
|
||||||
} elseif ($type === dibi::BOOL) {
|
} elseif ($type === DibiType::BOOL) {
|
||||||
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
|
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
|
||||||
|
|
||||||
} elseif ($type === dibi::DATE || $type === dibi::DATETIME) {
|
} elseif ($type === DibiType::DATE || $type === DibiType::DATETIME) {
|
||||||
if ((int) $value !== 0 || substr((string) $value, 0, 3) === '00:') { // '', NULL, FALSE, '0000-00-00', ...
|
if ((int) $value !== 0 || substr((string) $value, 0, 3) === '00:') { // '', NULL, FALSE, '0000-00-00', ...
|
||||||
$value = new DibiDateTime($value);
|
$value = new DibiDateTime($value);
|
||||||
$row[$key] = empty($this->formats[$type]) ? $value : $value->format($this->formats[$type]);
|
$row[$key] = empty($this->formats[$type]) ? $value : $value->format($this->formats[$type]);
|
||||||
}
|
}
|
||||||
|
|
||||||
} elseif ($type === dibi::BINARY) {
|
} elseif ($type === DibiType::BINARY) {
|
||||||
$row[$key] = $this->getResultDriver()->unescapeBinary($value);
|
$row[$key] = $this->getResultDriver()->unescapeBinary($value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -515,7 +515,7 @@ class DibiResult extends DibiObject implements IDataSource
|
|||||||
/**
|
/**
|
||||||
* Define column type.
|
* Define column type.
|
||||||
* @param string column
|
* @param string column
|
||||||
* @param string type (use constant Dibi::*)
|
* @param string type (use constant DibiType::*)
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
final public function setType($col, $type)
|
final public function setType($col, $type)
|
||||||
@@ -537,7 +537,7 @@ class DibiResult extends DibiObject implements IDataSource
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets data format.
|
* Sets data format.
|
||||||
* @param string type (use constant Dibi::*)
|
* @param string type (use constant DibiType::*)
|
||||||
* @param string format
|
* @param string format
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
|
31
src/Dibi/Type.php
Normal file
31
src/Dibi/Type.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This file is part of the "dibi" - smart database abstraction layer.
|
||||||
|
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data types.
|
||||||
|
*
|
||||||
|
* @package dibi
|
||||||
|
*/
|
||||||
|
class DibiType
|
||||||
|
{
|
||||||
|
const
|
||||||
|
TEXT = 's', // as 'string'
|
||||||
|
BINARY = 'bin',
|
||||||
|
BOOL = 'b',
|
||||||
|
INTEGER = 'i',
|
||||||
|
FLOAT = 'f',
|
||||||
|
DATE = 'd',
|
||||||
|
DATETIME = 't',
|
||||||
|
TIME = 't';
|
||||||
|
|
||||||
|
final public function __construct()
|
||||||
|
{
|
||||||
|
throw new LogicException('Cannot instantiate static class ' . __CLASS__);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -14,37 +14,39 @@
|
|||||||
*/
|
*/
|
||||||
class dibi
|
class dibi
|
||||||
{
|
{
|
||||||
/** column type */
|
const
|
||||||
const TEXT = 's', // as 'string'
|
AFFECTED_ROWS = 'a',
|
||||||
BINARY = 'bin',
|
IDENTIFIER = 'n';
|
||||||
BOOL = 'b',
|
|
||||||
INTEGER = 'i',
|
|
||||||
FLOAT = 'f',
|
|
||||||
DATE = 'd',
|
|
||||||
DATETIME = 't',
|
|
||||||
TIME = 't';
|
|
||||||
|
|
||||||
const IDENTIFIER = 'n',
|
|
||||||
AFFECTED_ROWS = 'a';
|
|
||||||
|
|
||||||
/** @deprecated */
|
|
||||||
const FIELD_TEXT = self::TEXT,
|
|
||||||
FIELD_BINARY = self::BINARY,
|
|
||||||
FIELD_BOOL = self::BOOL,
|
|
||||||
FIELD_INTEGER = self::INTEGER,
|
|
||||||
FIELD_FLOAT = self::FLOAT,
|
|
||||||
FIELD_DATE = self::DATE,
|
|
||||||
FIELD_DATETIME = self::DATETIME,
|
|
||||||
FIELD_TIME = self::TIME;
|
|
||||||
|
|
||||||
/** version */
|
/** version */
|
||||||
const VERSION = '2.4-dev',
|
const
|
||||||
|
VERSION = '2.4-dev',
|
||||||
REVISION = 'released on 2015-10-08';
|
REVISION = 'released on 2015-10-08';
|
||||||
|
|
||||||
/** sorting order */
|
/** sorting order */
|
||||||
const ASC = 'ASC',
|
const
|
||||||
|
ASC = 'ASC',
|
||||||
DESC = 'DESC';
|
DESC = 'DESC';
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
|
const
|
||||||
|
TEXT = DibiType::TEXT,
|
||||||
|
BINARY = DibiType::BINARY,
|
||||||
|
BOOL = DibiType::BOOL,
|
||||||
|
INTEGER = DibiType::INTEGER,
|
||||||
|
FLOAT = DibiType::FLOAT,
|
||||||
|
DATE = DibiType::DATE,
|
||||||
|
DATETIME = DibiType::DATETIME,
|
||||||
|
TIME = DibiType::TIME,
|
||||||
|
FIELD_TEXT = DibiType::TEXT,
|
||||||
|
FIELD_BINARY = DibiType::BINARY,
|
||||||
|
FIELD_BOOL = DibiType::BOOL,
|
||||||
|
FIELD_INTEGER = DibiType::INTEGER,
|
||||||
|
FIELD_FLOAT = DibiType::FLOAT,
|
||||||
|
FIELD_DATE = DibiType::DATE,
|
||||||
|
FIELD_DATETIME = DibiType::DATETIME,
|
||||||
|
FIELD_TIME = DibiType::TIME;
|
||||||
|
|
||||||
/** @var DibiConnection[] Connection registry storage for DibiConnection objects */
|
/** @var DibiConnection[] Connection registry storage for DibiConnection objects */
|
||||||
private static $registry = [];
|
private static $registry = [];
|
||||||
|
|
||||||
|
@@ -61,6 +61,7 @@ spl_autoload_register(function ($class) {
|
|||||||
'DibiSqliteReflector' => 'Drivers/SqliteReflector.php',
|
'DibiSqliteReflector' => 'Drivers/SqliteReflector.php',
|
||||||
'DibiTableInfo' => 'Reflection/Table.php',
|
'DibiTableInfo' => 'Reflection/Table.php',
|
||||||
'DibiTranslator' => 'Translator.php',
|
'DibiTranslator' => 'Translator.php',
|
||||||
|
'DibiType' => 'Type.php',
|
||||||
'IDataSource' => 'interfaces.php',
|
'IDataSource' => 'interfaces.php',
|
||||||
'IDibiDriver' => 'interfaces.php',
|
'IDibiDriver' => 'interfaces.php',
|
||||||
'IDibiReflector' => 'interfaces.php',
|
'IDibiReflector' => 'interfaces.php',
|
||||||
|
@@ -10,7 +10,7 @@ $conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
|||||||
$res = $conn->query('SELECT * FROM [customers]');
|
$res = $conn->query('SELECT * FROM [customers]');
|
||||||
|
|
||||||
// auto-converts this column to integer
|
// auto-converts this column to integer
|
||||||
$res->setType('customer_id', Dibi::DATETIME, 'H:i j.n.Y');
|
$res->setType('customer_id', DibiType::DATETIME, 'H:i j.n.Y');
|
||||||
|
|
||||||
Assert::equal(new DibiRow([
|
Assert::equal(new DibiRow([
|
||||||
'customer_id' => new DibiDateTime('1970-01-01 01:00:01'),
|
'customer_id' => new DibiDateTime('1970-01-01 01:00:01'),
|
||||||
|
Reference in New Issue
Block a user