PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/database/databasequery.php

https://github.com/o0khoiclub0o/comvi
PHP | 463 lines | 253 code | 75 blank | 135 comment | 27 complexity | 76bb1332d4a7ec614fb3b6e6ca53a730 MD5 | raw file
  1. <?php
  2. /**
  3. * Query Building Class.
  4. *
  5. * @package Comvi.Framework
  6. * @subpackage Database
  7. */
  8. class CDatabaseQuery
  9. {
  10. /**
  11. * @var string The query type.
  12. */
  13. protected $_type = '';
  14. /**
  15. * @var object The select element.
  16. */
  17. protected $_select = null;
  18. /**
  19. * @var object The delete element.
  20. */
  21. protected $_delete = null;
  22. /**
  23. * @var object The update element.
  24. */
  25. protected $_update = null;
  26. /**
  27. * @var object The insert element.
  28. */
  29. protected $_insert = null;
  30. /**
  31. * @var object The from element.
  32. */
  33. protected $_from = null;
  34. /**
  35. * @var object The join element.
  36. */
  37. protected $_join = null;
  38. /**
  39. * @var object The set element.
  40. */
  41. protected $_set = null;
  42. /**
  43. * @var object The where element.
  44. */
  45. protected $_where = null;
  46. /**
  47. * @var object The group by element.
  48. */
  49. protected $_group = null;
  50. /**
  51. * @var object The having element.
  52. */
  53. protected $_having = null;
  54. /**
  55. * @var object The order element.
  56. */
  57. protected $_order = null;
  58. /**
  59. * Clear data from the query or a specific clause of the query.
  60. *
  61. * @param string $clear Optionally, the name of the clause to clear, or nothing to clear the whole query.
  62. *
  63. * @return void
  64. */
  65. public function clear($clause = null)
  66. {
  67. switch ($clause)
  68. {
  69. case 'select':
  70. $this->_select = null;
  71. $this->_type = null;
  72. break;
  73. case 'delete':
  74. $this->_delete = null;
  75. $this->_type = null;
  76. break;
  77. case 'update':
  78. $this->_update = null;
  79. $this->_type = null;
  80. break;
  81. case 'insert':
  82. $this->_insert = null;
  83. $this->_type = null;
  84. break;
  85. case 'from':
  86. $this->_from = null;
  87. break;
  88. case 'join':
  89. $this->_join = null;
  90. break;
  91. case 'set':
  92. $this->_set = null;
  93. break;
  94. case 'where':
  95. $this->_where = null;
  96. break;
  97. case 'group':
  98. $this->_group = null;
  99. break;
  100. case 'having':
  101. $this->_having = null;
  102. break;
  103. case 'order':
  104. $this->_order = null;
  105. break;
  106. default:
  107. $this->_type = null;
  108. $this->_select = null;
  109. $this->_delete = null;
  110. $this->_update = null;
  111. $this->_insert = null;
  112. $this->_from = null;
  113. $this->_join = null;
  114. $this->_set = null;
  115. $this->_where = null;
  116. $this->_group = null;
  117. $this->_having = null;
  118. $this->_order = null;
  119. break;
  120. }
  121. return $this;
  122. }
  123. /**
  124. * @param mixed $columns A string or an array of field names.
  125. *
  126. * @return CDatabaseQuery Returns this object to allow chaining.
  127. */
  128. public function select($columns)
  129. {
  130. $this->_type = 'select';
  131. if (is_null($this->_select)) {
  132. $this->_select = new CDatabaseQueryElement('SELECT', $columns);
  133. }
  134. else {
  135. $this->_select->append($columns);
  136. }
  137. return $this;
  138. }
  139. /**
  140. * @param string $table The name of the table to delete from.
  141. *
  142. * @return CDatabaseQuery Returns this object to allow chaining.
  143. */
  144. public function delete($table = null)
  145. {
  146. $this->_type = 'delete';
  147. $this->_delete = new CDatabaseQueryElement('DELETE', null);
  148. if (!empty($table)) {
  149. $this->from($table);
  150. }
  151. return $this;
  152. }
  153. /**
  154. * @param mixed $tables A string or array of table names.
  155. *
  156. * @return CDatabaseQuery Returns this object to allow chaining.
  157. */
  158. public function insert($tables)
  159. {
  160. $this->_type = 'insert';
  161. $this->_insert = new CDatabaseQueryElement('INSERT INTO', $tables);
  162. return $this;
  163. }
  164. /**
  165. * @param mixed $tables A string or array of table names.
  166. *
  167. * @return CDatabaseQuery Returns this object to allow chaining.
  168. */
  169. public function update($tables)
  170. {
  171. $this->_type = 'update';
  172. $this->_update = new CDatabaseQueryElement('UPDATE', $tables);
  173. return $this;
  174. }
  175. /**
  176. * @param mixed A string or array of table names.
  177. *
  178. * @return CDatabaseQuery Returns this object to allow chaining.
  179. */
  180. public function from($tables)
  181. {
  182. if (is_null($this->_from)) {
  183. $this->_from = new CDatabaseQueryElement('FROM', $tables);
  184. }
  185. else {
  186. $this->_from->append($tables);
  187. }
  188. return $this;
  189. }
  190. /**
  191. * @param string $type
  192. * @param string $conditions
  193. *
  194. * @return CDatabaseQuery Returns this object to allow chaining.
  195. */
  196. public function join($type, $conditions)
  197. {
  198. if (is_null($this->_join)) {
  199. $this->_join = array();
  200. }
  201. $this->_join[] = new CDatabaseQueryElement(strtoupper($type) . ' JOIN', $conditions);
  202. return $this;
  203. }
  204. /**
  205. * @param string $conditions
  206. *
  207. * @return CDatabaseQuery Returns this object to allow chaining.
  208. */
  209. public function innerJoin($conditions)
  210. {
  211. $this->join('INNER', $conditions);
  212. return $this;
  213. }
  214. /**
  215. * @param string $conditions
  216. *
  217. * @return CDatabaseQuery Returns this object to allow chaining.
  218. */
  219. public function outerJoin($conditions)
  220. {
  221. $this->join('OUTER', $conditions);
  222. return $this;
  223. }
  224. /**
  225. * @param string $conditions
  226. *
  227. * @return CDatabaseQuery Returns this object to allow chaining.
  228. */
  229. public function leftJoin($conditions)
  230. {
  231. $this->join('LEFT', $conditions);
  232. return $this;
  233. }
  234. /**
  235. * @param string $conditions
  236. *
  237. * @return CDatabaseQuery Returns this object to allow chaining.
  238. */
  239. public function rightJoin($conditions)
  240. {
  241. $this->join('RIGHT', $conditions);
  242. return $this;
  243. }
  244. /**
  245. * @param mixed $conditions A string or array of conditions.
  246. * @param string $glue
  247. *
  248. * @return CDatabaseQuery Returns this object to allow chaining.
  249. */
  250. public function set($conditions, $glue=',')
  251. {
  252. if (is_null($this->_set)) {
  253. $glue = strtoupper($glue);
  254. $this->_set = new CDatabaseQueryElement('SET', $conditions, "\n\t$glue ");
  255. }
  256. else {
  257. $this->_set->append($conditions);
  258. }
  259. return $this;
  260. }
  261. /**
  262. * @param mixed $conditions A string or array of where conditions.
  263. * @param string $glue
  264. *
  265. * @return CDatabaseQuery Returns this object to allow chaining.
  266. */
  267. public function where($conditions, $glue = 'AND')
  268. {
  269. if (is_null($this->_where)) {
  270. $glue = strtoupper($glue);
  271. $this->_where = new CDatabaseQueryElement('WHERE', $conditions, " $glue ");
  272. }
  273. else {
  274. $this->_where->append($conditions);
  275. }
  276. return $this;
  277. }
  278. /**
  279. * @param mixed $columns A string or array of ordering columns.
  280. *
  281. * @return CDatabaseQuery Returns this object to allow chaining.
  282. */
  283. public function group($columns)
  284. {
  285. if (is_null($this->_group)) {
  286. $this->_group = new CDatabaseQueryElement('GROUP BY', $columns);
  287. }
  288. else {
  289. $this->_group->append($columns);
  290. }
  291. return $this;
  292. }
  293. /**
  294. * @param mixed $conditions A string or array of columns.
  295. * @param string $glue
  296. *
  297. * @return CDatabaseQuery Returns this object to allow chaining.
  298. */
  299. public function having($conditions, $glue='AND')
  300. {
  301. if (is_null($this->_having)) {
  302. $glue = strtoupper($glue);
  303. $this->_having = new CDatabaseQueryElement('HAVING', $conditions, " $glue ");
  304. }
  305. else {
  306. $this->_having->append($conditions);
  307. }
  308. return $this;
  309. }
  310. /**
  311. * @param mixed $columns A string or array of ordering columns.
  312. *
  313. * @return CDatabaseQuery Returns this object to allow chaining.
  314. */
  315. public function order($columns)
  316. {
  317. if (is_null($this->_order)) {
  318. $this->_order = new CDatabaseQueryElement('ORDER BY', $columns);
  319. }
  320. else {
  321. $this->_order->append($columns);
  322. }
  323. return $this;
  324. }
  325. /**
  326. * Magic function to convert the query to a string.
  327. *
  328. * @return string The completed query.
  329. */
  330. public function __toString()
  331. {
  332. $query = '';
  333. switch ($this->_type)
  334. {
  335. case 'select':
  336. $query .= (string) $this->_select;
  337. $query .= (string) $this->_from;
  338. if ($this->_join) {
  339. // special case for joins
  340. foreach ($this->_join as $join) {
  341. $query .= (string) $join;
  342. }
  343. }
  344. if ($this->_where) {
  345. $query .= (string) $this->_where;
  346. }
  347. if ($this->_group) {
  348. $query .= (string) $this->_group;
  349. }
  350. if ($this->_having) {
  351. $query .= (string) $this->_having;
  352. }
  353. if ($this->_order) {
  354. $query .= (string) $this->_order;
  355. }
  356. break;
  357. case 'delete':
  358. $query .= (string) $this->_delete;
  359. $query .= (string) $this->_from;
  360. if ($this->_join) {
  361. // special case for joins
  362. foreach ($this->_join as $join) {
  363. $query .= (string) $join;
  364. }
  365. }
  366. if ($this->_where) {
  367. $query .= (string) $this->_where;
  368. }
  369. break;
  370. case 'update':
  371. $query .= (string) $this->_update;
  372. $query .= (string) $this->_set;
  373. if ($this->_where) {
  374. $query .= (string) $this->_where;
  375. }
  376. break;
  377. case 'insert':
  378. $query .= (string) $this->_insert;
  379. $query .= (string) $this->_set;
  380. if ($this->_where) {
  381. $query .= (string) $this->_where;
  382. }
  383. break;
  384. }
  385. return $query;
  386. }
  387. }
  388. ?>