1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-19 15:45:27 +01:00

type constants dibi::* moved to new class Type

This commit is contained in:
David Grudl 2015-10-07 23:37:58 +02:00
parent 785a021b8d
commit ae68965710
9 changed files with 87 additions and 53 deletions

View File

@ -22,9 +22,9 @@ dibi::connect([
// using manual hints
$res = dibi::query('SELECT * FROM [customers]');
$res->setType('customer_id', Dibi::INTEGER)
->setType('added', Dibi::DATETIME)
->setFormat(dibi::DATETIME, 'Y-m-d H:i:s');
$res->setType('customer_id', DibiType::INTEGER)
->setType('added', DibiType::DATETIME)
->setFormat(DibiType::DATETIME, 'Y-m-d H:i:s');
Tracy\Dumper::dump($res->fetch());

View File

@ -454,8 +454,8 @@ class DibiConnection extends DibiObject
public function createResultSet(IDibiResultDriver $resultDriver)
{
$res = new DibiResult($resultDriver);
return $res->setFormat(dibi::DATE, $this->config['result']['formatDate'])
->setFormat(dibi::DATETIME, $this->config['result']['formatDateTime']);
return $res->setFormat(DibiType::DATE, $this->config['result']['formatDate'])
->setFormat(DibiType::DATETIME, $this->config['result']['formatDateTime']);
}

View File

@ -95,11 +95,11 @@ class DibiHelpers
public static function escape($driver, $value, $type)
{
static $types = [
dibi::TEXT => 'text',
dibi::BINARY => 'binary',
dibi::BOOL => 'bool',
dibi::DATE => 'date',
dibi::DATETIME => 'datetime',
DibiType::TEXT => 'text',
DibiType::BINARY => 'binary',
DibiType::BOOL => 'bool',
DibiType::DATE => 'date',
DibiType::DATETIME => 'datetime',
dibi::IDENTIFIER => 'identifier',
];
if (isset($types[$type])) {

View File

@ -171,15 +171,15 @@ class DibiColumnInfo extends DibiObject
public static function detectType($type)
{
static $patterns = [
'^_' => dibi::TEXT, // PostgreSQL arrays
'BYTEA|BLOB|BIN' => dibi::BINARY,
'TEXT|CHAR|POINT|INTERVAL' => dibi::TEXT,
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT' => dibi::INTEGER,
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => dibi::FLOAT,
'^TIME$' => dibi::TIME,
'TIME' => dibi::DATETIME, // DATETIME, TIMESTAMP
'DATE' => dibi::DATE,
'BOOL' => dibi::BOOL,
'^_' => DibiType::TEXT, // PostgreSQL arrays
'BYTEA|BLOB|BIN' => DibiType::BINARY,
'TEXT|CHAR|POINT|INTERVAL' => DibiType::TEXT,
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT' => DibiType::INTEGER,
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => DibiType::FLOAT,
'^TIME$' => DibiType::TIME,
'TIME' => DibiType::DATETIME, // DATETIME, TIMESTAMP
'DATE' => DibiType::DATE,
'BOOL' => DibiType::BOOL,
];
foreach ($patterns as $s => $val) {
@ -187,7 +187,7 @@ class DibiColumnInfo extends DibiObject
return $val;
}
}
return dibi::TEXT;
return DibiType::TEXT;
}

View File

@ -488,24 +488,24 @@ class DibiResult extends DibiObject implements IDataSource
continue;
}
$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;
} 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;
} elseif ($type === dibi::BOOL) {
} elseif ($type === DibiType::BOOL) {
$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', ...
$value = new DibiDateTime($value);
$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);
}
}
@ -515,7 +515,7 @@ class DibiResult extends DibiObject implements IDataSource
/**
* Define column type.
* @param string column
* @param string type (use constant Dibi::*)
* @param string type (use constant DibiType::*)
* @return self
*/
final public function setType($col, $type)
@ -537,7 +537,7 @@ class DibiResult extends DibiObject implements IDataSource
/**
* Sets data format.
* @param string type (use constant Dibi::*)
* @param string type (use constant DibiType::*)
* @param string format
* @return self
*/

31
src/Dibi/Type.php Normal file
View 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__);
}
}

View File

@ -14,37 +14,39 @@
*/
class dibi
{
/** column type */
const TEXT = 's', // as 'string'
BINARY = 'bin',
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;
const
AFFECTED_ROWS = 'a',
IDENTIFIER = 'n';
/** version */
const VERSION = '2.4-dev',
const
VERSION = '2.4-dev',
REVISION = 'released on 2015-10-08';
/** sorting order */
const ASC = 'ASC',
const
ASC = 'ASC',
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 */
private static $registry = [];

View File

@ -61,6 +61,7 @@ spl_autoload_register(function ($class) {
'DibiSqliteReflector' => 'Drivers/SqliteReflector.php',
'DibiTableInfo' => 'Reflection/Table.php',
'DibiTranslator' => 'Translator.php',
'DibiType' => 'Type.php',
'IDataSource' => 'interfaces.php',
'IDibiDriver' => 'interfaces.php',
'IDibiReflector' => 'interfaces.php',

View File

@ -10,7 +10,7 @@ $conn->loadFile(__DIR__ . "/data/$config[system].sql");
$res = $conn->query('SELECT * FROM [customers]');
// 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([
'customer_id' => new DibiDateTime('1970-01-01 01:00:01'),