Skip to content

Fix NULL handling when binding boolean parameter for native pgsql driver #7024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 3.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Driver/PgSQL/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@
}

if ($type === ParameterType::BOOLEAN) {
$this->parameters[$this->parameterMap[$param]] = (bool) $value === false ? 'f' : 't';
$booleanValue = null;
if ($value !== null) {
$booleanValue = (bool) $value === false ? 'f' : 't';

Check warning on line 87 in src/Driver/PgSQL/Statement.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/PgSQL/Statement.php#L85-L87

Added lines #L85 - L87 were not covered by tests
}

$this->parameters[$this->parameterMap[$param]] = $booleanValue;

Check warning on line 90 in src/Driver/PgSQL/Statement.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/PgSQL/Statement.php#L90

Added line #L90 was not covered by tests
$this->parameterTypes[$this->parameterMap[$param]] = ParameterType::STRING;
} else {
$this->parameters[$this->parameterMap[$param]] = $value;
Expand Down
23 changes: 19 additions & 4 deletions tests/Functional/BooleanBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;

use function assert;
use function is_bool;

class BooleanBindingTest extends FunctionalTestCase
{
protected function setUp(): void
Expand All @@ -18,7 +21,7 @@ protected function setUp(): void
}

$table = new Table('boolean_test_table');
$table->addColumn('val', 'boolean');
$table->addColumn('val', 'boolean', ['notnull' => false]);
$this->dropAndCreateTable($table);
}

Expand All @@ -28,7 +31,7 @@ protected function tearDown(): void
}

/** @dataProvider booleanProvider */
public function testBooleanInsert(bool $input): void
public function testBooleanInsert(?bool $input): void
{
$queryBuilder = $this->connection->createQueryBuilder();

Expand All @@ -37,11 +40,23 @@ public function testBooleanInsert(bool $input): void
])->executeStatement();

self::assertSame(1, $result);

$valueFromDatabase = $this->connection->createQueryBuilder()
->select('val')->from('boolean_test_table')
->executeQuery()->fetchOne();

assert($valueFromDatabase === null || is_bool($valueFromDatabase));

self::assertSame(
$input,
$this->connection->convertToPHPValue($valueFromDatabase, 'boolean'),
'Must return from database the same value inserted.',
);
}

/** @return bool[][] */
/** @return array<int, list<bool|null>> */
public static function booleanProvider(): array
{
return [[true], [false]];
return [[true], [false], [null]];
}
}
Loading