3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 namespace Civi\Api4\Query
;
14 use Civi\API\SelectQuery
;
15 use Civi\Api4\Event\Events
;
16 use Civi\Api4\Event\PostSelectQueryEvent
;
17 use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable
;
18 use Civi\Api4\Service\Schema\Joinable\Joinable
;
19 use Civi\Api4\Utils\FormattingUtil
;
20 use Civi\Api4\Utils\CoreUtil
;
21 use Civi\Api4\Utils\SelectUtil
;
22 use CRM_Core_DAO_AllCoreTables
as AllCoreTables
;
25 * A query `node` may be in one of three formats:
27 * * leaf: [$fieldName, $operator, $criteria]
28 * * negated: ['NOT', $node]
29 * * branch: ['OR|NOT', [$node, $node, ...]]
31 * Leaf operators are one of:
33 * * '=', '<=', '>=', '>', '<', 'LIKE', "<>", "!=",
34 * * "NOT LIKE", 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN',
35 * * 'IS NOT NULL', or 'IS NULL'.
37 class Api4SelectQuery
extends SelectQuery
{
42 protected $apiVersion = 4;
45 * @var \Civi\Api4\Service\Schema\Joinable\Joinable[]
46 * The joinable tables that have been joined so far
48 protected $joinedTables = [];
53 protected $selectAliases = [];
56 * If set to an array, this will start collecting debug info.
60 public $debugOutput = NULL;
73 * @param \Civi\Api4\Generic\DAOGetAction $apiGet
75 public function __construct($apiGet) {
76 $this->entity
= $apiGet->getEntityName();
77 $this->checkPermissions
= $apiGet->getCheckPermissions();
78 $this->select
= $apiGet->getSelect();
79 $this->where
= $apiGet->getWhere();
80 $this->groupBy
= $apiGet->getGroupBy();
81 $this->orderBy
= $apiGet->getOrderBy();
82 $this->limit
= $apiGet->getLimit();
83 $this->offset
= $apiGet->getOffset();
84 $this->having
= $apiGet->getHaving();
85 if ($apiGet->getDebug()) {
86 $this->debugOutput
=& $apiGet->_debugOutput
;
88 $baoName = CoreUtil
::getBAOFromApiName($this->entity
);
89 $this->entityFieldNames
= array_column($baoName::fields(), 'name');
90 foreach ($apiGet->entityFields() as $path => $field) {
91 $field['sql_name'] = '`' . self
::MAIN_TABLE_ALIAS
. '`.`' . $field['column_name'] . '`';
92 $this->addSpecField($path, $field);
95 $this->constructQueryObject($baoName);
97 // Add ACLs first to avoid redundant subclauses
98 $this->query
->where($this->getAclClause(self
::MAIN_TABLE_ALIAS
, $baoName));
102 * Builds final sql statement after all params are set.
105 * @throws \API_Exception
106 * @throws \CRM_Core_Exception
107 * @throws \Civi\API\Exception\UnauthorizedException
109 public function getSql() {
110 $this->buildSelectClause();
111 $this->buildWhereClause();
112 $this->buildOrderBy();
114 $this->buildGroupBy();
115 $this->buildHavingClause();
116 return $this->query
->toSQL();
120 * Why walk when you can
124 public function run() {
126 $sql = $this->getSql();
127 if (is_array($this->debugOutput
)) {
128 $this->debugOutput
['sql'][] = $sql;
130 $query = \CRM_Core_DAO
::executeQuery($sql);
132 while ($query->fetch()) {
133 $id = $query->id ??
$i++
;
134 if (in_array('row_count', $this->select
)) {
135 $results[]['row_count'] = (int) $query->c
;
139 foreach ($this->selectAliases
as $alias => $expr) {
140 $returnName = $alias;
141 $alias = str_replace('.', '_', $alias);
142 $results[$id][$returnName] = property_exists($query, $alias) ?
$query->$alias : NULL;
145 $event = new PostSelectQueryEvent($results, $this);
146 \Civi
::dispatcher()->dispatch(Events
::POST_SELECT_QUERY
, $event);
148 return $event->getResults();
151 protected function buildSelectClause() {
152 // An empty select is the same as *
153 if (empty($this->select
)) {
154 $this->select
= $this->entityFieldNames
;
156 elseif (in_array('row_count', $this->select
)) {
157 $this->query
->select("COUNT(*) AS `c`");
161 // Always select ID (unless we're doing groupBy).
162 if (!$this->groupBy
) {
163 $this->select
= array_merge(['id'], $this->select
);
166 // Expand wildcards in joins (the api wrapper already expanded non-joined wildcards)
167 $wildFields = array_filter($this->select
, function($item) {
168 return strpos($item, '*') !== FALSE && strpos($item, '.') !== FALSE && strpos($item, '(') === FALSE && strpos($item, ' ') === FALSE;
170 foreach ($wildFields as $item) {
171 $pos = array_search($item, array_values($this->select
));
172 $this->joinFK($item);
173 $matches = SelectUtil
::getMatchingFields($item, array_keys($this->apiFieldSpec
));
174 array_splice($this->select
, $pos, 1, $matches);
176 $this->select
= array_unique($this->select
);
178 foreach ($this->select
as $item) {
179 $expr = SqlExpression
::convert($item, TRUE);
181 foreach ($expr->getFields() as $fieldName) {
182 $field = $this->getField($fieldName);
183 // Remove expressions with unknown fields without raising an error
185 $this->select
= array_diff($this->select
, [$item]);
186 if (is_array($this->debugOutput
)) {
187 $this->debugOutput
['undefined_fields'][] = $fieldName;
191 elseif ($field['is_many']) {
196 $alias = $expr->getAlias();
197 $this->selectAliases
[$alias] = $expr->getExpr();
198 $this->query
->select($expr->render($this->apiFieldSpec
) . " AS `$alias`");
206 protected function buildWhereClause() {
207 foreach ($this->where
as $clause) {
208 $this->query
->where($this->treeWalkClauses($clause, 'WHERE'));
213 * Build HAVING clause.
215 * Every expression referenced must also be in the SELECT clause.
217 protected function buildHavingClause() {
218 foreach ($this->having
as $clause) {
219 $this->query
->having($this->treeWalkClauses($clause, 'HAVING'));
226 protected function buildOrderBy() {
227 foreach ($this->orderBy
as $item => $dir) {
228 if ($dir !== 'ASC' && $dir !== 'DESC') {
229 throw new \
API_Exception("Invalid sort direction. Cannot order by $item $dir");
231 $expr = SqlExpression
::convert($item);
232 foreach ($expr->getFields() as $fieldName) {
233 $this->getField($fieldName, TRUE);
235 $this->query
->orderBy($expr->render($this->apiFieldSpec
) . " $dir");
240 * @throws \CRM_Core_Exception
242 protected function buildLimit() {
243 if (!empty($this->limit
) ||
!empty($this->offset
)) {
244 $this->query
->limit($this->limit
, $this->offset
);
249 * Adds GROUP BY clause to query
251 protected function buildGroupBy() {
252 foreach ($this->groupBy
as $item) {
253 $expr = SqlExpression
::convert($item);
254 foreach ($expr->getFields() as $fieldName) {
255 $this->getField($fieldName, TRUE);
257 $this->query
->groupBy($expr->render($this->apiFieldSpec
));
262 * Recursively validate and transform a branch or leaf clause array to SQL.
264 * @param array $clause
265 * @param string $type
267 * @return string SQL where clause
269 * @throws \API_Exception
270 * @uses composeClause() to generate the SQL etc.
272 protected function treeWalkClauses($clause, $type) {
273 switch ($clause[0]) {
277 if (count($clause[1]) === 1) {
278 // a single set so AND|OR is immaterial
279 return $this->treeWalkClauses($clause[1][0], $type);
282 $sql_subclauses = [];
283 foreach ($clause[1] as $subclause) {
284 $sql_subclauses[] = $this->treeWalkClauses($subclause, $type);
286 return '(' . implode("\n" . $clause[0], $sql_subclauses) . ')';
290 // If we get a group of clauses with no operator, assume AND
291 if (!is_string($clause[1][0])) {
292 $clause[1] = ['AND', $clause[1]];
294 return 'NOT (' . $this->treeWalkClauses($clause[1], $type) . ')';
297 return $this->composeClause($clause, $type);
302 * Validate and transform a leaf clause array to SQL.
303 * @param array $clause [$fieldName, $operator, $criteria]
304 * @param string $type
307 * @throws \API_Exception
310 protected function composeClause(array $clause, string $type) {
311 // Pad array for unary operators
312 list($expr, $operator, $value) = array_pad($clause, 3, NULL);
314 // For WHERE clause, expr must be the name of a field.
315 if ($type === 'WHERE') {
316 $field = $this->getField($expr, TRUE);
317 FormattingUtil
::formatInputValue($value, $field, $this->getEntity());
318 $fieldAlias = $field['sql_name'];
320 // For HAVING, expr must be an item in the SELECT clause
322 if (isset($this->selectAliases
[$expr])) {
325 elseif (in_array($expr, $this->selectAliases
)) {
326 $fieldAlias = array_search($expr, $this->selectAliases
);
329 throw new \
API_Exception("Invalid expression in $type clause: '$expr'. Must use a value from SELECT clause.");
333 $sql_clause = \CRM_Core_DAO
::createSQLFilter($fieldAlias, [$operator => $value]);
334 if ($sql_clause === NULL) {
335 throw new \
API_Exception("Invalid value in $type clause for '$expr'");
343 protected function getFields() {
344 return $this->apiFieldSpec
;
348 * Fetch a field from the getFields list
350 * @param string $fieldName
351 * @param bool $strict
352 * In strict mode, this will throw an exception if the field doesn't exist
354 * @return string|null
355 * @throws \API_Exception
357 public function getField($fieldName, $strict = FALSE) {
358 // Perform join if field not yet available - this will add it to apiFieldSpec
359 if (!isset($this->apiFieldSpec
[$fieldName]) && strpos($fieldName, '.')) {
360 $this->joinFK($fieldName);
362 $field = $this->apiFieldSpec
[$fieldName] ??
NULL;
363 if ($strict && !$field) {
364 throw new \
API_Exception("Invalid field '$fieldName'");
370 * Joins a path and adds all fields in the joined eneity to apiFieldSpec
373 * @throws \API_Exception
376 protected function joinFK($key) {
377 if (isset($this->apiFieldSpec
[$key])) {
381 $pathArray = explode('.', $key);
383 /** @var \Civi\Api4\Service\Schema\Joiner $joiner */
384 $joiner = \Civi
::container()->get('joiner');
385 // The last item in the path is the field name. We don't care about that; we'll add all fields from the joined entity.
386 array_pop($pathArray);
387 $pathString = implode('.', $pathArray);
389 if (!$joiner->canJoin($this, $pathString)) {
393 $joinPath = $joiner->join($this, $pathString);
396 foreach ($joinPath as $joinable) {
397 if ($joinable->getJoinType() === Joinable
::JOIN_TYPE_ONE_TO_MANY
) {
402 /** @var \Civi\Api4\Service\Schema\Joinable\Joinable $lastLink */
403 $lastLink = array_pop($joinPath);
405 // Custom field names are already prefixed
406 $isCustom = $lastLink instanceof CustomGroupJoinable
;
408 array_pop($pathArray);
410 $prefix = $pathArray ?
implode('.', $pathArray) . '.' : '';
411 // Cache field info for retrieval by $this->getField()
412 $joinEntity = $lastLink->getEntity();
413 foreach ($lastLink->getEntityFields() as $fieldObject) {
414 $fieldArray = ['entity' => $joinEntity] +
$fieldObject->toArray();
415 $fieldArray['sql_name'] = '`' . $lastLink->getAlias() . '`.`' . $fieldArray['column_name'] . '`';
416 $fieldArray['is_custom'] = $isCustom;
417 $fieldArray['is_join'] = TRUE;
418 $fieldArray['is_many'] = $isMany;
419 $this->addSpecField($prefix . $fieldArray['name'], $fieldArray);
424 * @param \Civi\Api4\Service\Schema\Joinable\Joinable $joinable
428 public function addJoinedTable(Joinable
$joinable) {
429 $this->joinedTables
[] = $joinable;
435 * @return FALSE|string
437 public function getFrom() {
438 return AllCoreTables
::getTableForClass(AllCoreTables
::getFullName($this->entity
));
444 public function getEntity() {
445 return $this->entity
;
451 public function getSelect() {
452 return $this->select
;
458 public function getWhere() {
465 public function getOrderBy() {
466 return $this->orderBy
;
472 public function getLimit() {
479 public function getOffset() {
480 return $this->offset
;
486 public function getSelectFields() {
487 return $this->selectFields
;
493 public function isFillUniqueFields() {
494 return $this->isFillUniqueFields
;
498 * @return \CRM_Utils_SQL_Select
500 public function getQuery() {
507 public function getJoins() {
514 public function getApiFieldSpec() {
515 return $this->apiFieldSpec
;
521 public function getEntityFieldNames() {
522 return $this->entityFieldNames
;
528 public function getAclFields() {
529 return $this->aclFields
;
533 * @return bool|string
535 public function getCheckPermissions() {
536 return $this->checkPermissions
;
542 public function getApiVersion() {
543 return $this->apiVersion
;
547 * @return \Civi\Api4\Service\Schema\Joinable\Joinable[]
549 public function getJoinedTables() {
550 return $this->joinedTables
;
554 * @return \Civi\Api4\Service\Schema\Joinable\Joinable
556 public function getJoinedTable($alias) {
557 foreach ($this->joinedTables
as $join) {
558 if ($join->getAlias() == $alias) {
565 * Get table name on basis of entity
567 * @param string $baoName
571 public function constructQueryObject($baoName) {
572 if (strstr($this->entity
, 'Custom_')) {
573 $this->query
= \CRM_Utils_SQL_Select
::from(CoreUtil
::getCustomTableByName(str_replace('Custom_', '', $this->entity
)) . ' ' . self
::MAIN_TABLE_ALIAS
);
574 $this->entityFieldNames
= array_keys($this->apiFieldSpec
);
577 $bao = new $baoName();
578 $this->query
= \CRM_Utils_SQL_Select
::from($bao->tableName() . ' ' . self
::MAIN_TABLE_ALIAS
);
583 * Checks if a field either belongs to the main entity or is joinable 1-to-1.
585 * Used to determine if a field can be added to the SELECT of the main query,
586 * or if it must be fetched post-query.
588 * @param string $fieldPath
591 public function isOneToOneField(string $fieldPath) {
592 return strpos($fieldPath, '.') === FALSE ||
!array_filter($this->getPathJoinTypes($fieldPath));
596 * Separates a string like 'emails.location_type.label' into an array, where
597 * each value in the array tells whether it is 1-1 or 1-n join type
599 * @param string $pathString
600 * Dot separated path to the field
603 * Index is table alias and value is boolean whether is 1-to-many join
605 public function getPathJoinTypes($pathString) {
606 $pathParts = explode('.', $pathString);
608 array_pop($pathParts);
611 $isMultipleChecker = function($alias) use ($query) {
612 foreach ($query->getJoinedTables() as $table) {
613 if ($table->getAlias() === $alias) {
614 return $table->getJoinType() === Joinable
::JOIN_TYPE_ONE_TO_MANY
;
620 foreach ($pathParts as $part) {
621 $path[$part] = $isMultipleChecker($part);
631 private function addSpecField($path, $field) {
632 // Only add field to spec if we have permission
633 if ($this->checkPermissions
&& !empty($field['permission']) && !\CRM_Core_Permission
::check($field['permission'])) {
634 $this->apiFieldSpec
[$path] = FALSE;
638 $defaults['is_custom'] = $defaults['is_join'] = $defaults['is_many'] = FALSE;
640 $this->apiFieldSpec
[$path] = $field;