Skip to content

Commit

Permalink
Factorize WHERE and HAVING predicate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Jan 23, 2020
1 parent d3ba7e6 commit b3a4372
Showing 1 changed file with 38 additions and 53 deletions.
91 changes: 38 additions & 53 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,11 +787,7 @@ public function set(string $key, string $value) : self
*/
public function where($predicate, ...$predicates) : self
{
if ($predicate instanceof CompositeExpression && ! $predicates) {
$this->where = $predicate;
} else {
$this->where = new CompositeExpression(CompositeExpression::TYPE_AND, array_merge([$predicate], $predicates));
}
$this->where = $this->createPredicate($predicate, ...$predicates);

$this->state = self::STATE_DIRTY;

Expand Down Expand Up @@ -819,17 +815,7 @@ public function where($predicate, ...$predicates) : self
*/
public function andWhere($predicate, ...$predicates) : self
{
$allPredicates = array_merge([$predicate], $predicates);

if ($this->where !== null && $this->where->getType() === CompositeExpression::TYPE_AND) {
$this->where->addMultiple($allPredicates);
} else {
if ($this->where !== null) {
array_unshift($allPredicates, $this->where);
}

$this->where = new CompositeExpression(CompositeExpression::TYPE_AND, $allPredicates);
}
$this->where = $this->appendToPredicate($this->where, CompositeExpression::TYPE_AND, $predicate, ...$predicates);

$this->state = self::STATE_DIRTY;

Expand Down Expand Up @@ -857,17 +843,7 @@ public function andWhere($predicate, ...$predicates) : self
*/
public function orWhere($predicate, ...$predicates) : self
{
$allPredicates = array_merge([$predicate], $predicates);

if ($this->where !== null && $this->where->getType() === CompositeExpression::TYPE_OR) {
$this->where->addMultiple($allPredicates);
} else {
if ($this->where !== null) {
array_unshift($allPredicates, $this->where);
}

$this->where = new CompositeExpression(CompositeExpression::TYPE_OR, $allPredicates);
}
$this->where = $this->appendToPredicate($this->where, CompositeExpression::TYPE_OR, $predicate, ...$predicates);

$this->state = self::STATE_DIRTY;

Expand Down Expand Up @@ -989,11 +965,7 @@ public function values(array $values) : self
*/
public function having($predicate, ...$predicates) : self
{
if ($predicate instanceof CompositeExpression && ! $predicates) {
$this->having = $predicate;
} else {
$this->having = new CompositeExpression(CompositeExpression::TYPE_AND, array_merge([$predicate], $predicates));
}
$this->having = $this->createPredicate($predicate, ...$predicates);

$this->state = self::STATE_DIRTY;

Expand All @@ -1011,17 +983,7 @@ public function having($predicate, ...$predicates) : self
*/
public function andHaving($predicate, ...$predicates) : self
{
$allPredicates = array_merge([$predicate], $predicates);

if ($this->having !== null && $this->having->getType() === CompositeExpression::TYPE_AND) {
$this->having->addMultiple($allPredicates);
} else {
if ($this->having !== null) {
array_unshift($allPredicates, $this->having);
}

$this->having = new CompositeExpression(CompositeExpression::TYPE_AND, $allPredicates);
}
$this->having = $this->appendToPredicate($this->having, CompositeExpression::TYPE_AND, $predicate, ...$predicates);

$this->state = self::STATE_DIRTY;

Expand All @@ -1039,21 +1001,44 @@ public function andHaving($predicate, ...$predicates) : self
*/
public function orHaving($predicate, ...$predicates) : self
{
$allPredicates = array_merge([$predicate], $predicates);
$this->having = $this->appendToPredicate($this->having, CompositeExpression::TYPE_OR, $predicate, ...$predicates);

if ($this->having !== null && $this->having->getType() === CompositeExpression::TYPE_OR) {
$this->having->addMultiple($allPredicates);
} else {
if ($this->having !== null) {
array_unshift($allPredicates, $this->having);
}
$this->state = self::STATE_DIRTY;

$this->having = new CompositeExpression(CompositeExpression::TYPE_OR, $allPredicates);
return $this;
}

/**
* Creates a CompositeExpression from one or more predicates combined by the AND logic.
*
* @param string|CompositeExpression $predicate
* @param string|CompositeExpression ...$predicates
*/
private function createPredicate($predicate, ...$predicates) : CompositeExpression
{
if ($predicate instanceof CompositeExpression && ! $predicates) {
return $predicate;
}

$this->state = self::STATE_DIRTY;
return new CompositeExpression(CompositeExpression::TYPE_AND, array_merge([$predicate], $predicates));
}

return $this;
/**
* Appends the given predicates combined by the given type of logic to the given query clause.
*
* @param string|CompositeExpression ...$predicates
*/
private function appendToPredicate(?CompositeExpression $predicate, string $type, ...$predicates) : CompositeExpression
{
if ($predicate !== null) {
if ($predicate->getType() === $type) {
return $predicate->addMultiple($predicates);
}

array_unshift($predicates, $predicate);
}

return new CompositeExpression($type, $predicates);
}

/**
Expand Down

0 comments on commit b3a4372

Please sign in to comment.