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.

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