diff --git a/src/Database/Drivers/Engines/MySQLEngine.php b/src/Database/Drivers/Engines/MySQLEngine.php index c8b957d72..841dc9a18 100644 --- a/src/Database/Drivers/Engines/MySQLEngine.php +++ b/src/Database/Drivers/Engines/MySQLEngine.php @@ -176,9 +176,6 @@ 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(...), 'TINY' => $meta['length'] === 1 && $converter->convertBoolean ? $converter->toBool(...) : $converter->toInt(...), diff --git a/src/Database/Drivers/Engines/SQLServerEngine.php b/src/Database/Drivers/Engines/SQLServerEngine.php index 06614eae7..33101e633 100644 --- a/src/Database/Drivers/Engines/SQLServerEngine.php +++ b/src/Database/Drivers/Engines/SQLServerEngine.php @@ -223,7 +223,9 @@ public function resolveColumnConverter(array $meta, TypeConverter $converter): ? return match ($meta['nativeType']) { 'timestamp' => null, // timestamp does not mean time in sqlsrv 'decimal', 'numeric', - 'double', 'double precision', 'float', 'real', 'money', 'smallmoney' => fn($value): float => (float) (is_string($value) && str_starts_with($value, '.') ? '0' . $value : $value), + 'double', 'double precision', 'float', 'real', 'money', 'smallmoney' => $converter->convertDecimal + ? fn($value): float => (float) (is_string($value) && str_starts_with($value, '.') ? '0' . $value : $value) + : null, default => $converter->resolve($meta), }; } diff --git a/src/Database/Factory.php b/src/Database/Factory.php index 84057d55c..c1e389eb5 100644 --- a/src/Database/Factory.php +++ b/src/Database/Factory.php @@ -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]); diff --git a/src/Database/TypeConverter.php b/src/Database/TypeConverter.php index 2fa4e4bac..62f49467c 100644 --- a/src/Database/TypeConverter.php +++ b/src/Database/TypeConverter.php @@ -38,6 +38,7 @@ final class TypeConverter public bool $convertBoolean = true; public bool $convertDateTime = true; + public bool $convertDecimal = true; public bool $newDateTime = true; @@ -64,8 +65,10 @@ public function resolve(array $meta): ?\Closure { return match ($this->detectType($meta['nativeType'])) { self::Integer => $this->toInt(...), - self::Float, - self::Decimal => $this->toFloat(...), + self::Float => $this->toFloat(...), + self::Decimal => $this->convertDecimal + ? ($meta['scale'] === 0 ? $this->toInt(...) : $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, @@ -75,7 +78,7 @@ public function resolve(array $meta): ?\Closure } - public function toInt(int|string $value): int|float + public function toInt(int|float|string $value): int|float { return is_float($tmp = $value * 1) ? $value : $tmp; } diff --git a/tests/Database/connection.options.mysql.phpt b/tests/Database/connection.options.mysql.phpt index 390cf0462..9040f52fb 100644 --- a/tests/Database/connection.options.mysql.phpt +++ b/tests/Database/connection.options.mysql.phpt @@ -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); +}); diff --git a/tests/Database/connection.options.sqlsrv.phpt b/tests/Database/connection.options.sqlsrv.phpt new file mode 100644 index 000000000..5e6ff4021 --- /dev/null +++ b/tests/Database/connection.options.sqlsrv.phpt @@ -0,0 +1,40 @@ + 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', $row->decimal); + Assert::same('1', $row->numeric_10_0); + Assert::same('1.10', $row->numeric_10_2); +});