Skip to content

Commit

Permalink
FIX Use OR conjuctive in filterAny aggregate queries
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed May 15, 2023
1 parent 7210ac8 commit 5d11357
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
13 changes: 11 additions & 2 deletions src/ORM/DataList.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,23 @@ public function filterAny()
throw new InvalidArgumentException('Incorrect number of arguments passed to filterAny()');
}

return $this->alterDataQuery(function (DataQuery $query) use ($whereArguments) {
$subquery = $query->disjunctiveGroup();
$list = $this->alterDataQuery(function (DataQuery $query) use ($whereArguments) {
$clause = 'WHERE';
foreach (array_keys($whereArguments) as $field) {
if (preg_match('#\.(COUNT|SUM|AVG|MIN|MAX)\(#', strtoupper($field))) {
$clause = 'HAVING';
break;
}
}
$subquery = $query->disjunctiveGroup($clause);

foreach ($whereArguments as $field => $value) {
$filter = $this->createSearchFilter($field, $value);
$filter->apply($subquery);
}
});

return $list;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/ORM/DataQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,9 @@ public function having($having)
*
* @return DataQuery_SubGroup
*/
public function disjunctiveGroup()
public function disjunctiveGroup(string $clause = 'WHERE')
{
return new DataQuery_SubGroup($this, 'OR');
return new DataQuery_SubGroup($this, 'OR', $clause);
}

/**
Expand All @@ -672,9 +672,9 @@ public function disjunctiveGroup()
*
* @return DataQuery_SubGroup
*/
public function conjunctiveGroup()
public function conjunctiveGroup(string $clause = 'WHERE')
{
return new DataQuery_SubGroup($this, 'AND');
return new DataQuery_SubGroup($this, 'AND', $clause);
}

/**
Expand Down
52 changes: 39 additions & 13 deletions src/ORM/DataQuery_SubGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,32 @@
*/
class DataQuery_SubGroup extends DataQuery implements SQLConditionGroup
{
private string $clause;

/**
*
* @var SQLSelect
*/
protected $whereQuery;

public function __construct(DataQuery $base, $connective)
/**
* @var SQLSelect
*/
protected $havingQuery;

public function __construct(DataQuery $base, $connective, string $clause = 'WHERE')
{
parent::__construct($base->dataClass);
$this->query = $base->query;
$this->whereQuery = new SQLSelect();
$this->whereQuery->setConnective($connective);

$base->where($this);
$this->clause = strtoupper($clause);
if ($this->clause === 'WHERE') {
$this->whereQuery = new SQLSelect();
$this->whereQuery->setConnective($connective);
$base->where($this);
} elseif ($this->clause === 'HAVING') {
$this->havingQuery = new SQLSelect();
$this->havingQuery->setConnective($connective);
$base->having($this);
}
}

public function where($filter)
Expand All @@ -49,18 +60,33 @@ public function whereAny($filter)
return $this;
}

public function having($filter)
{
if ($filter) {
$this->havingQuery->addHaving($filter);
}

return $this;
}

public function conditionSQL(&$parameters)
{
$parameters = [];

// Ignore empty conditions
$where = $this->whereQuery->getWhere();
if (empty($where)) {
return null;
if ($this->clause === 'WHERE') {
$where = $this->whereQuery->getWhere();
if (!empty($where)) {
$sql = DB::get_conn()->getQueryBuilder()->buildWhereFragment($this->whereQuery, $parameters);
return preg_replace('/^\s*WHERE\s*/i', '', $sql ?? '');
}
} elseif ($this->clause === 'HAVING') {
$having = $this->havingQuery->getHaving();
if (!empty($having)) {
$sql = DB::get_conn()->getQueryBuilder()->buildHavingFragment($this->havingQuery, $parameters);
return preg_replace('/^\s*HAVING\s*/i', '', $sql ?? '');
}
}

// Allow database to manage joining of conditions
$sql = DB::get_conn()->getQueryBuilder()->buildWhereFragment($this->whereQuery, $parameters);
return preg_replace('/^\s*WHERE\s*/i', '', $sql ?? '');
return null;
}
}
1 change: 0 additions & 1 deletion src/ORM/ManyManyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
class ManyManyList extends RelationList
{

/**
* @var string $joinTable
*/
Expand Down

0 comments on commit 5d11357

Please sign in to comment.