QueryInterface.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. /**
  9. * The QueryInterface defines the minimum set of methods to be implemented by a database query.
  10. *
  11. * The default implementation of this interface is provided by [[QueryTrait]].
  12. *
  13. * It has support for getting [[one]] instance or [[all]].
  14. * Allows pagination via [[limit]] and [[offset]].
  15. * Sorting is supported via [[orderBy]] and items can be limited to match some conditions using [[where]].
  16. *
  17. * By calling [[createCommand()]], we can get a [[Command]] instance which can be further
  18. * used to perform/execute the DB query against a database.
  19. *
  20. * @author Qiang Xue <[email protected]>
  21. * @author Carsten Brandt <[email protected]>
  22. * @since 2.0
  23. */
  24. interface QueryInterface
  25. {
  26. /**
  27. * Executes the query and returns all results as an array.
  28. * @param Connection $db the database connection used to execute the query.
  29. * If this parameter is not given, the `db` application component will be used.
  30. * @return array the query results. If the query results in nothing, an empty array will be returned.
  31. */
  32. public function all($db = null);
  33. /**
  34. * Executes the query and returns a single row of result.
  35. * @param Connection $db the database connection used to execute the query.
  36. * If this parameter is not given, the `db` application component will be used.
  37. * @return array|boolean the first row (in terms of an array) of the query result. False is returned if the query
  38. * results in nothing.
  39. */
  40. public function one($db = null);
  41. /**
  42. * Returns the number of records.
  43. * @param string $q the COUNT expression. Defaults to '*'.
  44. * @param Connection $db the database connection used to execute the query.
  45. * If this parameter is not given, the `db` application component will be used.
  46. * @return integer number of records
  47. */
  48. public function count($q = '*', $db = null);
  49. /**
  50. * Returns a value indicating whether the query result contains any row of data.
  51. * @param Connection $db the database connection used to execute the query.
  52. * If this parameter is not given, the `db` application component will be used.
  53. * @return boolean whether the query result contains any row of data.
  54. */
  55. public function exists($db = null);
  56. /**
  57. * Sets the [[indexBy]] property.
  58. * @param string|callable $column the name of the column by which the query results should be indexed by.
  59. * This can also be a callable (e.g. anonymous function) that returns the index value based on the given
  60. * row data. The signature of the callable should be:
  61. *
  62. * ~~~
  63. * function ($row)
  64. * {
  65. * // return the index value corresponding to $row
  66. * }
  67. * ~~~
  68. *
  69. * @return static the query object itself
  70. */
  71. public function indexBy($column);
  72. /**
  73. * Sets the WHERE part of the query.
  74. *
  75. * The method requires a $condition parameter.
  76. *
  77. * The $condition parameter should be an array in one of the following two formats:
  78. *
  79. * - hash format: `['column1' => value1, 'column2' => value2, ...]`
  80. * - operator format: `[operator, operand1, operand2, ...]`
  81. *
  82. * A condition in hash format represents the following SQL expression in general:
  83. * `column1=value1 AND column2=value2 AND ...`. In case when a value is an array,
  84. * an `IN` expression will be generated. And if a value is null, `IS NULL` will be used
  85. * in the generated expression. Below are some examples:
  86. *
  87. * - `['type' => 1, 'status' => 2]` generates `(type = 1) AND (status = 2)`.
  88. * - `['id' => [1, 2, 3], 'status' => 2]` generates `(id IN (1, 2, 3)) AND (status = 2)`.
  89. * - `['status' => null] generates `status IS NULL`.
  90. *
  91. * A condition in operator format generates the SQL expression according to the specified operator, which
  92. * can be one of the followings:
  93. *
  94. * - `and`: the operands should be concatenated together using `AND`. For example,
  95. * `['and', 'id=1', 'id=2']` will generate `id=1 AND id=2`. If an operand is an array,
  96. * it will be converted into a string using the rules described here. For example,
  97. * `['and', 'type=1', ['or', 'id=1', 'id=2']]` will generate `type=1 AND (id=1 OR id=2)`.
  98. * The method will NOT do any quoting or escaping.
  99. *
  100. * - `or`: similar to the `and` operator except that the operands are concatenated using `OR`.
  101. *
  102. * - `between`: operand 1 should be the column name, and operand 2 and 3 should be the
  103. * starting and ending values of the range that the column is in.
  104. * For example, `['between', 'id', 1, 10]` will generate `id BETWEEN 1 AND 10`.
  105. *
  106. * - `not between`: similar to `between` except the `BETWEEN` is replaced with `NOT BETWEEN`
  107. * in the generated condition.
  108. *
  109. * - `in`: operand 1 should be a column or DB expression, and operand 2 be an array representing
  110. * the range of the values that the column or DB expression should be in. For example,
  111. * `['in', 'id', [1, 2, 3]]` will generate `id IN (1, 2, 3)`.
  112. * The method will properly quote the column name and escape values in the range.
  113. *
  114. * - `not in`: similar to the `in` operator except that `IN` is replaced with `NOT IN` in the generated condition.
  115. *
  116. * - `like`: operand 1 should be a column or DB expression, and operand 2 be a string or an array representing
  117. * the values that the column or DB expression should be like.
  118. * For example, `['like', 'name', 'tester']` will generate `name LIKE '%tester%'`.
  119. * When the value range is given as an array, multiple `LIKE` predicates will be generated and concatenated
  120. * using `AND`. For example, `['like', 'name', ['test', 'sample']]` will generate
  121. * `name LIKE '%test%' AND name LIKE '%sample%'`.
  122. * The method will properly quote the column name and escape special characters in the values.
  123. * Sometimes, you may want to add the percentage characters to the matching value by yourself, you may supply
  124. * a third operand `false` to do so. For example, `['like', 'name', '%tester', false]` will generate `name LIKE '%tester'`.
  125. *
  126. * - `or like`: similar to the `like` operator except that `OR` is used to concatenate the `LIKE`
  127. * predicates when operand 2 is an array.
  128. *
  129. * - `not like`: similar to the `like` operator except that `LIKE` is replaced with `NOT LIKE`
  130. * in the generated condition.
  131. *
  132. * - `or not like`: similar to the `not like` operator except that `OR` is used to concatenate
  133. * the `NOT LIKE` predicates.
  134. *
  135. * @param array $condition the conditions that should be put in the WHERE part.
  136. * @return static the query object itself
  137. * @see andWhere()
  138. * @see orWhere()
  139. */
  140. public function where($condition);
  141. /**
  142. * Adds an additional WHERE condition to the existing one.
  143. * The new condition and the existing one will be joined using the 'AND' operator.
  144. * @param string|array $condition the new WHERE condition. Please refer to [[where()]]
  145. * on how to specify this parameter.
  146. * @return static the query object itself
  147. * @see where()
  148. * @see orWhere()
  149. */
  150. public function andWhere($condition);
  151. /**
  152. * Adds an additional WHERE condition to the existing one.
  153. * The new condition and the existing one will be joined using the 'OR' operator.
  154. * @param string|array $condition the new WHERE condition. Please refer to [[where()]]
  155. * on how to specify this parameter.
  156. * @return static the query object itself
  157. * @see where()
  158. * @see andWhere()
  159. */
  160. public function orWhere($condition);
  161. /**
  162. * Sets the ORDER BY part of the query.
  163. * @param string|array $columns the columns (and the directions) to be ordered by.
  164. * Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array
  165. * (e.g. `['id' => SORT_ASC, 'name' => SORT_DESC]`).
  166. * The method will automatically quote the column names unless a column contains some parenthesis
  167. * (which means the column contains a DB expression).
  168. * @return static the query object itself
  169. * @see addOrderBy()
  170. */
  171. public function orderBy($columns);
  172. /**
  173. * Adds additional ORDER BY columns to the query.
  174. * @param string|array $columns the columns (and the directions) to be ordered by.
  175. * Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array
  176. * (e.g. `['id' => SORT_ASC, 'name' => SORT_DESC]`).
  177. * The method will automatically quote the column names unless a column contains some parenthesis
  178. * (which means the column contains a DB expression).
  179. * @return static the query object itself
  180. * @see orderBy()
  181. */
  182. public function addOrderBy($columns);
  183. /**
  184. * Sets the LIMIT part of the query.
  185. * @param integer $limit the limit. Use null or negative value to disable limit.
  186. * @return static the query object itself
  187. */
  188. public function limit($limit);
  189. /**
  190. * Sets the OFFSET part of the query.
  191. * @param integer $offset the offset. Use null or negative value to disable offset.
  192. * @return static the query object itself
  193. */
  194. public function offset($offset);
  195. }