Where.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Database query builder for WHERE statements. See [Query Builder](/database/query/builder) for usage and examples.
  4. *
  5. * @package Kohana/Database
  6. * @category Query
  7. * @author Kohana Team
  8. * @copyright (c) Kohana Team
  9. * @license https://koseven.ga/LICENSE.md
  10. */
  11. abstract class Kohana_Database_Query_Builder_Where extends Database_Query_Builder {
  12. // WHERE ...
  13. protected $_where = [];
  14. // ORDER BY ...
  15. protected $_order_by = [];
  16. // LIMIT ...
  17. protected $_limit = NULL;
  18. /**
  19. * Alias of and_where()
  20. *
  21. * @param mixed $column column name or array($column, $alias) or object
  22. * @param string $op logic operator
  23. * @param mixed $value column value
  24. * @return $this
  25. */
  26. public function where($column, $op, $value)
  27. {
  28. return $this->and_where($column, $op, $value);
  29. }
  30. /**
  31. * Creates a new "AND WHERE" condition for the query.
  32. *
  33. * @param mixed $column column name or array($column, $alias) or object
  34. * @param string $op logic operator
  35. * @param mixed $value column value
  36. * @return $this
  37. */
  38. public function and_where($column, $op, $value)
  39. {
  40. $this->_where[] = ['AND' => [$column, $op, $value]];
  41. return $this;
  42. }
  43. /**
  44. * Creates a new "OR WHERE" condition for the query.
  45. *
  46. * @param mixed $column column name or array($column, $alias) or object
  47. * @param string $op logic operator
  48. * @param mixed $value column value
  49. * @return $this
  50. */
  51. public function or_where($column, $op, $value)
  52. {
  53. $this->_where[] = ['OR' => [$column, $op, $value]];
  54. return $this;
  55. }
  56. /**
  57. * Alias of and_where_open()
  58. *
  59. * @return $this
  60. */
  61. public function where_open()
  62. {
  63. return $this->and_where_open();
  64. }
  65. /**
  66. * Opens a new "AND WHERE (...)" grouping.
  67. *
  68. * @return $this
  69. */
  70. public function and_where_open()
  71. {
  72. $this->_where[] = ['AND' => '('];
  73. return $this;
  74. }
  75. /**
  76. * Opens a new "OR WHERE (...)" grouping.
  77. *
  78. * @return $this
  79. */
  80. public function or_where_open()
  81. {
  82. $this->_where[] = ['OR' => '('];
  83. return $this;
  84. }
  85. /**
  86. * Closes an open "WHERE (...)" grouping.
  87. *
  88. * @return $this
  89. */
  90. public function where_close()
  91. {
  92. return $this->and_where_close();
  93. }
  94. /**
  95. * Closes an open "WHERE (...)" grouping or removes the grouping when it is
  96. * empty.
  97. *
  98. * @return $this
  99. */
  100. public function where_close_empty()
  101. {
  102. $group = end($this->_where);
  103. if ($group AND reset($group) === '(')
  104. {
  105. array_pop($this->_where);
  106. return $this;
  107. }
  108. return $this->where_close();
  109. }
  110. /**
  111. * Closes an open "WHERE (...)" grouping.
  112. *
  113. * @return $this
  114. */
  115. public function and_where_close()
  116. {
  117. $this->_where[] = ['AND' => ')'];
  118. return $this;
  119. }
  120. /**
  121. * Closes an open "WHERE (...)" grouping.
  122. *
  123. * @return $this
  124. */
  125. public function or_where_close()
  126. {
  127. $this->_where[] = ['OR' => ')'];
  128. return $this;
  129. }
  130. /**
  131. * Applies sorting with "ORDER BY ..."
  132. *
  133. * @param mixed $column column name or array($column, $alias) or object
  134. * @param string $direction direction of sorting
  135. * @return $this
  136. */
  137. public function order_by($column, $direction = NULL)
  138. {
  139. $this->_order_by[] = [$column, $direction];
  140. return $this;
  141. }
  142. /**
  143. * Return up to "LIMIT ..." results
  144. *
  145. * @param integer $number maximum results to return or NULL to reset
  146. * @return $this
  147. */
  148. public function limit($number)
  149. {
  150. $this->_limit = ($number === NULL) ? NULL : (int) $number;
  151. return $this;
  152. }
  153. } // End Database_Query_Builder_Where