QueryInterface.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.1
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Datasource;
  16. /**
  17. * The basis for every query object
  18. *
  19. * @package Cake\Datasource
  20. */
  21. interface QueryInterface
  22. {
  23. /**
  24. * Returns a key => value array representing a single aliased field
  25. * that can be passed directly to the select() method.
  26. * The key will contain the alias and the value the actual field name.
  27. *
  28. * If the field is already aliased, then it will not be changed.
  29. * If no $alias is passed, the default table for this query will be used.
  30. *
  31. * @param string $field The field to alias
  32. * @param string|null $alias the alias used to prefix the field
  33. * @return array
  34. */
  35. public function aliasField($field, $alias = null);
  36. /**
  37. * Runs `aliasField()` for each field in the provided list and returns
  38. * the result under a single array.
  39. *
  40. * @param array $fields The fields to alias
  41. * @param string|null $defaultAlias The default alias
  42. * @return array
  43. */
  44. public function aliasFields($fields, $defaultAlias = null);
  45. /**
  46. * Fetch the results for this query.
  47. *
  48. * Will return either the results set through setResult(), or execute this query
  49. * and return the ResultSetDecorator object ready for streaming of results.
  50. *
  51. * ResultSetDecorator is a traversable object that implements the methods found
  52. * on Cake\Collection\Collection.
  53. *
  54. * @return \Cake\Datasource\ResultSetInterface
  55. */
  56. public function all();
  57. /**
  58. * Populates or adds parts to current query clauses using an array.
  59. * This is handy for passing all query clauses at once. The option array accepts:
  60. *
  61. * - fields: Maps to the select method
  62. * - conditions: Maps to the where method
  63. * - limit: Maps to the limit method
  64. * - order: Maps to the order method
  65. * - offset: Maps to the offset method
  66. * - group: Maps to the group method
  67. * - having: Maps to the having method
  68. * - contain: Maps to the contain options for eager loading
  69. * - join: Maps to the join method
  70. * - page: Maps to the page method
  71. *
  72. * ### Example:
  73. *
  74. * ```
  75. * $query->applyOptions([
  76. * 'fields' => ['id', 'name'],
  77. * 'conditions' => [
  78. * 'created >=' => '2013-01-01'
  79. * ],
  80. * 'limit' => 10
  81. * ]);
  82. * ```
  83. *
  84. * Is equivalent to:
  85. *
  86. * ```
  87. * $query
  88. * ->select(['id', 'name'])
  89. * ->where(['created >=' => '2013-01-01'])
  90. * ->limit(10)
  91. * ```
  92. *
  93. * @param array $options list of query clauses to apply new parts to.
  94. * @return $this
  95. */
  96. public function applyOptions(array $options);
  97. /**
  98. * Apply custom finds to against an existing query object.
  99. *
  100. * Allows custom find methods to be combined and applied to each other.
  101. *
  102. * ```
  103. * $repository->find('all')->find('recent');
  104. * ```
  105. *
  106. * The above is an example of stacking multiple finder methods onto
  107. * a single query.
  108. *
  109. * @param string $finder The finder method to use.
  110. * @param array $options The options for the finder.
  111. * @return $this Returns a modified query.
  112. */
  113. public function find($finder, array $options = []);
  114. /**
  115. * Returns the first result out of executing this query, if the query has not been
  116. * executed before, it will set the limit clause to 1 for performance reasons.
  117. *
  118. * ### Example:
  119. *
  120. * ```
  121. * $singleUser = $query->select(['id', 'username'])->first();
  122. * ```
  123. *
  124. * @return mixed the first result from the ResultSet
  125. */
  126. public function first();
  127. /**
  128. * Returns the total amount of results for the query.
  129. *
  130. * @return int
  131. */
  132. public function count();
  133. /**
  134. * Sets the number of records that should be retrieved from database,
  135. * accepts an integer or an expression object that evaluates to an integer.
  136. * In some databases, this operation might not be supported or will require
  137. * the query to be transformed in order to limit the result set size.
  138. *
  139. * ### Examples
  140. *
  141. * ```
  142. * $query->limit(10) // generates LIMIT 10
  143. * $query->limit($query->newExpr()->add(['1 + 1'])); // LIMIT (1 + 1)
  144. * ```
  145. *
  146. * @param int $num number of records to be returned
  147. * @return $this
  148. */
  149. public function limit($num);
  150. /**
  151. * Sets the number of records that should be skipped from the original result set
  152. * This is commonly used for paginating large results. Accepts an integer or an
  153. * expression object that evaluates to an integer.
  154. *
  155. * In some databases, this operation might not be supported or will require
  156. * the query to be transformed in order to limit the result set size.
  157. *
  158. * ### Examples
  159. *
  160. * ```
  161. * $query->offset(10) // generates OFFSET 10
  162. * $query->offset($query->newExpr()->add(['1 + 1'])); // OFFSET (1 + 1)
  163. * ```
  164. *
  165. * @param int $num number of records to be skipped
  166. * @return $this
  167. */
  168. public function offset($num);
  169. /**
  170. * Adds a single or multiple fields to be used in the ORDER clause for this query.
  171. * Fields can be passed as an array of strings, array of expression
  172. * objects, a single expression or a single string.
  173. *
  174. * If an array is passed, keys will be used as the field itself and the value will
  175. * represent the order in which such field should be ordered. When called multiple
  176. * times with the same fields as key, the last order definition will prevail over
  177. * the others.
  178. *
  179. * By default this function will append any passed argument to the list of fields
  180. * to be selected, unless the second argument is set to true.
  181. *
  182. * ### Examples:
  183. *
  184. * ```
  185. * $query->order(['title' => 'DESC', 'author_id' => 'ASC']);
  186. * ```
  187. *
  188. * Produces:
  189. *
  190. * `ORDER BY title DESC, author_id ASC`
  191. *
  192. * ```
  193. * $query->order(['title' => 'DESC NULLS FIRST'])->order('author_id');
  194. * ```
  195. *
  196. * Will generate:
  197. *
  198. * `ORDER BY title DESC NULLS FIRST, author_id`
  199. *
  200. * ```
  201. * $expression = $query->newExpr()->add(['id % 2 = 0']);
  202. * $query->order($expression)->order(['title' => 'ASC']);
  203. * ```
  204. *
  205. * Will become:
  206. *
  207. * `ORDER BY (id %2 = 0), title ASC`
  208. *
  209. * If you need to set complex expressions as order conditions, you
  210. * should use `orderAsc()` or `orderDesc()`.
  211. *
  212. * @param array|string $fields fields to be added to the list
  213. * @param bool $overwrite whether to reset order with field list or not
  214. * @return $this
  215. */
  216. public function order($fields, $overwrite = false);
  217. /**
  218. * Set the page of results you want.
  219. *
  220. * This method provides an easier to use interface to set the limit + offset
  221. * in the record set you want as results. If empty the limit will default to
  222. * the existing limit clause, and if that too is empty, then `25` will be used.
  223. *
  224. * Pages should start at 1.
  225. *
  226. * @param int $num The page number you want.
  227. * @param int|null $limit The number of rows you want in the page. If null
  228. * the current limit clause will be used.
  229. * @return $this
  230. */
  231. public function page($num, $limit = null);
  232. /**
  233. * Returns an array representation of the results after executing the query.
  234. *
  235. * @return array
  236. */
  237. public function toArray();
  238. /**
  239. * Returns the default repository object that will be used by this query,
  240. * that is, the repository that will appear in the from clause.
  241. *
  242. * @param \Cake\Datasource\RepositoryInterface|null $repository The default repository object to use
  243. * @return \Cake\Datasource\RepositoryInterface|$this
  244. */
  245. public function repository(RepositoryInterface $repository = null);
  246. /**
  247. * Adds a condition or set of conditions to be used in the WHERE clause for this
  248. * query. Conditions can be expressed as an array of fields as keys with
  249. * comparison operators in it, the values for the array will be used for comparing
  250. * the field to such literal. Finally, conditions can be expressed as a single
  251. * string or an array of strings.
  252. *
  253. * When using arrays, each entry will be joined to the rest of the conditions using
  254. * an AND operator. Consecutive calls to this function will also join the new
  255. * conditions specified using the AND operator. Additionally, values can be
  256. * expressed using expression objects which can include other query objects.
  257. *
  258. * Any conditions created with this methods can be used with any SELECT, UPDATE
  259. * and DELETE type of queries.
  260. *
  261. * ### Conditions using operators:
  262. *
  263. * ```
  264. * $query->where([
  265. * 'posted >=' => new DateTime('3 days ago'),
  266. * 'title LIKE' => 'Hello W%',
  267. * 'author_id' => 1,
  268. * ], ['posted' => 'datetime']);
  269. * ```
  270. *
  271. * The previous example produces:
  272. *
  273. * `WHERE posted >= 2012-01-27 AND title LIKE 'Hello W%' AND author_id = 1`
  274. *
  275. * Second parameter is used to specify what type is expected for each passed
  276. * key. Valid types can be used from the mapped with Database\Type class.
  277. *
  278. * ### Nesting conditions with conjunctions:
  279. *
  280. * ```
  281. * $query->where([
  282. * 'author_id !=' => 1,
  283. * 'OR' => ['published' => true, 'posted <' => new DateTime('now')],
  284. * 'NOT' => ['title' => 'Hello']
  285. * ], ['published' => boolean, 'posted' => 'datetime']
  286. * ```
  287. *
  288. * The previous example produces:
  289. *
  290. * `WHERE author_id = 1 AND (published = 1 OR posted < '2012-02-01') AND NOT (title = 'Hello')`
  291. *
  292. * You can nest conditions using conjunctions as much as you like. Sometimes, you
  293. * may want to define 2 different options for the same key, in that case, you can
  294. * wrap each condition inside a new array:
  295. *
  296. * `$query->where(['OR' => [['published' => false], ['published' => true]])`
  297. *
  298. * Keep in mind that every time you call where() with the third param set to false
  299. * (default), it will join the passed conditions to the previous stored list using
  300. * the AND operator. Also, using the same array key twice in consecutive calls to
  301. * this method will not override the previous value.
  302. *
  303. * ### Using expressions objects:
  304. *
  305. * ```
  306. * $exp = $query->newExpr()->add(['id !=' => 100, 'author_id' != 1])->tieWith('OR');
  307. * $query->where(['published' => true], ['published' => 'boolean'])->where($exp);
  308. * ```
  309. *
  310. * The previous example produces:
  311. *
  312. * `WHERE (id != 100 OR author_id != 1) AND published = 1`
  313. *
  314. * Other Query objects that be used as conditions for any field.
  315. *
  316. * ### Adding conditions in multiple steps:
  317. *
  318. * You can use callable functions to construct complex expressions, functions
  319. * receive as first argument a new QueryExpression object and this query instance
  320. * as second argument. Functions must return an expression object, that will be
  321. * added the list of conditions for the query using the AND operator.
  322. *
  323. * ```
  324. * $query
  325. * ->where(['title !=' => 'Hello World'])
  326. * ->where(function ($exp, $query) {
  327. * $or = $exp->or_(['id' => 1]);
  328. * $and = $exp->and_(['id >' => 2, 'id <' => 10]);
  329. * return $or->add($and);
  330. * });
  331. * ```
  332. *
  333. * * The previous example produces:
  334. *
  335. * `WHERE title != 'Hello World' AND (id = 1 OR (id > 2 AND id < 10))`
  336. *
  337. * ### Conditions as strings:
  338. *
  339. * ```
  340. * $query->where(['articles.author_id = authors.id', 'modified IS NULL']);
  341. * ```
  342. *
  343. * The previous example produces:
  344. *
  345. * `WHERE articles.author_id = authors.id AND modified IS NULL`
  346. *
  347. * Please note that when using the array notation or the expression objects, all
  348. * values will be correctly quoted and transformed to the correspondent database
  349. * data type automatically for you, thus securing your application from SQL injections.
  350. * If you use string conditions make sure that your values are correctly quoted.
  351. * The safest thing you can do is to never use string conditions.
  352. *
  353. * @param string|array|callable|null $conditions The conditions to filter on.
  354. * @param array $types associative array of type names used to bind values to query
  355. * @param bool $overwrite whether to reset conditions with passed list or not
  356. * @return $this
  357. */
  358. public function where($conditions = null, $types = [], $overwrite = false);
  359. }