SelectInterface.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?php
  2. namespace Drupal\Core\Database\Query;
  3. use Drupal\Core\Database\Connection;
  4. /**
  5. * Interface definition for a Select Query object.
  6. *
  7. * @ingroup database
  8. */
  9. interface SelectInterface extends ConditionInterface, AlterableInterface, ExtendableInterface, PlaceholderInterface {
  10. /* Alter accessors to expose the query data to alter hooks. */
  11. /**
  12. * Returns a reference to the fields array for this query.
  13. *
  14. * Because this method returns by reference, alter hooks may edit the fields
  15. * array directly to make their changes. If just adding fields, however, the
  16. * use of addField() is preferred.
  17. *
  18. * Note that this method must be called by reference as well:
  19. *
  20. * @code
  21. * $fields =& $query->getFields();
  22. * @endcode
  23. *
  24. * @return
  25. * A reference to the fields array structure.
  26. */
  27. public function &getFields();
  28. /**
  29. * Returns a reference to the expressions array for this query.
  30. *
  31. * Because this method returns by reference, alter hooks may edit the expressions
  32. * array directly to make their changes. If just adding expressions, however, the
  33. * use of addExpression() is preferred.
  34. *
  35. * Note that this method must be called by reference as well:
  36. *
  37. * @code
  38. * $fields =& $query->getExpressions();
  39. * @endcode
  40. *
  41. * @return
  42. * A reference to the expression array structure.
  43. */
  44. public function &getExpressions();
  45. /**
  46. * Returns a reference to the order by array for this query.
  47. *
  48. * Because this method returns by reference, alter hooks may edit the order-by
  49. * array directly to make their changes. If just adding additional ordering
  50. * fields, however, the use of orderBy() is preferred.
  51. *
  52. * Note that this method must be called by reference as well:
  53. *
  54. * @code
  55. * $fields =& $query->getOrderBy();
  56. * @endcode
  57. *
  58. * @return
  59. * A reference to the expression array structure.
  60. */
  61. public function &getOrderBy();
  62. /**
  63. * Returns a reference to the group-by array for this query.
  64. *
  65. * Because this method returns by reference, alter hooks may edit the group-by
  66. * array directly to make their changes. If just adding additional grouping
  67. * fields, however, the use of groupBy() is preferred.
  68. *
  69. * Note that this method must be called by reference as well:
  70. *
  71. * @code
  72. * $fields =& $query->getGroupBy();
  73. * @endcode
  74. *
  75. * @return
  76. * A reference to the group-by array structure.
  77. */
  78. public function &getGroupBy();
  79. /**
  80. * Returns a reference to the tables array for this query.
  81. *
  82. * Because this method returns by reference, alter hooks may edit the tables
  83. * array directly to make their changes. If just adding tables, however, the
  84. * use of the join() methods is preferred.
  85. *
  86. * Note that this method must be called by reference as well:
  87. *
  88. * @code
  89. * $tables =& $query->getTables();
  90. * @endcode
  91. *
  92. * @return
  93. * A reference to the tables array structure.
  94. */
  95. public function &getTables();
  96. /**
  97. * Returns a reference to the union queries for this query. This include
  98. * queries for UNION, UNION ALL, and UNION DISTINCT.
  99. *
  100. * Because this method returns by reference, alter hooks may edit the tables
  101. * array directly to make their changes. If just adding union queries,
  102. * however, the use of the union() method is preferred.
  103. *
  104. * Note that this method must be called by reference as well:
  105. *
  106. * @code
  107. * $fields =& $query->getUnion();
  108. * @endcode
  109. *
  110. * @return
  111. * A reference to the union query array structure.
  112. */
  113. public function &getUnion();
  114. /**
  115. * Escapes characters that work as wildcard characters in a LIKE pattern.
  116. *
  117. * @param $string
  118. * The string to escape.
  119. *
  120. * @return string
  121. * The escaped string.
  122. *
  123. * @see \Drupal\Core\Database\Connection::escapeLike()
  124. */
  125. public function escapeLike($string);
  126. /**
  127. * Escapes a field name string.
  128. *
  129. * Force all field names to be strictly alphanumeric-plus-underscore.
  130. * For some database drivers, it may also wrap the field name in
  131. * database-specific escape characters.
  132. *
  133. * @param string $string
  134. * An unsanitized field name.
  135. *
  136. * @return
  137. * The sanitized field name string.
  138. */
  139. public function escapeField($string);
  140. /**
  141. * Compiles and returns an associative array of the arguments for this prepared statement.
  142. *
  143. * @param $queryPlaceholder
  144. * When collecting the arguments of a subquery, the main placeholder
  145. * object should be passed as this parameter.
  146. *
  147. * @return
  148. * An associative array of all placeholder arguments for this query.
  149. */
  150. public function getArguments(PlaceholderInterface $queryPlaceholder = NULL);
  151. /* Query building operations */
  152. /**
  153. * Sets this query to be DISTINCT.
  154. *
  155. * @param $distinct
  156. * TRUE to flag this query DISTINCT, FALSE to disable it.
  157. * @return \Drupal\Core\Database\Query\SelectInterface
  158. * The called object.
  159. */
  160. public function distinct($distinct = TRUE);
  161. /**
  162. * Adds a field to the list to be SELECTed.
  163. *
  164. * @param $table_alias
  165. * The name of the table from which the field comes, as an alias. Generally
  166. * you will want to use the return value of join() here to ensure that it is
  167. * valid.
  168. * @param $field
  169. * The name of the field.
  170. * @param $alias
  171. * The alias for this field. If not specified, one will be generated
  172. * automatically based on the $table_alias and $field. The alias will be
  173. * checked for uniqueness, so the requested alias may not be the alias
  174. * that is assigned in all cases.
  175. * @return
  176. * The unique alias that was assigned for this field.
  177. */
  178. public function addField($table_alias, $field, $alias = NULL);
  179. /**
  180. * Add multiple fields from the same table to be SELECTed.
  181. *
  182. * This method does not return the aliases set for the passed fields. In the
  183. * majority of cases that is not a problem, as the alias will be the field
  184. * name. However, if you do need to know the alias you can call getFields()
  185. * and examine the result to determine what alias was created. Alternatively,
  186. * simply use addField() for the few fields you care about and this method for
  187. * the rest.
  188. *
  189. * @param $table_alias
  190. * The name of the table from which the field comes, as an alias. Generally
  191. * you will want to use the return value of join() here to ensure that it is
  192. * valid.
  193. * @param $fields
  194. * An indexed array of fields present in the specified table that should be
  195. * included in this query. If not specified, $table_alias.* will be generated
  196. * without any aliases.
  197. * @return \Drupal\Core\Database\Query\SelectInterface
  198. * The called object.
  199. */
  200. public function fields($table_alias, array $fields = []);
  201. /**
  202. * Adds an expression to the list of "fields" to be SELECTed.
  203. *
  204. * An expression can be any arbitrary string that is valid SQL. That includes
  205. * various functions, which may in some cases be database-dependent. This
  206. * method makes no effort to correct for database-specific functions.
  207. *
  208. * @param $expression
  209. * The expression string. May contain placeholders.
  210. * @param $alias
  211. * The alias for this expression. If not specified, one will be generated
  212. * automatically in the form "expression_#". The alias will be checked for
  213. * uniqueness, so the requested alias may not be the alias that is assigned
  214. * in all cases.
  215. * @param $arguments
  216. * Any placeholder arguments needed for this expression.
  217. * @return
  218. * The unique alias that was assigned for this expression.
  219. */
  220. public function addExpression($expression, $alias = NULL, $arguments = []);
  221. /**
  222. * Default Join against another table in the database.
  223. *
  224. * This method is a convenience method for innerJoin().
  225. *
  226. * @param $table
  227. * The table against which to join. May be a string or another SelectQuery
  228. * object. If a query object is passed, it will be used as a subselect.
  229. * Unless the table name starts with the database / schema name and a dot
  230. * it will be prefixed.
  231. * @param $alias
  232. * The alias for the table. In most cases this should be the first letter
  233. * of the table, or the first letter of each "word" in the table.
  234. * @param $condition
  235. * The condition on which to join this table. If the join requires values,
  236. * this clause should use a named placeholder and the value or values to
  237. * insert should be passed in the 4th parameter. For the first table joined
  238. * on a query, this value is ignored as the first table is taken as the base
  239. * table. The token %alias can be used in this string to be replaced with
  240. * the actual alias. This is useful when $alias is modified by the database
  241. * system, for example, when joining the same table more than once.
  242. * @param $arguments
  243. * An array of arguments to replace into the $condition of this join.
  244. * @return
  245. * The unique alias that was assigned for this table.
  246. */
  247. public function join($table, $alias = NULL, $condition = NULL, $arguments = []);
  248. /**
  249. * Inner Join against another table in the database.
  250. *
  251. * @param $table
  252. * The table against which to join. May be a string or another SelectQuery
  253. * object. If a query object is passed, it will be used as a subselect.
  254. * Unless the table name starts with the database / schema name and a dot
  255. * it will be prefixed.
  256. * @param $alias
  257. * The alias for the table. In most cases this should be the first letter
  258. * of the table, or the first letter of each "word" in the table.
  259. * @param $condition
  260. * The condition on which to join this table. If the join requires values,
  261. * this clause should use a named placeholder and the value or values to
  262. * insert should be passed in the 4th parameter. For the first table joined
  263. * on a query, this value is ignored as the first table is taken as the base
  264. * table. The token %alias can be used in this string to be replaced with
  265. * the actual alias. This is useful when $alias is modified by the database
  266. * system, for example, when joining the same table more than once.
  267. * @param $arguments
  268. * An array of arguments to replace into the $condition of this join.
  269. * @return
  270. * The unique alias that was assigned for this table.
  271. */
  272. public function innerJoin($table, $alias = NULL, $condition = NULL, $arguments = []);
  273. /**
  274. * Left Outer Join against another table in the database.
  275. *
  276. * @param $table
  277. * The table against which to join. May be a string or another SelectQuery
  278. * object. If a query object is passed, it will be used as a subselect.
  279. * Unless the table name starts with the database / schema name and a dot
  280. * it will be prefixed.
  281. * @param $alias
  282. * The alias for the table. In most cases this should be the first letter
  283. * of the table, or the first letter of each "word" in the table.
  284. * @param $condition
  285. * The condition on which to join this table. If the join requires values,
  286. * this clause should use a named placeholder and the value or values to
  287. * insert should be passed in the 4th parameter. For the first table joined
  288. * on a query, this value is ignored as the first table is taken as the base
  289. * table. The token %alias can be used in this string to be replaced with
  290. * the actual alias. This is useful when $alias is modified by the database
  291. * system, for example, when joining the same table more than once.
  292. * @param $arguments
  293. * An array of arguments to replace into the $condition of this join.
  294. * @return
  295. * The unique alias that was assigned for this table.
  296. */
  297. public function leftJoin($table, $alias = NULL, $condition = NULL, $arguments = []);
  298. /**
  299. * Right Outer Join against another table in the database.
  300. *
  301. * @param $table
  302. * The table against which to join. May be a string or another SelectQuery
  303. * object. If a query object is passed, it will be used as a subselect.
  304. * Unless the table name starts with the database / schema name and a dot
  305. * it will be prefixed.
  306. * @param $alias
  307. * The alias for the table. In most cases this should be the first letter
  308. * of the table, or the first letter of each "word" in the table.
  309. * @param $condition
  310. * The condition on which to join this table. If the join requires values,
  311. * this clause should use a named placeholder and the value or values to
  312. * insert should be passed in the 4th parameter. For the first table joined
  313. * on a query, this value is ignored as the first table is taken as the base
  314. * table. The token %alias can be used in this string to be replaced with
  315. * the actual alias. This is useful when $alias is modified by the database
  316. * system, for example, when joining the same table more than once.
  317. * @param $arguments
  318. * An array of arguments to replace into the $condition of this join.
  319. * @return
  320. * The unique alias that was assigned for this table.
  321. *
  322. * @deprecated as of Drupal 8.1.x, will be removed in Drupal 9.0.0. Instead,
  323. * change the query to use leftJoin(). For instance:
  324. * db_query('A')->rightJoin('B') is identical to
  325. * db_query('B')->leftJoin('A'). This functionality has been deprecated
  326. * because SQLite does not support it.
  327. *
  328. * @see https://www.drupal.org/node/2765249
  329. */
  330. public function rightJoin($table, $alias = NULL, $condition = NULL, $arguments = []);
  331. /**
  332. * Join against another table in the database.
  333. *
  334. * This method does the "hard" work of queuing up a table to be joined against.
  335. * In some cases, that may include dipping into the Schema API to find the necessary
  336. * fields on which to join.
  337. *
  338. * @param $type
  339. * The type of join. Typically one one of INNER, LEFT OUTER, and RIGHT OUTER.
  340. * @param $table
  341. * The table against which to join. May be a string or another SelectQuery
  342. * object. If a query object is passed, it will be used as a subselect.
  343. * Unless the table name starts with the database / schema name and a dot
  344. * it will be prefixed.
  345. * @param $alias
  346. * The alias for the table. In most cases this should be the first letter
  347. * of the table, or the first letter of each "word" in the table. If omitted,
  348. * one will be dynamically generated.
  349. * @param $condition
  350. * The condition on which to join this table. If the join requires values,
  351. * this clause should use a named placeholder and the value or values to
  352. * insert should be passed in the 4th parameter. For the first table joined
  353. * on a query, this value is ignored as the first table is taken as the base
  354. * table. The token %alias can be used in this string to be replaced with
  355. * the actual alias. This is useful when $alias is modified by the database
  356. * system, for example, when joining the same table more than once.
  357. * @param $arguments
  358. * An array of arguments to replace into the $condition of this join.
  359. * @return
  360. * The unique alias that was assigned for this table.
  361. */
  362. public function addJoin($type, $table, $alias = NULL, $condition = NULL, $arguments = []);
  363. /**
  364. * Orders the result set by a given field.
  365. *
  366. * If called multiple times, the query will order by each specified field in the
  367. * order this method is called.
  368. *
  369. * If the query uses DISTINCT or GROUP BY conditions, fields or expressions
  370. * that are used for the order must be selected to be compatible with some
  371. * databases like PostgreSQL. The PostgreSQL driver can handle simple cases
  372. * automatically but it is suggested to explicitly specify them. Additionally,
  373. * when ordering on an alias, the alias must be added before orderBy() is
  374. * called.
  375. *
  376. * @param $field
  377. * The field on which to order. The field is escaped for security so only
  378. * valid field and alias names are possible. To order by an expression, add
  379. * the expression with addExpression() first and then use the alias to order
  380. * on.
  381. *
  382. * Example:
  383. * @code
  384. * $query->addExpression('SUBSTRING(thread, 1, (LENGTH(thread) - 1))', 'order_field');
  385. * $query->orderBy('order_field', 'ASC');
  386. * @endcode
  387. * @param $direction
  388. * The direction to sort. Legal values are "ASC" and "DESC". Any other value
  389. * will be converted to "ASC".
  390. * @return \Drupal\Core\Database\Query\SelectInterface
  391. * The called object.
  392. */
  393. public function orderBy($field, $direction = 'ASC');
  394. /**
  395. * Orders the result set by a random value.
  396. *
  397. * This may be stacked with other orderBy() calls. If so, the query will order
  398. * by each specified field, including this one, in the order called. Although
  399. * this method may be called multiple times on the same query, doing so
  400. * is not particularly useful.
  401. *
  402. * Note: The method used by most drivers may not scale to very large result
  403. * sets. If you need to work with extremely large data sets, you may create
  404. * your own database driver by subclassing off of an existing driver and
  405. * implementing your own randomization mechanism. See
  406. *
  407. * http://jan.kneschke.de/projects/mysql/order-by-rand/
  408. *
  409. * for an example of such an alternate sorting mechanism.
  410. *
  411. * @return \Drupal\Core\Database\Query\SelectInterface
  412. * The called object
  413. */
  414. public function orderRandom();
  415. /**
  416. * Restricts a query to a given range in the result set.
  417. *
  418. * If this method is called with no parameters, will remove any range
  419. * directives that have been set.
  420. *
  421. * @param $start
  422. * The first record from the result set to return. If NULL, removes any
  423. * range directives that are set.
  424. * @param $length
  425. * The number of records to return from the result set.
  426. * @return \Drupal\Core\Database\Query\SelectInterface
  427. * The called object.
  428. */
  429. public function range($start = NULL, $length = NULL);
  430. /**
  431. * Add another Select query to UNION to this one.
  432. *
  433. * Union queries consist of two or more queries whose
  434. * results are effectively concatenated together. Queries
  435. * will be UNIONed in the order they are specified, with
  436. * this object's query coming first. Duplicate columns will
  437. * be discarded. All forms of UNION are supported, using
  438. * the second '$type' argument.
  439. *
  440. * Note: All queries UNIONed together must have the same
  441. * field structure, in the same order. It is up to the
  442. * caller to ensure that they match properly. If they do
  443. * not, an SQL syntax error will result.
  444. *
  445. * @param $query
  446. * The query to UNION to this query.
  447. * @param $type
  448. * The type of UNION to add to the query. Defaults to plain
  449. * UNION.
  450. * @return \Drupal\Core\Database\Query\SelectInterface
  451. * The called object.
  452. */
  453. public function union(SelectInterface $query, $type = '');
  454. /**
  455. * Groups the result set by the specified field.
  456. *
  457. * @param $field
  458. * The field on which to group. This should be the field as aliased.
  459. * @return \Drupal\Core\Database\Query\SelectInterface
  460. * The called object.
  461. */
  462. public function groupBy($field);
  463. /**
  464. * Get the equivalent COUNT query of this query as a new query object.
  465. *
  466. * @return \Drupal\Core\Database\Query\SelectInterface
  467. * A new SelectQuery object with no fields or expressions besides COUNT(*).
  468. */
  469. public function countQuery();
  470. /**
  471. * Indicates if preExecute() has already been called on that object.
  472. *
  473. * @return
  474. * TRUE is this query has already been prepared, FALSE otherwise.
  475. */
  476. public function isPrepared();
  477. /**
  478. * Generic preparation and validation for a SELECT query.
  479. *
  480. * @return
  481. * TRUE if the validation was successful, FALSE if not.
  482. */
  483. public function preExecute(SelectInterface $query = NULL);
  484. /**
  485. * Runs the query against the database.
  486. *
  487. * @return \Drupal\Core\Database\StatementInterface|null
  488. * A prepared statement, or NULL if the query is not valid.
  489. */
  490. public function execute();
  491. /**
  492. * Helper function to build most common HAVING conditional clauses.
  493. *
  494. * This method can take a variable number of parameters. If called with two
  495. * parameters, they are taken as $field and $value with $operator having a value
  496. * of IN if $value is an array and = otherwise.
  497. *
  498. * @param $field
  499. * The name of the field to check. If you would like to add a more complex
  500. * condition involving operators or functions, use having().
  501. * @param $value
  502. * The value to test the field against. In most cases, this is a scalar. For more
  503. * complex options, it is an array. The meaning of each element in the array is
  504. * dependent on the $operator.
  505. * @param $operator
  506. * The comparison operator, such as =, <, or >=. It also accepts more complex
  507. * options such as IN, LIKE, or BETWEEN. Defaults to IN if $value is an array
  508. * = otherwise.
  509. * @return \Drupal\Core\Database\Query\ConditionInterface
  510. * The called object.
  511. */
  512. public function havingCondition($field, $value = NULL, $operator = NULL);
  513. /**
  514. * Gets a list of all conditions in the HAVING clause.
  515. *
  516. * This method returns by reference. That allows alter hooks to access the
  517. * data structure directly and manipulate it before it gets compiled.
  518. *
  519. * @return array
  520. * An array of conditions.
  521. *
  522. * @see \Drupal\Core\Database\Query\ConditionInterface::conditions()
  523. */
  524. public function &havingConditions();
  525. /**
  526. * Gets a list of all values to insert into the HAVING clause.
  527. *
  528. * @return array
  529. * An associative array of placeholders and values.
  530. */
  531. public function havingArguments();
  532. /**
  533. * Adds an arbitrary HAVING clause to the query.
  534. *
  535. * @param $snippet
  536. * A portion of a HAVING clause as a prepared statement. It must use named
  537. * placeholders, not ? placeholders.
  538. * @param $args
  539. * (optional) An associative array of arguments.
  540. *
  541. * @return $this
  542. */
  543. public function having($snippet, $args = []);
  544. /**
  545. * Compiles the HAVING clause for later retrieval.
  546. *
  547. * @param $connection
  548. * The database connection for which to compile the clause.
  549. */
  550. public function havingCompile(Connection $connection);
  551. /**
  552. * Sets a condition in the HAVING clause that the specified field be NULL.
  553. *
  554. * @param $field
  555. * The name of the field to check.
  556. *
  557. * @return $this
  558. */
  559. public function havingIsNull($field);
  560. /**
  561. * Sets a condition in the HAVING clause that the specified field be NOT NULL.
  562. *
  563. * @param $field
  564. * The name of the field to check.
  565. *
  566. * @return $this
  567. */
  568. public function havingIsNotNull($field);
  569. /**
  570. * Sets a HAVING condition that the specified subquery returns values.
  571. *
  572. * @param \Drupal\Core\Database\Query\SelectInterface $select
  573. * The subquery that must contain results.
  574. *
  575. * @return $this
  576. */
  577. public function havingExists(SelectInterface $select);
  578. /**
  579. * Sets a HAVING condition that the specified subquery returns no values.
  580. *
  581. * @param \Drupal\Core\Database\Query\SelectInterface $select
  582. * The subquery that must contain results.
  583. *
  584. * @return $this
  585. */
  586. public function havingNotExists(SelectInterface $select);
  587. /**
  588. * Clone magic method.
  589. *
  590. * Select queries have dependent objects that must be deep-cloned. The
  591. * connection object itself, however, should not be cloned as that would
  592. * duplicate the connection itself.
  593. */
  594. public function __clone();
  595. /**
  596. * Add FOR UPDATE to the query.
  597. *
  598. * FOR UPDATE prevents the rows retrieved by the SELECT statement from being
  599. * modified or deleted by other transactions until the current transaction
  600. * ends. Other transactions that attempt UPDATE, DELETE, or SELECT FOR UPDATE
  601. * of these rows will be blocked until the current transaction ends.
  602. *
  603. * @param $set
  604. * IF TRUE, FOR UPDATE will be added to the query, if FALSE then it won't.
  605. *
  606. * @return \Drupal\Core\Database\Query\ConditionInterface
  607. * The called object.
  608. */
  609. public function forUpdate($set = TRUE);
  610. /**
  611. * Returns a string representation of how the query will be executed in SQL.
  612. *
  613. * @return string
  614. * The Select Query object expressed as a string.
  615. */
  616. public function __toString();
  617. }