@@ -209,12 +209,21 @@ class BaseBuilder
209209 protected $ canLimitWhereUpdates = true ;
210210
211211 /**
212- * Ignore errors on insert statements
213- * for example for duplicate keys.
212+ * Ignore data that cause certain
213+ * exceptions, for example in case of
214+ * duplicate keys.
214215 *
215216 * @var bool
216217 */
217- protected $ insertIgnore = false ;
218+ protected $ ignore = false ;
219+
220+ /**
221+ * Specifies which sql statements
222+ * support the ignore option.
223+ *
224+ * @var array
225+ */
226+ protected $ supportedIgnoreStatements = [];
218227
219228 //--------------------------------------------------------------------
220229
@@ -267,15 +276,16 @@ public function getBinds(): array
267276 /**
268277 * Ignore
269278 *
270- * Set ignore Flag for next insert query.
279+ * Set ignore Flag for next insert,
280+ * update or delete query.
271281 *
272282 * @param bool $ignore
273283 *
274284 * @return BaseBuilder
275285 */
276286 public function ignore (bool $ ignore = true )
277287 {
278- $ this ->insertIgnore = $ ignore ;
288+ $ this ->ignore = $ ignore ;
279289
280290 return $ this ;
281291 }
@@ -1692,7 +1702,7 @@ public function insertBatch($set = null, $escape = null, $batchSize = 100, $test
16921702 }
16931703 }
16941704
1695- $ this ->insertIgnore = false ;
1705+ $ this ->ignore = false ;
16961706
16971707 if (! $ testing )
16981708 {
@@ -1717,7 +1727,7 @@ public function insertBatch($set = null, $escape = null, $batchSize = 100, $test
17171727 */
17181728 protected function _insertBatch ($ table , $ keys , $ values )
17191729 {
1720- return 'INSERT INTO ' . $ table . ' ( ' . implode (', ' , $ keys ) . ') VALUES ' . implode (', ' , $ values );
1730+ return 'INSERT ' . $ this -> compileIgnore ( ' insert ' ) . ' INTO ' . $ table . ' ( ' . implode (', ' , $ keys ) . ') VALUES ' . implode (', ' , $ values );
17211731 }
17221732
17231733 //--------------------------------------------------------------------
@@ -1844,7 +1854,7 @@ public function insert($set = null, $escape = null, $test = false)
18441854 ), array_keys ($ this ->QBSet ), array_values ($ this ->QBSet )
18451855 );
18461856
1847- $ this ->insertIgnore = false ;
1857+ $ this ->ignore = false ;
18481858
18491859 if ($ test === false )
18501860 {
@@ -1901,7 +1911,7 @@ protected function validateInsert()
19011911 */
19021912 protected function _insert ($ table , array $ keys , array $ unescapedKeys )
19031913 {
1904- return 'INSERT INTO ' . $ table . ' ( ' . implode (', ' , $ keys ) . ') VALUES ( ' . implode (', ' , $ unescapedKeys ) . ') ' ;
1914+ return 'INSERT ' . $ this -> compileIgnore ( ' insert ' ) . ' INTO ' . $ table . ' ( ' . implode (', ' , $ keys ) . ') VALUES ( ' . implode (', ' , $ unescapedKeys ) . ') ' ;
19051915 }
19061916
19071917 //--------------------------------------------------------------------
@@ -2016,6 +2026,8 @@ public function getCompiledUpdate($reset = true)
20162026 * @param mixed $where
20172027 * @param integer $limit
20182028 * @param boolean $test Are we testing the code?
2029+ *
2030+ * @throws DatabaseException
20192031 *
20202032 * @return boolean TRUE on success, FALSE on failure
20212033 */
@@ -2048,6 +2060,8 @@ public function update($set = null, $where = null, int $limit = null, $test = fa
20482060
20492061 $ sql = $ this ->_update ($ this ->QBFrom [0 ], $ this ->QBSet );
20502062
2063+ $ this ->ignore = false ;
2064+
20512065 if (! $ test )
20522066 {
20532067 $ this ->resetWrite ();
@@ -2080,12 +2094,14 @@ public function update($set = null, $where = null, int $limit = null, $test = fa
20802094 */
20812095 protected function _update ($ table , $ values )
20822096 {
2097+ $ valStr = [];
2098+
20832099 foreach ($ values as $ key => $ val )
20842100 {
2085- $ valstr [] = $ key . ' = ' . $ val ;
2101+ $ valStr [] = $ key . ' = ' . $ val ;
20862102 }
20872103
2088- return 'UPDATE ' . $ table . ' SET ' . implode (', ' , $ valstr )
2104+ return 'UPDATE ' . $ this -> compileIgnore ( ' update ' ) . $ table . ' SET ' . implode (', ' , $ valStr )
20892105 . $ this ->compileWhereHaving ('QBWhere ' )
20902106 . $ this ->compileOrderBy ()
20912107 . ($ this ->QBLimit ? $ this ->_limit (' ' ) : '' );
@@ -2193,6 +2209,8 @@ public function updateBatch($set = null, $index = null, $batchSize = 100, $retur
21932209 $ this ->QBWhere = [];
21942210 }
21952211
2212+ $ this ->ignore = false ;
2213+
21962214 $ this ->resetWrite ();
21972215
21982216 return $ returnSQL ? $ savedSQL : $ affected_rows ;
@@ -2214,6 +2232,8 @@ public function updateBatch($set = null, $index = null, $batchSize = 100, $retur
22142232 protected function _updateBatch ($ table , $ values , $ index )
22152233 {
22162234 $ ids = [];
2235+ $ final = [];
2236+
22172237 foreach ($ values as $ key => $ val )
22182238 {
22192239 $ ids [] = $ val [$ index ];
@@ -2237,7 +2257,7 @@ protected function _updateBatch($table, $values, $index)
22372257
22382258 $ this ->where ($ index . ' IN( ' . implode (', ' , $ ids ) . ') ' , null , false );
22392259
2240- return 'UPDATE ' . $ table . ' SET ' . substr ($ cases , 0 , -2 ) . $ this ->compileWhereHaving ('QBWhere ' );
2260+ return 'UPDATE ' . $ this -> compileIgnore ( ' update ' ) . $ table . ' SET ' . substr ($ cases , 0 , -2 ) . $ this ->compileWhereHaving ('QBWhere ' );
22412261 }
22422262
22432263 //--------------------------------------------------------------------
@@ -2306,6 +2326,8 @@ public function emptyTable($test = false)
23062326
23072327 $ sql = $ this ->_delete ($ table );
23082328
2329+ $ this ->ignore = false ;
2330+
23092331 if ($ test )
23102332 {
23112333 return $ sql ;
@@ -2422,6 +2444,8 @@ public function delete($where = '', $limit = null, $reset_data = true, $returnSQ
24222444
24232445 $ sql = $ this ->_delete ($ table );
24242446
2447+ $ this ->ignore = false ;
2448+
24252449 if (! empty ($ limit ))
24262450 {
24272451 $ this ->QBLimit = $ limit ;
@@ -2496,7 +2520,7 @@ public function decrement(string $column, int $value = 1)
24962520 */
24972521 protected function _delete ($ table )
24982522 {
2499- return 'DELETE FROM ' . $ table . $ this ->compileWhereHaving ('QBWhere ' )
2523+ return 'DELETE ' . $ this -> compileIgnore ( ' delete ' ) . ' FROM ' . $ table . $ this ->compileWhereHaving ('QBWhere ' )
25002524 . ($ this ->QBLimit ? ' LIMIT ' . $ this ->QBLimit : '' );
25012525 }
25022526
@@ -2611,6 +2635,32 @@ protected function compileSelect($select_override = false)
26112635 return $ sql ;
26122636 }
26132637
2638+ //--------------------------------------------------------------------
2639+
2640+ /**
2641+ * Compile Ignore Statement
2642+ *
2643+ * Checks if the ignore option is supported by
2644+ * the Database Driver for the specific statement.
2645+ *
2646+ * @param string $statement
2647+ *
2648+ * @return string
2649+ */
2650+ protected function compileIgnore (string $ statement )
2651+ {
2652+ $ sql = '' ;
2653+
2654+ if (
2655+ $ this ->ignore &&
2656+ isset ($ this ->supportedIgnoreStatements [$ statement ])
2657+ ) {
2658+ $ sql = trim ($ this ->supportedIgnoreStatements [$ statement ]).' ' ;
2659+ }
2660+
2661+ return $ sql ;
2662+ }
2663+
26142664 //--------------------------------------------------------------------
26152665
26162666 /**
0 commit comments