PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/mysql.query.replace.class.php

https://github.com/magedeveloper/frosted-mysql-library
PHP | 473 lines | 396 code | 32 blank | 45 comment | 32 complexity | 404e140b355deca108c4ad848b9bcbd2 MD5 | raw file
  1. <?php
  2. /**
  3. * Frosted MySQL Library Replace Query Class
  4. * - - - - - - - - - -
  5. * Add "REPLACE" 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_Replace extends mysqlClass_Abstract implements mysqlClass_Queries
  17. {
  18. /**
  19. * insert error messages
  20. * @var string
  21. */
  22. const MESSAGE_AFTER_FIELDS = "you cannot add columns after using 'set' or 'select'";
  23. const MESSAGE_AFTER_SET = "you cannot use 'set' after adding columns or a select";
  24. const MESSAGE_AFTER_SELECT = "you cannot use 'select' after adding values or 'set'";
  25. const MESSAGE_BEFORE_VALUES = "you have to specify a column list before adding values";
  26. const MESSAGE_VALUES_COUNT = "value count doesn't match columns";
  27. const MESSAGE_VALUES_MISSING = "columns not found in values";
  28. /*
  29. ** public
  30. */
  31. /**
  32. * reset insert class
  33. * @param boolean $format
  34. * @return mysqlClass_Select
  35. */
  36. public function resetQuery($format)
  37. {
  38. parent::resetQuery($format);
  39. $this->query["table"] = NULL;
  40. $this->query["low"] = false;
  41. $this->query["delayed"] = false;
  42. $this->query["ignore"] = false;
  43. $this->query["columns"] = array();
  44. $this->query["values"] = array();
  45. $this->query["set"] = array();
  46. $this->query["select"] = NULL;
  47. return $this;
  48. }
  49. /*
  50. ** query related
  51. */
  52. /**
  53. * add table to insert query
  54. * @param string $table
  55. * @return mysqlClass_Insert
  56. */
  57. public function table($table)
  58. {
  59. if( is_string($table) )
  60. $this->query["table"] = $table;
  61. else if( is_array($table) )
  62. foreach( $table as $_table => $_name )
  63. if( !is_numeric($_table) )
  64. $this->query["table"] = $_table;
  65. else
  66. $this->query["table"] = $_name;
  67. return $this;
  68. }
  69. /**
  70. * alias of 'table'
  71. * @param string $table
  72. * @return mysqlClass_Insert
  73. */
  74. public function into($table)
  75. {
  76. return $this->table($table);
  77. }
  78. /**
  79. * add 'low priority' to query
  80. * @param boolean $low
  81. * @return mysqlClass_Insert
  82. */
  83. public function lowPriority($low = true)
  84. {
  85. if( (bool)$low )
  86. {
  87. $this->query["delayed"] = false;
  88. $this->query["high"] = false;
  89. }
  90. $this->query["low"] = (bool)$low;
  91. return $this;
  92. }
  93. /**
  94. * add 'delayed' to insert query
  95. * @param boolean $delayed
  96. * @return mysqlClass_Insert
  97. */
  98. public function delayed($delayed = true)
  99. {
  100. if( (bool)$delayed )
  101. {
  102. $this->query["low"] = false;
  103. $this->query["high"] = false;
  104. }
  105. $this->query["delayed"] = (bool)$delayed;
  106. return $this;
  107. }
  108. /**
  109. * add 'ignore' to insert query
  110. * @param boolean $ignore
  111. * @return mysqlClass_Insert
  112. */
  113. public function ignore($ignore = true)
  114. {
  115. $this->query["ignore"] = (bool)$ignore;
  116. return $this;
  117. }
  118. /**
  119. * add columns to insert query
  120. * @param string|array $columns
  121. * @return mysqlClass_Insert
  122. */
  123. public function columns($columns)
  124. {
  125. if( count($this->query["set"]) > 0 || !is_null($this->query["select"]) )
  126. {
  127. $this->parent->createError(self::MESSAGE_AFTER_FIELDS);
  128. return $this;
  129. }
  130. if( func_num_args() == 1 && is_string($columns) && !in_array($columns, $this->query["columns"]) )
  131. {
  132. $this->query["columns"][] = $columns;
  133. return $this;
  134. }
  135. foreach( func_get_args() as $column )
  136. {
  137. if( !is_array($column) && !in_array($column, $this->query["columns"]) )
  138. {
  139. $this->query["columns"][] = $column;
  140. }
  141. else
  142. {
  143. foreach( $column as $_column )
  144. if( !in_array($_column, $this->query["columns"]) )
  145. $this->query["columns"][] = $_column;
  146. }
  147. }
  148. return $this;
  149. }
  150. /**
  151. * alias of "columns"
  152. * @param string|array $fields
  153. * @return mysqlClass_Insert
  154. */
  155. public function fields($fields)
  156. {
  157. if( count($this->query["set"]) > 0 || !is_null($this->query["select"]) )
  158. {
  159. $this->parent->createError(self::MESSAGE_AFTER_FIELDS);
  160. return $this;
  161. }
  162. if( func_num_args() == 1 && is_string($fields) && !in_array($fields, $this->query["columns"]) )
  163. {
  164. $this->query["columns"][] = $fields;
  165. return $this;
  166. }
  167. return call_user_func_array(array($this, "columns"), func_get_args());
  168. }
  169. /**
  170. * add 'values' to insert query
  171. * @param string|array $values
  172. * @return mysqlClass_Insert
  173. */
  174. public function values($values)
  175. {
  176. $columnCount = count($this->query["columns"]);
  177. if( $columnCount == 0 )
  178. {
  179. $this->parent->createError(self::MESSAGE_BEFORE_VALUES);
  180. return $this;
  181. }
  182. if( count($this->query["set"]) > 0 || !is_null($this->query["select"]) )
  183. {
  184. $this->parent->createError(self::MESSAGE_AFTER_FIELDS);
  185. return $this;
  186. }
  187. if( func_num_args() == 1 && $columnCount == 1 && !is_array($values) )
  188. {
  189. $this->query["values"][] = array($values);
  190. return $this;
  191. }
  192. // count params
  193. $count = 0;
  194. $values = array();
  195. foreach( func_get_args() as $value )
  196. {
  197. if( !is_array($value) )
  198. {
  199. $count++;
  200. $values[] = $this->parent->escape($value);
  201. }
  202. else
  203. {
  204. foreach( $value as $_key => $_value )
  205. {
  206. $count++;
  207. if( is_numeric($_key) )
  208. $values[] = $this->parent->escape($_value);
  209. else
  210. $values[$_key] = $this->parent->escape($_value);
  211. }
  212. }
  213. }
  214. // check if params count match fields
  215. if( $count == $columnCount )
  216. {
  217. $this->query["values"][] = $values;
  218. }
  219. // check if fields names are in values
  220. else if( $count > $columnCount )
  221. {
  222. $_values = array();
  223. foreach( $this->query["columns"] as $column )
  224. {
  225. if( !isset($values[$column]) ) break;
  226. $_values[] = $values[$column];
  227. }
  228. if( count($_values) == $columnCount )
  229. $this->query["values"][] = $_values;
  230. else
  231. $this->parent->createError(self::MESSAGE_VALUES_MISSING);
  232. }
  233. // values doesn't match
  234. else
  235. {
  236. $this->parent->createError(self::MESSAGE_VALUES_COUNT);
  237. }
  238. return $this;
  239. }
  240. /**
  241. * add 'set' to insert query
  242. * @param string|array $column
  243. * @param string $replace
  244. * @return mysqlClass_Insert
  245. */
  246. public function set($column, $replace = NULL)
  247. {
  248. if( count($this->query["columns"]) > 0 || !is_null($this->query["select"]) )
  249. {
  250. $this->parent->createError(self::MESSAGE_AFTER_SET);
  251. return $this;
  252. }
  253. if( is_null($replace) )
  254. {
  255. if( is_array($column) )
  256. {
  257. foreach( $column as $_column => $_replace )
  258. {
  259. if( !is_numeric($_column) )
  260. if( strpos($_column, "?") === false && strpos($column, "=") === false )
  261. $this->query["set"][] = $_column . " = " . $this->parent->escape($_replace);
  262. else
  263. $this->query["set"][] = str_replace("?", $this->parent->escape($_replace), $_column);
  264. else
  265. $this->query["set"][] = $_replace;
  266. }
  267. }
  268. else
  269. {
  270. $this->query["set"][] = $column;
  271. }
  272. }
  273. else
  274. {
  275. if( strpos($column, "?") === false && strpos($column, "=") === false )
  276. $this->query["set"][] = $column . " = " . $this->parent->escape($replace);
  277. else
  278. $this->query["set"][] = str_replace("?", $this->parent->escape($replace), $column);
  279. }
  280. return $this;
  281. }
  282. /**
  283. * add a select statement to insert query
  284. * @param string|mysqlClass_Select $subSelect
  285. * @return mysqlClass_Insert
  286. */
  287. public function select($subSelect)
  288. {
  289. if( count($this->query["values"]) > 0 || count($this->query["set"]) > 0 )
  290. {
  291. $this->parent->createError(self::MESSAGE_AFTER_SELECT);
  292. return $this;
  293. }
  294. if( is_string($subSelect) || $subSelect instanceof mysqlClass_Select )
  295. {
  296. $this->query["select"] = $subSelect;
  297. }
  298. return $this;
  299. }
  300. /*
  301. ** build
  302. */
  303. /**
  304. * build mysql insert query string
  305. * @param integer $formatOffset
  306. * @return string
  307. */
  308. public function build($formatOffset = 0)
  309. {
  310. $this->formatOffset += $formatOffset;
  311. $offset = str_pad("", $this->formatOffset, " ");
  312. // end if no table is set
  313. if( is_null($this->query["table"]) ) return NULL;
  314. $query = $this->format ? $offset . "REPLACE " : "REPLACE ";
  315. // low priority
  316. if( $this->query["low"] ) $query .= $this->format ? "\n" . $offset . " LOW_PRIORITY " : "LOW_PRIORITY ";
  317. // delayed
  318. if( $this->query["delayed"] ) $query .= $this->format ? "\n" . $offset . " DELAYED " : "DELAYED ";
  319. // ignore
  320. if( $this->query["ignore"] ) $query .= $this->format ? "\n" . $offset . " IGNORE \n" : "IGNORE ";
  321. $query .= $this->format ? $offset . "INTO " : "INTO ";
  322. // table
  323. $query .= $this->format ? "\n" . $offset . " " . $this->query["table"] . "\n" : $this->query["table"] . " ";
  324. // columns
  325. if( !empty($this->query["columns"]) )
  326. {
  327. if( $this->format )
  328. {
  329. $query .= $offset . " (";
  330. for( $i = 0; $i < count($this->query["columns"]); $i++ )
  331. {
  332. $query .= " " . $this->query["columns"][$i];
  333. $query .= $i < count($this->query["columns"]) - 1 ? "," : NULL;
  334. }
  335. $query .= " ) \n";
  336. }
  337. else
  338. $query .= "(" . join(",", $this->query["columns"]) . ") ";
  339. if( !empty($this->query["values"]) )
  340. {
  341. $query .= $this->format ? "VALUES\n" : "VALUES ";
  342. for( $i = 0; $i < count($this->query["values"]); $i++ )
  343. {
  344. if( $this->format )
  345. {
  346. $query .= $offset . " " . "( " . join(", ", $this->query["values"][$i]) . " )";
  347. $query .= $i < count($this->query["values"]) - 1 ? ",\n" : NULL;
  348. }
  349. else
  350. {
  351. $query .= "(" . join(",", $this->query["values"][$i]) . ")";
  352. $query .= $i < count($this->query["values"]) - 1 ? "," : NULL;
  353. }
  354. }
  355. $query .= $this->format ? "\n" : " ";
  356. }
  357. }
  358. // set
  359. else if( !empty($this->query["set"]) )
  360. {
  361. $query .= $this->format ? "SET\n" : "SET ";
  362. for( $i = 0; $i < count($this->query["set"]); $i++ )
  363. {
  364. if( $this->format )
  365. {
  366. $query .= $offset . " " . $this->query["set"][$i] . "";
  367. $query .= $i < count($this->query["set"]) - 1 ? ", \n" : NULL;
  368. }
  369. else
  370. {
  371. $query .= "(" . join(",", $this->query["set"]) . ")";
  372. $query .= $i < count($this->query["values"]) - 1 ? "," : NULL;
  373. }
  374. }
  375. $query .= $this->format ? "\n" : " ";
  376. }
  377. // select
  378. if( !is_null($this->query["select"]) )
  379. {
  380. if( is_string($this->query["select"]) )
  381. {
  382. if( $this->format )
  383. $query .= $offset . " ( " . $this->query["select"] . " ) \n";
  384. else
  385. $query .= "(" . $this->query["select"] . ") ";
  386. }
  387. else
  388. {
  389. $select = $this->query["select"];
  390. if( $select instanceof mysqlClass_Select )
  391. {
  392. if( $this->format )
  393. $query .= $offset . " (" . trim($select->build($this->formatOffset + 4)) . ") \n";
  394. else
  395. $query .= "(" . $select->build() . ") ";
  396. }
  397. }
  398. }
  399. return $query;
  400. }
  401. }