Update.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * A class for creating SQL SELECT 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_Update extends Staple_Query
  24. {
  25. const LOW_PRIORITY = 'LOW_PRIORITY';
  26. const IGNORE = 'IGNORE';
  27. /**
  28. * An array of flags to apply to the query
  29. * @var array[string]
  30. */
  31. protected $flags = array();
  32. /**
  33. * The data with which to update.
  34. * @var array[string]
  35. */
  36. public $data = array();
  37. /**
  38. * Holds the order of the SQL query. It can be either a string or an array of the columns to order by.
  39. * @var string | array
  40. */
  41. protected $order;
  42. /**
  43. * Limit number of rows to return.
  44. * @var int
  45. */
  46. protected $limit;
  47. /**
  48. * The Limit Offset. Used to skip a number of rows before selecting.
  49. * @var int
  50. */
  51. protected $limitOffset;
  52. public function __construct($table = NULL, array $data = NULL, $db = NULL, $order = NULL, $limit = NULL)
  53. {
  54. $this->data = new Staple_Query_DataSet();
  55. if(isset($db))
  56. {
  57. $this->setDb($db);
  58. }
  59. if(isset($table))
  60. {
  61. $this->setTable($table);
  62. }
  63. if(isset($data))
  64. {
  65. $this->setData($data);
  66. }
  67. if(isset($order))
  68. {
  69. $this->orderBy($order);
  70. }
  71. if(isset($limit))
  72. {
  73. $this->limit($limit);
  74. }
  75. }
  76. public function addFlag($flag)
  77. {
  78. switch($flag)
  79. {
  80. case self::LOW_PRIORITY:
  81. case self::IGNORE:
  82. $this->flags[] = $flag;
  83. break;
  84. }
  85. return $this;
  86. }
  87. public function clearFlags()
  88. {
  89. $this->flags = array();
  90. return $this;
  91. }
  92. /**
  93. * Adds or replaces data in the insert dataset.
  94. * @param array $data
  95. * @throws Exception
  96. */
  97. public function addData(array $data)
  98. {
  99. $this->data->addData($data);
  100. return $this;
  101. }
  102. /**
  103. * Adds or replaces a specific column value. Alias is set Data Column
  104. * @param string $column
  105. * @param mixed $data
  106. * @throws Exception
  107. * @see self::setDataColumn
  108. */
  109. public function addDataColumn($column, $data)
  110. {
  111. return $this->setDataColumn($column, $data);
  112. }
  113. /**
  114. * Adds a literal value to the dataset without conversion.
  115. * @param string $column
  116. * @param string $value
  117. */
  118. public function addLiteralColumn($column, $value)
  119. {
  120. return $this->setDataColumn($column, $value, true);
  121. }
  122. //----------------------------------------------GETTERS AND SETTERS----------------------------------------------
  123. /**
  124. * @return the $columns
  125. */
  126. public function getData()
  127. {
  128. return $this->data;
  129. }
  130. /**
  131. * Returns the order.
  132. * @return string | array
  133. */
  134. public function getOrder()
  135. {
  136. return $this->order;
  137. }
  138. /**
  139. * @return the $limit
  140. */
  141. public function getLimit()
  142. {
  143. return $this->limit;
  144. }
  145. /**
  146. * @return the $limitOffset
  147. */
  148. public function getLimitOffset()
  149. {
  150. return $this->limitOffset;
  151. }
  152. /**
  153. * Set the order.
  154. * @param string | array $order
  155. */
  156. public function setOrder($order)
  157. {
  158. $this->order = $order;
  159. return $this;
  160. }
  161. /**
  162. * @param int $limit
  163. * @return Staple_Query_Select
  164. */
  165. public function setLimit($limit)
  166. {
  167. $this->limit = (int)$limit;
  168. return $this;
  169. }
  170. /**
  171. * Sets the $data
  172. * @param Staple_Query_DataSet
  173. */
  174. public function setData($data)
  175. {
  176. if($data instanceof Staple_Query_DataSet)
  177. {
  178. $this->data = $data;
  179. }
  180. elseif(is_array($data))
  181. {
  182. $this->data = new Staple_Query_DataSet($data);
  183. }
  184. else
  185. {
  186. throw new Exception('Data must be an instance of Staple_Query_DataSet or an array', Staple_Error::APPLICATION_ERROR);
  187. }
  188. return $this;
  189. }
  190. /**
  191. * Sets the specified value for a specific column.
  192. * @param string $column
  193. * @param mixed $data
  194. * @param bool $literal
  195. * @throws Exception
  196. */
  197. public function setDataColumn($column,$data,$literal = false)
  198. {
  199. if($literal === true)
  200. {
  201. $this->data->addLiteralColumn($column, $data);
  202. }
  203. else
  204. {
  205. $this->data[$column] = $data;
  206. }
  207. return $this;
  208. }
  209. /**
  210. * Alias of setOrder()
  211. * @see self::setOrder()
  212. */
  213. public function orderBy($order)
  214. {
  215. return $this->setOrder($order);
  216. }
  217. /**
  218. * Sets the limit and the offset in one function.
  219. * @param int | Staple_Pager $limit
  220. * @param int $offset
  221. */
  222. public function limit($limit,$offset = NULL)
  223. {
  224. if($limit instanceof Staple_Pager)
  225. {
  226. $this->setLimit($limit->getItemsPerPage());
  227. $this->setLimitOffset($limit->getStartingItem());
  228. }
  229. else
  230. {
  231. $this->setLimit($limit);
  232. if(isset($offset))
  233. $this->setLimitOffset($offset);
  234. }
  235. return $this;
  236. }
  237. /**
  238. * @param int $limitOffset
  239. * @return Staple_Query_Select
  240. */
  241. public function setLimitOffset($limitOffset)
  242. {
  243. $this->limitOffset = (int)$limitOffset;
  244. return $this;
  245. }
  246. /*-----------------------------------------------BUILD FUNCTION-----------------------------------------------*/
  247. /**
  248. *
  249. * @see Staple_Query::build()
  250. */
  251. function build()
  252. {
  253. $stmt = 'UPDATE ';
  254. //Flags
  255. if(count($this->flags) > 0)
  256. {
  257. $stmt .= ' '.implode(' ', $this->flags);
  258. }
  259. //Table
  260. if(is_array($this->table))
  261. {
  262. $stmt .= ' '; //A little extra space
  263. foreach($this->table as $alias=>$table)
  264. {
  265. $stmt .= $table;
  266. if(is_string($alias))
  267. {
  268. $stmt .= ' AS `'.$alias.'`';
  269. }
  270. }
  271. }
  272. else
  273. {
  274. $stmt .= ' '.$this->table;
  275. }
  276. //SET data
  277. if(count($this->data) >= 0)
  278. {
  279. $stmt .= "\nSET ";
  280. $stmt .= $this->data->getUpdateString();
  281. }
  282. //WHERE CLAUSE
  283. if(count($this->where) > 0)
  284. {
  285. $stmt .= "\nWHERE ".implode(' AND ', $this->where);
  286. }
  287. //Can only order and limit on a single table query
  288. if(!is_array($this->table))
  289. {
  290. //ORDER CLAUSE
  291. if(isset($this->order))
  292. {
  293. $stmt .= "\nORDER BY ";
  294. if(is_array($this->order))
  295. {
  296. $stmt .= implode(',', $this->order);
  297. }
  298. else
  299. {
  300. $stmt .= $this->order;
  301. }
  302. }
  303. //LIMIT CLAUSE
  304. if(isset($this->limit))
  305. {
  306. $stmt .= "\nLIMIT ".$this->getLimit();
  307. if(isset($this->limitOffset))
  308. {
  309. $stmt .= ' OFFSET '.$this->limitOffset;
  310. }
  311. }
  312. }
  313. return $stmt;
  314. }
  315. }
  316. ?>