WhereQuery.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace KarmaFW\Database\Sql;
  3. use \KarmaFW\App;
  4. //use \KarmaFW\Database\Sql\SqlResultSetError;
  5. class WhereQuery
  6. {
  7. protected $table_name;
  8. protected $db;
  9. protected $where = [];
  10. protected $alias = null;
  11. protected $selects = [];
  12. protected $join = [];
  13. protected $sets = [];
  14. protected $orders = [];
  15. protected $groups = [];
  16. protected $options = [];
  17. public function __construct($table_name, $db=null)
  18. {
  19. $this->table_name = $table_name;
  20. if (empty($db)) {
  21. $db = App::getDb();
  22. }
  23. $this->db = $db;
  24. }
  25. public function where($where)
  26. {
  27. $this->where = $where + $this->where;
  28. return $this;
  29. }
  30. public function alias($alias)
  31. {
  32. $this->alias = $alias;
  33. $this->options['alias'] = $this->alias;
  34. return $this;
  35. }
  36. public function options($options)
  37. {
  38. $this->options += $options;
  39. $this->options = $this->options;
  40. return $this;
  41. }
  42. public function getOptions()
  43. {
  44. return $this->options;
  45. }
  46. public function setOptions($options=[])
  47. {
  48. $this->options = $options;
  49. }
  50. public function select($select)
  51. {
  52. if (! is_array($select)) {
  53. $select = [ (string) $select ];
  54. }
  55. $this->selects = $select + $this->selects;
  56. $this->options['select'] = $this->selects;
  57. return $this;
  58. }
  59. public function set(array $set)
  60. {
  61. $this->sets = $set + $this->sets;
  62. return $this;
  63. }
  64. public function join($join)
  65. {
  66. if (! is_array($join)) {
  67. $join = [ (string) $join ];
  68. }
  69. $this->join = $join + $this->join;
  70. $this->options['join'] = $this->join;
  71. return $this;
  72. }
  73. public function group($group_by)
  74. {
  75. if (! is_array($group_by)) {
  76. $group_by = [ (string) $group_by ];
  77. }
  78. $this->groups = $group_by + $this->groups;
  79. $this->options['order by'] = $this->groups;
  80. return $this;
  81. }
  82. public function order($order_by)
  83. {
  84. if (! is_array($order_by)) {
  85. $order_by = [ (string) $order_by ];
  86. }
  87. $this->orders = $order_by + $this->orders;
  88. $this->options['order by'] = $this->orders;
  89. return $this;
  90. }
  91. public function insert($options=[])
  92. {
  93. return $this->db->getTable($this->table_name)->insert($this->sets, $this->options + $options);
  94. }
  95. public function update($options=[])
  96. {
  97. return $this->db->getTable($this->table_name)->update($this->sets, $this->where, $this->options + $options);
  98. }
  99. public function delete($options=[])
  100. {
  101. return $this->db->getTable($this->table_name)->delete($this->where, $this->options + $options);
  102. }
  103. public function get($options=[])
  104. {
  105. if (empty($options['select'])) {
  106. $options['select'] = empty($this->selects) ? '*' : $this->selects;
  107. }
  108. return $this->db->getTable($this->table_name)->all($this->where, $this->options + $options);
  109. }
  110. public function getAll($options=[])
  111. {
  112. return $this->get($options);
  113. }
  114. public function getOne($options=[])
  115. {
  116. $select = empty($this->selects) ? '*' : $this->selects;
  117. if (empty($options['select'])) {
  118. $options['select'] = $select;
  119. }
  120. return $this->db->getTable($this->table_name)->one($this->where, $this->options + $options);
  121. }
  122. public function getCount($options=[])
  123. {
  124. $select = empty($this->selects) ? '*' : $this->selects;
  125. if (empty($options['select'])) {
  126. $options['select'] = $select;
  127. }
  128. return $this->db->getTable($this->table_name)->count($this->where, $this->options + $options);
  129. }
  130. public function getAllWithFoundRows($options=[])
  131. {
  132. $select = empty($this->selects) ? '*' : $this->selects;
  133. if (empty($options['select'])) {
  134. $options['select'] = $select;
  135. }
  136. return $this->db->getTable($this->table_name)->getAllWithFoundRows($this->where, $this->options + $options);
  137. }
  138. public function getAllPagination($nb_per_page=10, $page_idx=1, $options=[])
  139. {
  140. $select = empty($this->selects) ? '*' : $this->selects;
  141. if (empty($options['select'])) {
  142. $options['select'] = $select;
  143. }
  144. return $this->db->getTable($this->table_name)->getAllPagination($this->where, $nb_per_page, $page_idx, $this->options + $options);
  145. }
  146. }