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.

266 lines
8.4 KiB

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