Query.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * A class for building database queries.
  4. * Right now the class only supports the MySQL database.
  5. *
  6. * @author Ironpilot
  7. * @copyright Copywrite (c) 2011, STAPLE CODE
  8. *
  9. * This file is part of the STAPLE Framework.
  10. *
  11. * The STAPLE Framework is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by the
  13. * Free Software Foundation, either version 3 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * The STAPLE Framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. abstract class Staple_Query
  26. {
  27. /**
  28. * Table to act upon.
  29. * @var mixed
  30. */
  31. public $table;
  32. /**
  33. * The database object. A database object is required to properly escape input.
  34. * @var mysqli
  35. */
  36. protected $db;
  37. /**
  38. * An array of Where Clauses. The clauses are additive, using the AND conjunction.
  39. * @var array[Staple_Query_Condition]
  40. */
  41. protected $where = array();
  42. public function __construct($table = NULL, mysqli $db = NULL)
  43. {
  44. if($db instanceof mysqli)
  45. {
  46. $this->setDb($db);
  47. }
  48. else
  49. {
  50. try {
  51. $this->setDb(Staple_DB::get());
  52. }
  53. catch (Exception $e)
  54. {
  55. $this->setDb(new mysqli());
  56. }
  57. }
  58. if(!($this->db instanceof mysqli))
  59. {
  60. throw new Exception('Unable to create database object', Staple_Error::DB_ERROR);
  61. }
  62. //Set Table
  63. if(isset($table))
  64. {
  65. $this->setTable($table);
  66. }
  67. }
  68. /**
  69. * Execute the build function and return the result when converting to a string.
  70. */
  71. public function __toString()
  72. {
  73. return $this->build();
  74. }
  75. /**
  76. * @return the $table
  77. */
  78. public function getTable()
  79. {
  80. return $this->table;
  81. }
  82. /**
  83. * @return the $db
  84. */
  85. public function getDb()
  86. {
  87. return $this->db;
  88. }
  89. /**
  90. * @param mixed $table
  91. * @param string $alias
  92. */
  93. public function setTable($table,$alias = NULL)
  94. {
  95. if(isset($alias) && is_string($table))
  96. {
  97. $this->table = array($alias=>$table);
  98. }
  99. else
  100. {
  101. $this->table = $table;
  102. }
  103. return $this;
  104. }
  105. /**
  106. * @param mysqli $db
  107. */
  108. public function setDb(mysqli $db)
  109. {
  110. $this->db = $db;
  111. return $this;
  112. }
  113. abstract function build();
  114. /**
  115. * Executes the query and returns the result.
  116. * @return mysqli_result | bool
  117. */
  118. public function Execute()
  119. {
  120. if($this->db instanceof mysqli)
  121. {
  122. return $this->db->query($this->build());
  123. }
  124. else
  125. {
  126. try
  127. {
  128. $this->db = Staple_DB::get();
  129. }
  130. catch (Exception $e)
  131. {
  132. //@todo try for a default connection if no staple connection
  133. throw new Exception('No Database Connection', Staple_Error::DB_ERROR);
  134. }
  135. if($this->db instanceof mysqli)
  136. {
  137. return $this->db->query($this->build());
  138. }
  139. }
  140. return false;
  141. }
  142. /*-----------------------------------------------WHERE CLAUSES-----------------------------------------------*/
  143. public function addWhere(Staple_Query_Condition $where)
  144. {
  145. $this->where[] = $where;
  146. return $this;
  147. }
  148. public function clearWhere()
  149. {
  150. $this->where = array();
  151. return $this;
  152. }
  153. public function whereCondition($column, $operator, $value, $columnJoin = NULL)
  154. {
  155. $this->addWhere(Staple_Query_Condition::Get($column, $operator, $value, $columnJoin));
  156. return $this;
  157. }
  158. /**
  159. * An open ended where statement
  160. * @param string | Staple_Query_Select $statement
  161. */
  162. public function whereStatement($statement)
  163. {
  164. $this->addWhere(Staple_Query_Condition::Statement($statement));
  165. return $this;
  166. }
  167. /**
  168. * SQL WHERE =
  169. * @param string $column
  170. * @param mixed $value
  171. * @param boolean $columnJoin
  172. */
  173. public function whereEqual($column, $value, $columnJoin = NULL)
  174. {
  175. $this->addWhere(Staple_Query_Condition::Equal($column, $value, $columnJoin));
  176. return $this;
  177. }
  178. /**
  179. * SQL LIKE Clause
  180. * @param string $column
  181. * @param mixed $value
  182. */
  183. public function whereLike($column, $value)
  184. {
  185. $this->addWhere(Staple_Query_Condition::Like($column, $value));
  186. return $this;
  187. }
  188. /**
  189. * SQL IS NULL Clause
  190. * @param string $column
  191. */
  192. public function whereNull($column)
  193. {
  194. $this->addWhere(Staple_Query_Condition::Null($column));
  195. return $this;
  196. }
  197. /**
  198. * SQL IN Clause
  199. * @param string $column
  200. * @param array | Staple_Query_Select $values
  201. */
  202. public function whereIn($column, $values)
  203. {
  204. $this->addWhere(Staple_Query_Condition::In($column, $values));
  205. return $this;
  206. }
  207. /**
  208. * SQL BETWEEN Clause
  209. * @param string $column
  210. * @param mixed $start
  211. * @param mixed $end
  212. */
  213. public function whereBetween($column, $start, $end)
  214. {
  215. $this->addWhere(Staple_Query_Condition::Between($column, $start, $end));
  216. return $this;
  217. }
  218. /*-----------------------------------------------UTILITY FUNCTIONS-----------------------------------------------*/
  219. /**
  220. * Converts a PHP data type into a compatible MySQL string.
  221. * @param mixed $inValue
  222. * @return string
  223. */
  224. public static function convertTypes($inValue, Staple_DB $db = NULL)
  225. {
  226. if(!($db instanceof mysqli))
  227. {
  228. try{
  229. $db = Staple_DB::get();
  230. }
  231. catch (Exception $e)
  232. {
  233. throw new Exception('No Database Connection', Staple_Error::DB_ERROR);
  234. }
  235. }
  236. //Decided to error on the side of caution and represent floats as strings in SQL statements
  237. if(is_string($inValue) || is_float($inValue))
  238. {
  239. return "'".$db->real_escape_string($inValue)."'";
  240. }
  241. elseif(is_bool($inValue))
  242. {
  243. return ($inValue) ? 'TRUE' : 'FALSE';
  244. }
  245. elseif(is_null($inValue))
  246. {
  247. return 'NULL';
  248. }
  249. elseif(is_array($inValue))
  250. {
  251. return "'".$db->real_escape_string(implode(" ", $inValue))."'";
  252. }
  253. elseif($inValue instanceof DateTime)
  254. {
  255. return "'".$db->real_escape_string($inValue->format('Y-m-d H:i:s'))."'";
  256. }
  257. else
  258. {
  259. return "'".$db->real_escape_string((string)$inValue)."'";
  260. }
  261. }
  262. }
  263. ?>