Union.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * A Class for creating a UNION query in MySQL
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. */
  25. class Staple_Query_Union
  26. {
  27. const DISTINCT = 'DISTINCT';
  28. const ALL = 'ALL';
  29. /**
  30. * The database object. A database object is required to properly escape input.
  31. * @var mysqli
  32. */
  33. protected $db;
  34. /**
  35. * UNION flag: ALL | DISTINCT
  36. * @var string
  37. */
  38. protected $flag;
  39. /**
  40. * An array of the queries to union.
  41. * @var array[Staple_Query]
  42. */
  43. protected $queries = array();
  44. /**
  45. * Holds the order of the SQL query. It can be either a string or an array of the columns to order by.
  46. * @var string | array
  47. */
  48. protected $order;
  49. /**
  50. * Limit number of rows to return.
  51. * @var int
  52. */
  53. protected $limit;
  54. /**
  55. * The Limit Offset. Used to skip a number of rows before selecting.
  56. * @var int
  57. */
  58. protected $limitOffset;
  59. /**
  60. * Constructor accepts an array of Staple_Query_Select elements and a database connection.
  61. * @param array $queries
  62. * @param mysqli $db
  63. */
  64. public function __construct(array $queries = array(), mysqli $db = NULL)
  65. {
  66. //Process Database connection
  67. if($db instanceof mysqli)
  68. {
  69. $this->setDb($db);
  70. }
  71. else
  72. {
  73. try {
  74. $this->setDb(Staple_DB::get());
  75. }
  76. catch (Exception $e)
  77. {
  78. $this->setDb(new mysqli());
  79. }
  80. }
  81. //No DB = Bad
  82. if(!($this->db instanceof mysqli))
  83. {
  84. throw new Exception('Unable to create database object', Staple_Error::DB_ERROR);
  85. }
  86. foreach($queries as $q)
  87. {
  88. if($q instanceof Staple_Query_Select)
  89. {
  90. $this->addQuery($q);
  91. }
  92. }
  93. }
  94. /**
  95. * Builds the query on string conversion.
  96. * @return string
  97. */
  98. public function __toString()
  99. {
  100. return $this->build();
  101. }
  102. /**
  103. * @return mysqli $db
  104. */
  105. public function getDb()
  106. {
  107. return $this->db;
  108. }
  109. /**
  110. * Get the UNION flag: ALL | DISTINCT
  111. */
  112. public function getFlag()
  113. {
  114. return $this->flag;
  115. }
  116. /**
  117. * Returns the order.
  118. * @return string | array
  119. */
  120. public function getOrder()
  121. {
  122. return $this->order;
  123. }
  124. /**
  125. * @return the $limit
  126. */
  127. public function getLimit()
  128. {
  129. return $this->limit;
  130. }
  131. /**
  132. * @return the $limitOffset
  133. */
  134. public function getLimitOffset()
  135. {
  136. return $this->limitOffset;
  137. }
  138. /**
  139. * @param mysqli $db
  140. */
  141. public function setDb(mysqli $db)
  142. {
  143. $this->db = $db;
  144. return $this;
  145. }
  146. /**
  147. * Set the UNION flag.
  148. * @param string $flag
  149. */
  150. public function setFlag($flag)
  151. {
  152. $flag = strtoupper($flag);
  153. switch($flag)
  154. {
  155. case self::ALL:
  156. case self::DISTINCT:
  157. $this->flag = $flag;
  158. }
  159. return $this;
  160. }
  161. /**
  162. * Resets all of the columns in all the currently added queries to the specified column array.
  163. */
  164. public function setColumns(array $columns)
  165. {
  166. foreach($this->queries as $query)
  167. {
  168. $query->setColumns($columns);
  169. }
  170. return $this;
  171. }
  172. /**
  173. * Set the order.
  174. * @param string | array $order
  175. */
  176. public function setOrder($order)
  177. {
  178. $this->order = $order;
  179. return $this;
  180. }
  181. /**
  182. * @param int $limit
  183. * @return Staple_Query_Select
  184. */
  185. public function setLimit($limit)
  186. {
  187. $this->limit = (int)$limit;
  188. return $this;
  189. }
  190. /**
  191. * @param int $limitOffset
  192. * @return Staple_Query_Select
  193. */
  194. public function setLimitOffset($limitOffset)
  195. {
  196. $this->limitOffset = (int)$limitOffset;
  197. return $this;
  198. }
  199. /**
  200. * Add a query to the UNION
  201. * @param Staple_Query_Select $query
  202. */
  203. public function addQuery(Staple_Query_Select $query)
  204. {
  205. $this->queries[] = $query;
  206. return $this;
  207. }
  208. /**
  209. * Remove a query from the UNION
  210. * @param Staple_Query_Select $query
  211. */
  212. public function removeQuery(Staple_Query_Select $query)
  213. {
  214. if(($key = array_search($query, $this->queries, true)) !== false)
  215. {
  216. unset($this->queries[$key]);
  217. }
  218. return $this;
  219. }
  220. function build()
  221. {
  222. $stmt = '';
  223. if(count($this->queries) <= 0)
  224. {
  225. return 'SELECT 0 FROM (SELECT 0) AS `a` WHERE 1=0';
  226. }
  227. elseif(count($this->queries) == 1)
  228. {
  229. $stmt .= 'SELECT * FROM ('.implode('', $this->queries).') AS `stapleunion` ';
  230. }
  231. else
  232. {
  233. if(isset($this->flag))
  234. {
  235. $glue = ")\nUNION {$this->flag} \n(";
  236. }
  237. else
  238. {
  239. $glue = ")\nUNION \n(";
  240. }
  241. $stmt .= '('.implode($glue, $this->queries).')';
  242. }
  243. //ORDER CLAUSE
  244. if(isset($this->order))
  245. {
  246. $stmt .= "\nORDER BY ";
  247. if(is_array($this->order))
  248. {
  249. $stmt .= implode(',', $this->order);
  250. }
  251. else
  252. {
  253. $stmt .= $this->order;
  254. }
  255. }
  256. //LIMIT CLAUSE
  257. if(isset($this->limit))
  258. {
  259. $stmt .= "\nLIMIT ".$this->getLimit();
  260. if(isset($this->limitOffset))
  261. {
  262. $stmt .= ' OFFSET '.$this->limitOffset;
  263. }
  264. }
  265. return $stmt;
  266. }
  267. /**
  268. * Alias of setOrder()
  269. * @see self::setOrder()
  270. */
  271. public function orderBy($order)
  272. {
  273. return $this->setOrder($order);
  274. }
  275. /**
  276. * Sets the limit and the offset in one function.
  277. * @param int | Staple_Pager $limit
  278. * @param int $offset
  279. */
  280. public function limit($limit,$offset = NULL)
  281. {
  282. if($limit instanceof Staple_Pager)
  283. {
  284. $this->setLimit($limit->getItemsPerPage());
  285. $this->setLimitOffset($limit->getStartingItem());
  286. }
  287. else
  288. {
  289. $this->setLimit($limit);
  290. if(isset($offset))
  291. $this->setLimitOffset($offset);
  292. }
  293. return $this;
  294. }
  295. /**
  296. * Executes the query.
  297. * @return mysqli_result | bool
  298. */
  299. public function Execute()
  300. {
  301. if($this->db instanceof mysqli)
  302. {
  303. return $this->db->query($this->build());
  304. }
  305. else
  306. {
  307. try
  308. {
  309. $this->db = Staple_DB::get();
  310. }
  311. catch (Exception $e)
  312. {
  313. //@todo try for a default connection if no staple connection
  314. throw new Exception('No Database Connection', Staple_Error::DB_ERROR);
  315. }
  316. if($this->db instanceof mysqli)
  317. {
  318. return $this->db->query($this->build());
  319. }
  320. }
  321. return false;
  322. }
  323. }
  324. ?>