Skip to content

Commit

Permalink
Fix BC break
Browse files Browse the repository at this point in the history
Ignore empty strings in QueryBuilder::and/orWhere() & and/orHaving().
  • Loading branch information
BenMorel committed Sep 23, 2020
1 parent 5d22823 commit 0325f42
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;

use function array_filter;
use function array_key_exists;
use function array_keys;
use function array_unshift;
Expand Down Expand Up @@ -829,6 +830,7 @@ public function where($predicates)
public function andWhere($where)
{
$args = func_get_args();
$args = array_filter($args); // https://github.com/doctrine/dbal/issues/4282
$where = $this->getQueryPart('where');

if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_AND) {
Expand Down Expand Up @@ -862,6 +864,7 @@ public function andWhere($where)
public function orWhere($where)
{
$args = func_get_args();
$args = array_filter($args); // https://github.com/doctrine/dbal/issues/4282
$where = $this->getQueryPart('where');

if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_OR) {
Expand Down Expand Up @@ -1010,6 +1013,7 @@ public function having($having)
public function andHaving($having)
{
$args = func_get_args();
$args = array_filter($args); // https://github.com/doctrine/dbal/issues/4282
$having = $this->getQueryPart('having');

if ($having instanceof CompositeExpression && $having->getType() === CompositeExpression::TYPE_AND) {
Expand All @@ -1033,6 +1037,7 @@ public function andHaving($having)
public function orHaving($having)
{
$args = func_get_args();
$args = array_filter($args); // https://github.com/doctrine/dbal/issues/4282
$having = $this->getQueryPart('having');

if ($having instanceof CompositeExpression && $having->getType() === CompositeExpression::TYPE_OR) {
Expand Down
100 changes: 100 additions & 0 deletions tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
Expand Up @@ -949,4 +949,104 @@ public function testJoinWithNonUniqueAliasThrowsException(): void

$qb->getSQL();
}

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

$qb->select('id')
->from('foo');

$qb->andWhere('', 'a = b');

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

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

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

$qb->andWhere('', 'c = d');

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

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

$qb->select('id')
->from('foo');

$qb->orWhere('', 'a = b');

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

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

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

$qb->orWhere('', 'c = d');

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

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

$qb->select('id')
->from('foo');

$qb->andHaving('', 'a = b');

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

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

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

$qb->andHaving('', 'c = d');

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

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

$qb->select('id')
->from('foo');

$qb->orHaving('', 'a = b');

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

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

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

$qb->orHaving('', 'c = d');

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

0 comments on commit 0325f42

Please sign in to comment.