Skip navigation
Help

select.inc

  1. drupal
    1. 7 drupal/includes/database/select.inc
    2. 7 drupal/includes/database/sqlite/select.inc
    3. 7 drupal/includes/database/pgsql/select.inc

Interfaces

NameDescription
QueryExtendableInterfaceInterface for extendable query objects.
SelectQueryInterfaceInterface definition for a Select Query object.

Classes

NameDescription
SelectQueryQuery builder for SELECT statements.
SelectQueryExtenderThe base extender class for Select queries.

File

drupal/includes/database/select.inc
View source
  1. <?php
  2. /**
  3. * @ingroup database
  4. * @{
  5. */
  6. require_once dirname(__FILE__) . '/query.inc';
  7. /**
  8. * Interface for extendable query objects.
  9. *
  10. * "Extenders" follow the "Decorator" OOP design pattern. That is, they wrap
  11. * and "decorate" another object. In our case, they implement the same interface
  12. * as select queries and wrap a select query, to which they delegate almost all
  13. * operations. Subclasses of this class may implement additional methods or
  14. * override existing methods as appropriate. Extenders may also wrap other
  15. * extender objects, allowing for arbitrarily complex "enhanced" queries.
  16. */
  17. interface QueryExtendableInterface {
  18. /**
  19. * Enhance this object by wrapping it in an extender object.
  20. *
  21. * @param $extender_name
  22. * The base name of the extending class. The base name will be checked
  23. * against the current database connection to allow driver-specific subclasses
  24. * as well, using the same logic as the query objects themselves. For example,
  25. * PagerDefault_mysql is the MySQL-specific override for PagerDefault.
  26. * @return QueryExtendableInterface
  27. * The extender object, which now contains a reference to this object.
  28. */
  29. public function extend($extender_name);
  30. }
  31. /**
  32. * Interface definition for a Select Query object.
  33. */
  34. interface SelectQueryInterface extends QueryConditionInterface, QueryAlterableInterface, QueryExtendableInterface, QueryPlaceholderInterface {
  35. /* Alter accessors to expose the query data to alter hooks. */
  36. /**
  37. * Returns a reference to the fields array for this query.
  38. *
  39. * Because this method returns by reference, alter hooks may edit the fields
  40. * array directly to make their changes. If just adding fields, however, the
  41. * use of addField() is preferred.
  42. *
  43. * Note that this method must be called by reference as well:
  44. *
  45. * @code
  46. * $fields =& $query->getFields();
  47. * @endcode
  48. *
  49. * @return
  50. * A reference to the fields array structure.
  51. */
  52. public function &getFields();
  53. /**
  54. * Returns a reference to the expressions array for this query.
  55. *
  56. * Because this method returns by reference, alter hooks may edit the expressions
  57. * array directly to make their changes. If just adding expressions, however, the
  58. * use of addExpression() is preferred.
  59. *
  60. * Note that this method must be called by reference as well:
  61. *
  62. * @code
  63. * $fields =& $query->getExpressions();
  64. * @endcode
  65. *
  66. * @return
  67. * A reference to the expression array structure.
  68. */
  69. public function &getExpressions();
  70. /**
  71. * Returns a reference to the order by array for this query.
  72. *
  73. * Because this method returns by reference, alter hooks may edit the order-by
  74. * array directly to make their changes. If just adding additional ordering
  75. * fields, however, the use of orderBy() is preferred.
  76. *
  77. * Note that this method must be called by reference as well:
  78. *
  79. * @code
  80. * $fields =& $query->getOrderBy();
  81. * @endcode
  82. *
  83. * @return
  84. * A reference to the expression array structure.
  85. */
  86. public function &getOrderBy();
  87. /**
  88. * Returns a reference to the group-by array for this query.
  89. *
  90. * Because this method returns by reference, alter hooks may edit the group-by
  91. * array directly to make their changes. If just adding additional grouping
  92. * fields, however, the use of groupBy() is preferred.
  93. *
  94. * Note that this method must be called by reference as well:
  95. *
  96. * @code
  97. * $fields =& $query->getGroupBy();
  98. * @endcode
  99. *
  100. * @return
  101. * A reference to the group-by array structure.
  102. */
  103. public function &getGroupBy();
  104. /**
  105. * Returns a reference to the tables array for this query.
  106. *
  107. * Because this method returns by reference, alter hooks may edit the tables
  108. * array directly to make their changes. If just adding tables, however, the
  109. * use of the join() methods is preferred.
  110. *
  111. * Note that this method must be called by reference as well:
  112. *
  113. * @code
  114. * $fields =& $query->getTables();
  115. * @endcode
  116. *
  117. * @return
  118. * A reference to the tables array structure.
  119. */
  120. public function &getTables();
  121. /**
  122. * Returns a reference to the union queries for this query. This include
  123. * queries for UNION, UNION ALL, and UNION DISTINCT.
  124. *
  125. * Because this method returns by reference, alter hooks may edit the tables
  126. * array directly to make their changes. If just adding union queries,
  127. * however, the use of the union() method is preferred.
  128. *
  129. * Note that this method must be called by reference as well:
  130. *
  131. * @code
  132. * $fields =& $query->getUnion();
  133. * @endcode
  134. *
  135. * @return
  136. * A reference to the union query array structure.
  137. */
  138. public function &getUnion();
  139. /**
  140. * Compiles and returns an associative array of the arguments for this prepared statement.
  141. *
  142. * @param $queryPlaceholder
  143. * When collecting the arguments of a subquery, the main placeholder
  144. * object should be passed as this parameter.
  145. *
  146. * @return
  147. * An associative array of all placeholder arguments for this query.
  148. */
  149. public function getArguments(QueryPlaceholderInterface $queryPlaceholder = NULL);
  150. /* Query building operations */
  151. /**
  152. * Sets this query to be DISTINCT.
  153. *
  154. * @param $distinct
  155. * TRUE to flag this query DISTINCT, FALSE to disable it.
  156. * @return SelectQueryInterface
  157. * The called object.
  158. */
  159. public function distinct($distinct = TRUE);
  160. /**
  161. * Adds a field to the list to be SELECTed.
  162. *
  163. * @param $table_alias
  164. * The name of the table from which the field comes, as an alias. Generally
  165. * you will want to use the return value of join() here to ensure that it is
  166. * valid.
  167. * @param $field
  168. * The name of the field.
  169. * @param $alias
  170. * The alias for this field. If not specified, one will be generated
  171. * automatically based on the $table_alias and $field. The alias will be
  172. * checked for uniqueness, so the requested alias may not be the alias
  173. * that is assigned in all cases.
  174. * @return
  175. * The unique alias that was assigned for this field.
  176. */
  177. public function addField($table_alias, $field, $alias = NULL);
  178. /**
  179. * Add multiple fields from the same table to be SELECTed.
  180. *
  181. * This method does not return the aliases set for the passed fields. In the
  182. * majority of cases that is not a problem, as the alias will be the field
  183. * name. However, if you do need to know the alias you can call getFields()
  184. * and examine the result to determine what alias was created. Alternatively,
  185. * simply use addField() for the few fields you care about and this method for
  186. * the rest.
  187. *
  188. * @param $table_alias
  189. * The name of the table from which the field comes, as an alias. Generally
  190. * you will want to use the return value of join() here to ensure that it is
  191. * valid.
  192. * @param $fields
  193. * An indexed array of fields present in the specified table that should be
  194. * included in this query. If not specified, $table_alias.* will be generated
  195. * without any aliases.
  196. * @return SelectQueryInterface
  197. * The called object.
  198. */
  199. public function fields($table_alias, array $fields = array());
  200. /**
  201. * Adds an expression to the list of "fields" to be SELECTed.
  202. *
  203. * An expression can be any arbitrary string that is valid SQL. That includes
  204. * various functions, which may in some cases be database-dependent. This
  205. * method makes no effort to correct for database-specific functions.
  206. *
  207. * @param $expression
  208. * The expression string. May contain placeholders.
  209. * @param $alias
  210. * The alias for this expression. If not specified, one will be generated
  211. * automatically in the form "expression_#". The alias will be checked for
  212. * uniqueness, so the requested alias may not be the alias that is assigned
  213. * in all cases.
  214. * @param $arguments
  215. * Any placeholder arguments needed for this expression.
  216. * @return
  217. * The unique alias that was assigned for this expression.
  218. */
  219. public function addExpression($expression, $alias = NULL, $arguments = array());
  220. /**
  221. * Default Join against another table in the database.
  222. *
  223. * This method is a convenience method for innerJoin().
  224. *
  225. * @param $table
  226. * The table against which to join.
  227. * @param $alias
  228. * The alias for the table. In most cases this should be the first letter
  229. * of the table, or the first letter of each "word" in the table.
  230. * @param $condition
  231. * The condition on which to join this table. If the join requires values,
  232. * this clause should use a named placeholder and the value or values to
  233. * insert should be passed in the 4th parameter. For the first table joined
  234. * on a query, this value is ignored as the first table is taken as the base
  235. * table. The token %alias can be used in this string to be replaced with
  236. * the actual alias. This is useful when $alias is modified by the database
  237. * system, for example, when joining the same table more than once.
  238. * @param $arguments
  239. * An array of arguments to replace into the $condition of this join.
  240. * @return
  241. * The unique alias that was assigned for this table.
  242. */
  243. public function join($table, $alias = NULL, $condition = NULL, $arguments = array());
  244. /**
  245. * Inner Join against another table in the database.
  246. *
  247. * @param $table
  248. * The table against which to join.
  249. * @param $alias
  250. * The alias for the table. In most cases this should be the first letter
  251. * of the table, or the first letter of each "word" in the table.
  252. * @param $condition
  253. * The condition on which to join this table. If the join requires values,
  254. * this clause should use a named placeholder and the value or values to
  255. * insert should be passed in the 4th parameter. For the first table joined
  256. * on a query, this value is ignored as the first table is taken as the base
  257. * table. The token %alias can be used in this string to be replaced with
  258. * the actual alias. This is useful when $alias is modified by the database
  259. * system, for example, when joining the same table more than once.
  260. * @param $arguments
  261. * An array of arguments to replace into the $condition of this join.
  262. * @return
  263. * The unique alias that was assigned for this table.
  264. */
  265. public function innerJoin($table, $alias = NULL, $condition = NULL, $arguments = array());
  266. /**
  267. * Left Outer Join against another table in the database.
  268. *
  269. * @param $table
  270. * The table against which to join.
  271. * @param $alias
  272. * The alias for the table. In most cases this should be the first letter
  273. * of the table, or the first letter of each "word" in the table.
  274. * @param $condition
  275. * The condition on which to join this table. If the join requires values,
  276. * this clause should use a named placeholder and the value or values to
  277. * insert should be passed in the 4th parameter. For the first table joined
  278. * on a query, this value is ignored as the first table is taken as the base
  279. * table. The token %alias can be used in this string to be replaced with
  280. * the actual alias. This is useful when $alias is modified by the database
  281. * system, for example, when joining the same table more than once.
  282. * @param $arguments
  283. * An array of arguments to replace into the $condition of this join.
  284. * @return
  285. * The unique alias that was assigned for this table.
  286. */
  287. public function leftJoin($table, $alias = NULL, $condition = NULL, $arguments = array());
  288. /**
  289. * Right Outer Join against another table in the database.
  290. *
  291. * @param $table
  292. * The table against which to join.
  293. * @param $alias
  294. * The alias for the table. In most cases this should be the first letter
  295. * of the table, or the first letter of each "word" in the table.
  296. * @param $condition
  297. * The condition on which to join this table. If the join requires values,
  298. * this clause should use a named placeholder and the value or values to
  299. * insert should be passed in the 4th parameter. For the first table joined
  300. * on a query, this value is ignored as the first table is taken as the base
  301. * table. The token %alias can be used in this string to be replaced with
  302. * the actual alias. This is useful when $alias is modified by the database
  303. * system, for example, when joining the same table more than once.
  304. * @param $arguments
  305. * An array of arguments to replace into the $condition of this join.
  306. * @return
  307. * The unique alias that was assigned for this table.
  308. */
  309. public function rightJoin($table, $alias = NULL, $condition = NULL, $arguments = array());
  310. /**
  311. * Join against another table in the database.
  312. *
  313. * This method does the "hard" work of queuing up a table to be joined against.
  314. * In some cases, that may include dipping into the Schema API to find the necessary
  315. * fields on which to join.
  316. *
  317. * @param $type
  318. * The type of join. Typically one one of INNER, LEFT OUTER, and RIGHT OUTER.
  319. * @param $table
  320. * The table against which to join. May be a string or another SelectQuery
  321. * object. If a query object is passed, it will be used as a subselect.
  322. * @param $alias
  323. * The alias for the table. In most cases this should be the first letter
  324. * of the table, or the first letter of each "word" in the table. If omitted,
  325. * one will be dynamically generated.
  326. * @param $condition
  327. * The condition on which to join this table. If the join requires values,
  328. * this clause should use a named placeholder and the value or values to
  329. * insert should be passed in the 4th parameter. For the first table joined
  330. * on a query, this value is ignored as the first table is taken as the base
  331. * table. The token %alias can be used in this string to be replaced with
  332. * the actual alias. This is useful when $alias is modified by the database
  333. * system, for example, when joining the same table more than once.
  334. * @param $arguments
  335. * An array of arguments to replace into the $condition of this join.
  336. * @return
  337. * The unique alias that was assigned for this table.
  338. */
  339. public function addJoin($type, $table, $alias = NULL, $condition = NULL, $arguments = array());
  340. /**
  341. * Orders the result set by a given field.
  342. *
  343. * If called multiple times, the query will order by each specified field in the
  344. * order this method is called.
  345. *
  346. * If the query uses DISTINCT or GROUP BY conditions, fields or expressions
  347. * that are used for the order must be selected to be compatible with some
  348. * databases like PostgreSQL. The PostgreSQL driver can handle simple cases
  349. * automatically but it is suggested to explicitly specify them. Additionally,
  350. * when ordering on an alias, the alias must be added before orderBy() is
  351. * called.
  352. *
  353. * @param $field
  354. * The field on which to order.
  355. * @param $direction
  356. * The direction to sort. Legal values are "ASC" and "DESC".
  357. * @return SelectQueryInterface
  358. * The called object.
  359. */
  360. public function orderBy($field, $direction = 'ASC');
  361. /**
  362. * Orders the result set by a random value.
  363. *
  364. * This may be stacked with other orderBy() calls. If so, the query will order
  365. * by each specified field, including this one, in the order called. Although
  366. * this method may be called multiple times on the same query, doing so
  367. * is not particularly useful.
  368. *
  369. * Note: The method used by most drivers may not scale to very large result
  370. * sets. If you need to work with extremely large data sets, you may create
  371. * your own database driver by subclassing off of an existing driver and
  372. * implementing your own randomization mechanism. See
  373. *
  374. * http://jan.kneschke.de/projects/mysql/order-by-rand/
  375. *
  376. * for an example of such an alternate sorting mechanism.
  377. *
  378. * @return SelectQueryInterface
  379. * The called object
  380. */
  381. public function orderRandom();
  382. /**
  383. * Restricts a query to a given range in the result set.
  384. *
  385. * If this method is called with no parameters, will remove any range
  386. * directives that have been set.
  387. *
  388. * @param $start
  389. * The first record from the result set to return. If NULL, removes any
  390. * range directives that are set.
  391. * @param $length
  392. * The number of records to return from the result set.
  393. * @return SelectQueryInterface
  394. * The called object.
  395. */
  396. public function range($start = NULL, $length = NULL);
  397. /**
  398. * Add another Select query to UNION to this one.
  399. *
  400. * Union queries consist of two or more queries whose
  401. * results are effectively concatenated together. Queries
  402. * will be UNIONed in the order they are specified, with
  403. * this object's query coming first. Duplicate columns will
  404. * be discarded. All forms of UNION are supported, using
  405. * the second '$type' argument.
  406. *
  407. * Note: All queries UNIONed together must have the same
  408. * field structure, in the same order. It is up to the
  409. * caller to ensure that they match properly. If they do
  410. * not, an SQL syntax error will result.
  411. *
  412. * @param $query
  413. * The query to UNION to this query.
  414. * @param $type
  415. * The type of UNION to add to the query. Defaults to plain
  416. * UNION.
  417. * @return SelectQueryInterface
  418. * The called object.
  419. */
  420. public function union(SelectQueryInterface $query, $type = '');
  421. /**
  422. * Groups the result set by the specified field.
  423. *
  424. * @param $field
  425. * The field on which to group. This should be the field as aliased.
  426. * @return SelectQueryInterface
  427. * The called object.
  428. */
  429. public function groupBy($field);
  430. /**
  431. * Get the equivalent COUNT query of this query as a new query object.
  432. *
  433. * @return SelectQueryInterface
  434. * A new SelectQuery object with no fields or expressions besides COUNT(*).
  435. */
  436. public function countQuery();
  437. /**
  438. * Indicates if preExecute() has already been called on that object.
  439. *
  440. * @return
  441. * TRUE is this query has already been prepared, FALSE otherwise.
  442. */
  443. public function isPrepared();
  444. /**
  445. * Generic preparation and validation for a SELECT query.
  446. *
  447. * @return
  448. * TRUE if the validation was successful, FALSE if not.
  449. */
  450. public function preExecute(SelectQueryInterface $query = NULL);
  451. /**
  452. * Helper function to build most common HAVING conditional clauses.
  453. *
  454. * This method can take a variable number of parameters. If called with two
  455. * parameters, they are taken as $field and $value with $operator having a value
  456. * of IN if $value is an array and = otherwise.
  457. *
  458. * @param $field
  459. * The name of the field to check. If you would like to add a more complex
  460. * condition involving operators or functions, use having().
  461. * @param $value
  462. * The value to test the field against. In most cases, this is a scalar. For more
  463. * complex options, it is an array. The meaning of each element in the array is
  464. * dependent on the $operator.
  465. * @param $operator
  466. * The comparison operator, such as =, <, or >=. It also accepts more complex
  467. * options such as IN, LIKE, or BETWEEN. Defaults to IN if $value is an array
  468. * = otherwise.
  469. * @return QueryConditionInterface
  470. * The called object.
  471. */
  472. public function havingCondition($field, $value = NULL, $operator = NULL);
  473. /**
  474. * Clone magic method.
  475. *
  476. * Select queries have dependent objects that must be deep-cloned. The
  477. * connection object itself, however, should not be cloned as that would
  478. * duplicate the connection itself.
  479. */
  480. public function __clone();
  481. /**
  482. * Add FOR UPDATE to the query.
  483. *
  484. * FOR UPDATE prevents the rows retrieved by the SELECT statement from being
  485. * modified or deleted by other transactions until the current transaction
  486. * ends. Other transactions that attempt UPDATE, DELETE, or SELECT FOR UPDATE
  487. * of these rows will be blocked until the current transaction ends.
  488. *
  489. * @param $set
  490. * IF TRUE, FOR UPDATE will be added to the query, if FALSE then it won't.
  491. *
  492. * @return QueryConditionInterface
  493. * The called object.
  494. */
  495. public function forUpdate($set = TRUE);
  496. }
  497. /**
  498. * The base extender class for Select queries.
  499. */
  500. class SelectQueryExtender implements SelectQueryInterface {
  501. /**
  502. * The SelectQuery object we are extending/decorating.
  503. *
  504. * @var SelectQueryInterface
  505. */
  506. protected $query;
  507. /**
  508. * The connection object on which to run this query.
  509. *
  510. * @var DatabaseConnection
  511. */
  512. protected $connection;
  513. /**
  514. * The placeholder counter.
  515. */
  516. protected $placeholder = 0;
  517. public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) {
  518. $this->query = $query;
  519. $this->connection = $connection;
  520. }
  521. /* Implementations of QueryPlaceholderInterface. */
  522. public function nextPlaceholder() {
  523. return $this->placeholder++;
  524. }
  525. /* Implementations of QueryAlterableInterface. */
  526. public function addTag($tag) {
  527. $this->query->addTag($tag);
  528. return $this;
  529. }
  530. public function hasTag($tag) {
  531. return $this->query->hasTag($tag);
  532. }
  533. public function hasAllTags() {
  534. return call_user_func_array(array($this->query, 'hasAllTags'), func_get_args());
  535. }
  536. public function hasAnyTag() {
  537. return call_user_func_array(array($this->query, 'hasAnyTags'), func_get_args());
  538. }
  539. public function addMetaData($key, $object) {
  540. $this->query->addMetaData($key, $object);
  541. return $this;
  542. }
  543. public function getMetaData($key) {
  544. return $this->query->getMetaData($key);
  545. }
  546. /* Implementations of QueryConditionInterface for the WHERE clause. */
  547. public function condition($field, $value = NULL, $operator = NULL) {
  548. $this->query->condition($field, $value, $operator);
  549. return $this;
  550. }
  551. public function &conditions() {
  552. return $this->query->conditions();
  553. }
  554. public function arguments() {
  555. return $this->query->arguments();
  556. }
  557. public function where($snippet, $args = array()) {
  558. $this->query->where($snippet, $args);
  559. return $this;
  560. }
  561. public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
  562. return $this->condition->compile($connection, isset($queryPlaceholder) ? $queryPlaceholder : $this);
  563. }
  564. /* Implementations of QueryConditionInterface for the HAVING clause. */
  565. public function havingCondition($field, $value = NULL, $operator = '=') {
  566. $this->query->condition($field, $value, $operator, $num_args);
  567. return $this;
  568. }
  569. public function &havingConditions() {
  570. return $this->having->conditions();
  571. }
  572. public function havingArguments() {
  573. return $this->having->arguments();
  574. }
  575. public function having($snippet, $args = array()) {
  576. $this->query->having($snippet, $args);
  577. return $this;
  578. }
  579. public function havingCompile(DatabaseConnection $connection) {
  580. return $this->query->havingCompile($connection);
  581. }
  582. /* Implementations of QueryExtendableInterface. */
  583. public function extend($extender_name) {
  584. // The extender can be anywhere so this needs to go to the registry, which
  585. // is surely loaded by now.
  586. $class = $this->connection->getDriverClass($extender_name, array(), TRUE);
  587. return new $class($this, $this->connection);
  588. }
  589. /* Alter accessors to expose the query data to alter hooks. */
  590. public function &getFields() {
  591. return $this->query->getFields();
  592. }
  593. public function &getExpressions() {
  594. return $this->query->getExpressions();
  595. }
  596. public function &getOrderBy() {
  597. return $this->query->getOrderBy();
  598. }
  599. public function &getGroupBy() {
  600. return $this->query->getGroupBy();
  601. }
  602. public function &getTables() {
  603. return $this->query->getTables();
  604. }
  605. public function &getUnion() {
  606. return $this->query->getUnion();
  607. }
  608. public function getArguments(QueryPlaceholderInterface $queryPlaceholder = NULL) {
  609. return $this->query->getArguments($queryPlaceholder);
  610. }
  611. public function isPrepared() {
  612. return $this->query->isPrepared();
  613. }
  614. public function preExecute(SelectQueryInterface $query = NULL) {
  615. // If no query object is passed in, use $this.
  616. if (!isset($query)) {
  617. $query = $this;
  618. }
  619. return $this->query->preExecute($query);
  620. }
  621. public function execute() {
  622. // By calling preExecute() here, we force it to preprocess the extender
  623. // object rather than just the base query object. That means
  624. // hook_query_alter() gets access to the extended object.
  625. if (!$this->preExecute($this)) {
  626. return NULL;
  627. }
  628. return $this->query->execute();
  629. }
  630. public function distinct($distinct = TRUE) {
  631. $this->query->distinct($distinct);
  632. return $this;
  633. }
  634. public function addField($table_alias, $field, $alias = NULL) {
  635. return $this->query->addField($table_alias, $field, $alias);
  636. }
  637. public function fields($table_alias, array $fields = array()) {
  638. $this->query->fields($table_alias, $fields);
  639. return $this;
  640. }
  641. public function addExpression($expression, $alias = NULL, $arguments = array()) {
  642. return $this->query->addExpression($expression, $alias, $arguments);
  643. }
  644. public function join($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  645. return $this->query->join($table, $alias, $condition, $arguments);
  646. }
  647. public function innerJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  648. return $this->query->innerJoin($table, $alias, $condition, $arguments);
  649. }
  650. public function leftJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  651. return $this->query->leftJoin($table, $alias, $condition, $arguments);
  652. }
  653. public function rightJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  654. return $this->query->rightJoin($table, $alias, $condition, $arguments);
  655. }
  656. public function addJoin($type, $table, $alias = NULL, $condition = NULL, $arguments = array()) {
  657. return $this->query->addJoin($type, $table, $alias, $condition, $arguments);
  658. }
  659. public function orderBy($field, $direction = 'ASC') {
  660. $this->query->orderBy($field, $direction);
  661. return $this;
  662. }
  663. public function orderRandom() {
  664. $this->query->orderRandom();
  665. return $this;
  666. }
  667. public function range($start = NULL, $length = NULL) {
  668. $this->query->range($start, $length);
  669. return $this;
  670. }
  671. public function union(SelectQueryInterface $query, $type = '') {
  672. $this->query->union($query, $type);
  673. return $this;
  674. }
  675. public function groupBy($field) {
  676. $this->query->groupBy($field);
  677. return $this;
  678. }
  679. public function forUpdate($set = TRUE) {
  680. $this->query->forUpdate($set);
  681. return $this;
  682. }
  683. public function countQuery() {
  684. // Create our new query object that we will mutate into a count query.
  685. $count = clone($this);
  686. // Zero-out existing fields and expressions.
  687. $fields =& $count->getFields();
  688. $fields = array();
  689. $expressions =& $count->getExpressions();
  690. $expressions = array();
  691. // Also remove 'all_fields' statements, which are expanded into tablename.*
  692. // when the query is executed.
  693. $tables = &$count->getTables();
  694. foreach ($tables as $alias => &$table) {
  695. unset($table['all_fields']);
  696. }
  697. // Ordering a count query is a waste of cycles, and breaks on some
  698. // databases anyway.
  699. $orders = &$count->getOrderBy();
  700. $orders = array();
  701. // COUNT() is an expression, so we add that back in.
  702. $count->addExpression('COUNT(*)');
  703. return $count;
  704. }
  705. function isNull($field) {
  706. $this->query->isNull($field);
  707. return $this;
  708. }
  709. function isNotNull($field) {
  710. $this->query->isNotNull($field);
  711. return $this;
  712. }
  713. public function exists(SelectQueryInterface $select) {
  714. $this->query->exists($select);
  715. return $this;
  716. }
  717. public function notExists(SelectQueryInterface $select) {
  718. $this->query->notExists($select);
  719. return $this;
  720. }
  721. public function __toString() {
  722. return (string) $this->query;
  723. }
  724. public function __clone() {
  725. // We need to deep-clone the query we're wrapping, which in turn may
  726. // deep-clone other objects. Exciting!
  727. $this->query = clone($this->query);
  728. }
  729. /**
  730. * Magic override for undefined methods.
  731. *
  732. * If one extender extends another extender, then methods in the inner extender
  733. * will not be exposed on the outer extender. That's because we cannot know
  734. * in advance what those methods will be, so we cannot provide wrapping
  735. * implementations as we do above. Instead, we use this slower catch-all method
  736. * to handle any additional methods.
  737. */
  738. public function __call($method, $args) {
  739. $return = call_user_func_array(array($this->query, $method), $args);
  740. // Some methods will return the called object as part of a fluent interface.
  741. // Others will return some useful value. If it's a value, then the caller
  742. // probably wants that value. If it's the called object, then we instead
  743. // return this object. That way we don't "lose" an extender layer when
  744. // chaining methods together.
  745. if ($return instanceof SelectQueryInterface) {
  746. return $this;
  747. }
  748. else {
  749. return $return;
  750. }
  751. }
  752. }
  753. /**
  754. * Query builder for SELECT statements.
  755. */
  756. class SelectQuery extends Query implements SelectQueryInterface {
  757. /**
  758. * The fields to SELECT.
  759. *
  760. * @var array
  761. */
  762. protected $fields = array();
  763. /**
  764. * The expressions to SELECT as virtual fields.
  765. *
  766. * @var array
  767. */
  768. protected $expressions = array();
  769. /**
  770. * The tables against which to JOIN.
  771. *
  772. * This property is a nested array. Each entry is an array representing
  773. * a single table against which to join. The structure of each entry is:
  774. *
  775. * array(
  776. * 'type' => $join_type (one of INNER, LEFT OUTER, RIGHT OUTER),
  777. * 'table' => $table,
  778. * 'alias' => $alias_of_the_table,
  779. * 'condition' => $condition_clause_on_which_to_join,
  780. * 'arguments' => $array_of_arguments_for_placeholders_in_the condition.
  781. * 'all_fields' => TRUE to SELECT $alias.*, FALSE or NULL otherwise.
  782. * )
  783. *
  784. * If $table is a string, it is taken as the name of a table. If it is
  785. * a SelectQuery object, it is taken as a subquery.
  786. *
  787. * @var array
  788. */
  789. protected $tables = array();
  790. /**
  791. * The fields by which to order this query.
  792. *
  793. * This is an associative array. The keys are the fields to order, and the value
  794. * is the direction to order, either ASC or DESC.
  795. *
  796. * @var array
  797. */
  798. protected $order = array();
  799. /**
  800. * The fields by which to group.
  801. *
  802. * @var array
  803. */
  804. protected $group = array();
  805. /**
  806. * The conditional object for the WHERE clause.
  807. *
  808. * @var DatabaseCondition
  809. */
  810. protected $where;
  811. /**
  812. * The conditional object for the HAVING clause.
  813. *
  814. * @var DatabaseCondition
  815. */
  816. protected $having;
  817. /**
  818. * Whether or not this query should be DISTINCT
  819. *
  820. * @var boolean
  821. */
  822. protected $distinct = FALSE;
  823. /**
  824. * The range limiters for this query.
  825. *
  826. * @var array
  827. */
  828. protected $range;
  829. /**
  830. * An array whose elements specify a query to UNION, and the UNION type. The
  831. * 'type' key may be '', 'ALL', or 'DISTINCT' to represent a 'UNION',
  832. * 'UNION ALL', or 'UNION DISTINCT' statement, respectively.
  833. *
  834. * All entries in this array will be applied from front to back, with the
  835. * first query to union on the right of the original query, the second union
  836. * to the right of the first, etc.
  837. *
  838. * @var array
  839. */
  840. protected $union = array();
  841. /**
  842. * Indicates if preExecute() has already been called.
  843. * @var boolean
  844. */
  845. protected $prepared = FALSE;
  846. /**
  847. * The FOR UPDATE status
  848. */
  849. protected $forUpdate = FALSE;
  850. public function __construct($table, $alias = NULL, DatabaseConnection $connection, $options = array()) {
  851. $options['return'] = Database::RETURN_STATEMENT;
  852. parent::__construct($connection, $options);
  853. $this->where = new DatabaseCondition('AND');
  854. $this->having = new DatabaseCondition('AND');
  855. $this->addJoin(NULL, $table, $alias);
  856. }
  857. /* Implementations of QueryAlterableInterface. */
  858. public function addTag($tag) {
  859. $this->alterTags[$tag] = 1;
  860. return $this;
  861. }
  862. public function hasTag($tag) {
  863. return isset($this->alterTags[$tag]);
  864. }
  865. public function hasAllTags() {
  866. return !(boolean)array_diff(func_get_args(), array_keys($this->alterTags));
  867. }
  868. public function hasAnyTag() {
  869. return (boolean)array_intersect(func_get_args(), array_keys($this->alterTags));
  870. }
  871. public function addMetaData($key, $object) {
  872. $this->alterMetaData[$key] = $object;
  873. return $this;
  874. }
  875. public function getMetaData($key) {
  876. return isset($this->alterMetaData[$key]) ? $this->alterMetaData[$key] : NULL;
  877. }
  878. /* Implementations of QueryConditionInterface for the WHERE clause. */
  879. public function condition($field, $value = NULL, $operator = NULL) {
  880. $this->where->condition($field, $value, $operator);
  881. return $this;
  882. }
  883. public function &conditions() {
  884. return $this->where->conditions();
  885. }
  886. public function arguments() {
  887. return $this->where->arguments();
  888. }
  889. public function where($snippet, $args = array()) {
  890. $this->where->where($snippet, $args);
  891. return $this;
  892. }
  893. public function isNull($field) {
  894. $this->where->isNull($field);
  895. return $this;
  896. }
  897. public function isNotNull($field) {
  898. $this->where->isNotNull($field);
  899. return $this;
  900. }
  901. public function exists(SelectQueryInterface $select) {
  902. $this->where->exists($select);
  903. return $this;
  904. }
  905. public function notExists(SelectQueryInterface $select) {
  906. $this->where->notExists($select);
  907. return $this;
  908. }
  909. public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
  910. return $this->where->compile($connection, isset($queryPlaceholder) ? $queryPlaceholder : $this);
  911. }
  912. /* Implementations of QueryConditionInterface for the HAVING clause. */
  913. public function havingCondition($field, $value = NULL, $operator = NULL) {
  914. $this->having->condition($field, $value, $operator);
  915. return $this;
  916. }
  917. public function &havingConditions() {
  918. return $this->having->conditions();
  919. }
  920. public function havingArguments() {
  921. return $this->having->arguments();
  922. }
  923. public function having($snippet, $args = array()) {
  924. $this->having->where($snippet, $args);
  925. return $this;
  926. }
  927. public function havingCompile(DatabaseConnection $connection) {
  928. return $this->having->compile($connection, $this);
  929. }
  930. /* Implementations of QueryExtendableInterface. */
  931. public function extend($extender_name) {
  932. $override_class = $extender_name . '_' . $this->connection->driver();
  933. if (class_exists($override_class)) {
  934. $extender_name = $override_class;
  935. }
  936. return new $extender_name($this, $this->connection);
  937. }
  938. public function havingIsNull($field) {
  939. $this->having->isNull($field);
  940. return $this;
  941. }
  942. public function havingIsNotNull($field) {
  943. $this->having->isNotNull($field);
  944. return $this;
  945. }
  946. public function havingExists(SelectQueryInterface $select) {
  947. $this->having->exists($select);
  948. return $this;
  949. }
  950. public function havingNotExists(SelectQueryInterface $select) {
  951. $this->having->notExists($select);
  952. return $this;
  953. }
  954. public function forUpdate($set = TRUE) {
  955. if (isset($set)) {
  956. $this->forUpdate = $set;
  957. }
  958. return $this;
  959. }
  960. /* Alter accessors to expose the query data to alter hooks. */
  961. public function &getFields() {
  962. return $this->fields;
  963. }
  964. public function &getExpressions() {
  965. return $this->expressions;
  966. }
  967. public function &getOrderBy() {
  968. return $this->order;
  969. }
  970. public function &getGroupBy() {
  971. return $this->group;
  972. }
  973. public function &getTables() {
  974. return $this->tables;
  975. }
  976. public function &getUnion() {
  977. return $this->union;
  978. }
  979. public function getArguments(QueryPlaceholderInterface $queryPlaceholder = NULL) {
  980. if (!isset($queryPlaceholder)) {
  981. $queryPlaceholder = $this;
  982. }
  983. $this->where->compile($this->connection, $queryPlaceholder);
  984. $this->having->compile($this->connection, $queryPlaceholder);
  985. $args = $this->where->arguments() + $this->having->arguments();
  986. foreach ($this->tables as $table) {
  987. if ($table['arguments']) {
  988. $args += $table['arguments'];
  989. }
  990. // If this table is a subquery, grab its arguments recursively.
  991. if ($table['table'] instanceof SelectQueryInterface) {
  992. $args += $table['table']->getArguments($queryPlaceholder);
  993. }
  994. }
  995. foreach ($this->expressions as $expression) {
  996. if ($expression['arguments']) {
  997. $args += $expression['arguments'];
  998. }
  999. }
  1000. // If there are any dependent queries to UNION,
  1001. // incorporate their arguments recursively.
  1002. foreach ($this->union as $union) {
  1003. $args += $union['query']->getArguments($queryPlaceholder);
  1004. }
  1005. return $args;
  1006. }
  1007. /**
  1008. * Indicates if preExecute() has already been called on that object.
  1009. */
  1010. public function isPrepared() {
  1011. return $this->prepared;
  1012. }
  1013. /**
  1014. * Generic preparation and validation for a SELECT query.
  1015. *
  1016. * @return
  1017. * TRUE if the validation was successful, FALSE if not.
  1018. */
  1019. public function preExecute(SelectQueryInterface $query = NULL) {
  1020. // If no query object is passed in, use $this.
  1021. if (!isset($query)) {
  1022. $query = $this;
  1023. }
  1024. // Only execute this once.
  1025. if ($query->isPrepared()) {
  1026. return TRUE;
  1027. }
  1028. // Modules may alter all queries or only those having a particular tag.
  1029. if (isset($this->alterTags)) {
  1030. $hooks = array('query');
  1031. foreach ($this->alterTags as $tag => $value) {
  1032. $hooks[] = 'query_' . $tag;
  1033. }
  1034. drupal_alter($hooks, $query);
  1035. }
  1036. $this->prepared = TRUE;
  1037. // Now also prepare any sub-queries.
  1038. foreach ($this->tables as $table) {
  1039. if ($table['table'] instanceof SelectQueryInterface) {
  1040. $table['table']->preExecute();
  1041. }
  1042. }
  1043. foreach ($this->union as $union) {
  1044. $union['query']->preExecute();
  1045. }
  1046. return $this->prepared;
  1047. }
  1048. public function execute() {
  1049. // If validation fails, simply return NULL.
  1050. // Note that validation routines in preExecute() may throw exceptions instead.
  1051. if (!$this->preExecute()) {
  1052. return NULL;
  1053. }
  1054. $args = $this->getArguments();
  1055. return $this->connection->query((string) $this, $args, $this->queryOptions);
  1056. }
  1057. public function distinct($distinct = TRUE) {
  1058. $this->distinct = $distinct;
  1059. return $this;
  1060. }
  1061. public function addField($table_alias, $field, $alias = NULL) {
  1062. // If no alias is specified, first try the field name itself.
  1063. if (empty($alias)) {
  1064. $alias = $field;
  1065. }
  1066. // If that's already in use, try the table name and field name.
  1067. if (!empty($this->fields[$alias])) {
  1068. $alias = $table_alias . '_' . $field;
  1069. }
  1070. // If that is already used, just add a counter until we find an unused alias.
  1071. $alias_candidate = $alias;
  1072. $count = 2;
  1073. while (!empty($this->fields[$alias_candidate])) {
  1074. $alias_candidate = $alias . '_' . $count++;
  1075. }
  1076. $alias = $alias_candidate;
  1077. $this->fields[$alias] = array(
  1078. 'field' => $field,
  1079. 'table' => $table_alias,
  1080. 'alias' => $alias,
  1081. );
  1082. return $alias;
  1083. }
  1084. public function fields($table_alias, array $fields = array()) {
  1085. if ($fields) {
  1086. foreach ($fields as $field) {
  1087. // We don't care what alias was assigned.
  1088. $this->addField($table_alias, $field);
  1089. }
  1090. }
  1091. else {
  1092. // We want all fields from this table.
  1093. $this->tables[$table_alias]['all_fields'] = TRUE;
  1094. }
  1095. return $this;
  1096. }
  1097. public function addExpression($expression, $alias = NULL, $arguments = array()) {
  1098. if (empty($alias)) {
  1099. $alias = 'expression';
  1100. }
  1101. $alias_candidate = $alias;
  1102. $count = 2;
  1103. while (!empty($this->expressions[$alias_candidate])) {
  1104. $alias_candidate = $alias . '_' . $count++;
  1105. }
  1106. $alias = $alias_candidate;
  1107. $this->expressions[$alias] = array(
  1108. 'expression' => $expression,
  1109. 'alias' => $alias,
  1110. 'arguments' => $arguments,
  1111. );
  1112. return $alias;
  1113. }
  1114. public function join($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  1115. return $this->addJoin('INNER', $table, $alias, $condition, $arguments);
  1116. }
  1117. public function innerJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  1118. return $this->addJoin('INNER', $table, $alias, $condition, $arguments);
  1119. }
  1120. public function leftJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  1121. return $this->addJoin('LEFT OUTER', $table, $alias, $condition, $arguments);
  1122. }
  1123. public function rightJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  1124. return $this->addJoin('RIGHT OUTER', $table, $alias, $condition, $arguments);
  1125. }
  1126. public function addJoin($type, $table, $alias = NULL, $condition = NULL, $arguments = array()) {
  1127. if (empty($alias)) {
  1128. if ($table instanceof SelectQueryInterface) {
  1129. $alias = 'subquery';
  1130. }
  1131. else {
  1132. $alias = $table;
  1133. }
  1134. }
  1135. $alias_candidate = $alias;
  1136. $count = 2;
  1137. while (!empty($this->tables[$alias_candidate])) {
  1138. $alias_candidate = $alias . '_' . $count++;
  1139. }
  1140. $alias = $alias_candidate;
  1141. if (is_string($condition)) {
  1142. $condition = str_replace('%alias', $alias, $condition);
  1143. }
  1144. $this->tables[$alias] = array(
  1145. 'join type' => $type,
  1146. 'table' => $table,
  1147. 'alias' => $alias,
  1148. 'condition' => $condition,
  1149. 'arguments' => $arguments,
  1150. );
  1151. return $alias;
  1152. }
  1153. public function orderBy($field, $direction = 'ASC') {
  1154. $this->order[$field] = $direction;
  1155. return $this;
  1156. }
  1157. public function orderRandom() {
  1158. $alias = $this->addExpression('RAND()', 'random_field');
  1159. $this->orderBy($alias);
  1160. return $this;
  1161. }
  1162. public function range($start = NULL, $length = NULL) {
  1163. $this->range = func_num_args() ? array('start' => $start, 'length' => $length) : array();
  1164. return $this;
  1165. }
  1166. public function union(SelectQueryInterface $query, $type = '') {
  1167. // Handle UNION aliasing.
  1168. switch ($type) {
  1169. // Fold UNION DISTINCT to UNION for better cross database support.
  1170. case 'DISTINCT':
  1171. case '':
  1172. $type = 'UNION';
  1173. break;
  1174. case 'ALL':
  1175. $type = 'UNION ALL';
  1176. default:
  1177. }
  1178. $this->union[] = array(
  1179. 'type' => $type,
  1180. 'query' => $query,
  1181. );
  1182. return $this;
  1183. }
  1184. public function groupBy($field) {
  1185. $this->group[$field] = $field;
  1186. return $this;
  1187. }
  1188. public function countQuery() {
  1189. // Create our new query object that we will mutate into a count query.
  1190. $count = clone($this);
  1191. $group_by = $count->getGroupBy();
  1192. if (!$count->distinct) {
  1193. // When not executing a distinct query, we can zero-out existing fields
  1194. // and expressions that are not used by a GROUP BY. Fields listed in
  1195. // the GROUP BY clause need to be present in the query.
  1196. $fields =& $count->getFields();
  1197. foreach (array_keys($fields) as $field) {
  1198. if (empty($group_by[$field])) {
  1199. unset($fields[$field]);
  1200. }
  1201. }
  1202. $expressions =& $count->getExpressions();
  1203. foreach (array_keys($expressions) as $field) {
  1204. if (empty($group_by[$field])) {
  1205. unset($expressions[$field]);
  1206. }
  1207. }
  1208. // Also remove 'all_fields' statements, which are expanded into tablename.*
  1209. // when the query is executed.
  1210. foreach ($count->tables as $alias => &$table) {
  1211. unset($table['all_fields']);
  1212. }
  1213. }
  1214. // If we've just removed all fields from the query, make sure there is at
  1215. // least one so that the query still runs.
  1216. $count->addExpression('1');
  1217. // Ordering a count query is a waste of cycles, and breaks on some
  1218. // databases anyway.
  1219. $orders = &$count->getOrderBy();
  1220. $orders = array();
  1221. if ($count->distinct && !empty($group_by)) {
  1222. // If the query is distinct and contains a GROUP BY, we need to remove the
  1223. // distinct because SQL99 does not support counting on distinct multiple fields.
  1224. $count->distinct = FALSE;
  1225. }
  1226. $query = $this->connection->select($count);
  1227. $query->addExpression('COUNT(*)');
  1228. return $query;
  1229. }
  1230. public function __toString() {
  1231. // Create a sanitized comment string to prepend to the query.
  1232. $comments = $this->connection->makeComment($this->comments);
  1233. // SELECT
  1234. $query = $comments . 'SELECT ';
  1235. if ($this->distinct) {
  1236. $query .= 'DISTINCT ';
  1237. }
  1238. // FIELDS and EXPRESSIONS
  1239. $fields = array();
  1240. foreach ($this->tables as $alias => $table) {
  1241. if (!empty($table['all_fields'])) {
  1242. $fields[] = $this->connection->escapeTable($alias) . '.*';
  1243. }
  1244. }
  1245. foreach ($this->fields as $alias => $field) {
  1246. // Always use the AS keyword for field aliases, as some
  1247. // databases require it (e.g., PostgreSQL).
  1248. $fields[] = (isset($field['table']) ? $this->connection->escapeTable($field['table']) . '.' : '') . $this->connection->escapeField($field['field']) . ' AS ' . $this->connection->escapeAlias($field['alias']);
  1249. }
  1250. foreach ($this->expressions as $alias => $expression) {
  1251. $fields[] = $expression['expression'] . ' AS ' . $this->connection->escapeAlias($expression['alias']);
  1252. }
  1253. $query .= implode(', ', $fields);
  1254. // FROM - We presume all queries have a FROM, as any query that doesn't won't need the query builder anyway.
  1255. $query .= "\nFROM ";
  1256. foreach ($this->tables as $alias => $table) {
  1257. $query .= "\n";
  1258. if (isset($table['join type'])) {
  1259. $query .= $table['join type'] . ' JOIN ';
  1260. }
  1261. // If the table is a subquery, compile it and integrate it into this query.
  1262. if ($table['table'] instanceof SelectQueryInterface) {
  1263. // Run preparation steps on this sub-query before converting to string.
  1264. $subquery = $table['table'];
  1265. $subquery->preExecute();
  1266. $table_string = '(' . (string) $subquery . ')';
  1267. }
  1268. else {
  1269. $table_string = '{' . $this->connection->escapeTable($table['table']) . '}';
  1270. }
  1271. // Don't use the AS keyword for table aliases, as some
  1272. // databases don't support it (e.g., Oracle).
  1273. $query .= $table_string . ' ' . $this->connection->escapeTable($table['alias']);
  1274. if (!empty($table['condition'])) {
  1275. $query .= ' ON ' . $table['condition'];
  1276. }
  1277. }
  1278. // WHERE
  1279. if (count($this->where)) {
  1280. // The following line will not generate placeholders correctly if there
  1281. // is a subquery. Fortunately, it is also called from getArguments() first
  1282. // so it's not a problem in practice... unless you try to call __toString()
  1283. // before calling getArguments(). That is a problem that we will have to
  1284. // fix in Drupal 8, because it requires more refactoring than we are
  1285. // able to do in Drupal 7.
  1286. // @todo Move away from __toString() For SelectQuery compilation at least.
  1287. $this->where->compile($this->connection, $this);
  1288. // There is an implicit string cast on $this->condition.
  1289. $query .= "\nWHERE " . $this->where;
  1290. }
  1291. // GROUP BY
  1292. if ($this->group) {
  1293. $query .= "\nGROUP BY " . implode(', ', $this->group);
  1294. }
  1295. // HAVING
  1296. if (count($this->having)) {
  1297. $this->having->compile($this->connection, $this);
  1298. // There is an implicit string cast on $this->having.
  1299. $query .= "\nHAVING " . $this->having;
  1300. }
  1301. // ORDER BY
  1302. if ($this->order) {
  1303. $query .= "\nORDER BY ";
  1304. $fields = array();
  1305. foreach ($this->order as $field => $direction) {
  1306. $fields[] = $field . ' ' . $direction;
  1307. }
  1308. $query .= implode(', ', $fields);
  1309. }
  1310. // RANGE
  1311. // There is no universal SQL standard for handling range or limit clauses.
  1312. // Fortunately, all core-supported databases use the same range syntax.
  1313. // Databases that need a different syntax can override this method and
  1314. // do whatever alternate logic they need to.
  1315. if (!empty($this->range)) {
  1316. $query .= "\nLIMIT " . (int) $this->range['length'] . " OFFSET " . (int) $this->range['start'];
  1317. }
  1318. // UNION is a little odd, as the select queries to combine are passed into
  1319. // this query, but syntactically they all end up on the same level.
  1320. if ($this->union) {
  1321. foreach ($this->union as $union) {
  1322. $query .= ' ' . $union['type'] . ' ' . (string) $union['query'];
  1323. }
  1324. }
  1325. if ($this->forUpdate) {
  1326. $query .= ' FOR UPDATE';
  1327. }
  1328. return $query;
  1329. }
  1330. public function __clone() {
  1331. // On cloning, also clone the dependent objects. However, we do not
  1332. // want to clone the database connection object as that would duplicate the
  1333. // connection itself.
  1334. $this->where = clone($this->where);
  1335. $this->having = clone($this->having);
  1336. foreach ($this->union as $key => $aggregate) {
  1337. $this->union[$key]['query'] = clone($aggregate['query']);
  1338. }
  1339. }
  1340. }
  1341. /**
  1342. * @} End of "ingroup database".
  1343. */