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.

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