Skip to content

Commit

Permalink
allow nested nest conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Mar 12, 2018
1 parent c179475 commit b875f6b
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions src/core/Directus/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
use Zend\Db\Sql\Predicate\NotIn;
use Zend\Db\Sql\Predicate\NotLike;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Predicate\Predicate;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;

class Builder
{
Expand Down Expand Up @@ -604,31 +607,7 @@ public function buildSelect()
}

foreach ($this->getWheres() as $condition) {
$logical = strtoupper(ArrayUtils::get($condition, 'logical', 'and'));

if (ArrayUtils::get($condition, 'type') === 'nest') {
$query = ArrayUtils::get($condition, 'query');
if ($logical === 'OR') {
$select->where->or;
}

$where = $select->where->nest();
// @NOTE: only allow one level nested
foreach($query->getWheres() as $nestCondition) {
if (ArrayUtils::get($nestCondition, 'logical') == 'or') {
$whereLogical = $where::OP_OR;
} else {
$whereLogical = $where::OP_AND;
}

$where->addPredicate($this->buildConditionExpression($nestCondition), $whereLogical);
$condition = null;
}

$where->unnest();
} else {
$select->where($this->buildConditionExpression($condition), $logical);
}
$this->buildCondition($select->where, $condition);
}

if ($this->groupBys !== null) {
Expand Down Expand Up @@ -693,6 +672,36 @@ public function getSql()
return $sql->getSqlStringForSqlObject($select, $this->connection->getPlatform());
}

/**
* Build the condition expressions
*
* @param Predicate $where
* @param array $condition
*/
protected function buildCondition(Predicate $where, array $condition)
{
$logical = strtoupper(ArrayUtils::get($condition, 'logical', 'and'));

if (ArrayUtils::get($condition, 'type') === 'nest') {
/** @var Builder $query */
$query = ArrayUtils::get($condition, 'query');
if ($logical === 'OR') {
$where->or;
}

$where = $where->nest();

foreach ($query->getWheres() as $condition) {
$query->from($this->getFrom());
$query->buildCondition($where, $condition);
}

$where->unnest();
} else {
$where->addPredicate($this->buildConditionExpression($condition), $logical);
}
}

protected function buildOrder()
{
$order = [];
Expand Down

0 comments on commit b875f6b

Please sign in to comment.