You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.2 KiB

  1. <?php
  2. class SqlCriteria
  3. {
  4. private $select = array();
  5. private $distinct = '';
  6. private $where = array();
  7. private $group_by = array();
  8. private $order = array('sort' => array(), 'order' => array());
  9. private $limit = '';
  10. /**
  11. * @var SqlModel
  12. */
  13. private $model;
  14. private $sql_expression;
  15. private $sql_expression_params = array();
  16. /**
  17. * @param $model SqlModel
  18. * @param $sql_expression string|null Sql expression with SELECT and FROM operators. If fetched, then SqlCriteria::select(), SqlCriteria::distinct() disabled for use.
  19. * @param $sql_expression_params array additional params to be replaced in sql expression
  20. */
  21. public function __construct($model, $sql_expression = null, $sql_expression_params = array())
  22. {
  23. $this->model = $model;
  24. $this->sql_expression = $sql_expression;
  25. $this->sql_expression_params = $sql_expression_params;
  26. }
  27. /**
  28. * @return SqlResultProvider
  29. */
  30. public function find()
  31. {
  32. return $this->model->find($this->select, $this->distinct, $this->where, $this->order, $this->limit, null, $this->group_by, $this->sql_expression, $this->sql_expression_params);
  33. }
  34. public function count()
  35. {
  36. return $this->model->find(new DbExpr('COUNT(*) as count'), '', $this->where, null, null, null, null, $this->sql_expression, $this->sql_expression_params)->fetchField('count');
  37. }
  38. /**
  39. * @param $cond string|array Condition with "?" placeholder @ex 'field=?' or 'field=1' or array('field=?' => 1', 'field=1')
  40. * @param $value string|array|DbExpr|null Value. Array transformed to DbExpr(implode(',' Array)) All elements in the array mast be integer
  41. * @return SqlCriteria
  42. * @desc Allow multiple calls
  43. */
  44. public function where($cond, $value = null)
  45. {
  46. if (is_null($value)) {
  47. if (is_array($cond)) {
  48. $this->where = $this->where + $cond;
  49. } else {
  50. $this->where[] = $cond;
  51. }
  52. } else {
  53. $this->where[$cond] = $value;
  54. }
  55. return $this;
  56. }
  57. /**
  58. * @param $field string
  59. * @param $value array
  60. * @return SqlCriteria
  61. */
  62. public function whereIn($field, $value)
  63. {
  64. return $this->where($field . ' in ?', $value);
  65. }
  66. /**
  67. * @param $field string
  68. * @param $value array
  69. * @return SqlCriteria
  70. */
  71. public function whereNotIn($field, $value)
  72. {
  73. return $this->where($field . ' not in ?', $value);
  74. }
  75. /**
  76. * @param $field string
  77. * @param $value array
  78. * @return SqlCriteria
  79. */
  80. public function whereNot($field, $value)
  81. {
  82. return $this->whereNotIn($field, $value);
  83. }
  84. public function groupBy($fields)
  85. {
  86. if (is_array($fields)) {
  87. $this->group_by = $this->group_by + $fields;
  88. } else {
  89. $this->group_by[] = $fields;
  90. }
  91. return $this;
  92. }
  93. /**
  94. * @param $field string Field @ex 'field'
  95. * @param $order_desc bool Descendant sort direction
  96. * @return SqlCriteria
  97. * @desc Allow multiple calls
  98. */
  99. public function order($field, $order_desc = false)
  100. {
  101. $this->order['sort'][] = $field;
  102. if ($order_desc) {
  103. $this->order['order'][$field] = 'desc';
  104. }
  105. return $this;
  106. }
  107. /**
  108. * @param $limit int
  109. * @param $offset int
  110. * @return SqlCriteria
  111. */
  112. public function limit($limit, $offset = 0)
  113. {
  114. if ($offset) {
  115. $this->limit = (int) $offset . ',';
  116. }
  117. $this->limit .= (int) $limit;
  118. return $this;
  119. }
  120. /**
  121. * @param string|array $fields
  122. * @ex SqlCriteria::select('field')
  123. * @ex SqlCriteria->select(array('field1', 'field2'))
  124. * @ex SqlCriteria->select('field1,field2')
  125. * @return SqlCriteria
  126. */
  127. public function select($fields)
  128. {
  129. $fields = array_map(function($item){return trim($item);},explode(',', $fields));
  130. $this->select = array_merge($this->select,$fields);
  131. return $this;
  132. }
  133. /**
  134. * @param $field string|bool If true then distinct by *
  135. * @return SqlCriteria
  136. */
  137. public function distinct($field)
  138. {
  139. $this->distinct = $field;
  140. return $this;
  141. }
  142. }