PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/mysql.query.update.class.php

https://github.com/magedeveloper/frosted-mysql-library
PHP | 400 lines | 237 code | 60 blank | 103 comment | 49 complexity | fc12e4077f90a83c11c5b8914f3c090b MD5 | raw file
  1. <?php
  2. /**
  3. * Frosted MySQL Library Update Query Class
  4. * - - - - - - - - - -
  5. * Add "UPDATE" functionality to Frosted MySQL Library and will not work without them.
  6. * - - - - - - - - - -
  7. * Licensed under MIT license
  8. * - - - - - - - - - -
  9. * @Creator Daniel 'Eisbehr' Kern
  10. * @Require PHP5
  11. * @Version 3.0
  12. * @Date 01.08.2013
  13. * @Update 01.08.2013
  14. * - - - - - - - - - -
  15. */
  16. class mysqlClass_Update extends mysqlClass_Abstract implements mysqlClass_Queries
  17. {
  18. /**
  19. * update error messages
  20. * @var string
  21. */
  22. const MESSAGE_ORDER = "you cannot use 'order' if you update more than one table";
  23. const MESSAGE_LIMIT = "you cannot use 'limit' if you update more than one table";
  24. /*
  25. ** public
  26. */
  27. /**
  28. * reset update class
  29. * @param boolean $format
  30. * @return mysqlClass_Update
  31. */
  32. public function resetQuery($format)
  33. {
  34. parent::resetQuery($format);
  35. $this->query["tables"] = array();
  36. $this->query["low"] = false;
  37. $this->query["ignore"] = false;
  38. $this->query["set"] = array();
  39. $this->query["where"] = array();
  40. $this->query["order"] = array();
  41. $this->query["limit"] = NULL;
  42. return $this;
  43. }
  44. /*
  45. ** query related
  46. */
  47. /**
  48. * add 'low priority' to query
  49. * @param boolean $low
  50. * @return mysqlClass_Update
  51. */
  52. public function lowPriority($low = true)
  53. {
  54. $this->query["low"] = (bool)$low;
  55. return $this;
  56. }
  57. /**
  58. * add 'ignore' to query
  59. * @param boolean $ignore
  60. * @return mysqlClass_Update
  61. */
  62. public function ignore($ignore = true)
  63. {
  64. $this->query["ignore"] = (bool)$ignore;
  65. return $this;
  66. }
  67. /**
  68. * add tables to query
  69. * @param string|array $table
  70. * @return mysqlClass_Update
  71. */
  72. public function table($table)
  73. {
  74. // only one string is set
  75. if( func_num_args() == 1 && is_string($table) )
  76. {
  77. $this->query["tables"][] = $table;
  78. return $this;
  79. }
  80. // add all tables to query
  81. foreach( func_get_args() as $param )
  82. {
  83. if( !is_array($param) )
  84. $this->query["tables"][] = $param;
  85. else
  86. foreach( $param as $database => $name )
  87. {
  88. if( !is_numeric($database) )
  89. $this->query["tables"][] = $database . " AS " . $name;
  90. else
  91. $this->query["tables"][] = $name;
  92. }
  93. }
  94. return $this;
  95. }
  96. /**
  97. * add 'set' to query
  98. * @param string|array $column
  99. * @param string $replace
  100. * @return mysqlClass_Update
  101. */
  102. public function set($column, $replace = NULL)
  103. {
  104. if( is_null($replace) )
  105. {
  106. if( is_array($column) )
  107. {
  108. foreach( $column as $_column => $_replace )
  109. {
  110. if( !is_numeric($_column) )
  111. $this->query["set"][] = str_replace("?", $this->parent->escape($_replace), $_column);
  112. else
  113. $this->query["set"][] = $_replace;
  114. }
  115. }
  116. else
  117. {
  118. $this->query["set"][] = $column;
  119. }
  120. }
  121. else
  122. {
  123. $this->query["set"][] = str_replace("?", $this->parent->escape($replace), $column);
  124. }
  125. return $this;
  126. }
  127. /**
  128. * add 'where' to query
  129. * @param string $condition
  130. * @param string $replace
  131. * @param string $nextRelation
  132. * @return mysqlClass_Update
  133. */
  134. public function where($condition, $replace = NULL, $nextRelation = mysqlClass::WHERE_AND)
  135. {
  136. // add condition
  137. if( !is_null($replace) )
  138. {
  139. if( is_array($replace) )
  140. {
  141. // escape all values
  142. foreach( $replace as &$value ) $value = $this->parent->escape($value);
  143. // format sub-query
  144. if( $this->format )
  145. {
  146. $condition = str_replace("ANY(?)", "\nANY\n(\n ?\n)", $condition);
  147. $condition = str_replace("IN(?)", "\nIN\n(\n ?\n)", $condition);
  148. $condition = str_replace("SOME(?)", "\nSOME\n(\n ?\n)", $condition);
  149. }
  150. $glue = $this->format ? ",\n " : ",";
  151. $this->query["where"][] = str_replace("?", join($glue, $replace), $condition);
  152. }
  153. else if( $replace instanceof mysqlClass_Select )
  154. $this->query["where"][] = array($condition, $replace);
  155. else
  156. $this->query["where"][] = str_replace("?", $this->parent->escape($replace), $condition);
  157. }
  158. else
  159. $this->query["where"][] = $condition;
  160. // add next relation
  161. if( strtoupper($nextRelation) == mysqlClass::WHERE_OR )
  162. $this->query["where"][] = mysqlClass::WHERE_OR;
  163. else
  164. $this->query["where"][] = mysqlClass::WHERE_AND;
  165. return $this;
  166. }
  167. /**
  168. * add 'or' related 'where' to query
  169. * @param string $condition
  170. * @param string $replace
  171. * @param string $nextRelation
  172. * @return mysqlClass_Update
  173. */
  174. public function orWhere($condition, $replace = NULL, $nextRelation = mysqlClass::WHERE_AND)
  175. {
  176. if( !empty($this->query["where"]) )
  177. $this->query["where"][(count($this->query["where"]) - 1)] = mysqlClass::WHERE_OR;
  178. return $this->where($condition, $replace, $nextRelation);
  179. }
  180. /**
  181. * add 'order' to query
  182. * @param string $field
  183. * @param string $order
  184. * @return mysqlClass_Update
  185. */
  186. public function orderBy($field, $order = mysqlClass::ORDER_ASC)
  187. {
  188. if( count($this->query["tables"]) >= 2 )
  189. {
  190. $this->parent->createError(self::MESSAGE_ORDER);
  191. return $this;
  192. }
  193. if( strtoupper($order) == mysqlClass::ORDER_DESC )
  194. $this->query["order"][] = $field . " " . mysqlClass::ORDER_DESC;
  195. else
  196. $this->query["order"][] = $field . " " . mysqlClass::ORDER_ASC;
  197. return $this;
  198. }
  199. /**
  200. * alias of 'orderBy'
  201. * @param string $field
  202. * @param string $order
  203. * @return mysqlClass_Update
  204. */
  205. public function order($field, $order = mysqlClass::ORDER_ASC)
  206. {
  207. return $this->orderBy($field, $order);
  208. }
  209. /**
  210. * add 'limit' to query
  211. * @param integer $limit
  212. * @return mysqlClass_Update
  213. */
  214. public function limit($limit)
  215. {
  216. if( count($this->query["tables"]) >= 2 )
  217. {
  218. $this->parent->createError(self::MESSAGE_LIMIT);
  219. return $this;
  220. }
  221. $this->query["limit"] = $limit;
  222. return $this;
  223. }
  224. /*
  225. ** build
  226. */
  227. /**
  228. * build mysql update query string
  229. * @param integer $formatOffset
  230. * @return string
  231. */
  232. public function build($formatOffset = 0)
  233. {
  234. $this->formatOffset += $formatOffset;
  235. $offset = str_pad("", $this->formatOffset, " ");
  236. // end if no table is set
  237. if( empty($this->query["tables"]) ) return NULL;
  238. $query = $this->format ? $offset . "UPDATE " : "UPDATE ";
  239. // low priority
  240. if( $this->query["low"] ) $query .= "LOW_PRIORITY ";
  241. // ignore
  242. if( $this->query["ignore"] ) $query .= "IGNORE ";
  243. $query .= $this->format ? $offset . "\n" : NULL;
  244. // tables
  245. if( count($this->query["tables"]) == 1 )
  246. $query .= $this->format ? $offset . " " . $this->query["tables"][0] . "\n" : $this->query["tables"][0] . " ";
  247. else if( $this->format )
  248. for( $i = 0; $i < count($this->query["tables"]); $i++ )
  249. {
  250. $query .= $offset . " " . $this->query["tables"][$i] . "";
  251. $query .= $i < count($this->query["tables"]) - 1 ? "," : NULL;
  252. $query .= "\n";
  253. }
  254. else
  255. $query .= join(",", $this->query["tables"]) . " ";
  256. // set
  257. if( !empty($this->query["set"]) )
  258. {
  259. $query .= $this->format ? "SET\n" : "SET ";
  260. if( $this->format )
  261. for( $i = 0; $i < count($this->query["set"]); $i++ )
  262. {
  263. $query .= $offset . " " . $this->query["set"][$i] . "";
  264. $query .= $i < count($this->query["set"]) - 1 ? ", \n" : NULL;
  265. }
  266. else
  267. $query .= join(",", $this->query["set"]);
  268. $query .= $this->format ? "\n" : " ";
  269. }
  270. // where
  271. if( !empty($this->query["where"]) )
  272. {
  273. if( $this->format )
  274. {
  275. $query .= $offset . "WHERE \n";
  276. for( $i = 0; $i < count($this->query["where"]); $i = $i + 2 )
  277. {
  278. if( is_array($this->query["where"][$i]) )
  279. {
  280. $select = $this->query["where"][$i][1];
  281. if( $select instanceof mysqlClass_Select )
  282. {
  283. $select = $select->build($this->formatOffset + 4);
  284. $select = trim($select);
  285. }
  286. $query .= $offset . " " . str_replace("?", "\n" . $offset . " (" . $select . ")", $this->query["where"][$i][0]);
  287. $query .= $i < count($this->query["where"]) - 2 ? " \n" . $offset . $this->query["where"][$i + 1] . " " : NULL;
  288. $query .= " \n";
  289. }
  290. else
  291. {
  292. $query .= $offset . " " . $this->query["where"][$i];
  293. $query .= $i < count($this->query["where"]) - 2 ? " \n" . $offset . $this->query["where"][$i + 1] . " " : NULL;
  294. $query .= " \n";
  295. }
  296. }
  297. }
  298. else
  299. {
  300. for( $i = 0; $i < count($this->query["where"]); $i = $i + 2 )
  301. {
  302. if( is_array($this->query["where"][$i]) )
  303. {
  304. $select = $this->query["where"][$i][1];
  305. if( $select instanceof mysqlClass_Select )
  306. $select = $select->build();
  307. $this->query["where"][$i] = str_replace("?", "(" . $select . ")", $this->query["where"][$i][0]);
  308. }
  309. }
  310. $where = array_slice($this->query["where"], 0, -1);
  311. $query .= "WHERE " . join(" ", $where) . " ";
  312. }
  313. }
  314. // add order
  315. if( !empty($this->query["order"]) && count($this->query["tables"]) == 1 )
  316. {
  317. if( $this->format )
  318. {
  319. $query .= $offset . "ORDER BY \n";
  320. for( $i = 0; $i < count($this->query["order"]); $i++ )
  321. {
  322. $query .= $offset . " " . $this->query["order"][$i];
  323. $query .= $i < count($this->query["order"]) - 1 ? "," : NULL;
  324. $query .= " \n";
  325. }
  326. }
  327. else
  328. {
  329. $query .= "ORDER BY " . join(",", $this->query["order"]) . " ";
  330. }
  331. }
  332. // add limit
  333. if( !empty($this->query["limit"]) && count($this->query["tables"]) == 1 )
  334. {
  335. $query .= $this->format ? $offset . "LIMIT \n" . $offset . " " : "LIMIT ";
  336. $query .= $this->query["limit"];
  337. }
  338. return $query;
  339. }
  340. }