Skip to content

Commit a4919dd

Browse files
authored
Fix binding null as a boolean parameter on pgsql (#7059)
1 parent 2022f9e commit a4919dd

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/Driver/PgSQL/Statement.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
8181
throw UnknownParameter::new((string) $param);
8282
}
8383

84+
if ($value === null) {
85+
$type = ParameterType::NULL;
86+
}
87+
8488
if ($type === ParameterType::BOOLEAN) {
8589
$this->parameters[$this->parameterMap[$param]] = (bool) $value === false ? 'f' : 't';
8690
$this->parameterTypes[$this->parameterMap[$param]] = ParameterType::STRING;

tests/Functional/BooleanBindingTest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Schema\Table;
99
use Doctrine\DBAL\Tests\FunctionalTestCase;
1010
use Doctrine\DBAL\Tests\TestUtil;
11+
use Doctrine\DBAL\Types\Types;
1112

1213
class BooleanBindingTest extends FunctionalTestCase
1314
{
@@ -18,7 +19,7 @@ protected function setUp(): void
1819
}
1920

2021
$table = new Table('boolean_test_table');
21-
$table->addColumn('val', 'boolean');
22+
$table->addColumn('val', 'boolean')->setNotnull(false);
2223
$this->dropAndCreateTable($table);
2324
}
2425

@@ -28,7 +29,7 @@ protected function tearDown(): void
2829
}
2930

3031
/** @dataProvider booleanProvider */
31-
public function testBooleanInsert(bool $input): void
32+
public function testBooleanParameterInsert(?bool $input): void
3233
{
3334
$queryBuilder = $this->connection->createQueryBuilder();
3435

@@ -37,11 +38,37 @@ public function testBooleanInsert(bool $input): void
3738
])->executeStatement();
3839

3940
self::assertSame(1, $result);
41+
42+
self::assertSame($input, $this->connection->convertToPHPValue(
43+
$this->connection->fetchOne('SELECT val FROM boolean_test_table'),
44+
Types::BOOLEAN,
45+
));
46+
}
47+
48+
/** @dataProvider booleanProvider */
49+
public function testBooleanTypeInsert(?bool $input): void
50+
{
51+
$queryBuilder = $this->connection->createQueryBuilder();
52+
53+
$result = $queryBuilder->insert('boolean_test_table')->values([
54+
'val' => $queryBuilder->createNamedParameter($input, Types::BOOLEAN),
55+
])->executeStatement();
56+
57+
self::assertSame(1, $result);
58+
59+
self::assertSame($input, $this->connection->convertToPHPValue(
60+
$this->connection->fetchOne('SELECT val FROM boolean_test_table'),
61+
Types::BOOLEAN,
62+
));
4063
}
4164

42-
/** @return bool[][] */
65+
/** @return array<string, array{bool|null}> */
4366
public static function booleanProvider(): array
4467
{
45-
return [[true], [false]];
68+
return [
69+
'true' => [true],
70+
'false' => [false],
71+
'null' => [null],
72+
];
4673
}
4774
}

0 commit comments

Comments
 (0)