Insert.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. /**
  3. * A class for creating SQL INSERT 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_Insert
  24. {
  25. const LOW = "LOW_PRIORITY";
  26. const DELAYED = "DELAYED";
  27. const HIGH = "HIGH_PRIORITY";
  28. /**
  29. * The database object. A database object is required to properly escape input.
  30. * @var mysqli
  31. */
  32. protected $db;
  33. /**
  34. * The data to insert. May be a Select Statement Object or an array of DataSets
  35. * @var Staple_Query_DataSet | Staple_Query_Select
  36. */
  37. protected $data;
  38. /**
  39. * The Priority parameter of the SQL statement
  40. * @var string
  41. */
  42. protected $priority;
  43. /**
  44. * A boolean value used to set the IGNORE parameter
  45. * @var boolean
  46. */
  47. protected $ignore = false;
  48. /**
  49. * Table to update.
  50. * @var string
  51. */
  52. protected $table;
  53. /**
  54. * Boolean flag for ON DUPLICATE KEY UPDATE
  55. * @var boolean
  56. */
  57. protected $updateOnDuplicate = false;
  58. /**
  59. * The columns to update on a duplicate key.
  60. * @var array[string]
  61. */
  62. protected $updateColumns = array();
  63. public function __construct($table = NULL, $data = NULL, $db = NULL, $priority = NULL)
  64. {
  65. $this->data = new Staple_Query_DataSet();
  66. //Process Database connection
  67. if($db instanceof mysqli)
  68. {
  69. $this->setDb($db);
  70. }
  71. else
  72. {
  73. try {
  74. $this->setDb(Staple_DB::get());
  75. }
  76. catch (Exception $e)
  77. {
  78. $this->setDb(new mysqli());
  79. }
  80. }
  81. //No DB = Bad
  82. if(!($this->db instanceof mysqli))
  83. {
  84. throw new Exception('Unable to create database object', Staple_Error::DB_ERROR);
  85. }
  86. //Set Table
  87. if(isset($table))
  88. {
  89. $this->setTable($table);
  90. }
  91. //Set Data
  92. if(isset($data))
  93. {
  94. $this->setData($data);
  95. }
  96. //Set Priority
  97. if(isset($priority))
  98. {
  99. $this->setPriority($priority);
  100. }
  101. }
  102. /**
  103. * Execute the build function and return the result when converting to a string.
  104. */
  105. public function __toString()
  106. {
  107. try {
  108. $msg = $this->build();
  109. }
  110. catch (Exception $e)
  111. {
  112. $msg = $e->getMessage();
  113. }
  114. return $msg;
  115. }
  116. /**
  117. *
  118. * @see Staple_Query::build()
  119. */
  120. function build()
  121. {
  122. //Statement Start
  123. $stmt = "INSERT ";
  124. //Flags
  125. if(isset($this->priority))
  126. {
  127. $stmt .= $this->priority.' ';
  128. }
  129. if($this->ignore === TRUE)
  130. {
  131. $stmt .= 'IGNORE ';
  132. }
  133. //Table
  134. $stmt .= "\nINTO ".$this->table.' ';
  135. //Data
  136. if($this->data instanceof Staple_Query_DataSet)
  137. {
  138. $stmt .= $this->data->getInsertString();
  139. }
  140. elseif($this->data instanceof Staple_Query_Select)
  141. {
  142. $stmt .= "\n".$this->data;
  143. }
  144. //Duplicate Updates
  145. if($this->updateOnDuplicate === true)
  146. {
  147. $first = true;
  148. $stmt .= "\nON DUPLICATE KEY UPDATE ";
  149. foreach($this->updateColumns as $ucol)
  150. {
  151. if($first === true)
  152. {
  153. $first = false;
  154. }
  155. else
  156. {
  157. $stmt .= ',';
  158. }
  159. $stmt .= " $ucol=VALUES($ucol)";
  160. }
  161. }
  162. return $stmt;
  163. }
  164. /**
  165. * Executes the query.
  166. * @return mysqli_result | bool
  167. */
  168. public function Execute()
  169. {
  170. if($this->db instanceof mysqli)
  171. {
  172. return $this->db->query($this->build());
  173. }
  174. else
  175. {
  176. try
  177. {
  178. $this->db = Staple_DB::get();
  179. }
  180. catch (Exception $e)
  181. {
  182. //@todo try for a default connection if no staple connection
  183. throw new Exception('No Database Connection', Staple_Error::DB_ERROR);
  184. }
  185. if($this->db instanceof mysqli)
  186. {
  187. return $this->db->query($this->build());
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. * Adds or replaces data in the insert dataset.
  194. * @param array $data
  195. * @throws Exception
  196. */
  197. public function addData(array $data)
  198. {
  199. if($this->data instanceof Staple_Query_Select)
  200. {
  201. throw new Exception('Cannot add data to an INSERT ... SELECT statement.', Staple_Error::DB_ERROR);
  202. }
  203. $this->data->addData($data);
  204. return $this;
  205. }
  206. /**
  207. * Adds or replaces a specific column value. Alias is set Data Column
  208. * @param string $column
  209. * @param mixed $data
  210. * @throws Exception
  211. * @see self::setDataColumn
  212. */
  213. public function addDataColumn($column, $data)
  214. {
  215. return $this->setDataColumn($column, $data);
  216. }
  217. /**
  218. * Adds a literal value to the dataset without conversion.
  219. * @param string $column
  220. * @param string $value
  221. */
  222. public function addLiteralColumn($column, $value)
  223. {
  224. return $this->setDataColumn($column, $value, true);
  225. }
  226. //----------------------------------------------GETTERS AND SETTERS----------------------------------------------
  227. /**
  228. * @return the $db
  229. */
  230. public function getDb()
  231. {
  232. return $this->db;
  233. }
  234. /**
  235. * @return the $data
  236. */
  237. public function getData()
  238. {
  239. return $this->data;
  240. }
  241. /**
  242. * @return the $priority
  243. */
  244. public function getPriority()
  245. {
  246. return $this->priority;
  247. }
  248. /**
  249. * @return the $ignore
  250. */
  251. public function getIgnore()
  252. {
  253. return $this->ignore;
  254. }
  255. /**
  256. * @return the $table
  257. */
  258. public function getTable()
  259. {
  260. return $this->table;
  261. }
  262. /**
  263. * @return the $updateOnDuplicate
  264. */
  265. public function getUpdateOnDuplicate()
  266. {
  267. return $this->updateOnDuplicate;
  268. }
  269. /**
  270. * @return the $updateColumns
  271. */
  272. public function getUpdateColumns()
  273. {
  274. return $this->updateColumns;
  275. }
  276. /**
  277. * @param mysqli $db
  278. */
  279. public function setDb(mysqli $db)
  280. {
  281. $this->db = $db;
  282. return $this;
  283. }
  284. /**
  285. * Sets the $data
  286. * @param Staple_Query_Select | Staple_Query_DataSet | array $data
  287. */
  288. public function setData($data)
  289. {
  290. if($data instanceof Staple_Query_Select || $data instanceof Staple_Query_DataSet)
  291. {
  292. $this->data = $data;
  293. }
  294. elseif(is_array($data))
  295. {
  296. $this->data = new Staple_Query_DataSet($data);
  297. }
  298. else
  299. {
  300. throw new Exception('Data must be an instance of Staple_Query_DataSet, an instance of Staple_Query_Select or an array', Staple_Error::APPLICATION_ERROR);
  301. }
  302. return $this;
  303. }
  304. /**
  305. * Sets the specified value for a specific column.
  306. * @param string $column
  307. * @param mixed $data
  308. * @param bool $literal
  309. * @throws Exception
  310. */
  311. public function setDataColumn($column,$data,$literal = false)
  312. {
  313. if($this->data instanceof Staple_Query_Select)
  314. {
  315. throw new Exception('Cannot add data to an INSERT ... SELECT statement.', Staple_Error::DB_ERROR);
  316. }
  317. if($literal === true)
  318. {
  319. $this->data->addLiteralColumn($column, $data);
  320. }
  321. else
  322. {
  323. $this->data[$column] = $data;
  324. }
  325. return $this;
  326. }
  327. /**
  328. * @param string $priority
  329. */
  330. public function setPriority($priority)
  331. {
  332. switch($priority)
  333. {
  334. case self::DELAYED:
  335. $this->priority = self::DELAYED;
  336. break;
  337. case self::HIGH:
  338. $this->priority = self::HIGH;
  339. case self::LOW:
  340. $this->priority = self::LOW;
  341. break;
  342. default: $this->priority = NULL;
  343. }
  344. return $this;
  345. }
  346. /**
  347. * @param boolean $ignore
  348. */
  349. public function setIgnore($ignore)
  350. {
  351. $this->ignore = (bool)$ignore;
  352. return $this;
  353. }
  354. /**
  355. * @param string $table
  356. */
  357. public function setTable($table)
  358. {
  359. $this->table = $table;
  360. return $this;
  361. }
  362. /**
  363. * @param bool $updateOnDuplicate
  364. */
  365. public function setUpdateOnDuplicate($updateOnDuplicate)
  366. {
  367. $this->updateOnDuplicate = (bool)$updateOnDuplicate;
  368. return $this;
  369. }
  370. /**
  371. * @param array[string] $updateColumns
  372. */
  373. public function setUpdateColumns(array $updateColumns)
  374. {
  375. $this->updateColumns = $updateColumns;
  376. return $this;
  377. }
  378. /**
  379. * Setup On Duplicate Key Update Syntax
  380. * @param bool $bool
  381. */
  382. public function onDuplicateKeyUpdate($bool = true)
  383. {
  384. $this->setUpdateOnDuplicate((bool)$bool);
  385. return $this;
  386. }
  387. }
  388. ?>