Condition.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * A class to contain SQL condional statements.
  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. class Staple_Query_Condition
  24. {
  25. const EQUAL = '=';
  26. const GREATER = '>';
  27. const GREATER_EQUAL = '>=';
  28. const LESS = '<';
  29. const LESS_EQUAL = '<=';
  30. const NOTEQUAL = '!=';
  31. const IN = "IN";
  32. const IS = "IS";
  33. const BETWEEN = "BETWEEN";
  34. /**
  35. * The column for the where
  36. * @var string
  37. */
  38. protected $column;
  39. /**
  40. * The operator of the where clause
  41. * @var string
  42. */
  43. protected $operator;
  44. /**
  45. * The value of the comparison
  46. * @var string | int | bool
  47. */
  48. protected $value;
  49. /**
  50. * An override statement that represents the WHERE clause.
  51. * @var string
  52. */
  53. public $statement;
  54. /**
  55. * True or false whether the value is a table column.
  56. * @var bool
  57. */
  58. protected $columnJoin = false;
  59. public function __construct($statement = NULL)
  60. {
  61. if(isset($statement))
  62. {
  63. $this->setStatement($statement);
  64. }
  65. }
  66. /**
  67. * Returns the string of the where clause.
  68. * @return string
  69. */
  70. public function __toString()
  71. {
  72. try {
  73. $return = $this->build();
  74. }
  75. catch (Exception $e)
  76. {
  77. //on an error return SQL that eliminates any results from the query.
  78. return "1 = 2";
  79. }
  80. return $return;
  81. }
  82. /**
  83. * Sets the where statement.
  84. * @param unknown_type $where
  85. */
  86. public function setWhere($where)
  87. {
  88. $this->statement = (string)$where;
  89. return $this;
  90. }
  91. public function build()
  92. {
  93. if(isset($this->statement))
  94. {
  95. return '('.$this->statement.')';
  96. }
  97. else
  98. {
  99. if(strtoupper($this->operator) == self::IN)
  100. {
  101. $value = "(";
  102. if(is_array($this->value))
  103. {
  104. foreach($this->value as $aValue)
  105. {
  106. if(strlen($value) > 1)
  107. {
  108. $value .= ",";
  109. }
  110. $value .= $this->columnJoin ? $aValue : Staple_Query::convertTypes($aValue);
  111. }
  112. }
  113. elseif($this->value instanceof Staple_Query_Select)
  114. {
  115. $value .= $this->value;
  116. }
  117. else
  118. {
  119. $value = $this->columnJoin ? $this->value : Staple_Query::convertTypes($this->value);
  120. }
  121. $value .= ")";
  122. }
  123. elseif (strtoupper($this->operator) == self::BETWEEN)
  124. {
  125. $value = $this->getValue();
  126. }
  127. else
  128. {
  129. $value = $this->columnJoin ? $this->value : Staple_Query::convertTypes($this->value);
  130. }
  131. return $this->column.' '.$this->operator.' '.$value;
  132. }
  133. }
  134. /*-----------------------------------------------GETTERS AND SETTERS-----------------------------------------------*/
  135. /**
  136. * @return the $column
  137. */
  138. public function getColumn()
  139. {
  140. return $this->column;
  141. }
  142. /**
  143. * @return the $operator
  144. */
  145. public function getOperator()
  146. {
  147. return $this->operator;
  148. }
  149. /**
  150. * @return the $value
  151. */
  152. public function getValue()
  153. {
  154. return $this->value;
  155. }
  156. /**
  157. * @return the $statement
  158. */
  159. public function getStatement()
  160. {
  161. return $this->statement;
  162. }
  163. /**
  164. * @param string $column
  165. */
  166. public function setColumn($column)
  167. {
  168. $this->column = $column;
  169. return $this;
  170. }
  171. /**
  172. * @param string $operator
  173. */
  174. public function setOperator($operator)
  175. {
  176. $this->operator = $operator;
  177. return $this;
  178. }
  179. /**
  180. * @param string $value
  181. */
  182. public function setValue($value)
  183. {
  184. $this->value = $value;
  185. return $this;
  186. }
  187. /**
  188. * @param string $statement
  189. */
  190. public function setStatement($statement)
  191. {
  192. $this->statement = $statement;
  193. return $this;
  194. }
  195. /**
  196. * @return bool $columnJoin
  197. */
  198. public function getColumnJoin()
  199. {
  200. return $this->columnJoin;
  201. }
  202. /**
  203. * @param bool $columnJoin
  204. */
  205. public function setColumnJoin($columnJoin)
  206. {
  207. $this->columnJoin = (bool)$columnJoin;
  208. return $this;
  209. }
  210. /*-----------------------------------------------CONDITION ENCAPSULATORS-----------------------------------------------*/
  211. public static function Get($column, $operator, $value, $columnJoin = NULL)
  212. {
  213. $obj = new static();
  214. $obj->setColumn($column)
  215. ->setOperator($operator)
  216. ->setValue($value);
  217. if(isset($columnJoin))
  218. $obj->setColumnJoin($columnJoin);
  219. return $obj;
  220. }
  221. public static function Statement($statement)
  222. {
  223. $class = new static();
  224. $class->setStatement($statement);
  225. return $class;
  226. }
  227. /**
  228. * Setup a SQL WHERE clause where a column is equal to a value.
  229. * @param string $column
  230. * @param mixed $value
  231. * @param bool $columnJoin
  232. * @return Staple_Query_Condition
  233. */
  234. public static function Equal($column, $value, $columnJoin = NULL)
  235. {
  236. $obj = new static();
  237. $obj->setColumn($column)
  238. ->setValue($value);
  239. //Check for NULLS
  240. is_null($value) ? $obj->setOperator(self::IS) : $obj->setOperator(self::EQUAL);
  241. if(isset($columnJoin))
  242. $obj->setColumnJoin($columnJoin);
  243. return $obj;
  244. }
  245. public static function Like($column, $value, $columnJoin = NULL)
  246. {
  247. $obj = new static();
  248. $obj->setColumn($column)
  249. ->setOperator('LIKE')
  250. ->setValue($value);
  251. if(isset($columnJoin))
  252. $obj->setColumnJoin($columnJoin);
  253. return $obj;
  254. }
  255. public static function Null($column)
  256. {
  257. $obj = new static();
  258. $obj->setColumn($column)
  259. ->setOperator('IS')
  260. ->setValue(NULL);
  261. return $obj;
  262. }
  263. public static function In($column, $values, $columnJoin = NULL)
  264. {
  265. $obj = new static();
  266. $obj->setColumn($column)
  267. ->setOperator(self::IN)
  268. ->setValue($values);
  269. if(isset($columnJoin))
  270. $obj->setColumnJoin($columnJoin);
  271. return $obj;
  272. }
  273. public static function Between($column, $start, $end)
  274. {
  275. $obj = new static();
  276. $obj->setColumn($column)
  277. ->setOperator(self::BETWEEN)
  278. ->setValue(Staple_Query::convertTypes($start)." AND ".Staple_Query::convertTypes($end))
  279. ->setColumnJoin(true);
  280. return $obj;
  281. }
  282. }
  283. ?>