diff --git a/src/Database/Drivers/Engines/SQLServerEngine.php b/src/Database/Drivers/Engines/SQLServerEngine.php index 33101e633..3125def32 100644 --- a/src/Database/Drivers/Engines/SQLServerEngine.php +++ b/src/Database/Drivers/Engines/SQLServerEngine.php @@ -222,6 +222,7 @@ public function resolveColumnConverter(array $meta, TypeConverter $converter): ? { return match ($meta['nativeType']) { 'timestamp' => null, // timestamp does not mean time in sqlsrv + 'bit' => $converter->convertBoolean ? $converter->toBool(...) : $converter->toInt(...), 'decimal', 'numeric', 'double', 'double precision', 'float', 'real', 'money', 'smallmoney' => $converter->convertDecimal ? fn($value): float => (float) (is_string($value) && str_starts_with($value, '.') ? '0' . $value : $value) diff --git a/src/Database/Drivers/PDO/SQLSrv/Driver.php b/src/Database/Drivers/PDO/SQLSrv/Driver.php index 8025fae1e..1709d8bad 100644 --- a/src/Database/Drivers/PDO/SQLSrv/Driver.php +++ b/src/Database/Drivers/PDO/SQLSrv/Driver.php @@ -14,6 +14,8 @@ /** * PDO SQL Server database driver. + * Options: + * - convertBoolean => converts BIT to boolean */ class Driver implements Drivers\Driver { diff --git a/tests/Database/ResultSet.normalizeRow.sqlsrv.phpt b/tests/Database/ResultSet.normalizeRow.sqlsrv.phpt index 09ea94fe7..293abb319 100644 --- a/tests/Database/ResultSet.normalizeRow.sqlsrv.phpt +++ b/tests/Database/ResultSet.normalizeRow.sqlsrv.phpt @@ -21,7 +21,7 @@ $res = $connection->query('SELECT * FROM types'); Assert::equal([ 'bigint' => 1, 'binary_3' => "\x00\x00\xFF", - 'bit' => '1', + 'bit' => true, 'char_5' => 'a ', 'date' => new DateTime('2012-10-13 00:00:00'), 'datetime' => new DateTime('2012-10-13 10:10:10'), @@ -54,7 +54,7 @@ Assert::equal([ Assert::equal([ 'bigint' => 0, 'binary_3' => "\x00\x00\x00", - 'bit' => '0', + 'bit' => false, 'char_5' => ' ', 'date' => new DateTime('0001-01-01 00:00:00'), 'datetime' => new DateTime('1753-01-01 00:00:00'), diff --git a/tests/Database/connection.options.sqlsrv.phpt b/tests/Database/connection.options.sqlsrv.phpt index 5e6ff4021..a22fb0d2f 100644 --- a/tests/Database/connection.options.sqlsrv.phpt +++ b/tests/Database/connection.options.sqlsrv.phpt @@ -38,3 +38,25 @@ test('convertDecimal = false', function () { Assert::same('1', $row->numeric_10_0); Assert::same('1.10', $row->numeric_10_2); }); + + +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); +}); + +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); +}); + +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); +});