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 19, 2024
1 parent 4b69d7e commit cd9e82d
Show file tree
Hide file tree
Showing 4 changed files with 39 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 @@ -226,6 +226,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' => fn($value): float => (float) (is_string($value) && str_starts_with($value, '.') ? '0' . $value : $value),
default => $converter->resolve($meta['nativeType']),
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
34 changes: 34 additions & 0 deletions tests/Database/Connection.sqlsrv.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

declare(strict_types=1);

use Tester\Assert;

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


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);
});
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

0 comments on commit cd9e82d

Please sign in to comment.