SelectInterface.php 24 KB

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