Unverified Commit f9a0563f authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #3864 from BenMorel/2.11-nonempty-compositeexpression

CompositeExpression and()/or() factory methods
parents 9b2db8d2 1d503253
...@@ -10,8 +10,9 @@ The usage of the `andX()` and `orX()` methods of the `ExpressionBuilder` class h ...@@ -10,8 +10,9 @@ The usage of the `andX()` and `orX()` methods of the `ExpressionBuilder` class h
## Deprecated `CompositeExpression` methods ## Deprecated `CompositeExpression` methods
The usage of the `add()` and `addMultiple()` methods of the `CompositeExpression` class has been deprecated. Use `with()` instead, which returns a new instance. - The usage of the `add()` and `addMultiple()` methods of the `CompositeExpression` class has been deprecated. Use `with()` instead, which returns a new instance.
In the future, the `add*()` methods will be removed and the class will be effectively immutable. In the future, the `add*()` methods will be removed and the class will be effectively immutable.
- The usage of the `CompositeExpression` constructor has been deprecated. Use the `and()` / `or()` factory methods.
## Deprecated calling `QueryBuilder` methods with an array argument ## Deprecated calling `QueryBuilder` methods with an array argument
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Query\Expression; namespace Doctrine\DBAL\Query\Expression;
use Countable; use Countable;
use function array_merge;
use function count; use function count;
use function implode; use function implode;
...@@ -36,6 +37,8 @@ class CompositeExpression implements Countable ...@@ -36,6 +37,8 @@ class CompositeExpression implements Countable
private $parts = []; private $parts = [];
/** /**
* @internal Use the and() / or() factory methods.
*
* @param string $type Instance type of composite expression. * @param string $type Instance type of composite expression.
* @param self[]|string[] $parts Composition of expressions to be joined on composite expression. * @param self[]|string[] $parts Composition of expressions to be joined on composite expression.
*/ */
...@@ -46,6 +49,16 @@ class CompositeExpression implements Countable ...@@ -46,6 +49,16 @@ class CompositeExpression implements Countable
$this->addMultiple($parts); $this->addMultiple($parts);
} }
public static function and($part, ...$parts) : self
{
return new self(self::TYPE_AND, array_merge([$part], $parts));
}
public static function or($part, ...$parts) : self
{
return new self(self::TYPE_OR, array_merge([$part], $parts));
}
/** /**
* Adds multiple parts to composite expression. * Adds multiple parts to composite expression.
* *
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace Doctrine\DBAL\Query\Expression; namespace Doctrine\DBAL\Query\Expression;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use function array_merge;
use function func_get_arg; use function func_get_arg;
use function func_get_args; use function func_get_args;
use function func_num_args; use function func_num_args;
...@@ -47,7 +46,7 @@ class ExpressionBuilder ...@@ -47,7 +46,7 @@ class ExpressionBuilder
*/ */
public function and($expression, ...$expressions) : CompositeExpression public function and($expression, ...$expressions) : CompositeExpression
{ {
return new CompositeExpression(CompositeExpression::TYPE_AND, array_merge([$expression], $expressions)); return CompositeExpression::and($expression, ...$expressions);
} }
/** /**
...@@ -58,7 +57,7 @@ class ExpressionBuilder ...@@ -58,7 +57,7 @@ class ExpressionBuilder
*/ */
public function or($expression, ...$expressions) : CompositeExpression public function or($expression, ...$expressions) : CompositeExpression
{ {
return new CompositeExpression(CompositeExpression::TYPE_OR, array_merge([$expression], $expressions)); return CompositeExpression::or($expression, ...$expressions);
} }
/** /**
......
...@@ -799,7 +799,7 @@ class QueryBuilder ...@@ -799,7 +799,7 @@ class QueryBuilder
public function where($predicates) public function where($predicates)
{ {
if (! (func_num_args() === 1 && $predicates instanceof CompositeExpression)) { if (! (func_num_args() === 1 && $predicates instanceof CompositeExpression)) {
$predicates = new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args()); $predicates = CompositeExpression::and(...func_get_args());
} }
return $this->add('where', $predicates); return $this->add('where', $predicates);
...@@ -832,7 +832,7 @@ class QueryBuilder ...@@ -832,7 +832,7 @@ class QueryBuilder
$where = $where->with(...$args); $where = $where->with(...$args);
} else { } else {
array_unshift($args, $where); array_unshift($args, $where);
$where = new CompositeExpression(CompositeExpression::TYPE_AND, $args); $where = CompositeExpression::and(...$args);
} }
return $this->add('where', $where, true); return $this->add('where', $where, true);
...@@ -865,7 +865,7 @@ class QueryBuilder ...@@ -865,7 +865,7 @@ class QueryBuilder
$where = $where->with(...$args); $where = $where->with(...$args);
} else { } else {
array_unshift($args, $where); array_unshift($args, $where);
$where = new CompositeExpression(CompositeExpression::TYPE_OR, $args); $where = CompositeExpression::or(...$args);
} }
return $this->add('where', $where, true); return $this->add('where', $where, true);
...@@ -990,7 +990,7 @@ class QueryBuilder ...@@ -990,7 +990,7 @@ class QueryBuilder
public function having($having) public function having($having)
{ {
if (! (func_num_args() === 1 && $having instanceof CompositeExpression)) { if (! (func_num_args() === 1 && $having instanceof CompositeExpression)) {
$having = new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args()); $having = CompositeExpression::and(...func_get_args());
} }
return $this->add('having', $having); return $this->add('having', $having);
...@@ -1013,7 +1013,7 @@ class QueryBuilder ...@@ -1013,7 +1013,7 @@ class QueryBuilder
$having = $having->with(...$args); $having = $having->with(...$args);
} else { } else {
array_unshift($args, $having); array_unshift($args, $having);
$having = new CompositeExpression(CompositeExpression::TYPE_AND, $args); $having = CompositeExpression::and(...$args);
} }
return $this->add('having', $having); return $this->add('having', $having);
...@@ -1036,7 +1036,7 @@ class QueryBuilder ...@@ -1036,7 +1036,7 @@ class QueryBuilder
$having = $having->with(...$args); $having = $having->with(...$args);
} else { } else {
array_unshift($args, $having); array_unshift($args, $having);
$having = new CompositeExpression(CompositeExpression::TYPE_OR, $args); $having = CompositeExpression::or(...$args);
} }
return $this->add('having', $having); return $this->add('having', $having);
......
...@@ -12,7 +12,7 @@ class CompositeExpressionTest extends DbalTestCase ...@@ -12,7 +12,7 @@ class CompositeExpressionTest extends DbalTestCase
{ {
public function testCount() : void public function testCount() : void
{ {
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']); $expr = CompositeExpression::or('u.group_id = 1');
self::assertCount(1, $expr); self::assertCount(1, $expr);
...@@ -23,7 +23,7 @@ class CompositeExpressionTest extends DbalTestCase ...@@ -23,7 +23,7 @@ class CompositeExpressionTest extends DbalTestCase
public function testAdd() : void public function testAdd() : void
{ {
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']); $expr = CompositeExpression::or('u.group_id = 1');
self::assertCount(1, $expr); self::assertCount(1, $expr);
...@@ -31,7 +31,7 @@ class CompositeExpressionTest extends DbalTestCase ...@@ -31,7 +31,7 @@ class CompositeExpressionTest extends DbalTestCase
self::assertCount(1, $expr); self::assertCount(1, $expr);
$expr->add(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1'])); $expr->add(CompositeExpression::or('u.user_id = 1'));
self::assertCount(2, $expr); self::assertCount(2, $expr);
...@@ -46,16 +46,16 @@ class CompositeExpressionTest extends DbalTestCase ...@@ -46,16 +46,16 @@ class CompositeExpressionTest extends DbalTestCase
public function testWith() : void public function testWith() : void
{ {
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, ['u.group_id = 1']); $expr = CompositeExpression::or('u.group_id = 1');
self::assertCount(1, $expr); self::assertCount(1, $expr);
// test immutability // test immutability
$expr->with(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1'])); $expr->with(CompositeExpression::or('u.user_id = 1'));
self::assertCount(1, $expr); self::assertCount(1, $expr);
$expr = $expr->with(new CompositeExpression(CompositeExpression::TYPE_OR, ['u.user_id = 1'])); $expr = $expr->with(CompositeExpression::or('u.user_id = 1'));
self::assertCount(2, $expr); self::assertCount(2, $expr);
...@@ -106,9 +106,9 @@ class CompositeExpressionTest extends DbalTestCase ...@@ -106,9 +106,9 @@ class CompositeExpressionTest extends DbalTestCase
CompositeExpression::TYPE_AND, CompositeExpression::TYPE_AND,
[ [
'u.user = 1', 'u.user = 1',
new CompositeExpression( CompositeExpression::or(
CompositeExpression::TYPE_OR, 'u.group_id = 1',
['u.group_id = 1', 'u.group_id = 2'] 'u.group_id = 2'
), ),
], ],
'(u.user = 1) AND ((u.group_id = 1) OR (u.group_id = 2))', '(u.user = 1) AND ((u.group_id = 1) OR (u.group_id = 2))',
...@@ -117,9 +117,9 @@ class CompositeExpressionTest extends DbalTestCase ...@@ -117,9 +117,9 @@ class CompositeExpressionTest extends DbalTestCase
CompositeExpression::TYPE_OR, CompositeExpression::TYPE_OR,
[ [
'u.group_id = 1', 'u.group_id = 1',
new CompositeExpression( CompositeExpression::and(
CompositeExpression::TYPE_AND, 'u.user = 1',
['u.user = 1', 'u.group_id = 2'] 'u.group_id = 2'
), ),
], ],
'(u.group_id = 1) OR ((u.user = 1) AND (u.group_id = 2))', '(u.group_id = 1) OR ((u.user = 1) AND (u.group_id = 2))',
......
...@@ -79,9 +79,9 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -79,9 +79,9 @@ class ExpressionBuilderTest extends DbalTestCase
[ [
[ [
'u.user = 1', 'u.user = 1',
new CompositeExpression( CompositeExpression::or(
CompositeExpression::TYPE_OR, 'u.group_id = 1',
['u.group_id = 1', 'u.group_id = 2'] 'u.group_id = 2'
), ),
], ],
'(u.user = 1) AND ((u.group_id = 1) OR (u.group_id = 2))', '(u.user = 1) AND ((u.group_id = 1) OR (u.group_id = 2))',
...@@ -89,9 +89,9 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -89,9 +89,9 @@ class ExpressionBuilderTest extends DbalTestCase
[ [
[ [
'u.group_id = 1', 'u.group_id = 1',
new CompositeExpression( CompositeExpression::and(
CompositeExpression::TYPE_AND, 'u.user = 1',
['u.user = 1', 'u.group_id = 2'] 'u.group_id = 2'
), ),
], ],
'(u.group_id = 1) AND ((u.user = 1) AND (u.group_id = 2))', '(u.group_id = 1) AND ((u.user = 1) AND (u.group_id = 2))',
...@@ -152,9 +152,9 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -152,9 +152,9 @@ class ExpressionBuilderTest extends DbalTestCase
[ [
[ [
'u.user = 1', 'u.user = 1',
new CompositeExpression( CompositeExpression::or(
CompositeExpression::TYPE_OR, 'u.group_id = 1',
['u.group_id = 1', 'u.group_id = 2'] 'u.group_id = 2'
), ),
], ],
'(u.user = 1) OR ((u.group_id = 1) OR (u.group_id = 2))', '(u.user = 1) OR ((u.group_id = 1) OR (u.group_id = 2))',
...@@ -162,9 +162,9 @@ class ExpressionBuilderTest extends DbalTestCase ...@@ -162,9 +162,9 @@ class ExpressionBuilderTest extends DbalTestCase
[ [
[ [
'u.group_id = 1', 'u.group_id = 1',
new CompositeExpression( CompositeExpression::and(
CompositeExpression::TYPE_AND, 'u.user = 1',
['u.user = 1', 'u.group_id = 2'] 'u.group_id = 2'
), ),
], ],
'(u.group_id = 1) OR ((u.user = 1) AND (u.group_id = 2))', '(u.group_id = 1) OR ((u.user = 1) AND (u.group_id = 2))',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment