Skip to content

Commit

Permalink
PascalCase constants
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 10, 2024
1 parent 9908f6f commit d2d19d5
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 70 deletions.
6 changes: 3 additions & 3 deletions examples/result-set-data-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
// using manual hints
$res = $dibi->query('SELECT * FROM [customers]');

$res->setType('customer_id', Type::INTEGER)
->setType('added', Type::DATETIME)
->setFormat(Type::DATETIME, 'Y-m-d H:i:s');
$res->setType('customer_id', Type::Integer)
->setType('added', Type::DateTime)
->setFormat(Type::DateTime, 'Y-m-d H:i:s');


Tracy\Dumper::dump($res->fetch());
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

```php
$result->setType('id', Dibi\Type::INTEGER); // id will be integer
$result->setType('id', Dibi\Type::Integer); // id will be integer
$row = $result->fetch();

is_int($row->id) // true
Expand Down
6 changes: 3 additions & 3 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public function __construct(array $config, ?string $name = null)
$this->config = $config;

$this->formats = [
Type::DATE => $this->config['result']['formatDate'],
Type::DATETIME => $this->config['result']['formatDateTime'],
Type::Date => $this->config['result']['formatDate'],
Type::DateTime => $this->config['result']['formatDateTime'],
Type::JSON => $this->config['result']['formatJson'] ?? 'array',
Type::TIME_INTERVAL => $this->config['result']['formatTimeInterval'] ?? null,
Type::TimeInterval => $this->config['result']['formatTimeInterval'] ?? null,
];

// profiler
Expand Down
5 changes: 4 additions & 1 deletion src/Dibi/Drivers/FirebirdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
*/
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 */
private $connection;
Expand Down
15 changes: 12 additions & 3 deletions src/Dibi/Drivers/MySqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,18 @@
*/
class MySqliDriver implements Dibi\Driver
{
public const ERROR_ACCESS_DENIED = 1045;
public const ERROR_DUPLICATE_ENTRY = 1062;
public const ERROR_DATA_TRUNCATED = 1265;
public const ErrorAccessDenied = 1045;
public const ErrorDuplicateEntry = 1062;
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 bool $buffered = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/MySqliResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getResultColumns(): array
'table' => $row['orgtable'],
'fullname' => $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'],
'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,
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/OracleResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function getResultColumns(): array
'name' => oci_field_name($this->resultSet, $i),
'table' => null,
'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,
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/PdoResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getResultColumns(): array
'name' => $row['name'],
'table' => $row['table'],
'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'],
'vendor' => $row,
];
Expand Down
18 changes: 12 additions & 6 deletions src/Dibi/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@
*/
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 = [
'SELECT' => ['SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY',
Expand Down Expand Up @@ -140,7 +146,7 @@ public function __call(string $clause, array $args): static
$this->cursor = &$this->clauses[$clause];

// TODO: really delete?
if ($args === [self::REMOVE]) {
if ($args === [self::Remove]) {
$this->cursor = null;
return $this;
}
Expand All @@ -156,7 +162,7 @@ public function __call(string $clause, array $args): static
}
} else {
// append to currect flow
if ($args === [self::REMOVE]) {
if ($args === [self::Remove]) {
return $this;
}

Expand Down Expand Up @@ -279,15 +285,15 @@ public function setupResult(string $method): static
/**
* Generates and executes SQL query.
* 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
*/
public function execute(?string $return = null): Result|int|null
{
$res = $this->query($this->_export());
return match ($return) {
\dibi::IDENTIFIER => $this->connection->getInsertId(),
\dibi::AFFECTED_ROWS => $this->connection->getAffectedRows(),
self::Identifier => $this->connection->getInsertId(),
self::AffectedRows => $this->connection->getAffectedRows(),
default => $res,
};
}
Expand Down
32 changes: 16 additions & 16 deletions src/Dibi/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ public static function getSuggestion(array $items, string $value): ?string
public static function escape(Driver $driver, $value, string $type): string
{
$types = [
Type::TEXT => 'text',
Type::BINARY => 'binary',
Type::BOOL => 'bool',
Type::DATE => 'date',
Type::DATETIME => 'datetime',
\dibi::IDENTIFIER => 'identifier',
Type::Text => 'text',
Type::Binary => 'binary',
Type::Bool => 'bool',
Type::Date => 'date',
Type::DateTime => 'datetime',
Fluent::Identifier => 'identifier',
];
if (isset($types[$type])) {
return $driver->{'escape' . $types[$type]}($value);
Expand All @@ -181,16 +181,16 @@ public static function escape(Driver $driver, $value, string $type): string
public static function detectType(string $type): ?string
{
$patterns = [
'^_' => Type::TEXT, // PostgreSQL arrays
'RANGE$' => Type::TEXT, // PostgreSQL range types
'BYTEA|BLOB|BIN' => Type::BINARY,
'TEXT|CHAR|POINT|INTERVAL|STRING' => Type::TEXT,
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT|^TINY$' => Type::INTEGER,
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => Type::FLOAT,
'^TIME$' => Type::TIME,
'TIME' => Type::DATETIME, // DATETIME, TIMESTAMP
'DATE' => Type::DATE,
'BOOL' => Type::BOOL,
'^_' => Type::Text, // PostgreSQL arrays
'RANGE$' => Type::Text, // PostgreSQL range types
'BYTEA|BLOB|BIN' => Type::Binary,
'TEXT|CHAR|POINT|INTERVAL|STRING' => Type::Text,
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT|^TINY$' => Type::Integer,
'CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER' => Type::Float,
'^TIME$' => Type::Time,
'TIME' => Type::DateTime, // DATETIME, TIMESTAMP
'DATE' => Type::Date,
'BOOL' => Type::Bool,
'JSON' => Type::JSON,
];

Expand Down
14 changes: 7 additions & 7 deletions src/Dibi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,15 @@ private function normalize(array &$row): void
if ($type === null || $format === 'native') {
$row[$key] = $value;

} elseif ($type === Type::TEXT) {
} elseif ($type === Type::Text) {
$row[$key] = (string) $value;

} elseif ($type === Type::INTEGER) {
} elseif ($type === Type::Integer) {
$row[$key] = is_float($tmp = $value * 1)
? (is_string($value) ? $value : (int) $value)
: $tmp;

} elseif ($type === Type::FLOAT) {
} elseif ($type === Type::Float) {
$value = ltrim((string) $value, '0');
$p = strpos($value, '.');
$e = strpos($value, 'e');
Expand All @@ -483,23 +483,23 @@ private function normalize(array &$row): void
? $float
: $value;

} elseif ($type === Type::BOOL) {
} elseif ($type === Type::Bool) {
$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', ...
$value = new DateTime($value);
$row[$key] = $format ? $value->format($format) : $value;
} else {
$row[$key] = null;
}
} elseif ($type === Type::TIME_INTERVAL) {
} elseif ($type === Type::TimeInterval) {
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m);
$value = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
$value->invert = (int) (bool) $m[1];
$row[$key] = $format ? $value->format($format) : $value;

} elseif ($type === Type::BINARY) {
} elseif ($type === Type::Binary) {
$row[$key] = is_string($value)
? $this->getResultDriver()->unescapeBinary($value)
: $value;
Expand Down
45 changes: 36 additions & 9 deletions src/Dibi/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,43 @@
class Type
{
public const
TEXT = 's', // as 'string'
BINARY = 'bin',
Text = 's', // as 'string'
Binary = 'bin',
JSON = 'json',
BOOL = 'b',
INTEGER = 'i',
FLOAT = 'f',
DATE = 'd',
DATETIME = 'dt',
TIME = 't',
TIME_INTERVAL = 'ti';
Bool = 'b',
Integer = 'i',
Float = 'f',
Date = 'd',
DateTime = 'dt',
Time = 't',
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()
Expand Down
14 changes: 9 additions & 5 deletions src/Dibi/dibi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@
*/
class dibi
{
public const
AFFECTED_ROWS = 'a',
IDENTIFIER = 'n';
public const Version = '5.0.1';

/** @deprecated use dibi::Version */
public const VERSION = self::Version;

/** @deprecated use Dibi\Fluent::AffectedRows */
public const AFFECTED_ROWS = Dibi\Fluent::AffectedRows;

/** version */
public const VERSION = '5.0.1';
/** @deprecated use Dibi\Fluent::Identifier */
public const IDENTIFIER = Dibi\Fluent::Identifier;

/** sorting order */
public const
Expand Down
2 changes: 1 addition & 1 deletion tests/dibi/Fluent.select.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Assert::same(
(string) $fluent,
);

$fluent->orderBy(Dibi\Fluent::REMOVE);
$fluent->orderBy(Dibi\Fluent::Remove);

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)'),
Expand Down
Loading

0 comments on commit d2d19d5

Please sign in to comment.