Skip to content

Commit

Permalink
SqlsrvDriver: converts BIT to boolean (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 28, 2024
1 parent 44a98a1 commit dec69cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Database/Drivers/Engines/SQLServerEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Drivers/PDO/SQLSrv/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

/**
* PDO SQL Server database driver.
* Options:
* - convertBoolean => converts BIT to boolean
*/
class Driver implements Drivers\Driver
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/ResultSet.normalizeRow.sqlsrv.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down
22 changes: 22 additions & 0 deletions tests/Database/connection.options.sqlsrv.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit dec69cc

Please sign in to comment.