Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
52a346c
ResultSet: added fetchList() as alias for fetchFields() & shortcuts
dg Aug 28, 2024
7c4be75
github actions updated
dg Sep 2, 2024
caaaa42
cs
dg Sep 4, 2024
3a1030c
opened 4.0-dev
dg Mar 1, 2021
e67b168
readme: added jumbo
dg May 16, 2024
35cf35a
removed old Driver::SUPPORT & MySqlDriver::Error constants (BC break)
dg May 10, 2024
49c05db
deprecated methods trigger notices
dg Jan 19, 2022
0e7f616
DatabaseExtension: removed obsolete option 'reflection'
dg Aug 29, 2024
4d4ae57
Driver::getColumns() nativetype -> nativeType, autoincrement -> autoI…
dg Sep 4, 2024
c2fe331
Reflection: added 'scale' field
dg Sep 4, 2024
c26d1e1
returns date-time as immutable Nette\Database\DateTime by default (BC…
dg Aug 27, 2024
5059ce6
MySQL: convertBoolean is true by default (BC break)
dg Aug 28, 2024
923bc75
removed aliases for ISupplementalDriver, IConventions (BC break)
dg Aug 10, 2024
1727e55
removed IRow & IRowContainer (BC break)
dg Aug 10, 2024
5c5b607
removed formatLike() (BC break)
dg Sep 4, 2024
6cffe42
ConnectionPanel: refactoring, initialize($addBarPanel) is true by def…
dg Aug 27, 2024
40ab5c6
connections are always lazy (BC break)
dg Aug 29, 2024
8c86228
renamed Nette\Database\ResultSet -> Result
dg Aug 28, 2024
96c2e8a
renamed Nette\Database\Driver -> Nette\Database\Drivers\Engine (BC br…
dg Aug 28, 2024
f3e3cd9
renamed drivers to *Engine
dg Sep 3, 2024
362e62c
added new drivers
dg Aug 16, 2024
b435456
Connection: the driver is created in the constructor
dg Sep 3, 2024
e511b44
connections & engines are created by Driver
dg Sep 4, 2024
9718880
PDO replaced by Connection
dg Sep 4, 2024
b775226
added Connection::getServerVersion()
dg Aug 15, 2024
b11c0bf
added Connection::execute()
dg Sep 4, 2024
be8936f
Connection::getPdo() deprecated
dg Aug 15, 2024
0cf2662
added TypeConverter
dg Aug 17, 2024
4415f62
uses TypeConverter to convert values to PHP
dg Sep 4, 2024
bb7f7fb
added option convertDateTime
dg Sep 3, 2024
0eb80cf
added option convertDecimal & converts to int/float [Closes #257]
dg Sep 3, 2024
68f45f8
SqlsrvDriver: converts BIT to boolean (BC break)
dg Aug 17, 2024
9075da3
removed custom rowNormalizer (BC break)
dg Sep 4, 2024
4bb25d2
PDOStatement replaced by Result
dg Aug 29, 2024
1e40d23
Result::getColumnMeta is cached (ref #212)
dg Aug 15, 2024
9bc920b
try/catch checking and converting of PDOException moved to drivers
dg Sep 4, 2024
955b992
DriverException::getCode() returns driver error code instead of SQLSt…
dg Aug 27, 2024
235def4
Connection: calling query() moved here from Result
dg Sep 3, 2024
2478ffb
added Connection::getLastQuery(), Result::getQuery(), DriverException…
dg Sep 3, 2024
3cadf6e
Connection::getInsertId() returns int|string or exception (BC break)
dg Aug 14, 2024
11f5d20
drivers: getForeignKeys() works with multi-column foreign keys
dg Sep 4, 2024
f981ccc
Explorer, Selection, Structure: change of constructor dependencies (B…
dg Aug 13, 2024
9b50bd2
Connection::getDsn() deprecated (BC break)
dg Aug 17, 2024
45c712f
auxiliary commit
dg Aug 29, 2024
b9d5ce0
merging Connection & Explorer classes into one (BC break)
dg Sep 6, 2024
33708a6
removed IStructure (BC break)
dg Sep 2, 2024
24167bc
Engine::applyLimit() returns string (BC break)
dg Sep 3, 2024
1ef0bd4
Engine::delimite() -> delimit() (BC break)
dg Sep 4, 2024
b7aeb45
Selection: uses yield for iteration
dg Sep 5, 2024
2e2aa3b
Result: uses yield for iteration
dg Sep 5, 2024
4b58125
added Explorer::createFromDsn() & createFromParameters()
dg Sep 3, 2024
a1798c0
wip php 8.4 test
dg Aug 14, 2024
1769e30
Remove importants from database panel css to allow overwrite it in cu…
website21cz Oct 15, 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
added option convertDecimal & converts to int/float [Closes #257]
  • Loading branch information
dg committed Sep 6, 2024
commit 0eb80cf166d354deaeaec4ad2bcdba2ad102e314
2 changes: 1 addition & 1 deletion src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Connection
'pdo-sqlite' => Drivers\PDO\SQLite\Driver::class,
'pdo-sqlsrv' => Drivers\PDO\SQLSrv\Driver::class,
];
private const TypeConverterOptions = ['convertBoolean', 'convertDateTime', 'newDateTime'];
private const TypeConverterOptions = ['convertBoolean', 'convertDateTime', 'convertDecimal', 'newDateTime'];

/** @var array<callable(self): void> Occurs after connection is established */
public array $onConnect = [];
Expand Down
3 changes: 0 additions & 3 deletions src/Database/Drivers/Engines/MySQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ public function getForeignKeys(string $table): array
public function convertToPhp(mixed $value, array $meta, TypeConverter $converter): mixed
{
return match ($meta['nativeType']) {
'NEWDECIMAL' => $meta['scale'] === 0
? $converter->toInt($value)
: $converter->toFloat($value),
'TINY' => $meta['size'] === 1 && $converter->convertBoolean
? $converter->toBool($value)
: $converter->toInt($value),
Expand Down
9 changes: 6 additions & 3 deletions src/Database/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class TypeConverter

public bool $convertBoolean = true;
public bool $convertDateTime = true;
public bool $convertDecimal = true;
public bool $newDateTime = true;


Expand All @@ -64,8 +65,10 @@ public function convertToPhp(mixed $value, array $meta): mixed
{
return match ($this->detectType($meta['nativeType'])) {
self::Integer => $this->toInt($value),
self::Float,
self::Decimal => $this->toFloat($value),
self::Float => $this->toFloat($value),
self::Decimal => $this->convertDecimal
? ($meta['scale'] === 0 ? $this->toInt($value) : $this->toFloat($value))
: $value,
self::Boolean => $this->convertBoolean ? $this->toBool($value) : $value,
self::DateTime, self::Date => $this->convertDateTime ? $this->toDateTime($value) : $value,
self::Time => $this->convertDateTime ? $this->toTime($value) : $value,
Expand All @@ -75,7 +78,7 @@ public function convertToPhp(mixed $value, array $meta): mixed
}


public function toInt(int|string $value): int|float
public function toInt(int|float|string $value): int|float|string
{
return is_float($tmp = $value * 1) ? $value : $tmp;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Database/ResultSet.normalizeRow.sqlsrv.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Assert::equal([
'date' => new DateTime('2012-10-13 00:00:00'),
'datetime' => new DateTime('2012-10-13 10:10:10'),
'datetime2' => new DateTime('2012-10-13 10:10:10'),
'decimal' => 1.0,
'decimal' => 1,
'float' => 1.1,
'geography' => "\xe6\x10\x00\x00\x01\x14\x87\x16\xd9\xce\xf7\xd3G@\xd7\xa3p=\n\x97^\xc0\x87\x16\xd9\xce\xf7\xd3G@\xcb\xa1E\xb6\xf3\x95^\xc0",
'geometry' => "\x00\x00\x00\x00\x01\x04\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00Y@\x00\x00\x00\x00\x00\x00Y@\x00\x00\x00\x00\x00\x004@\x00\x00\x00\x00\x00\x80f@\x00\x00\x00\x00\x00\x80f@\x00\x00\x00\x00\x00\x80f@\x01\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\x02",
Expand All @@ -35,7 +35,7 @@ Assert::equal([
'money' => 1111.1,
'nchar' => 'a',
'ntext' => 'a',
'numeric_10_0' => 1.0,
'numeric_10_0' => 1,
'numeric_10_2' => 1.1,
'nvarchar' => 'a',
'real' => 1.1,
Expand All @@ -59,7 +59,7 @@ Assert::equal([
'date' => new DateTime('0001-01-01 00:00:00'),
'datetime' => new DateTime('1753-01-01 00:00:00'),
'datetime2' => new DateTime('0001-01-01 00:00:00'),
'decimal' => 0.0,
'decimal' => 0,
'float' => 0.5,
'geography' => null,
'geometry' => null,
Expand All @@ -68,7 +68,7 @@ Assert::equal([
'money' => 0.0,
'nchar' => ' ',
'ntext' => '',
'numeric_10_0' => 0.0,
'numeric_10_0' => 0,
'numeric_10_2' => 0.5,
'nvarchar' => '',
'real' => 0.0,
Expand Down
34 changes: 34 additions & 0 deletions tests/Database/connection.options.mysql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,37 @@ test('convertDateTime = true', function () {
$field = $connection->fetchField('SELECT NOW()');
Assert::type(Nette\Database\DateTime::class, $field);
});


test('default convertDecimal', function () {
$connection = connectToDB(['convertDecimal' => null])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/mysql-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::same(1, $row->decimal);
Assert::same(1.1, $row->decimal2);

$fields = $connection->fetchFields('SELECT 10, 10.5');
Assert::same([10, 10.5], $fields);
});

test('convertDecimal = false', function () {
$connection = connectToDB(['convertDecimal' => false])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/mysql-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::same('1', $row->decimal);
Assert::same('1.10', $row->decimal2);

$fields = $connection->fetchFields('SELECT 10, 10.5');
Assert::same([10, '10.5'], $fields);
});

test('convertDecimal = true', function () {
$connection = connectToDB(['convertDecimal' => true])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/mysql-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::same(1, $row->decimal);
Assert::same(1.1, $row->decimal2);

$fields = $connection->fetchFields('SELECT 10, 10.5');
Assert::same([10, 10.5], $fields);
});
40 changes: 40 additions & 0 deletions tests/Database/connection.options.sqlsrv.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Test: Nette\Database\Connection sqlsrv options.
* @dataProvider? databases.ini sqlsrv
*/

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


test('default convertDecimal', function () {
$connection = connectToDB(['convertDecimal' => null])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/sqlsrv-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::same(1, $row->decimal);
Assert::same(1, $row->numeric_10_0);
Assert::same(1.1, $row->numeric_10_2);
});

test('convertDecimal = true', function () {
$connection = connectToDB(['convertDecimal' => true])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/sqlsrv-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::same(1, $row->decimal);
Assert::same(1, $row->numeric_10_0);
Assert::same(1.1, $row->numeric_10_2);
});

test('convertDecimal = false', function () {
$connection = connectToDB(['convertDecimal' => false])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/sqlsrv-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::same('1', $row->decimal);
Assert::same('1', $row->numeric_10_0);
Assert::same('1.10', $row->numeric_10_2);
});