Skip to content

Commit

Permalink
APIv4 - Allow functions in WHERE clause
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Dec 27, 2021
1 parent 60fcac4 commit 62c7b5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Civi/Api4/Query/Api4SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,13 @@ protected function composeClause(array $clause, string $type, int $depth) {

// For WHERE clause, expr must be the name of a field.
if ($type === 'WHERE' && !$isExpression) {
$field = $this->getField($expr, TRUE);
FormattingUtil::formatInputValue($value, $expr, $field, $operator);
$fieldAlias = $this->getExpression($expr)->render($this->apiFieldSpec);
$expr = $this->getExpression($expr, ['SqlField', 'SqlFunction']);
if ($expr->getType() === 'SqlField') {
$fieldName = count($expr->getFields()) === 1 ? $expr->getFields()[0] : NULL;
$field = $this->getField($fieldName, TRUE);
FormattingUtil::formatInputValue($value, $fieldName, $field, $operator);
}
$fieldAlias = $expr->render($this->apiFieldSpec);
}
// For HAVING, expr must be an item in the SELECT clause
elseif ($type === 'HAVING') {
Expand Down Expand Up @@ -592,11 +596,12 @@ protected function createSQLClause($fieldAlias, $operator, $value, $field, int $

/**
* @param string $expr
* @param array $allowedTypes
* @return SqlExpression
* @throws \API_Exception
*/
protected function getExpression(string $expr) {
$sqlExpr = SqlExpression::convert($expr);
protected function getExpression(string $expr, $allowedTypes = NULL) {
$sqlExpr = SqlExpression::convert($expr, FALSE, $allowedTypes);
foreach ($sqlExpr->getFields() as $fieldName) {
$this->getField($fieldName, TRUE);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/phpunit/api/v4/Action/SqlFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,23 @@ public function testRandFunction() {
$this->assertLessThan(1, $result[0]['rand']);
}

public function testYearInWhereClause() {
$lastName = uniqid(__FUNCTION__);
$sampleData = [
['first_name' => 'abc', 'last_name' => $lastName, 'birth_date' => '2009-11-11'],
['first_name' => 'def', 'last_name' => $lastName, 'birth_date' => '2009-01-01'],
['first_name' => 'def', 'last_name' => $lastName, 'birth_date' => '2010-01-01'],
];
Contact::save(FALSE)
->setRecords($sampleData)
->execute();

$result = Contact::get(FALSE)
->addWhere('last_name', '=', $lastName)
->addWhere('YEAR(birth_date)', '=', 2009)
->selectRowCount()
->execute();
$this->assertCount(2, $result);
}

}

0 comments on commit 62c7b5c

Please sign in to comment.