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.

237 lines
7.0 KiB

  1. <?php
  2. /**
  3. * Класс модели данных
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  6. * @link http://netmonsters.ru
  7. * @package Majestic
  8. * @subpackage Model
  9. * @since 2011-11-11
  10. */
  11. /**
  12. * @property SqlDbDriver $db
  13. */
  14. abstract class SqlModel extends Model
  15. {
  16. /**
  17. * @param string $ident
  18. * @return string Quoted identifier.
  19. */
  20. public function identify($ident)
  21. {
  22. return $this->db->quoteIdentifier($ident);
  23. }
  24. /**
  25. * @param mixed $value
  26. * @return string Quoted value.
  27. */
  28. public function quote($value)
  29. {
  30. return $this->db->quote($value);
  31. }
  32. public function getDb()
  33. {
  34. return $this->db;
  35. }
  36. /**
  37. * @param int $id
  38. * @return object
  39. */
  40. public function get($id)
  41. {
  42. $sql = 'SELECT * FROM :table WHERE :pk=?';
  43. return $this->fetch($sql, $id);
  44. }
  45. /**
  46. * @param array $data
  47. * @param mixed $where
  48. * @return int Number of affected rows
  49. */
  50. public function update($data, $where)
  51. {
  52. if (is_int($where) || $where === (string)(int)$where) {
  53. $where = $this->identify($this->key) . '=' . (int)$where;
  54. }
  55. return parent::update($data, $where);
  56. }
  57. /**
  58. * @param int $id Int id
  59. * @return int Number of affected rows
  60. */
  61. public function delete($id)
  62. {
  63. $where = $this->identify($this->key) . '=' . (int)$id;
  64. return $this->db->delete($this->table(), $where);
  65. }
  66. /**
  67. * Creates order sql string
  68. *
  69. * @param array $params
  70. * @param array $sortable
  71. * @return string
  72. */
  73. protected function order($params, $sortable = array('id'))
  74. {
  75. $sql = '';
  76. if (isset($params['sort'])) {
  77. if (!is_array($params['sort'])) {
  78. if (isset($params['order'])) {
  79. $params['order'] = array($params['sort'] => $params['order']);
  80. }
  81. $params['sort'] = array($params['sort']);
  82. }
  83. $order_list = array();
  84. for ($i = 0; $i < count($params['sort']); $i++) {
  85. $order = (isset($params['order'][$params['sort'][$i]]) && $params['order'][$params['sort'][$i]] == 'desc') ? 'DESC' : 'ASC';
  86. if (in_array($params['sort'][$i], $sortable)) {
  87. $order_list[] = $this->identify($params['sort'][$i]) . ' ' . $order;
  88. }
  89. }
  90. if ($order_list) {
  91. $sql = ' ORDER BY ' . implode(',', $order_list);
  92. }
  93. }
  94. return $sql;
  95. }
  96. /**
  97. * Searches using like
  98. *
  99. * @param array $params
  100. * @param array $searchable
  101. * @param string $table_prefix
  102. * @return string
  103. */
  104. protected function search($params, $searchable = array('id'), $table_prefix = '')
  105. {
  106. $sql = '';
  107. if (isset($params['q']) && isset($params['qt']) && in_array($params['qt'], $searchable)) {
  108. if ($table_prefix) {
  109. $sql = $table_prefix . '.';
  110. }
  111. $sql .= $this->identify($params['qt']) . ' LIKE ' . $this->quote('%' . $params['q'] . '%');
  112. }
  113. return $sql;
  114. }
  115. /**
  116. * This method appends to params table and primary key.
  117. * So they can be accessed through `:table` and `:pk` placeholders.
  118. *
  119. * @param string $sql
  120. * @param array $params
  121. * @return DbStatement
  122. */
  123. protected function query($sql, $params = array())
  124. {
  125. if (!is_array($params)) {
  126. $params = array($params);
  127. }
  128. $params = array(
  129. 'table' => new DbExpr($this->identify($this->table())),
  130. 'pk' => new DbExpr($this->identify($this->key)),
  131. ) + $params;
  132. return $this->db->query($sql, $params);
  133. }
  134. /**
  135. * @param string $data Request
  136. * @param array $params Request parameters
  137. * @param string $field Requested field name
  138. * @param CacheKey $cache_key Key for caching in
  139. * @return mixed
  140. */
  141. protected function fetchField($data, $params = array(), $field, $cache_key = null)
  142. {
  143. if (!$cache_key || !$result = $cache_key->get()) {
  144. $result = $this->query($data, $params)->fetchField($field);
  145. if ($cache_key) {
  146. $cache_key->set($result);
  147. }
  148. }
  149. return $result;
  150. }
  151. /**
  152. * @param string $data Request
  153. * @param array $params Request parameters
  154. * @param CacheKey $cache_key Key for caching in
  155. * @return mixed
  156. */
  157. protected function fetch($data, $params = array(), $cache_key = null)
  158. {
  159. if (!$cache_key || !$result = $cache_key->get()) {
  160. $result = $this->query($data, $params)->fetch();
  161. if ($cache_key) {
  162. $cache_key->set($result);
  163. }
  164. }
  165. return $result;
  166. }
  167. /**
  168. * @param string $data
  169. * @param array $params
  170. * @param CacheKey $cache_key
  171. * @return array
  172. */
  173. protected function fetchAll($data, $params = array(), $cache_key = null)
  174. {
  175. if (!$cache_key || !$result = $cache_key->get()) {
  176. $result = $this->query($data, $params)->fetchAll();
  177. if ($cache_key) {
  178. $cache_key->set($result);
  179. }
  180. }
  181. return $result;
  182. }
  183. /**
  184. * @param $select array
  185. * @param $distinct string|bool
  186. * @param $where array @ex array('field=?' => $value, 'field=1')
  187. * @param $order array @ex array('sort' => array('field1', 'field2'), 'order' => array('field2' => 'desc'))
  188. * @param $limit string @ex '30' or '30,30'
  189. * @param $heaving TODO
  190. * @param $group_by TODO
  191. * @param $sql_expression null|string
  192. * @param $sql_expression_params array
  193. * @param $cache_key CacheKey|null
  194. * @return SqlResultProvider
  195. */
  196. public function find($select, $distinct, $where, $order, $limit, $heaving = null, $group_by = null, $sql_expression = null, $sql_expression_params = array(), $cache_key = null)
  197. {
  198. $select = $this->db->selectExpr($select, $distinct);
  199. $where = $this->db->whereExpr($where);
  200. $group_by = $this->db->groupByExpr($group_by);
  201. $order = isset($order['sort']) ? $this->order($order, $order['sort']) : false;
  202. $limit = $this->db->limitExpr($limit);
  203. $result = $this->query(
  204. (($sql_expression) ? $sql_expression : ('SELECT ' . $select . ' FROM ' . $this->table()))
  205. . (($where) ? (' WHERE ' . $where) : '')
  206. . (($group_by) ? (' GROUP BY ' . $group_by) : '')
  207. . (($order) ? ($order) : '')
  208. . (($limit) ? (' LIMIT ' . $limit) : ''),
  209. $sql_expression_params,
  210. $cache_key
  211. );
  212. return new SqlResultProvider($result);
  213. }
  214. /**
  215. * @param $sql_expression null
  216. * @param $sql_expression_params array
  217. * @return SqlCriteria
  218. */
  219. public function criteria($sql_expression = null, $sql_expression_params = array())
  220. {
  221. return new SqlCriteria($this, $sql_expression, $sql_expression_params);
  222. }
  223. }