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.

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