PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/source/endeleza/query/query.php

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