3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Dear God Why Do I Have To Write This (Dumb SQL Builder)
33 * $select = CRM_Utils_SQL_Select::from('civicrm_activity act')
34 * ->join('absence', 'inner join civicrm_activity absence on absence.id = act.source_record_id')
35 * ->where('activity_type_id = #type', array('type' => 234))
36 * ->where('status_id IN (#statuses)', array('statuses' => array(1,2,3))
37 * ->where('subject like @subj', array('subj' => '%hello%'))
38 * ->where('!dynamicColumn = 1', array('dynamicColumn' => 'coalesce(is_active,0)'))
39 * ->where('!column = @value', array(
40 * 'column' => $customField->column_name,
41 * 'value' => $form['foo']
43 * echo $select->toSQL();
48 * - No knowledge of the underlying SQL API (except for escaping -- CRM_Core_DAO::escapeString)
49 * - No knowledge of the underlying data model
50 * - SQL clauses correspond to PHP functions ($select->where("foo_id=123"))
51 * - Variable escaping is concise and controllable based on prefixes, eg
52 * - similar to Drupal's t()
53 * - use "@varname" to insert the escaped value
54 * - use "!varname" to insert raw (unescaped) values
55 * - use "#varname" to insert a numerical value (these are validated but not escaped)
56 * - to disable any preprocessing, simply omit the variable list
57 * - control characters (@!#) are mandatory in expressions but optional in arg-keys
58 * - Variables may be individual values or arrays; arrays are imploded with commas
59 * - Conditionals are AND'd; if you need OR's, do it yourself
60 * - Use classes/functions with documentation (rather than undocumented array-trees)
61 * - For any given string, interpolation is only performed once. After an interpolation,
62 * a string may never again be subjected to interpolation.
64 * The "interpolate-once" principle can be enforced by either interpolating on input
65 * xor output. The notations for input and output interpolation are a bit different,
66 * and they may not be mixed.
69 * // Interpolate on input. Set params when using them.
70 * $select->where('activity_type_id = #type', array(
74 * // Interpolate on output. Set params independently.
76 * ->where('activity_type_id = #type')
77 * ->param('type', 234),
81 * @copyright CiviCRM LLC (c) 2004-2019
83 class CRM_Utils_SQL_Select
extends CRM_Utils_SQL_BaseParamQuery
{
85 private $insertInto = NULL;
86 private $insertVerb = 'INSERT INTO ';
87 private $insertIntoFields = [];
88 private $selects = [];
92 private $groupBys = [];
93 private $havings = [];
94 private $orderBys = [];
95 private $limit = NULL;
96 private $offset = NULL;
97 private $distinct = NULL;
100 * Create a new SELECT query.
102 * @param string $from
103 * Table-name and optional alias.
104 * @param array $options
105 * @return CRM_Utils_SQL_Select
107 public static function from($from, $options = []) {
108 return new self($from, $options);
112 * Create a partial SELECT query.
114 * @param array $options
115 * @return CRM_Utils_SQL_Select
117 public static function fragment($options = []) {
118 return new self(NULL, $options);
122 * Create a new SELECT query.
124 * @param string $from
125 * Table-name and optional alias.
126 * @param array $options
128 public function __construct($from, $options = []) {
130 $this->mode
= isset($options['mode']) ?
$options['mode'] : self
::INTERPOLATE_AUTO
;
134 * Make a new copy of this query.
136 * @return CRM_Utils_SQL_Select
138 public function copy() {
143 * Merge something or other.
145 * @param array|CRM_Utils_SQL_Select $other
146 * @param array|NULL $parts
147 * ex: 'joins', 'wheres'
148 * @return CRM_Utils_SQL_Select
150 public function merge($other, $parts = NULL) {
151 if ($other === NULL) {
155 if (is_array($other)) {
156 foreach ($other as $fragment) {
157 $this->merge($fragment, $parts);
162 if ($this->mode
=== self
::INTERPOLATE_AUTO
) {
163 $this->mode
= $other->mode
;
165 elseif ($other->mode
=== self
::INTERPOLATE_AUTO
) {
168 elseif ($this->mode
!== $other->mode
) {
169 // Mixing modes will lead to someone getting an expected substitution.
170 throw new RuntimeException("Cannot merge queries that use different interpolation modes ({$this->mode} vs {$other->mode}).");
173 $arrayFields = ['insertIntoFields', 'selects', 'joins', 'wheres', 'groupBys', 'havings', 'orderBys', 'params'];
174 foreach ($arrayFields as $f) {
175 if ($parts === NULL ||
in_array($f, $parts)) {
176 $this->{$f} = array_merge($this->{$f}, $other->{$f});
180 $flatFields = ['insertInto', 'from', 'limit', 'offset'];
181 foreach ($flatFields as $f) {
182 if ($parts === NULL ||
in_array($f, $parts)) {
183 if ($other->{$f} !== NULL) {
184 $this->{$f} = $other->{$f};
193 * Add a new JOIN clause.
195 * Note: To add multiple JOINs at once, use $name===NULL and
196 * pass an array of $exprs.
198 * @param string|NULL $name
199 * The effective alias of the joined table.
200 * @param string|array $exprs
201 * The complete join expression (eg "INNER JOIN mytable myalias ON mytable.id = maintable.foo_id").
202 * @param array|null $args
203 * @return CRM_Utils_SQL_Select
205 public function join($name, $exprs, $args = NULL) {
206 if ($name !== NULL) {
207 $this->joins
[$name] = $this->interpolate($exprs, $args);
210 foreach ($exprs as $name => $expr) {
211 $this->joins
[$name] = $this->interpolate($expr, $args);
219 * Specify the column(s)/value(s) to return by adding to the SELECT clause
221 * @param string|array $exprs list of SQL expressions
222 * @param null|array $args use NULL to disable interpolation; use an array of variables to enable
223 * @return CRM_Utils_SQL_Select
225 public function select($exprs, $args = NULL) {
226 $exprs = (array) $exprs;
227 foreach ($exprs as $expr) {
228 $this->selects
[] = $this->interpolate($expr, $args);
234 * Return only distinct values
236 * @param bool $isDistinct allow DISTINCT select or not
237 * @return CRM_Utils_SQL_Select
239 public function distinct($isDistinct = TRUE) {
241 $this->distinct
= 'DISTINCT ';
247 * Limit results by adding extra condition(s) to the WHERE 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 where($exprs, $args = NULL) {
254 $exprs = (array) $exprs;
255 foreach ($exprs as $expr) {
256 $evaluatedExpr = $this->interpolate($expr, $args);
257 $this->wheres
[$evaluatedExpr] = $evaluatedExpr;
263 * Group results by adding extra items to the GROUP BY 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 groupBy($exprs, $args = NULL) {
270 $exprs = (array) $exprs;
271 foreach ($exprs as $expr) {
272 $evaluatedExpr = $this->interpolate($expr, $args);
273 $this->groupBys
[$evaluatedExpr] = $evaluatedExpr;
279 * Limit results by adding extra condition(s) to the HAVING 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
283 * @return CRM_Utils_SQL_Select
285 public function having($exprs, $args = NULL) {
286 $exprs = (array) $exprs;
287 foreach ($exprs as $expr) {
288 $evaluatedExpr = $this->interpolate($expr, $args);
289 $this->havings
[$evaluatedExpr] = $evaluatedExpr;
295 * Sort results by adding extra items to the ORDER BY clause.
297 * @param string|array $exprs list of SQL expressions
298 * @param null|array $args use NULL to disable interpolation; use an array of variables to enable
300 * @return \CRM_Utils_SQL_Select
302 public function orderBy($exprs, $args = NULL, $weight = 0) {
304 $exprs = (array) $exprs;
305 foreach ($exprs as $expr) {
306 $evaluatedExpr = $this->interpolate($expr, $args);
307 $this->orderBys
[$evaluatedExpr] = ['value' => $evaluatedExpr, 'weight' => $weight, 'guid' => $guid++
];
313 * Set one (or multiple) parameters to interpolate into the query.
315 * @param array|string $keys
316 * Key name, or an array of key-value pairs.
317 * @param null|mixed $value
318 * The new value of the parameter.
319 * Values may be strings, ints, or arrays thereof -- provided that the
320 * SQL query uses appropriate prefix (e.g. "@", "!", "#").
321 * @return \CRM_Utils_SQL_Select
323 public function param($keys, $value = NULL) {
324 // Why bother with an override? To provide bett er type-hinting in `@return`.
325 return parent
::param($keys, $value);
329 * Set a limit on the number of records to return.
333 * @return CRM_Utils_SQL_Select
334 * @throws CRM_Core_Exception
336 public function limit($limit, $offset = 0) {
337 if ($limit !== NULL && !is_numeric($limit)) {
338 throw new CRM_Core_Exception("Illegal limit");
340 if ($offset !== NULL && !is_numeric($offset)) {
341 throw new CRM_Core_Exception("Illegal offset");
343 $this->limit
= $limit;
344 $this->offset
= $offset;
349 * Insert the results of the SELECT query into another
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
357 * @see insertIntoField
359 public function insertInto($table, $fields = []) {
360 $this->insertInto
= $table;
361 $this->insertIntoField($fields);
366 * Wrapper function of insertInto fn but sets insertVerb = "INSERT IGNORE INTO "
368 * @param string $table
369 * The name of the other table (which receives new data).
370 * @param array $fields
371 * The fields to fill in the other table (in order).
372 * @return CRM_Utils_SQL_Select
374 public function insertIgnoreInto($table, $fields = []) {
375 $this->insertVerb
= "INSERT IGNORE INTO ";
376 return $this->insertInto($table, $fields);
380 * Wrapper function of insertInto fn but sets insertVerb = "REPLACE INTO "
382 * @param string $table
383 * The name of the other table (which receives new data).
384 * @param array $fields
385 * The fields to fill in the other table (in order).
387 public function replaceInto($table, $fields = []) {
388 $this->insertVerb
= "REPLACE INTO ";
389 return $this->insertInto($table, $fields);
393 * @param array $fields
394 * The fields to fill in the other table (in order).
395 * @return CRM_Utils_SQL_Select
397 public function insertIntoField($fields) {
398 $fields = (array) $fields;
399 foreach ($fields as $field) {
400 $this->insertIntoFields
[] = $field;
406 * @param array|NULL $parts
407 * List of fields to check (e.g. 'selects', 'joins').
411 public function isEmpty($parts = NULL) {
426 if ($parts !== NULL) {
427 $fields = array_intersect($fields, $parts);
429 foreach ($fields as $field) {
430 if (!empty($this->{$field})) {
441 public function toSQL() {
443 if ($this->insertInto
) {
444 $sql .= $this->insertVerb
. $this->insertInto
. ' (';
445 $sql .= implode(', ', $this->insertIntoFields
);
449 if ($this->selects
) {
450 $sql .= 'SELECT ' . $this->distinct
. implode(', ', $this->selects
) . "\n";
453 $sql .= 'SELECT *' . "\n";
455 if ($this->from
!== NULL) {
456 $sql .= 'FROM ' . $this->from
. "\n";
458 foreach ($this->joins
as $join) {
459 $sql .= $join . "\n";
462 $sql .= 'WHERE (' . implode(') AND (', $this->wheres
) . ")\n";
464 if ($this->groupBys
) {
465 $sql .= 'GROUP BY ' . implode(', ', $this->groupBys
) . "\n";
467 if ($this->havings
) {
468 $sql .= 'HAVING (' . implode(') AND (', $this->havings
) . ")\n";
470 if ($this->orderBys
) {
471 $orderBys = CRM_Utils_Array
::crmArraySortByField($this->orderBys
,
473 $orderBys = CRM_Utils_Array
::collect('value', $orderBys);
474 $sql .= 'ORDER BY ' . implode(', ', $orderBys) . "\n";
476 if ($this->limit
!== NULL) {
477 $sql .= 'LIMIT ' . $this->limit
. "\n";
478 if ($this->offset
!== NULL) {
479 $sql .= 'OFFSET ' . $this->offset
. "\n";
482 if ($this->mode
=== self
::INTERPOLATE_OUTPUT
) {
483 $sql = $this->interpolate($sql, $this->params
, self
::INTERPOLATE_OUTPUT
);
491 * To examine the results, use a function like `fetch()`, `fetchAll()`,
492 * `fetchValue()`, or `fetchMap()`.
494 * @param string|NULL $daoName
495 * The return object should be an instance of this class.
496 * Ex: 'CRM_Contact_BAO_Contact'.
497 * @param bool $i18nRewrite
498 * If the system has multilingual features, should the field/table
499 * names be rewritten?
500 * @return CRM_Core_DAO
501 * @see CRM_Core_DAO::executeQuery
502 * @see CRM_Core_I18n_Schema::rewriteQuery
504 public function execute($daoName = NULL, $i18nRewrite = TRUE) {
505 // Don't pass through $params. toSQL() handles interpolation.
508 // Don't pass through $abort, $trapException. Just use straight-up exceptions.
510 $trapException = FALSE;
511 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
513 // Don't pass through freeDAO. You can do it yourself.
516 return CRM_Core_DAO
::executeQuery($this->toSQL(), $params, $abort, $daoName,
517 $freeDAO, $i18nRewrite, $trapException);