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.

258 lines
8.0 KiB

  1. <?php
  2. class SqlCriteria
  3. {
  4. const JOIN_TYPE_DEFAULT = 100;
  5. const JOIN_TYPE_LEFT = 101;
  6. const JOIN_TYPE_RIGHT = 102;
  7. const JOIN_TYPE_INNER = 103;
  8. const JOIN_TYPE_OUTER = 104;
  9. private static $join_reserved_keyword = array(
  10. self::JOIN_TYPE_DEFAULT => 'JOIN',
  11. self::JOIN_TYPE_LEFT => 'LEFT JOIN',
  12. self::JOIN_TYPE_RIGHT => 'RIGHT JOIN',
  13. self::JOIN_TYPE_INNER => 'INNER JOIN',
  14. self::JOIN_TYPE_OUTER => 'OUTER JOIN',
  15. );
  16. private $select = array();
  17. private $distinct = '';
  18. private $where = array();
  19. private $group_by = array();
  20. private $order = array('sort' => array(), 'order' => array());
  21. private $limit = '';
  22. /**
  23. * @var SqlModel
  24. */
  25. private $model;
  26. private $sql_expression;
  27. private $sql_expression_params = array();
  28. private $sql_join_expressions = array();
  29. private $join_table_placeholder = array();
  30. /**
  31. * @param $model SqlModel
  32. * @param $sql_expression string|null Sql expression with SELECT and FROM operators. If fetched, then SqlCriteria::select(), SqlCriteria::distinct() disabled for use.
  33. * @param $sql_expression_params array additional params to be replaced in sql expression
  34. */
  35. public function __construct($model, $sql_expression = null, $sql_expression_params = array())
  36. {
  37. $this->model = $model;
  38. $this->sql_expression = $sql_expression;
  39. $this->sql_expression_params = $sql_expression_params;
  40. }
  41. /**
  42. * @return SqlResultProvider
  43. */
  44. public function find()
  45. {
  46. $this->defineJoinExpressions();
  47. 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);
  48. }
  49. private function defineJoinExpressions()
  50. {
  51. if ($this->sql_join_expressions) {
  52. if (!$this->sql_expression) {
  53. $select = $this->model->getDb()->selectExpr($this->select, $this->distinct);
  54. $this->sql_expression = 'SELECT ' . $select . ' FROM :table';
  55. }
  56. $this->sql_expression .= ' ' . implode(' ', $this->sql_join_expressions);
  57. }
  58. }
  59. public function delete()
  60. {
  61. return $this->model->find('', '', $this->where, null, null, null, null, 'DELETE FROM :table', $this->sql_expression_params)->affectedRows();
  62. }
  63. public function count()
  64. {
  65. $this->sql_expression = 'SELECT COUNT(*) as count FROM :table';
  66. $this->defineJoinExpressions();
  67. return $this->model->find(array(), '', $this->where, null, null, null, null, $this->sql_expression, $this->sql_expression_params)->fetchField('count');
  68. }
  69. private function defineJoinTablePlaceholder($table_name)
  70. {
  71. if (!isset($this->join_table_placeholder[$table_name])) {
  72. $this->join_table_placeholder[$table_name] = ':table' . (count($this->join_table_placeholder) + 1);
  73. }
  74. }
  75. public function getTablePh($table_name)
  76. {
  77. $this->defineJoinTablePlaceholder($table_name);
  78. return $this->join_table_placeholder[$table_name];
  79. }
  80. public function join($join_table_name, $join_field_name, $donor_table_name = null, $donor_field_name = null, $join_type = self::JOIN_TYPE_DEFAULT)
  81. {
  82. $donor_field_name = $donor_field_name ? : $join_field_name;
  83. $donor_table_placeholder = $donor_table_name ? $this->getTablePh($donor_table_name) : ':table';
  84. $join_table_placeholder = $this->getTablePh($join_table_name);
  85. $this->sql_join_expressions[] = self::$join_reserved_keyword[$join_type]
  86. . ' ' . $join_table_placeholder
  87. . ' ON ' . $donor_table_placeholder . '.' . $donor_field_name . '='
  88. . ' ' . $join_table_placeholder . '.' . $join_field_name;
  89. if ($donor_table_name) {
  90. $this->sql_expression_params[substr($donor_table_placeholder, 1)] = new DbExpr($this->model->identify($donor_table_name));
  91. }
  92. $this->sql_expression_params[substr($join_table_placeholder, 1)] = new DbExpr($this->model->identify($join_table_name));
  93. return $this;
  94. }
  95. /**
  96. * @param $cond string|array Condition with "?" placeholder @ex 'field=?' or 'field=1' or array('field=?' => 1', 'field=1')
  97. * @param $value string|array|DbExpr|null Value. Array transformed to DbExpr(implode(',' Array)) All elements in the array mast be integer
  98. * @return SqlCriteria
  99. * @desc Allow multiple calls
  100. */
  101. public function where($cond, $value = null)
  102. {
  103. if (is_null($value)) {
  104. if (is_array($cond)) {
  105. $this->where = $this->where + $cond;
  106. } else {
  107. $this->where[] = $cond;
  108. }
  109. } else {
  110. $this->where[$cond] = $value;
  111. }
  112. return $this;
  113. }
  114. public function whereJoin($join_table_name, $cond, $value)
  115. {
  116. $join_table_placeholder = $this->getTablePh($join_table_name);
  117. if (is_array($cond)) {
  118. $cond_replace = array();
  119. foreach ($cond as $key => $value) {
  120. $cond_replace[$this->getCondWithTablePlaceholderIfNeed($join_table_placeholder, $key)] = $value;
  121. }
  122. } else {
  123. $cond = $this->getCondWithTablePlaceholderIfNeed($join_table_placeholder, $cond);
  124. }
  125. return $this->where($cond, $value);
  126. }
  127. private function getCondWithTablePlaceholderIfNeed($table_placeholder, $cond)
  128. {
  129. if (!strstr('.', $cond)) {
  130. $cond = $table_placeholder . '.' . $cond;
  131. }
  132. return $cond;
  133. }
  134. /**
  135. * @param $field string
  136. * @param $value array
  137. * @return SqlCriteria
  138. */
  139. public function whereIn($field, $value)
  140. {
  141. return $this->where($field . ' in ?', $value);
  142. }
  143. /**
  144. * @param $field string
  145. * @param $value array
  146. * @return SqlCriteria
  147. */
  148. public function whereNotIn($field, $value)
  149. {
  150. return $this->where($field . ' not in ?', $value);
  151. }
  152. /**
  153. * @param $field string
  154. * @param $value array
  155. * @return SqlCriteria
  156. * @deprecated
  157. */
  158. public function whereNot($field, $value)
  159. {
  160. return $this->whereNotIn($field, $value);
  161. }
  162. public function groupBy($fields)
  163. {
  164. if (is_array($fields)) {
  165. $this->group_by = $this->group_by + $fields;
  166. } else {
  167. $this->group_by[] = $fields;
  168. }
  169. return $this;
  170. }
  171. /**
  172. * @param $field string Field @ex 'field'
  173. * @param $order_desc bool Descendant sort direction
  174. * @return SqlCriteria
  175. * @desc Allow multiple calls
  176. */
  177. public function order($field, $order_desc = false)
  178. {
  179. $this->order['sort'][] = $field;
  180. if ($order_desc) {
  181. $this->order['order'][$field] = 'desc';
  182. }
  183. return $this;
  184. }
  185. /**
  186. * @param $offset int
  187. * @param $limit int
  188. * @return SqlCriteria
  189. */
  190. public function limit($offset = 0, $limit)
  191. {
  192. if ($offset) {
  193. $this->limit = (int) $offset . ',';
  194. }
  195. $this->limit .= (int) $limit;
  196. return $this;
  197. }
  198. /**
  199. * @param string|array $fields
  200. * @param bool $convert_to_db_expression
  201. * @ex SqlCriteria::select('field')
  202. * @ex SqlCriteria->select(array('field1', 'field2'))
  203. * @ex SqlCriteria->select('field1,field2')
  204. * @return SqlCriteria
  205. */
  206. public function select($fields, $convert_to_db_expression = false)
  207. {
  208. if (!is_array($fields)) {
  209. $fields = explode(',', $fields);
  210. }
  211. $fields = array_map(function($item){return trim($item);},$fields);
  212. if ($convert_to_db_expression) {
  213. $fields = array_map(function($item){return new DbExpr($item);},$fields);
  214. }
  215. $this->select = array_merge($this->select,$fields);
  216. return $this;
  217. }
  218. /**
  219. * @param $field string|bool If true then distinct by *
  220. * @return SqlCriteria
  221. */
  222. public function distinct($field)
  223. {
  224. $this->distinct = $field;
  225. return $this;
  226. }
  227. }