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.

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