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.

150 lines
3.9 KiB

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