PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/aamenu/code/trunk/administrator/components/com_aamenu/libraries/jxtended/database/query.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 251 lines | 143 code | 21 blank | 87 comment | 21 complexity | 23f4a5b19a4b828aba1f756258c643cd MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: query.php 36 2009-05-19 01:06:55Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Database
  6. * @copyright Copyright (C) 2008 - 2009 JXtended, LLC. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://jxtended.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * Query Element Class
  13. *
  14. * @package JXtended.Libraries
  15. * @subpackage Database
  16. */
  17. class JXQueryElement
  18. {
  19. /** @var string The name of the element */
  20. var $_name = null;
  21. /** @var array An array of elements */
  22. var $_elements = null;
  23. /** @var string Glue piece */
  24. var $_glue = null;
  25. /**
  26. * Constructor
  27. * @param string The name of the element
  28. * @param mixed String or array
  29. * @param string The glue for elements
  30. */
  31. function JXQueryElement($name, $elements, $glue=',')
  32. {
  33. $this->_elements = array();
  34. $this->_name = $name;
  35. $this->append($elements);
  36. $this->_glue = $glue;
  37. }
  38. /**
  39. * Appends element parts to the internal list
  40. * @param mixed String or array
  41. */
  42. function append($elements)
  43. {
  44. if (is_array($elements)) {
  45. $this->_elements = array_unique(array_merge($this->_elements, $elements));
  46. } else {
  47. $this->_elements = array_unique(array_merge($this->_elements, array($elements)));
  48. }
  49. }
  50. /**
  51. * Render the query element
  52. * @return string
  53. */
  54. function toString()
  55. {
  56. return "\n{$this->_name} " . implode($this->_glue, $this->_elements);
  57. }
  58. }
  59. /**
  60. * Query Building Class
  61. *
  62. * @package JXtended.Libraries
  63. * @subpackage Database
  64. */
  65. class JXQuery
  66. {
  67. /** @var string The query type */
  68. var $_type = '';
  69. /** @var object The select element */
  70. var $_select = null;
  71. /** @var object The from element */
  72. var $_from = null;
  73. /** @var object The join element */
  74. var $_join = null;
  75. /** @var object The where element */
  76. var $_where = null;
  77. /** @var object The where element */
  78. var $_group = null;
  79. /** @var object The where element */
  80. var $_having = null;
  81. /** @var object The where element */
  82. var $_order = null;
  83. /**
  84. * Constructor
  85. */
  86. function JXQuery()
  87. {
  88. }
  89. /**
  90. * @param mixed A string or an array of field names
  91. */
  92. function select($columns)
  93. {
  94. $this->_type = 'select';
  95. if (is_null($this->_select)) {
  96. $this->_select = new JXQueryElement('SELECT', $columns);
  97. } else {
  98. $this->_select->append($columns);
  99. }
  100. }
  101. /**
  102. * @param mixed A string or array of table names
  103. */
  104. function from($tables)
  105. {
  106. if (is_null($this->_from)) {
  107. $this->_from = new JXQueryElement('FROM', $tables);
  108. } else {
  109. $this->_from->append($tables);
  110. }
  111. }
  112. /**
  113. * @param string
  114. * @param string
  115. */
  116. function join($type, $conditions)
  117. {
  118. if (is_null($this->_join)) {
  119. $this->_join = array();
  120. }
  121. $this->_join[] = new JXQueryElement(strtoupper($type) . ' JOIN', $conditions);
  122. }
  123. /**
  124. * @param string
  125. */
  126. function innerJoin($conditions)
  127. {
  128. $this->join('INNER', $conditions);
  129. }
  130. /**
  131. * @param string
  132. */
  133. function outerJoin($conditions)
  134. {
  135. $this->join('OUTER', $conditions);
  136. }
  137. /**
  138. * @param string
  139. */
  140. function leftJoin($conditions)
  141. {
  142. $this->join('LEFT', $conditions);
  143. }
  144. /**
  145. * @param string
  146. */
  147. function rightJoin($conditions)
  148. {
  149. $this->join('RIGHT', $conditions);
  150. }
  151. /**
  152. * @param mixed A string or array of where conditions
  153. * @param string
  154. */
  155. function where($conditions, $glue='AND')
  156. {
  157. if (is_null($this->_where)) {
  158. $glue = strtoupper($glue);
  159. $this->_where = new JXQueryElement( 'WHERE', $conditions, "\n\t$glue ");
  160. } else {
  161. $this->_where->append($conditions);
  162. }
  163. }
  164. /**
  165. * @param mixed A string or array of ordering columns
  166. */
  167. function group($columns)
  168. {
  169. if (is_null($this->_group)) {
  170. $this->_group = new JXQueryElement('GROUP BY', $columns);
  171. } else {
  172. $this->_group->append($columns);
  173. }
  174. }
  175. /**
  176. * @param mixed A string or array of ordering columns
  177. */
  178. function having($columns)
  179. {
  180. if (is_null($this->_having)) {
  181. $this->_having = new JXQueryElement('HAVING', $columns);
  182. } else {
  183. $this->_having->append($columns);
  184. }
  185. }
  186. /**
  187. * @param mixed A string or array of ordering columns
  188. */
  189. function order($columns)
  190. {
  191. if (is_null($this->_order)) {
  192. $this->_order = new JXQueryElement('ORDER BY', $columns);
  193. } else {
  194. $this->_order->append($columns);
  195. }
  196. }
  197. /**
  198. * @return string The completed query
  199. */
  200. function toString()
  201. {
  202. $query = '';
  203. switch ($this->_type)
  204. {
  205. case 'select':
  206. $query .= $this->_select->toString();
  207. $query .= $this->_from->toString();
  208. if ($this->_join) {
  209. // special case for joins
  210. foreach ($this->_join as $join) {
  211. $query .= $join->toString();
  212. }
  213. }
  214. if ($this->_where) {
  215. $query .= $this->_where->toString();
  216. }
  217. if ($this->_group) {
  218. $query .= $this->_group->toString();
  219. }
  220. if ($this->_having) {
  221. $query .= $this->_having->toString();
  222. }
  223. if ($this->_order) {
  224. $query .= $this->_order->toString();
  225. }
  226. break;
  227. }
  228. return $query;
  229. }
  230. }