/libraries/joomla/database/databasequery.php

https://github.com/pacoqueen/callao_chico · PHP · 576 lines · 279 code · 84 blank · 213 comment · 29 complexity · 3b851a5da15df441b30a17521eccd717 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: databasequery.php 20824 2011-02-21 23:12:11Z dextercowley $
  4. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. defined('JPATH_BASE') or die;
  8. /**
  9. * Query Element Class.
  10. *
  11. * @package Joomla.Framework
  12. * @subpackage Database
  13. * @since 1.6
  14. */
  15. class JDatabaseQueryElement
  16. {
  17. /**
  18. * @var string The name of the element.
  19. * @since 1.6
  20. */
  21. protected $_name = null;
  22. /**
  23. * @var array An array of elements.
  24. * @since 1.6
  25. */
  26. protected $_elements = null;
  27. /**
  28. * @var string Glue piece.
  29. * @since 1.6
  30. */
  31. protected $_glue = null;
  32. /**
  33. * Constructor.
  34. *
  35. * @param string $name The name of the element.
  36. * @param mixed $elements String or array.
  37. * @param string $glue The glue for elements.
  38. *
  39. * @return JDatabaseQueryElement
  40. * @since 1.6
  41. */
  42. public function __construct($name, $elements, $glue = ',')
  43. {
  44. $this->_elements = array();
  45. $this->_name = $name;
  46. $this->_glue = $glue;
  47. $this->append($elements);
  48. }
  49. /**
  50. * Magic function to convert the query element to a string.
  51. *
  52. * @return string
  53. * @since 1.6
  54. */
  55. public function __toString()
  56. {
  57. return PHP_EOL.$this->_name.' '.implode($this->_glue, $this->_elements);
  58. }
  59. /**
  60. * Appends element parts to the internal list.
  61. *
  62. * @param mixed String or array.
  63. *
  64. * @return void
  65. * @since 1.6
  66. */
  67. public function append($elements)
  68. {
  69. if (is_array($elements)) {
  70. $this->_elements = array_unique(array_merge($this->_elements, $elements));
  71. }
  72. else {
  73. $this->_elements = array_unique(array_merge($this->_elements, array($elements)));
  74. }
  75. }
  76. }
  77. /**
  78. * Query Building Class.
  79. *
  80. * @package Joomla.Framework
  81. * @subpackage Database
  82. * @since 1.6
  83. */
  84. class JDatabaseQuery
  85. {
  86. /**
  87. * @var string The query type.
  88. * @since 1.6
  89. */
  90. protected $_type = '';
  91. /**
  92. * @var object The select element.
  93. * @since 1.6
  94. */
  95. protected $_select = null;
  96. /**
  97. * @var object The delete element.
  98. * @since 1.6
  99. */
  100. protected $_delete = null;
  101. /**
  102. * @var object The update element.
  103. * @since 1.6
  104. */
  105. protected $_update = null;
  106. /**
  107. * @var object The insert element.
  108. * @since 1.6
  109. */
  110. protected $_insert = null;
  111. /**
  112. * @var object The from element.
  113. * @since 1.6
  114. */
  115. protected $_from = null;
  116. /**
  117. * @var object The join element.
  118. * @since 1.6
  119. */
  120. protected $_join = null;
  121. /**
  122. * @var object The set element.
  123. * @since 1.6
  124. */
  125. protected $_set = null;
  126. /**
  127. * @var object The where element.
  128. * @since 1.6
  129. */
  130. protected $_where = null;
  131. /**
  132. * @var object The group by element.
  133. * @since 1.6
  134. */
  135. protected $_group = null;
  136. /**
  137. * @var object The having element.
  138. * @since 1.6
  139. */
  140. protected $_having = null;
  141. /**
  142. * @var object The order element.
  143. * @since 1.6
  144. */
  145. protected $_order = null;
  146. /**
  147. * Clear data from the query or a specific clause of the query.
  148. *
  149. * @param string $clear Optionally, the name of the clause to clear, or nothing to clear the whole query.
  150. *
  151. * @return void
  152. * @since 1.6
  153. */
  154. public function clear($clause = null)
  155. {
  156. switch ($clause)
  157. {
  158. case 'select':
  159. $this->_select = null;
  160. $this->_type = null;
  161. break;
  162. case 'delete':
  163. $this->_delete = null;
  164. $this->_type = null;
  165. break;
  166. case 'update':
  167. $this->_update = null;
  168. $this->_type = null;
  169. break;
  170. case 'insert':
  171. $this->_insert = null;
  172. $this->_type = null;
  173. break;
  174. case 'from':
  175. $this->_from = null;
  176. break;
  177. case 'join':
  178. $this->_join = null;
  179. break;
  180. case 'set':
  181. $this->_set = null;
  182. break;
  183. case 'where':
  184. $this->_where = null;
  185. break;
  186. case 'group':
  187. $this->_group = null;
  188. break;
  189. case 'having':
  190. $this->_having = null;
  191. break;
  192. case 'order':
  193. $this->_order = null;
  194. break;
  195. default:
  196. $this->_type = null;
  197. $this->_select = null;
  198. $this->_delete = null;
  199. $this->_update = null;
  200. $this->_insert = null;
  201. $this->_from = null;
  202. $this->_join = null;
  203. $this->_set = null;
  204. $this->_where = null;
  205. $this->_group = null;
  206. $this->_having = null;
  207. $this->_order = null;
  208. break;
  209. }
  210. return $this;
  211. }
  212. /**
  213. * @param mixed $columns A string or an array of field names.
  214. *
  215. * @return JDatabaseQuery Returns this object to allow chaining.
  216. * @since 1.6
  217. */
  218. public function select($columns)
  219. {
  220. $this->_type = 'select';
  221. if (is_null($this->_select)) {
  222. $this->_select = new JDatabaseQueryElement('SELECT', $columns);
  223. }
  224. else {
  225. $this->_select->append($columns);
  226. }
  227. return $this;
  228. }
  229. /**
  230. * @param string $table The name of the table to delete from.
  231. *
  232. * @return JDatabaseQuery Returns this object to allow chaining.
  233. * @since 1.6
  234. */
  235. public function delete($table = null)
  236. {
  237. $this->_type = 'delete';
  238. $this->_delete = new JDatabaseQueryElement('DELETE', null);
  239. if (!empty($table)) {
  240. $this->from($table);
  241. }
  242. return $this;
  243. }
  244. /**
  245. * @param mixed $tables A string or array of table names.
  246. *
  247. * @return JDatabaseQuery Returns this object to allow chaining.
  248. * @since 1.6
  249. */
  250. public function insert($tables)
  251. {
  252. $this->_type = 'insert';
  253. $this->_insert = new JDatabaseQueryElement('INSERT INTO', $tables);
  254. return $this;
  255. }
  256. /**
  257. * @param mixed $tables A string or array of table names.
  258. *
  259. * @return JDatabaseQuery Returns this object to allow chaining.
  260. * @since 1.6
  261. */
  262. public function update($tables)
  263. {
  264. $this->_type = 'update';
  265. $this->_update = new JDatabaseQueryElement('UPDATE', $tables);
  266. return $this;
  267. }
  268. /**
  269. * @param mixed A string or array of table names.
  270. *
  271. * @return JDatabaseQuery Returns this object to allow chaining.
  272. * @since 1.6
  273. */
  274. public function from($tables)
  275. {
  276. if (is_null($this->_from)) {
  277. $this->_from = new JDatabaseQueryElement('FROM', $tables);
  278. }
  279. else {
  280. $this->_from->append($tables);
  281. }
  282. return $this;
  283. }
  284. /**
  285. * @param string $type
  286. * @param string $conditions
  287. *
  288. * @return JDatabaseQuery Returns this object to allow chaining.
  289. * @since 1.6
  290. */
  291. public function join($type, $conditions)
  292. {
  293. if (is_null($this->_join)) {
  294. $this->_join = array();
  295. }
  296. $this->_join[] = new JDatabaseQueryElement(strtoupper($type) . ' JOIN', $conditions);
  297. return $this;
  298. }
  299. /**
  300. * @param string $conditions
  301. *
  302. * @return JDatabaseQuery Returns this object to allow chaining.
  303. * @since 1.6
  304. */
  305. public function innerJoin($conditions)
  306. {
  307. $this->join('INNER', $conditions);
  308. return $this;
  309. }
  310. /**
  311. * @param string $conditions
  312. *
  313. * @return JDatabaseQuery Returns this object to allow chaining.
  314. * @since 1.6
  315. */
  316. public function outerJoin($conditions)
  317. {
  318. $this->join('OUTER', $conditions);
  319. return $this;
  320. }
  321. /**
  322. * @param string $conditions
  323. *
  324. * @return JDatabaseQuery Returns this object to allow chaining.
  325. * @since 1.6
  326. */
  327. public function leftJoin($conditions)
  328. {
  329. $this->join('LEFT', $conditions);
  330. return $this;
  331. }
  332. /**
  333. * @param string $conditions
  334. *
  335. * @return JDatabaseQuery Returns this object to allow chaining.
  336. * @since 1.6
  337. */
  338. public function rightJoin($conditions)
  339. {
  340. $this->join('RIGHT', $conditions);
  341. return $this;
  342. }
  343. /**
  344. * @param mixed $conditions A string or array of conditions.
  345. * @param string $glue
  346. *
  347. * @return JDatabaseQuery Returns this object to allow chaining.
  348. * @since 1.6
  349. */
  350. public function set($conditions, $glue=',')
  351. {
  352. if (is_null($this->_set)) {
  353. $glue = strtoupper($glue);
  354. $this->_set = new JDatabaseQueryElement('SET', $conditions, "\n\t$glue ");
  355. }
  356. else {
  357. $this->_set->append($conditions);
  358. }
  359. return $this;
  360. }
  361. /**
  362. * @param mixed $conditions A string or array of where conditions.
  363. * @param string $glue
  364. *
  365. * @return JDatabaseQuery Returns this object to allow chaining.
  366. * @since 1.6
  367. */
  368. public function where($conditions, $glue='AND')
  369. {
  370. if (is_null($this->_where)) {
  371. $glue = strtoupper($glue);
  372. $this->_where = new JDatabaseQueryElement('WHERE', $conditions, " $glue ");
  373. }
  374. else {
  375. $this->_where->append($conditions);
  376. }
  377. return $this;
  378. }
  379. /**
  380. * @param mixed $columns A string or array of ordering columns.
  381. *
  382. * @return JDatabaseQuery Returns this object to allow chaining.
  383. * @since 1.6
  384. */
  385. public function group($columns)
  386. {
  387. if (is_null($this->_group)) {
  388. $this->_group = new JDatabaseQueryElement('GROUP BY', $columns);
  389. }
  390. else {
  391. $this->_group->append($columns);
  392. }
  393. return $this;
  394. }
  395. /**
  396. * @param mixed $conditions A string or array of columns.
  397. * @param string $glue
  398. *
  399. * @return JDatabaseQuery Returns this object to allow chaining.
  400. * @since 1.6
  401. */
  402. public function having($conditions, $glue='AND')
  403. {
  404. if (is_null($this->_having)) {
  405. $glue = strtoupper($glue);
  406. $this->_having = new JDatabaseQueryElement('HAVING', $conditions, " $glue ");
  407. }
  408. else {
  409. $this->_having->append($conditions);
  410. }
  411. return $this;
  412. }
  413. /**
  414. * @param mixed $columns A string or array of ordering columns.
  415. *
  416. * @return JDatabaseQuery Returns this object to allow chaining.
  417. * @since 1.6
  418. */
  419. public function order($columns)
  420. {
  421. if (is_null($this->_order)) {
  422. $this->_order = new JDatabaseQueryElement('ORDER BY', $columns);
  423. }
  424. else {
  425. $this->_order->append($columns);
  426. }
  427. return $this;
  428. }
  429. /**
  430. * Magic function to convert the query to a string.
  431. *
  432. * @return string The completed query.
  433. * @since 1.6
  434. */
  435. public function __toString()
  436. {
  437. $query = '';
  438. switch ($this->_type)
  439. {
  440. case 'select':
  441. $query .= (string) $this->_select;
  442. $query .= (string) $this->_from;
  443. if ($this->_join) {
  444. // special case for joins
  445. foreach ($this->_join as $join) {
  446. $query .= (string) $join;
  447. }
  448. }
  449. if ($this->_where) {
  450. $query .= (string) $this->_where;
  451. }
  452. if ($this->_group) {
  453. $query .= (string) $this->_group;
  454. }
  455. if ($this->_having) {
  456. $query .= (string) $this->_having;
  457. }
  458. if ($this->_order) {
  459. $query .= (string) $this->_order;
  460. }
  461. break;
  462. case 'delete':
  463. $query .= (string) $this->_delete;
  464. $query .= (string) $this->_from;
  465. if ($this->_join) {
  466. // special case for joins
  467. foreach ($this->_join as $join) {
  468. $query .= (string) $join;
  469. }
  470. }
  471. if ($this->_where) {
  472. $query .= (string) $this->_where;
  473. }
  474. break;
  475. case 'update':
  476. $query .= (string) $this->_update;
  477. $query .= (string) $this->_set;
  478. if ($this->_where) {
  479. $query .= (string) $this->_where;
  480. }
  481. break;
  482. case 'insert':
  483. $query .= (string) $this->_insert;
  484. $query .= (string) $this->_set;
  485. if ($this->_where) {
  486. $query .= (string) $this->_where;
  487. }
  488. break;
  489. }
  490. return $query;
  491. }
  492. }