@@ -179,45 +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 )
184- insert_keyword = insert_keyword ( on_conflict )
186+ insert_mode = Keyword . get ( opts , :insert_mode )
187+ insert_keyword = insert_keyword ( insert_mode )
185188
186189 [
187190 insert_keyword ,
188191 quote_table ( prefix , table ) ,
189192 " (" ,
190193 fields ,
191194 ") " ,
192- insert_all ( rows ) | on_conflict ( on_conflict , header )
195+ insert_all ( rows ) | on_conflict ( on_conflict , header , opts )
193196 ]
194197 end
195198
196- defp insert_keyword ( { :nothing , _ , [ ] } ) , do: "INSERT IGNORE INTO "
197- defp insert_keyword ( _ ) , do: "INSERT INTO "
198-
199- def insert ( _prefix , _table , _header , _rows , _on_conflict , _returning , [ ] ) do
199+ def insert ( _prefix , _table , _header , _rows , _on_conflict , _returning , [ ] , _opts ) do
200200 error! ( nil , ":returning is not supported in insert/insert_all by MySQL" )
201201 end
202202
203- def insert ( _prefix , _table , _header , _rows , _on_conflict , _returning , _placeholders ) do
203+ def insert ( _prefix , _table , _header , _rows , _on_conflict , _returning , _placeholders , _opts ) do
204204 error! ( nil , ":placeholders is not supported by MySQL" )
205205 end
206206
207- 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
208217 error! ( nil , ":conflict_target is not supported in insert/insert_all by MySQL" )
209218 end
210219
211- defp on_conflict ( { :raise , _ , [ ] } , _header ) do
220+ defp on_conflict ( { :raise , _ , [ ] } , _header , _opts ) do
212221 [ ]
213222 end
214223
215- defp on_conflict ( { :nothing , _ , [ ] } , _header ) do
216- # Handled by INSERT IGNORE
217- [ ]
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
218234 end
219235
220- defp on_conflict ( { fields , _ , [ ] } , _header ) when is_list ( fields ) do
236+ defp on_conflict ( { fields , _ , [ ] } , _header , _opts ) when is_list ( fields ) do
221237 [
222238 " ON DUPLICATE KEY UPDATE "
223239 | Enum . map_intersperse ( fields , ?, , fn field ->
@@ -227,11 +243,11 @@ if Code.ensure_loaded?(MyXQL) do
227243 ]
228244 end
229245
230- defp on_conflict ( { % { wheres: [ ] } = query , _ , [ ] } , _header ) do
246+ defp on_conflict ( { % { wheres: [ ] } = query , _ , [ ] } , _header , _opts ) do
231247 [ " ON DUPLICATE KEY " | update_all ( query , "UPDATE " ) ]
232248 end
233249
234- defp on_conflict ( { _query , _ , [ ] } , _header ) do
250+ defp on_conflict ( { _query , _ , [ ] } , _header , _opts ) do
235251 error! (
236252 nil ,
237253 "Using a query with :where in combination with the :on_conflict option is not supported by MySQL"
0 commit comments