PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/mysql.query.delete.class.php

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