@@ -179,41 +179,61 @@ if Code.ensure_loaded?(MyXQL) do
179179 end
180180
181181 @ impl true
182- def insert ( prefix , table , header , rows , on_conflict , [ ] , [ ] ) do
182+ def insert ( prefix , table , header , rows , on_conflict , returning , placeholders , opts \\ [ ] )
183+
184+ def insert ( prefix , table , header , rows , on_conflict , [ ] , [ ] , opts ) do
183185 fields = quote_names ( header )
186+ insert_mode = Keyword . get ( opts , :insert_mode )
187+ insert_keyword = insert_keyword ( insert_mode )
184188
185189 [
186- "INSERT INTO " ,
190+ insert_keyword ,
187191 quote_table ( prefix , table ) ,
188192 " (" ,
189193 fields ,
190194 ") " ,
191- insert_all ( rows ) | on_conflict ( on_conflict , header )
195+ insert_all ( rows ) | on_conflict ( on_conflict , header , opts )
192196 ]
193197 end
194198
195- def insert ( _prefix , _table , _header , _rows , _on_conflict , _returning , [ ] ) do
199+ def insert ( _prefix , _table , _header , _rows , _on_conflict , _returning , [ ] , _opts ) do
196200 error! ( nil , ":returning is not supported in insert/insert_all by MySQL" )
197201 end
198202
199- def insert ( _prefix , _table , _header , _rows , _on_conflict , _returning , _placeholders ) do
203+ def insert ( _prefix , _table , _header , _rows , _on_conflict , _returning , _placeholders , _opts ) do
200204 error! ( nil , ":placeholders is not supported by MySQL" )
201205 end
202206
203- defp on_conflict ( { _ , _ , [ _ | _ ] } , _header ) do
207+ # INSERT IGNORE when insert_mode: :ignore_errors is passed, independent of on_conflict
208+ defp insert_keyword ( :ignore_errors ) do
209+ "INSERT IGNORE INTO "
210+ end
211+
212+ defp insert_keyword ( _ ) do
213+ "INSERT INTO "
214+ end
215+
216+ defp on_conflict ( { _ , _ , [ _ | _ ] } , _header , _opts ) do
204217 error! ( nil , ":conflict_target is not supported in insert/insert_all by MySQL" )
205218 end
206219
207- defp on_conflict ( { :raise , _ , [ ] } , _header ) do
220+ defp on_conflict ( { :raise , _ , [ ] } , _header , _opts ) do
208221 [ ]
209222 end
210223
211- defp on_conflict ( { :nothing , _ , [ ] } , [ field | _ ] ) do
212- quoted = quote_name ( field )
213- [ " ON DUPLICATE KEY UPDATE " , quoted , " = " | quoted ]
224+ # When insert_mode: :ignore_errors is used with on_conflict: :nothing,
225+ # INSERT IGNORE already handles conflicts - no ON DUPLICATE KEY UPDATE needed
226+ defp on_conflict ( { :nothing , _ , [ ] } , [ field | _ ] , opts ) do
227+ if Keyword . get ( opts , :insert_mode ) == :ignore_errors do
228+ [ ]
229+ else
230+ # Default :nothing without INSERT IGNORE - uses workaround to simulate "do nothing" behavior
231+ quoted = quote_name ( field )
232+ [ " ON DUPLICATE KEY UPDATE " , quoted , " = " | quoted ]
233+ end
214234 end
215235
216- defp on_conflict ( { fields , _ , [ ] } , _header ) when is_list ( fields ) do
236+ defp on_conflict ( { fields , _ , [ ] } , _header , _opts ) when is_list ( fields ) do
217237 [
218238 " ON DUPLICATE KEY UPDATE "
219239 | Enum . map_intersperse ( fields , ?, , fn field ->
@@ -223,11 +243,11 @@ if Code.ensure_loaded?(MyXQL) do
223243 ]
224244 end
225245
226- defp on_conflict ( { % { wheres: [ ] } = query , _ , [ ] } , _header ) do
246+ defp on_conflict ( { % { wheres: [ ] } = query , _ , [ ] } , _header , _opts ) do
227247 [ " ON DUPLICATE KEY " | update_all ( query , "UPDATE " ) ]
228248 end
229249
230- defp on_conflict ( { _query , _ , [ ] } , _header ) do
250+ defp on_conflict ( { _query , _ , [ ] } , _header , _opts ) do
231251 error! (
232252 nil ,
233253 "Using a query with :where in combination with the :on_conflict option is not supported by MySQL"
0 commit comments