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 +--------------------------------------------------------------------+
13 * Dear God Why Do I Have To Write This (Dumb SQL Builder)
17 * $select = CRM_Utils_SQL_Select::from('civicrm_activity act')
18 * ->join('absence', 'inner join civicrm_activity absence on absence.id = act.source_record_id')
19 * ->where('activity_type_id = #type', array('type' => 234))
20 * ->where('status_id IN (#statuses)', array('statuses' => array(1,2,3))
21 * ->where('subject like @subj', array('subj' => '%hello%'))
22 * ->where('!dynamicColumn = 1', array('dynamicColumn' => 'coalesce(is_active,0)'))
23 * ->where('!column = @value', array(
24 * 'column' => $customField->column_name,
25 * 'value' => $form['foo']
27 * echo $select->toSQL();
32 * - No knowledge of the underlying SQL API (except for escaping -- CRM_Core_DAO::escapeString)
33 * - No knowledge of the underlying data model
34 * - SQL clauses correspond to PHP functions ($select->where("foo_id=123"))
35 * - Variable escaping is concise and controllable based on prefixes, eg
36 * - similar to Drupal's t()
37 * - use "@varname" to insert the escaped value
38 * - use "!varname" to insert raw (unescaped) values
39 * - use "#varname" to insert a numerical value (these are validated but not escaped)
40 * - to disable any preprocessing, simply omit the variable list
41 * - control characters (@!#) are mandatory in expressions but optional in arg-keys
42 * - Variables may be individual values or arrays; arrays are imploded with commas
43 * - Conditionals are AND'd; if you need OR's, do it yourself
44 * - Use classes/functions with documentation (rather than undocumented array-trees)
45 * - For any given string, interpolation is only performed once. After an interpolation,
46 * a string may never again be subjected to interpolation.
48 * The "interpolate-once" principle can be enforced by either interpolating on input
49 * xor output. The notations for input and output interpolation are a bit different,
50 * and they may not be mixed.
53 * // Interpolate on input. Set params when using them.
54 * $select->where('activity_type_id = #type', array(
58 * // Interpolate on output. Set params independently.
60 * ->where('activity_type_id = #type')
61 * ->param('type', 234),
65 * @copyright CiviCRM LLC https://civicrm.org/licensing
67 class CRM_Utils_SQL_Select
extends CRM_Utils_SQL_BaseParamQuery
{
69 private $insertInto = NULL;
70 private $insertVerb = 'INSERT INTO ';
71 private $insertIntoFields = [];
72 private $selects = [];
76 private $groupBys = [];
77 private $havings = [];
78 private $orderBys = [];
79 private $limit = NULL;
80 private $offset = NULL;
81 private $distinct = NULL;
84 * Create a new SELECT query.
87 * Table-name and optional alias.
88 * @param array $options
89 * @return CRM_Utils_SQL_Select
91 public static function from($from, $options = []) {
92 return new self($from, $options);
96 * Create a partial SELECT query.
98 * @param array $options
99 * @return CRM_Utils_SQL_Select
101 public static function fragment($options = []) {
102 return new self(NULL, $options);
106 * Create a new SELECT query.
108 * @param string $from
109 * Table-name and optional alias.
110 * @param array $options
112 public function __construct($from, $options = []) {
114 $this->mode
= $options['mode'] ?? self
::INTERPOLATE_AUTO
;
118 * Make a new copy of this query.
120 * @return CRM_Utils_SQL_Select
122 public function copy() {
127 * Merge something or other.
129 * @param array|CRM_Utils_SQL_Select $other
130 * @param array|NULL $parts
131 * ex: 'joins', 'wheres'
132 * @return CRM_Utils_SQL_Select
134 public function merge($other, $parts = NULL) {
135 if ($other === NULL) {
139 if (is_array($other)) {
140 foreach ($other as $fragment) {
141 $this->merge($fragment, $parts);
146 if ($this->mode
=== self
::INTERPOLATE_AUTO
) {
147 $this->mode
= $other->mode
;
149 elseif ($other->mode
=== self
::INTERPOLATE_AUTO
) {
152 elseif ($this->mode
!== $other->mode
) {
153 // Mixing modes will lead to someone getting an expected substitution.
154 throw new RuntimeException("Cannot merge queries that use different interpolation modes ({$this->mode} vs {$other->mode}).");
157 $arrayFields = ['insertIntoFields', 'selects', 'joins', 'wheres', 'groupBys', 'havings', 'orderBys', 'params'];
158 foreach ($arrayFields as $f) {
159 if ($parts === NULL ||
in_array($f, $parts)) {
160 $this->{$f} = array_merge($this->{$f}, $other->{$f});
164 $flatFields = ['insertInto', 'from', 'limit', 'offset'];
165 foreach ($flatFields as $f) {
166 if ($parts === NULL ||
in_array($f, $parts)) {
167 if ($other->{$f} !== NULL) {
168 $this->{$f} = $other->{$f};
177 * Add a new JOIN clause.
179 * Note: To add multiple JOINs at once, use $name===NULL and
180 * pass an array of $exprs.
182 * @param string|NULL $name
183 * The effective alias of the joined table.
184 * @param string|array $exprs
185 * The complete join expression (eg "INNER JOIN mytable myalias ON mytable.id = maintable.foo_id").
186 * @param array|null $args
187 * @return CRM_Utils_SQL_Select
189 public function join($name, $exprs, $args = NULL) {
190 if ($name !== NULL) {
191 $this->joins
[$name] = $this->interpolate($exprs, $args);
194 foreach ($exprs as $name => $expr) {
195 $this->joins
[$name] = $this->interpolate($expr, $args);
203 * Specify the column(s)/value(s) to return by adding to the SELECT clause
205 * @param string|array $exprs list of SQL expressions
206 * @param null|array $args use NULL to disable interpolation; use an array of variables to enable
207 * @return CRM_Utils_SQL_Select
209 public function select($exprs, $args = NULL) {
210 $exprs = (array) $exprs;
211 foreach ($exprs as $expr) {
212 $this->selects
[] = $this->interpolate($expr, $args);
218 * Return only distinct values
220 * @param bool $isDistinct allow DISTINCT select or not
221 * @return CRM_Utils_SQL_Select
223 public function distinct($isDistinct = TRUE) {
225 $this->distinct
= 'DISTINCT ';
231 * Limit results by adding extra condition(s) to the WHERE clause
233 * @param string|array $exprs list of SQL expressions
234 * @param null|array $args use NULL to disable interpolation; use an array of variables to enable
235 * @return CRM_Utils_SQL_Select
237 public function where($exprs, $args = NULL) {
238 $exprs = (array) $exprs;
239 foreach ($exprs as $expr) {
240 $evaluatedExpr = $this->interpolate($expr, $args);
241 $this->wheres
[$evaluatedExpr] = $evaluatedExpr;
247 * Group results by adding extra items to the GROUP BY clause.
249 * @param string|array $exprs list of SQL expressions
250 * @param null|array $args use NULL to disable interpolation; use an array of variables to enable
251 * @return CRM_Utils_SQL_Select
253 public function groupBy($exprs, $args = NULL) {
254 $exprs = (array) $exprs;
255 foreach ($exprs as $expr) {
256 $evaluatedExpr = $this->interpolate($expr, $args);
257 $this->groupBys
[$evaluatedExpr] = $evaluatedExpr;
263 * Limit results by adding extra condition(s) to the HAVING clause
265 * @param string|array $exprs list of SQL expressions
266 * @param null|array $args use NULL to disable interpolation; use an array of variables to enable
267 * @return CRM_Utils_SQL_Select
269 public function having($exprs, $args = NULL) {
270 $exprs = (array) $exprs;
271 foreach ($exprs as $expr) {
272 $evaluatedExpr = $this->interpolate($expr, $args);
273 $this->havings
[$evaluatedExpr] = $evaluatedExpr;
279 * Sort results by adding extra items to the ORDER BY clause.
281 * @param string|array $exprs list of SQL expressions
282 * @param null|array $args use NULL to disable interpolation; use an array of variables to enable
284 * @return \CRM_Utils_SQL_Select
286 public function orderBy($exprs, $args = NULL, $weight = 0) {
288 $exprs = (array) $exprs;
289 foreach ($exprs as $expr) {
290 $evaluatedExpr = $this->interpolate($expr, $args);
291 $this->orderBys
[$evaluatedExpr] = ['value' => $evaluatedExpr, 'weight' => $weight, 'guid' => $guid++
];
297 * Set one (or multiple) parameters to interpolate into the query.
299 * @param array|string $keys
300 * Key name, or an array of key-value pairs.
301 * @param null|mixed $value
302 * The new value of the parameter.
303 * Values may be strings, ints, or arrays thereof -- provided that the
304 * SQL query uses appropriate prefix (e.g. "@", "!", "#").
305 * @return \CRM_Utils_SQL_Select
307 public function param($keys, $value = NULL) {
308 // Why bother with an override? To provide bett er type-hinting in `@return`.
309 return parent
::param($keys, $value);
313 * Set a limit on the number of records to return.
317 * @return CRM_Utils_SQL_Select
318 * @throws CRM_Core_Exception
320 public function limit($limit, $offset = 0) {
321 if ($limit !== NULL && !is_numeric($limit)) {
322 throw new CRM_Core_Exception("Illegal limit");
324 if ($offset !== NULL && !is_numeric($offset)) {
325 throw new CRM_Core_Exception("Illegal offset");
327 $this->limit
= $limit;
328 $this->offset
= $offset;
333 * Insert the results of the SELECT query into another
336 * @param string $table
337 * The name of the other table (which receives new data).
338 * @param array $fields
339 * The fields to fill in the other table (in order).
340 * @return CRM_Utils_SQL_Select
341 * @see insertIntoField
343 public function insertInto($table, $fields = []) {
344 $this->insertInto
= $table;
345 $this->insertIntoField($fields);
350 * Wrapper function of insertInto fn but sets insertVerb = "INSERT IGNORE INTO "
352 * @param string $table
353 * The name of the other table (which receives new data).
354 * @param array $fields
355 * The fields to fill in the other table (in order).
356 * @return CRM_Utils_SQL_Select
358 public function insertIgnoreInto($table, $fields = []) {
359 $this->insertVerb
= "INSERT IGNORE INTO ";
360 return $this->insertInto($table, $fields);
364 * Wrapper function of insertInto fn but sets insertVerb = "REPLACE INTO "
366 * @param string $table
367 * The name of the other table (which receives new data).
368 * @param array $fields
369 * The fields to fill in the other table (in order).
371 public function replaceInto($table, $fields = []) {
372 $this->insertVerb
= "REPLACE INTO ";
373 return $this->insertInto($table, $fields);
377 * @param array $fields
378 * The fields to fill in the other table (in order).
379 * @return CRM_Utils_SQL_Select
381 public function insertIntoField($fields) {
382 $fields = (array) $fields;
383 foreach ($fields as $field) {
384 $this->insertIntoFields
[] = $field;
390 * @param array|NULL $parts
391 * List of fields to check (e.g. 'selects', 'joins').
395 public function isEmpty($parts = NULL) {
410 if ($parts !== NULL) {
411 $fields = array_intersect($fields, $parts);
413 foreach ($fields as $field) {
414 if (!empty($this->{$field})) {
425 public function toSQL() {
427 if ($this->insertInto
) {
428 $sql .= $this->insertVerb
. $this->insertInto
. ' (';
429 $sql .= implode(', ', $this->insertIntoFields
);
433 if ($this->selects
) {
434 $sql .= 'SELECT ' . $this->distinct
. implode(', ', $this->selects
) . "\n";
437 $sql .= 'SELECT *' . "\n";
439 if ($this->from
!== NULL) {
440 $sql .= 'FROM ' . $this->from
. "\n";
442 foreach ($this->joins
as $join) {
443 $sql .= $join . "\n";
446 $sql .= 'WHERE (' . implode(') AND (', $this->wheres
) . ")\n";
448 if ($this->groupBys
) {
449 $sql .= 'GROUP BY ' . implode(', ', $this->groupBys
) . "\n";
451 if ($this->havings
) {
452 $sql .= 'HAVING (' . implode(') AND (', $this->havings
) . ")\n";
454 if ($this->orderBys
) {
455 $orderBys = CRM_Utils_Array
::crmArraySortByField($this->orderBys
,
457 $orderBys = CRM_Utils_Array
::collect('value', $orderBys);
458 $sql .= 'ORDER BY ' . implode(', ', $orderBys) . "\n";
460 if ($this->limit
!== NULL) {
461 $sql .= 'LIMIT ' . $this->limit
. "\n";
462 if ($this->offset
!== NULL) {
463 $sql .= 'OFFSET ' . $this->offset
. "\n";
466 if ($this->mode
=== self
::INTERPOLATE_OUTPUT
) {
467 $sql = $this->interpolate($sql, $this->params
, self
::INTERPOLATE_OUTPUT
);
475 * To examine the results, use a function like `fetch()`, `fetchAll()`,
476 * `fetchValue()`, or `fetchMap()`.
478 * @param string|NULL $daoName
479 * The return object should be an instance of this class.
480 * Ex: 'CRM_Contact_BAO_Contact'.
481 * @param bool $i18nRewrite
482 * If the system has multilingual features, should the field/table
483 * names be rewritten?
484 * @return CRM_Core_DAO
485 * @see CRM_Core_DAO::executeQuery
486 * @see CRM_Core_I18n_Schema::rewriteQuery
488 public function execute($daoName = NULL, $i18nRewrite = TRUE) {
489 // Don't pass through $params. toSQL() handles interpolation.
492 // Don't pass through $abort, $trapException. Just use straight-up exceptions.
494 $trapException = FALSE;
495 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
497 // Don't pass through freeDAO. You can do it yourself.
500 return CRM_Core_DAO
::executeQuery($this->toSQL(), $params, $abort, $daoName,
501 $freeDAO, $i18nRewrite, $trapException);