Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
367cc4e
Selection: support for generics
dg May 10, 2024
0608c21
PascalCase constants
dg May 10, 2024
90bb859
Reflection::getTable() can access unlisted table
dg May 13, 2024
a96116c
renamed IStructure::FIELD_* to Type::*
dg May 10, 2024
ebbb7cd
Driver::getColumns() added NDB type
dg May 13, 2024
da1b2c8
Column::$nativeType changed to NDB $type
dg May 10, 2024
d43272b
Helpers::detectType() supports 'INT UNSIGNED'
dg May 10, 2024
9e87eda
Reflection: removed $schema
dg May 13, 2024
e11f612
opened 4.0-dev
dg Mar 1, 2021
c4e5746
removed old Driver::SUPPORT constants (BC break)
dg May 10, 2024
57e5fa0
deprecated methods trigger notices
dg Jan 19, 2022
ccd13ce
returns always date-time as immutable Nette\Database\DateTime (BC break)
dg May 10, 2024
b23a3b3
Helpers::normalizeRow() & detection moved to new class RowNormalizer
dg May 13, 2024
3e7168b
RowNormalizer: refactoring
dg Dec 14, 2023
229fec5
RowNormalizer: added configuring methods [Closes #257]
dg Jan 19, 2022
98d167f
introduced PdoDriver, descendant of all PDO-based drivers
dg Sep 20, 2021
1297897
database connection moved to PdoDriver::connect()
dg Dec 3, 2023
38b88d5
drivers uses PDO instead of Connection
dg Nov 30, 2023
ccece35
some others methods moved to PdoDriver
dg Dec 13, 2023
c7c81ee
added ResultDriver
dg May 10, 2024
2cf6efa
exceptions are converted in PdoDriver
dg Sep 20, 2021
071d442
added Nette\Database\QueryException
dg Sep 20, 2021
860a033
drivers: getForeignKeys() works with multi-column foreign keys
dg Jan 19, 2022
c328e2c
readme: added jumbo
dg May 16, 2024
9bf1d51
added CredentialProvider
Jun 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
renamed IStructure::FIELD_* to Type::*
  • Loading branch information
dg committed May 13, 2024
commit a96116c8a1d7b246f13d8cc9aaa9265d8a0a3384
4 changes: 2 additions & 2 deletions src/Database/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ function getIndexes(string $table): array;
function getForeignKeys(string $table): array;

/**
* Returns associative array of detected types (IStructure::FIELD_*) in result set.
* @return array<string, string>
* Returns associative array of detected types in result set.
* @return array<string, Type::*>
*/
function getColumnTypes(\PDOStatement $statement): array;

Expand Down
7 changes: 4 additions & 3 deletions src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Database\Drivers;

use Nette;
use Nette\Database\Type;


/**
Expand Down Expand Up @@ -205,9 +206,9 @@ public function getColumnTypes(\PDOStatement $statement): array
$meta = $statement->getColumnMeta($col);
if (isset($meta['native_type'])) {
$types[$meta['name']] = match (true) {
$meta['native_type'] === 'NEWDECIMAL' && $meta['precision'] === 0 => Nette\Database\IStructure::FIELD_INTEGER,
$meta['native_type'] === 'TINY' && $meta['len'] === 1 && $this->supportBooleans => Nette\Database\IStructure::FIELD_BOOL,
$meta['native_type'] === 'TIME' => Nette\Database\IStructure::FIELD_TIME_INTERVAL,
$meta['native_type'] === 'NEWDECIMAL' && $meta['precision'] === 0 => Type::Integer,
$meta['native_type'] === 'TINY' && $meta['len'] === 1 && $this->supportBooleans => Type::Bool,
$meta['native_type'] === 'TIME' => Type::TimeInterval,
default => Nette\Database\Helpers::detectType($meta['native_type']),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function getColumnTypes(\PDOStatement $statement): array
$meta = $statement->getColumnMeta($col);
if (isset($meta['sqlite:decl_type'])) {
$types[$meta['name']] = $this->fmtDateTime === 'U' && in_array($meta['sqlite:decl_type'], ['DATE', 'DATETIME'], strict: true)
? Nette\Database\IStructure::FIELD_UNIX_TIMESTAMP
? Nette\Database\Type::UnixTimestamp
: Nette\Database\Helpers::detectType($meta['sqlite:decl_type']);
} elseif (isset($meta['native_type'])) {
$types[$meta['name']] = Nette\Database\Helpers::detectType($meta['native_type']);
Expand Down
36 changes: 19 additions & 17 deletions src/Database/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class Helpers
public static int $maxLength = 100;

public static array $typePatterns = [
'^_' => IStructure::FIELD_TEXT, // PostgreSQL arrays
'(TINY|SMALL|SHORT|MEDIUM|BIG|LONG)(INT)?|INT(EGER|\d+| IDENTITY)?|(SMALL|BIG|)SERIAL\d*|COUNTER|YEAR|BYTE|LONGLONG|UNSIGNED BIG INT' => IStructure::FIELD_INTEGER,
'(NEW)?DEC(IMAL)?(\(.*)?|NUMERIC|(SMALL)?MONEY|CURRENCY|NUMBER' => IStructure::FIELD_DECIMAL,
'REAL|DOUBLE( PRECISION)?|FLOAT\d*' => IStructure::FIELD_FLOAT,
'BOOL(EAN)?' => IStructure::FIELD_BOOL,
'TIME' => IStructure::FIELD_TIME,
'DATE' => IStructure::FIELD_DATE,
'(SMALL)?DATETIME(OFFSET)?\d*|TIME(STAMP.*)?' => IStructure::FIELD_DATETIME,
'BYTEA|(TINY|MEDIUM|LONG|)BLOB|(LONG )?(VAR)?BINARY|IMAGE' => IStructure::FIELD_BINARY,
'^_' => Type::Text, // PostgreSQL arrays
'(TINY|SMALL|SHORT|MEDIUM|BIG|LONG)(INT)?|INT(EGER|\d+| IDENTITY)?|(SMALL|BIG|)SERIAL\d*|COUNTER|YEAR|BYTE|LONGLONG|UNSIGNED BIG INT' => Type::Integer,
'(NEW)?DEC(IMAL)?(\(.*)?|NUMERIC|(SMALL)?MONEY|CURRENCY|NUMBER' => Type::Decimal,
'REAL|DOUBLE( PRECISION)?|FLOAT\d*' => Type::Float,
'BOOL(EAN)?' => Type::Bool,
'TIME' => Type::Time,
'DATE' => Type::Date,
'(SMALL)?DATETIME(OFFSET)?\d*|TIME(STAMP.*)?' => Type::DateTime,
'BYTEA|(TINY|MEDIUM|LONG|)BLOB|(LONG )?(VAR)?BINARY|IMAGE' => Type::Binary,
];


Expand Down Expand Up @@ -165,6 +165,7 @@ public static function dumpSql(string $sql, ?array $params = null, ?Connection $

/**
* Common column type detection.
* @return array<Type::*>
*/
public static function detectTypes(\PDOStatement $statement): array
{
Expand All @@ -183,6 +184,7 @@ public static function detectTypes(\PDOStatement $statement): array

/**
* Heuristic column type detection.
* @return Type::*
* @internal
*/
public static function detectType(string $type): string
Expand Down Expand Up @@ -210,36 +212,36 @@ public static function normalizeRow(
{
foreach ($resultSet->getColumnTypes() as $key => $type) {
$value = $row[$key];
if ($value === null || $value === false || $type === IStructure::FIELD_TEXT) {
if ($value === null || $value === false || $type === Type::Text) {
// do nothing
} elseif ($type === IStructure::FIELD_INTEGER) {
} elseif ($type === Type::Integer) {
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;

} elseif ($type === IStructure::FIELD_FLOAT || $type === IStructure::FIELD_DECIMAL) {
} elseif ($type === Type::Float || $type === Type::Decimal) {
if (is_string($value) && ($pos = strpos($value, '.')) !== false) {
$value = rtrim(rtrim($pos === 0 ? "0$value" : $value, '0'), '.');
}

$row[$key] = (float) $value;

} elseif ($type === IStructure::FIELD_BOOL) {
} elseif ($type === Type::Bool) {
$row[$key] = $value && $value !== 'f' && $value !== 'F';

} elseif ($type === IStructure::FIELD_DATETIME || $type === IStructure::FIELD_DATE) {
} elseif ($type === Type::DateTime || $type === Type::Date) {
$row[$key] = str_starts_with($value, '0000-00')
? null
: new $dateTimeClass($value);

} elseif ($type === IStructure::FIELD_TIME) {
} elseif ($type === Type::Time) {
$row[$key] = (new $dateTimeClass($value))->setDate(1, 1, 1);

} elseif ($type === IStructure::FIELD_TIME_INTERVAL) {
} elseif ($type === Type::TimeInterval) {
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)(\.\d+)?$#D', $value, $m);
$row[$key] = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
$row[$key]->f = isset($m[5]) ? (float) $m[5] : 0.0;
$row[$key]->invert = (int) (bool) $m[1];

} elseif ($type === IStructure::FIELD_UNIX_TIMESTAMP) {
} elseif ($type === Type::UnixTimestamp) {
$row[$key] = (new $dateTimeClass)->setTimestamp($value);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Database/IStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
interface IStructure
{
/** @deprecated use Type::* */
public const
FIELD_TEXT = 'string',
FIELD_BINARY = 'bin',
Expand Down
31 changes: 31 additions & 0 deletions src/Database/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database;



/**
* Column types
*/
final class Type
{
public const
Binary = 'bin',
Bool = 'bool',
Date = 'date',
DateTime = 'datetime',
Decimal = 'decimal',
Float = 'float',
Integer = 'int',
Text = 'string',
Time = 'time',
TimeInterval = 'timeint',
UnixTimestamp = 'timestamp';
}