Skip to content

Commit

Permalink
added option convertDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 28, 2024
1 parent 1dc1c62 commit fcbe307
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Database/Drivers/Engines/MySQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ public function getForeignKeys(string $table): array
public function resolveColumnConverter(array $meta, TypeConverter $converter): ?\Closure
{
return match ($meta['nativeType']) {
'NEWDECIMAL' => $meta['scale'] === 0
? $converter->toInt(...)
: $converter->toFloat(...),
'NEWDECIMAL' => $converter->convertDecimal
? ($meta['scale'] === 0 ? $converter->toInt(...) : $converter->toFloat(...))
: null,
'TINY' => $meta['length'] === 1 && $converter->convertBoolean
? $converter->toBool(...)
: $converter->toInt(...),
Expand Down
1 change: 1 addition & 0 deletions src/Database/Drivers/Engines/SQLServerEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public function getForeignKeys(string $table): array

public function resolveColumnConverter(array $meta, TypeConverter $converter): ?\Closure
{
dump($meta);
return match ($meta['nativeType']) {
'timestamp' => null, // timestamp does not mean time in sqlsrv
'bit' => $converter->convertBoolean ? $converter->toBool(...) : $converter->toInt(...),
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function createDriverFromDsn(
public function createTypeConverter(array &$options): TypeConverter
{
$converter = new TypeConverter;
foreach (['convertBoolean', 'convertDateTime', 'newDateTime'] as $opt) {
foreach (['convertBoolean', 'convertDateTime', 'convertDecimal', 'newDateTime'] as $opt) {
if (isset($options[$opt])) {
$converter->$opt = (bool) $options[$opt];
unset($options[$opt]);
Expand Down
5 changes: 3 additions & 2 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,8 @@ public function resolve(string $nativeType): ?\Closure
{
return match ($this->detectType($nativeType)) {
self::Integer => $this->toInt(...),
self::Float,
self::Decimal => $this->toFloat(...),
self::Float => $this->toFloat(...),
self::Decimal => $this->convertDecimal ? $this->toFloat(...) : null,
self::Boolean => $this->convertBoolean ? $this->toBool(...) : null,
self::DateTime, self::Date => $this->convertDateTime ? $this->toDateTime(...) : null,
self::Time => $this->convertDateTime ? $this->toTime(...) : null,
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);
});
52 changes: 49 additions & 3 deletions tests/Database/connection.options.sqlsrv.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,65 @@ test('default convertBoolean', function () {
$connection = connectToDB(['convertBoolean' => null])->getConnection();
Nette\Database\Helpers::loadFromFile($connection, __DIR__ . '/files/sqlsrv-nette_test3.sql');
$row = $connection->fetch('SELECT * FROM types');
Assert::equal(true, $row->bit);
Assert::same(true, $row->bit);
});

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

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


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.0, $row->decimal);
Assert::same(1.0, $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.0, $row->decimal);
Assert::same(1.0, $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.0, $row->decimal);
Assert::same(1.0, $row->numeric_10_0);
Assert::same(1.1, $row->numeric_10_2);
});

/*
array (4)
'name' => 'decimal'
'nativeType' => 'decimal'
'length' => 18
'precision' => 0
array (4)
'name' => 'numeric_10_0'
'nativeType' => 'numeric'
'length' => 10
'precision' => 0
array (4)
'name' => 'numeric_10_2'
'nativeType' => 'numeric'
'length' => 10
'precision' => 2
*/

0 comments on commit fcbe307

Please sign in to comment.