Skip to content

Commit

Permalink
Fix issue #4282, again
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Oct 8, 2020
1 parent c361b36 commit 26fcec6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function array_key_exists;
use function array_keys;
use function array_unshift;
use function count;
use function func_get_args;
use function func_num_args;
use function implode;
Expand Down Expand Up @@ -834,7 +835,9 @@ public function andWhere($where)
$where = $this->getQueryPart('where');

if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_AND) {
$where = $where->with(...$args);
if (count($args) > 0) {
$where = $where->with(...$args);
}
} else {
array_unshift($args, $where);
$where = CompositeExpression::and(...$args);
Expand Down Expand Up @@ -868,7 +871,9 @@ public function orWhere($where)
$where = $this->getQueryPart('where');

if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_OR) {
$where = $where->with(...$args);
if (count($args) > 0) {
$where = $where->with(...$args);
}
} else {
array_unshift($args, $where);
$where = CompositeExpression::or(...$args);
Expand Down
26 changes: 26 additions & 0 deletions tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,19 @@ public function testJoinWithNonUniqueAliasThrowsException(): void
$qb->getSQL();
}

public function testWhereExpressionAndWhereEmptyString(): void
{
$qb = new QueryBuilder($this->conn);

$qb->select('id')
->from('foo')
->where('a = b');

$qb->andWhere('');

self::assertSame('SELECT id FROM foo WHERE a = b', $qb->getSQL());
}

public function testAndWhereEmptyStringStartingWithEmptyExpression(): void
{
$qb = new QueryBuilder($this->conn);
Expand All @@ -975,6 +988,19 @@ public function testAndWhereEmptyStringStartingWithNonEmptyExpression(): void
self::assertSame('SELECT id FROM foo WHERE (a = b) AND (c = d)', $qb->getSQL());
}

public function testWhereExpressionOrWhereEmptyString(): void
{
$qb = new QueryBuilder($this->conn);

$qb->select('id')
->from('foo')
->orWhere('a = b');

$qb->orWhere('');

self::assertSame('SELECT id FROM foo WHERE a = b', $qb->getSQL());
}

public function testOrWhereEmptyStringStartingWithEmptyExpression(): void
{
$qb = new QueryBuilder($this->conn);
Expand Down

0 comments on commit 26fcec6

Please sign in to comment.