WhereQuery.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 $selects = [];
  11. protected $join = [];
  12. protected $sets = [];
  13. protected $orders = [];
  14. protected $options = [];
  15. public function __construct($table_name, $db=null)
  16. {
  17. $this->table_name = $table_name;
  18. if (empty($db)) {
  19. $db = App::getDb();
  20. }
  21. $this->db = $db;
  22. }
  23. public function where($where)
  24. {
  25. $this->where = $where + $this->where;
  26. return $this;
  27. }
  28. public function select($select)
  29. {
  30. if (! is_array($select)) {
  31. $select = [ (string) $select ];
  32. }
  33. $this->selects = $select + $this->selects;
  34. $this->options['select'] = $this->selects;
  35. return $this;
  36. }
  37. public function set(array $set)
  38. {
  39. $this->sets = $set + $this->sets;
  40. return $this;
  41. }
  42. public function join($join)
  43. {
  44. if (! is_array($join)) {
  45. $join = [ (string) $join ];
  46. }
  47. $this->join = $join + $this->join;
  48. $this->options['join'] = $this->join;
  49. return $this;
  50. }
  51. public function order($order)
  52. {
  53. if (! is_array($order)) {
  54. $order = [ (string) $order ];
  55. }
  56. $this->orders = $order + $this->orders;
  57. $this->options['order by'] = $this->orders;
  58. return $this;
  59. }
  60. public function insert($options=[])
  61. {
  62. return $this->db->getTable($this->table_name)->insert($this->sets, $options);
  63. }
  64. public function update($options=[])
  65. {
  66. return $this->db->getTable($this->table_name)->update($this->sets, $this->where, $options);
  67. }
  68. public function delete($options=[])
  69. {
  70. return $this->db->getTable($this->table_name)->delete($this->where, $options);
  71. }
  72. public function get($options=[])
  73. {
  74. if (empty($options['select'])) {
  75. $options['select'] = empty($this->selects) ? '*' : $this->selects;
  76. }
  77. return $this->db->getTable($this->table_name)->all($this->where, $options);
  78. }
  79. public function getAll($options=[])
  80. {
  81. return $this->get($options);
  82. }
  83. public function getOne($options=[])
  84. {
  85. $select = empty($this->selects) ? '*' : $this->selects;
  86. if (empty($options['select'])) {
  87. $options['select'] = $select;
  88. }
  89. return $this->db->getTable($this->table_name)->one($this->where, $options);
  90. }
  91. public function getCount($options=[])
  92. {
  93. $select = empty($this->selects) ? '*' : $this->selects;
  94. if (empty($options['select'])) {
  95. $options['select'] = $select;
  96. }
  97. return $this->db->getTable($this->table_name)->count($this->where, $options);
  98. }
  99. public function getAllWithFoundRows($options=[])
  100. {
  101. $select = empty($this->selects) ? '*' : $this->selects;
  102. if (empty($options['select'])) {
  103. $options['select'] = $select;
  104. }
  105. return $this->db->getTable($this->table_name)->getAllWithFoundRows($this->where, $options);
  106. }
  107. public function getAllPagination($nb_per_page=10, $page_idx=1, $options=[])
  108. {
  109. $select = empty($this->selects) ? '*' : $this->selects;
  110. if (empty($options['select'])) {
  111. $options['select'] = $select;
  112. }
  113. return $this->db->getTable($this->table_name)->getAllPagination($this->where, $nb_per_page, $page_idx, $options);
  114. }
  115. }