Skip to content
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

Fix regression in QueryBuilder::and|orWhere() #4330

Merged
merged 1 commit into from
Oct 9, 2020
Merged
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
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