1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-01 11:50:15 +02:00

PascalCase constants

This commit is contained in:
David Grudl
2024-05-10 15:44:51 +02:00
parent 490cf143ba
commit d707b4ba0e
16 changed files with 119 additions and 70 deletions

View File

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

View File

@@ -608,7 +608,7 @@ $database->query("UPDATE [:blog:items] SET [text]='Hello World'");
Dibi automatically detects the types of query columns and converts fields them to native PHP types. We can also specify the type manually. You can find the possible types in the `Dibi\Type` class. Dibi automatically detects the types of query columns and converts fields them to native PHP types. We can also specify the type manually. You can find the possible types in the `Dibi\Type` class.
```php ```php
$result->setType('id', Dibi\Type::INTEGER); // id will be integer $result->setType('id', Dibi\Type::Integer); // id will be integer
$row = $result->fetch(); $row = $result->fetch();
is_int($row->id) // true is_int($row->id) // true

View File

@@ -74,10 +74,10 @@ class Connection implements IConnection
$this->config = $config; $this->config = $config;
$this->formats = [ $this->formats = [
Type::DATE => $this->config['result']['formatDate'], Type::Date => $this->config['result']['formatDate'],
Type::DATETIME => $this->config['result']['formatDateTime'], Type::DateTime => $this->config['result']['formatDateTime'],
Type::JSON => $this->config['result']['formatJson'] ?? 'array', Type::JSON => $this->config['result']['formatJson'] ?? 'array',
Type::TIME_INTERVAL => $this->config['result']['formatTimeInterval'] ?? null, Type::TimeInterval => $this->config['result']['formatTimeInterval'] ?? null,
]; ];
// profiler // profiler

View File

@@ -26,7 +26,10 @@ use Dibi\Helpers;
*/ */
class FirebirdDriver implements Dibi\Driver class FirebirdDriver implements Dibi\Driver
{ {
public const ERROR_EXCEPTION_THROWN = -836; public const ErrorExceptionThrown = -836;
/** @deprecated use FirebirdDriver::ErrorExceptionThrown */
public const ERROR_EXCEPTION_THROWN = self::ErrorExceptionThrown;
/** @var resource */ /** @var resource */
private $connection; private $connection;

View File

@@ -32,9 +32,18 @@ use Dibi;
*/ */
class MySqliDriver implements Dibi\Driver class MySqliDriver implements Dibi\Driver
{ {
public const ERROR_ACCESS_DENIED = 1045; public const ErrorAccessDenied = 1045;
public const ERROR_DUPLICATE_ENTRY = 1062; public const ErrorDuplicateEntry = 1062;
public const ERROR_DATA_TRUNCATED = 1265; public const ErrorDataTruncated = 1265;
/** @deprecated use MySqliDriver::ErrorAccessDenied */
public const ERROR_ACCESS_DENIED = self::ErrorAccessDenied;
/** @deprecated use MySqliDriver::ErrorDuplicateEntry */
public const ERROR_DUPLICATE_ENTRY = self::ErrorDuplicateEntry;
/** @deprecated use MySqliDriver::ErrorDataTruncated */
public const ERROR_DATA_TRUNCATED = self::ErrorDataTruncated;
private \mysqli $connection; private \mysqli $connection;
private bool $buffered = false; private bool $buffered = false;

View File

@@ -103,7 +103,7 @@ class MySqliResult implements Dibi\ResultDriver
'table' => $row['orgtable'], 'table' => $row['orgtable'],
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'], 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
'nativetype' => $types[$row['type']] ?? $row['type'], 'nativetype' => $types[$row['type']] ?? $row['type'],
'type' => $row['type'] === MYSQLI_TYPE_TIME ? Dibi\Type::TIME_INTERVAL : null, 'type' => $row['type'] === MYSQLI_TYPE_TIME ? Dibi\Type::TimeInterval : null,
'vendor' => $row, 'vendor' => $row,
]; ];
} }

View File

@@ -80,7 +80,7 @@ class OracleResult implements Dibi\ResultDriver
'name' => oci_field_name($this->resultSet, $i), 'name' => oci_field_name($this->resultSet, $i),
'table' => null, 'table' => null,
'fullname' => oci_field_name($this->resultSet, $i), 'fullname' => oci_field_name($this->resultSet, $i),
'type' => $type === 'LONG' ? Dibi\Type::TEXT : null, 'type' => $type === 'LONG' ? Dibi\Type::Text : null,
'nativetype' => $type === 'NUMBER' && oci_field_scale($this->resultSet, $i) === 0 ? 'INTEGER' : $type, 'nativetype' => $type === 'NUMBER' && oci_field_scale($this->resultSet, $i) === 0 ? 'INTEGER' : $type,
]; ];
} }

View File

@@ -90,7 +90,7 @@ class PdoResult implements Dibi\ResultDriver
'name' => $row['name'], 'name' => $row['name'],
'table' => $row['table'], 'table' => $row['table'],
'nativetype' => $row['native_type'], 'nativetype' => $row['native_type'],
'type' => $row['native_type'] === 'TIME' && $this->driverName === 'mysql' ? Dibi\Type::TIME_INTERVAL : null, 'type' => $row['native_type'] === 'TIME' && $this->driverName === 'mysql' ? Dibi\Type::TimeInterval : null,
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'], 'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
'vendor' => $row, 'vendor' => $row,
]; ];

View File

@@ -45,7 +45,13 @@ namespace Dibi;
*/ */
class Fluent implements IDataSource class Fluent implements IDataSource
{ {
public const REMOVE = false; public const
AffectedRows = 'a',
Identifier = 'n',
Remove = false;
/** @deprecated use Fluent::Remove */
public const REMOVE = self::Remove;
public static array $masks = [ public static array $masks = [
'SELECT' => ['SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY', 'SELECT' => ['SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY',
@@ -140,7 +146,7 @@ class Fluent implements IDataSource
$this->cursor = &$this->clauses[$clause]; $this->cursor = &$this->clauses[$clause];
// TODO: really delete? // TODO: really delete?
if ($args === [self::REMOVE]) { if ($args === [self::Remove]) {
$this->cursor = null; $this->cursor = null;
return $this; return $this;
} }
@@ -156,7 +162,7 @@ class Fluent implements IDataSource
} }
} else { } else {
// append to currect flow // append to currect flow
if ($args === [self::REMOVE]) { if ($args === [self::Remove]) {
return $this; return $this;
} }
@@ -279,15 +285,15 @@ class Fluent implements IDataSource
/** /**
* Generates and executes SQL query. * Generates and executes SQL query.
* Returns result set or number of affected rows * Returns result set or number of affected rows
* @return ($return is \dibi::IDENTIFIER|\dibi::AFFECTED_ROWS ? int : Result) * @return ($return is self::Identifier|self::AffectedRows ? int : Result)
* @throws Exception * @throws Exception
*/ */
public function execute(?string $return = null): Result|int|null public function execute(?string $return = null): Result|int|null
{ {
$res = $this->query($this->_export()); $res = $this->query($this->_export());
return match ($return) { return match ($return) {
\dibi::IDENTIFIER => $this->connection->getInsertId(), self::Identifier => $this->connection->getInsertId(),
\dibi::AFFECTED_ROWS => $this->connection->getAffectedRows(), self::AffectedRows => $this->connection->getAffectedRows(),
default => $res, default => $res,
}; };
} }

View File

@@ -159,12 +159,12 @@ class Helpers
public static function escape(Driver $driver, $value, string $type): string public static function escape(Driver $driver, $value, string $type): string
{ {
$types = [ $types = [
Type::TEXT => 'text', Type::Text => 'text',
Type::BINARY => 'binary', Type::Binary => 'binary',
Type::BOOL => 'bool', Type::Bool => 'bool',
Type::DATE => 'date', Type::Date => 'date',
Type::DATETIME => 'datetime', Type::DateTime => 'datetime',
\dibi::IDENTIFIER => 'identifier', Fluent::Identifier => 'identifier',
]; ];
if (isset($types[$type])) { if (isset($types[$type])) {
return $driver->{'escape' . $types[$type]}($value); return $driver->{'escape' . $types[$type]}($value);
@@ -181,16 +181,16 @@ class Helpers
public static function detectType(string $type): ?string public static function detectType(string $type): ?string
{ {
$patterns = [ $patterns = [
'^_' => Type::TEXT, // PostgreSQL arrays '^_' => Type::Text, // PostgreSQL arrays
'RANGE$' => Type::TEXT, // PostgreSQL range types 'RANGE$' => Type::Text, // PostgreSQL range types
'BYTEA|BLOB|BIN' => Type::BINARY, 'BYTEA|BLOB|BIN' => Type::Binary,
'TEXT|CHAR|POINT|INTERVAL|STRING' => Type::TEXT, 'TEXT|CHAR|POINT|INTERVAL|STRING' => Type::Text,
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT|^TINY$' => Type::INTEGER, 'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT|^TINY$' => Type::Integer,
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => Type::FLOAT, 'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => Type::Float,
'^TIME$' => Type::TIME, '^TIME$' => Type::Time,
'TIME' => Type::DATETIME, // DATETIME, TIMESTAMP 'TIME' => Type::DateTime, // DATETIME, TIMESTAMP
'DATE' => Type::DATE, 'DATE' => Type::Date,
'BOOL' => Type::BOOL, 'BOOL' => Type::Bool,
'JSON' => Type::JSON, 'JSON' => Type::JSON,
]; ];

View File

@@ -457,15 +457,15 @@ class Result implements IDataSource
if ($type === null || $format === 'native') { if ($type === null || $format === 'native') {
$row[$key] = $value; $row[$key] = $value;
} elseif ($type === Type::TEXT) { } elseif ($type === Type::Text) {
$row[$key] = (string) $value; $row[$key] = (string) $value;
} elseif ($type === Type::INTEGER) { } elseif ($type === Type::Integer) {
$row[$key] = is_float($tmp = $value * 1) $row[$key] = is_float($tmp = $value * 1)
? (is_string($value) ? $value : (int) $value) ? (is_string($value) ? $value : (int) $value)
: $tmp; : $tmp;
} elseif ($type === Type::FLOAT) { } elseif ($type === Type::Float) {
$value = ltrim((string) $value, '0'); $value = ltrim((string) $value, '0');
$p = strpos($value, '.'); $p = strpos($value, '.');
$e = strpos($value, 'e'); $e = strpos($value, 'e');
@@ -483,23 +483,23 @@ class Result implements IDataSource
? $float ? $float
: $value; : $value;
} elseif ($type === Type::BOOL) { } elseif ($type === Type::Bool) {
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F'; $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) { } elseif ($type === Type::DateTime || $type === Type::Date || $type === Type::Time) {
if ($value && !str_starts_with((string) $value, '0000-00')) { // '', null, false, '0000-00-00', ... if ($value && !str_starts_with((string) $value, '0000-00')) { // '', null, false, '0000-00-00', ...
$value = new DateTime($value); $value = new DateTime($value);
$row[$key] = $format ? $value->format($format) : $value; $row[$key] = $format ? $value->format($format) : $value;
} else { } else {
$row[$key] = null; $row[$key] = null;
} }
} elseif ($type === Type::TIME_INTERVAL) { } elseif ($type === Type::TimeInterval) {
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m); preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m);
$value = new \DateInterval("PT$m[2]H$m[3]M$m[4]S"); $value = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
$value->invert = (int) (bool) $m[1]; $value->invert = (int) (bool) $m[1];
$row[$key] = $format ? $value->format($format) : $value; $row[$key] = $format ? $value->format($format) : $value;
} elseif ($type === Type::BINARY) { } elseif ($type === Type::Binary) {
$row[$key] = is_string($value) $row[$key] = is_string($value)
? $this->getResultDriver()->unescapeBinary($value) ? $this->getResultDriver()->unescapeBinary($value)
: $value; : $value;

View File

@@ -16,16 +16,43 @@ namespace Dibi;
class Type class Type
{ {
public const public const
TEXT = 's', // as 'string' Text = 's', // as 'string'
BINARY = 'bin', Binary = 'bin',
JSON = 'json', JSON = 'json',
BOOL = 'b', Bool = 'b',
INTEGER = 'i', Integer = 'i',
FLOAT = 'f', Float = 'f',
DATE = 'd', Date = 'd',
DATETIME = 'dt', DateTime = 'dt',
TIME = 't', Time = 't',
TIME_INTERVAL = 'ti'; TimeInterval = 'ti';
/** @deprecated use Type::Text */
public const TEXT = self::Text;
/** @deprecated use Type::Binary */
public const BINARY = self::Binary;
/** @deprecated use Type::Bool */
public const BOOL = self::Bool;
/** @deprecated use Type::Integer */
public const INTEGER = self::Integer;
/** @deprecated use Type::Float */
public const FLOAT = self::Float;
/** @deprecated use Type::Date */
public const DATE = self::Date;
/** @deprecated use Type::DateTime */
public const DATETIME = self::DateTime;
/** @deprecated use Type::Time */
public const TIME = self::Time;
/** @deprecated use Type::TimeInterval */
public const TIME_INTERVAL = self::TimeInterval;
final public function __construct() final public function __construct()

View File

@@ -37,12 +37,16 @@ declare(strict_types=1);
*/ */
class dibi class dibi
{ {
public const public const Version = '5.0.1';
AFFECTED_ROWS = 'a',
IDENTIFIER = 'n';
/** version */ /** @deprecated use dibi::Version */
public const VERSION = '5.0.1'; public const VERSION = self::Version;
/** @deprecated use Dibi\Fluent::AffectedRows */
public const AFFECTED_ROWS = Dibi\Fluent::AffectedRows;
/** @deprecated use Dibi\Fluent::Identifier */
public const IDENTIFIER = Dibi\Fluent::Identifier;
/** sorting order */ /** sorting order */
public const public const

View File

@@ -74,7 +74,7 @@ Assert::same(
(string) $fluent, (string) $fluent,
); );
$fluent->orderBy(Dibi\Fluent::REMOVE); $fluent->orderBy(Dibi\Fluent::Remove);
Assert::same( Assert::same(
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3)'), reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3)'),

View File

@@ -27,8 +27,8 @@ class MockResult extends Dibi\Result
test('', function () { test('', function () {
$result = new MockResult; $result = new MockResult;
$result->setType('col', Type::TEXT); $result->setType('col', Type::Text);
$result->setFormat(Type::TEXT, 'native'); $result->setFormat(Type::Text, 'native');
Assert::same(['col' => null], $result->test(['col' => null])); Assert::same(['col' => null], $result->test(['col' => null]));
Assert::same(['col' => true], $result->test(['col' => true])); Assert::same(['col' => true], $result->test(['col' => true]));
@@ -38,7 +38,7 @@ test('', function () {
test('', function () { test('', function () {
$result = new MockResult; $result = new MockResult;
$result->setType('col', Type::BOOL); $result->setType('col', Type::Bool);
Assert::same(['col' => null], $result->test(['col' => null])); Assert::same(['col' => null], $result->test(['col' => null]));
Assert::same(['col' => true], $result->test(['col' => true])); Assert::same(['col' => true], $result->test(['col' => true]));
@@ -60,7 +60,7 @@ test('', function () {
test('', function () { test('', function () {
$result = new MockResult; $result = new MockResult;
$result->setType('col', Type::TEXT); $result->setType('col', Type::Text);
Assert::same(['col' => null], $result->test(['col' => null])); Assert::same(['col' => null], $result->test(['col' => null]));
Assert::same(['col' => '1'], $result->test(['col' => true])); Assert::same(['col' => '1'], $result->test(['col' => true]));
@@ -76,7 +76,7 @@ test('', function () {
test('', function () { test('', function () {
$result = new MockResult; $result = new MockResult;
$result->setType('col', Type::FLOAT); $result->setType('col', Type::Float);
Assert::same(['col' => null], $result->test(['col' => null])); Assert::same(['col' => null], $result->test(['col' => null]));
Assert::same(['col' => 1.0], $result->test(['col' => true])); Assert::same(['col' => 1.0], $result->test(['col' => true]));
@@ -153,7 +153,7 @@ test('', function () {
test('', function () { test('', function () {
$result = new MockResult; $result = new MockResult;
$result->setType('col', Type::INTEGER); $result->setType('col', Type::Integer);
Assert::same(['col' => null], $result->test(['col' => null])); Assert::same(['col' => null], $result->test(['col' => null]));
Assert::same(['col' => 1], $result->test(['col' => true])); Assert::same(['col' => 1], $result->test(['col' => true]));
@@ -187,7 +187,7 @@ test('', function () {
test('', function () { test('', function () {
$result = new MockResult; $result = new MockResult;
$result->setType('col', Type::DATETIME); $result->setType('col', Type::DateTime);
Assert::same(['col' => null], $result->test(['col' => null])); Assert::same(['col' => null], $result->test(['col' => null]));
Assert::exception( Assert::exception(
@@ -206,8 +206,8 @@ test('', function () {
test('', function () { test('', function () {
$result = new MockResult; $result = new MockResult;
$result->setType('col', Type::DATETIME); $result->setType('col', Type::DateTime);
$result->setFormat(Type::DATETIME, 'Y-m-d H:i:s'); $result->setFormat(Type::DateTime, 'Y-m-d H:i:s');
Assert::same(['col' => null], $result->test(['col' => null])); Assert::same(['col' => null], $result->test(['col' => null]));
Assert::exception( Assert::exception(
@@ -226,7 +226,7 @@ test('', function () {
test('', function () { test('', function () {
$result = new MockResult; $result = new MockResult;
$result->setType('col', Type::DATE); $result->setType('col', Type::Date);
Assert::same(['col' => null], $result->test(['col' => null])); Assert::same(['col' => null], $result->test(['col' => null]));
Assert::exception( Assert::exception(
@@ -243,7 +243,7 @@ test('', function () {
test('', function () { test('', function () {
$result = new MockResult; $result = new MockResult;
$result->setType('col', Type::TIME); $result->setType('col', Type::Time);
Assert::same(['col' => null], $result->test(['col' => null])); Assert::same(['col' => null], $result->test(['col' => null]));
Assert::exception( Assert::exception(

View File

@@ -12,7 +12,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\Type::DATETIME); $res->setType('customer_id', Dibi\Type::DateTime);
Assert::equal(new Dibi\Row([ Assert::equal(new Dibi\Row([
'customer_id' => new Dibi\DateTime('1970-01-01 01:00:01'), 'customer_id' => new Dibi\DateTime('1970-01-01 01:00:01'),