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.

140 lines
3.7 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 Field @ex 'field'
  76. * @param $order_desc bool Descendant sort direction
  77. * @return SqlCriteria
  78. * @desc Allow multiple calls
  79. */
  80. public function order($field, $order_desc = false)
  81. {
  82. $this->order['sort'][] = $field;
  83. if ($order_desc) {
  84. $this->order['order'][$field] = 'desc';
  85. }
  86. return $this;
  87. }
  88. /**
  89. * @param $limit int
  90. * @param $offset int
  91. * @return SqlCriteria
  92. */
  93. public function limit($limit, $offset = 0)
  94. {
  95. if ($offset) {
  96. $this->limit = (int) $offset . ',';
  97. }
  98. $this->limit .= (int) $limit;
  99. return $this;
  100. }
  101. /**
  102. * @param string|array $fields
  103. * @ex SqlCriteria::select('field')
  104. * @ex SqlCriteria->select(array('field1', 'field2'))
  105. * @ex SqlCriteria->select('field1,field2')
  106. * @return SqlCriteria
  107. */
  108. public function select($fields)
  109. {
  110. $fields = explode(',', $fields);
  111. $this->select = array_merge($this->select,$fields);
  112. return $this;
  113. }
  114. /**
  115. * @param $field string|bool If true then distinct by *
  116. * @return SqlCriteria
  117. */
  118. public function distinct($field)
  119. {
  120. $this->distinct = $field;
  121. return $this;
  122. }
  123. }