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.

262 lines
8.1 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. if ($this->sql_expression && strpos($this->sql_expression, 'COUNT(*) as count')) {
  66. ;
  67. } else {
  68. $this->sql_expression = 'SELECT COUNT(*) as count FROM :table';
  69. }
  70. $this->defineJoinExpressions();
  71. return $this->model->find(array(), '', $this->where, null, null, null, null, $this->sql_expression, $this->sql_expression_params)->fetchField('count');
  72. }
  73. private function defineJoinTablePlaceholder($table_name)
  74. {
  75. if (!isset($this->join_table_placeholder[$table_name])) {
  76. $this->join_table_placeholder[$table_name] = ':table' . (count($this->join_table_placeholder) + 1);
  77. }
  78. }
  79. public function getTablePh($table_name)
  80. {
  81. $this->defineJoinTablePlaceholder($table_name);
  82. return $this->join_table_placeholder[$table_name];
  83. }
  84. public function join($join_table_name, $join_field_name, $donor_table_name = null, $donor_field_name = null, $join_type = self::JOIN_TYPE_DEFAULT)
  85. {
  86. $donor_field_name = $donor_field_name ? : $join_field_name;
  87. $donor_table_placeholder = $donor_table_name ? $this->getTablePh($donor_table_name) : ':table';
  88. $join_table_placeholder = $this->getTablePh($join_table_name);
  89. $this->sql_join_expressions[] = self::$join_reserved_keyword[$join_type]
  90. . ' ' . $join_table_placeholder
  91. . ' ON ' . $donor_table_placeholder . '.' . $donor_field_name . '='
  92. . ' ' . $join_table_placeholder . '.' . $join_field_name;
  93. if ($donor_table_name) {
  94. $this->sql_expression_params[substr($donor_table_placeholder, 1)] = new DbExpr($this->model->identify($donor_table_name));
  95. }
  96. $this->sql_expression_params[substr($join_table_placeholder, 1)] = new DbExpr($this->model->identify($join_table_name));
  97. return $this;
  98. }
  99. /**
  100. * @param $cond string|array Condition with "?" placeholder @ex 'field=?' or 'field=1' or array('field=?' => 1', 'field=1')
  101. * @param $value string|array|DbExpr|null Value. Array transformed to DbExpr(implode(',' Array)) All elements in the array mast be integer
  102. * @return SqlCriteria
  103. * @desc Allow multiple calls
  104. */
  105. public function where($cond, $value = null)
  106. {
  107. if (is_null($value)) {
  108. if (is_array($cond)) {
  109. $this->where = $this->where + $cond;
  110. } else {
  111. $this->where[] = $cond;
  112. }
  113. } else {
  114. $this->where[$cond] = $value;
  115. }
  116. return $this;
  117. }
  118. public function whereJoin($join_table_name, $cond, $value)
  119. {
  120. $join_table_placeholder = $this->getTablePh($join_table_name);
  121. if (is_array($cond)) {
  122. $cond_replace = array();
  123. foreach ($cond as $key => $value) {
  124. $cond_replace[$this->getCondWithTablePlaceholderIfNeed($join_table_placeholder, $key)] = $value;
  125. }
  126. } else {
  127. $cond = $this->getCondWithTablePlaceholderIfNeed($join_table_placeholder, $cond);
  128. }
  129. return $this->where($cond, $value);
  130. }
  131. private function getCondWithTablePlaceholderIfNeed($table_placeholder, $cond)
  132. {
  133. if (!strstr('.', $cond)) {
  134. $cond = $table_placeholder . '.' . $cond;
  135. }
  136. return $cond;
  137. }
  138. /**
  139. * @param $field string
  140. * @param $value array
  141. * @return SqlCriteria
  142. */
  143. public function whereIn($field, $value)
  144. {
  145. return $this->where($field . ' in ?', $value);
  146. }
  147. /**
  148. * @param $field string
  149. * @param $value array
  150. * @return SqlCriteria
  151. */
  152. public function whereNotIn($field, $value)
  153. {
  154. return $this->where($field . ' not in ?', $value);
  155. }
  156. /**
  157. * @param $field string
  158. * @param $value array
  159. * @return SqlCriteria
  160. * @deprecated
  161. */
  162. public function whereNot($field, $value)
  163. {
  164. return $this->whereNotIn($field, $value);
  165. }
  166. public function groupBy($fields)
  167. {
  168. if (is_array($fields)) {
  169. $this->group_by = $this->group_by + $fields;
  170. } else {
  171. $this->group_by[] = $fields;
  172. }
  173. return $this;
  174. }
  175. /**
  176. * @param $field string Field @ex 'field'
  177. * @param $order_desc bool Descendant sort direction
  178. * @return SqlCriteria
  179. * @desc Allow multiple calls
  180. */
  181. public function order($field, $order_desc = false)
  182. {
  183. $this->order['sort'][] = $field;
  184. if ($order_desc) {
  185. $this->order['order'][$field] = 'desc';
  186. }
  187. return $this;
  188. }
  189. /**
  190. * @param $offset int
  191. * @param $limit int
  192. * @return SqlCriteria
  193. */
  194. public function limit($offset = 0, $limit)
  195. {
  196. if ($offset) {
  197. $this->limit = (int) $offset . ',';
  198. }
  199. $this->limit .= (int) $limit;
  200. return $this;
  201. }
  202. /**
  203. * @param string|array $fields
  204. * @param bool $convert_to_db_expression
  205. * @ex SqlCriteria::select('field')
  206. * @ex SqlCriteria->select(array('field1', 'field2'))
  207. * @ex SqlCriteria->select('field1,field2')
  208. * @return SqlCriteria
  209. */
  210. public function select($fields, $convert_to_db_expression = false)
  211. {
  212. if (!is_array($fields)) {
  213. $fields = explode(',', $fields);
  214. }
  215. $fields = array_map(function($item){return trim($item);},$fields);
  216. if ($convert_to_db_expression) {
  217. $fields = array_map(function($item){return new DbExpr($item);},$fields);
  218. }
  219. $this->select = array_merge($this->select,$fields);
  220. return $this;
  221. }
  222. /**
  223. * @param $field string|bool If true then distinct by *
  224. * @return SqlCriteria
  225. */
  226. public function distinct($field)
  227. {
  228. $this->distinct = $field;
  229. return $this;
  230. }
  231. }