@@ -242,10 +242,11 @@ defmodule Ecto.Adapters.SQLite3.Connection do
242
242
insert ( prefix , table , header , rows , on_conflict , returning , [ ] )
243
243
end
244
244
245
- def insert ( prefix , table , [ ] , [ [ ] ] , _on_conflict , returning , [ ] ) do
245
+ def insert ( prefix , table , [ ] , [ [ ] ] , on_conflict , returning , [ ] ) do
246
246
[
247
247
"INSERT INTO " ,
248
248
quote_table ( prefix , table ) ,
249
+ insert_as ( on_conflict ) ,
249
250
" DEFAULT VALUES" ,
250
251
returning ( returning )
251
252
]
@@ -256,6 +257,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
256
257
[
257
258
"INSERT INTO " ,
258
259
quote_table ( prefix , table ) ,
260
+ insert_as ( on_conflict ) ,
259
261
" (" ,
260
262
fields ,
261
263
") " ,
@@ -639,49 +641,45 @@ defmodule Ecto.Adapters.SQLite3.Connection do
639
641
## Query generation
640
642
##
641
643
642
- def on_conflict ( { :raise , _ , [ ] } , _header ) , do: [ ]
644
+ defp on_conflict ( { :raise , _ , [ ] } , _header ) , do: [ ]
643
645
644
- def on_conflict ( { :nothing , _ , targets } , _header ) do
646
+ defp on_conflict ( { :nothing , _ , targets } , _header ) do
645
647
[ " ON CONFLICT " , conflict_target ( targets ) | "DO NOTHING" ]
646
648
end
647
649
648
- def on_conflict ( { :replace_all , _ , [ ] } , _header ) do
650
+ defp on_conflict ( { :replace_all , _ , { :constraint , _ } } , _header ) do
651
+ raise ArgumentError , "Upsert in SQLite3 does not support ON CONSTRAINT"
652
+ end
653
+
654
+ defp on_conflict ( { :replace_all , _ , [ ] } , _header ) do
649
655
raise ArgumentError , "Upsert in SQLite3 requires :conflict_target"
650
656
end
651
657
652
- def on_conflict ( { :replace_all , _ , { :constraint , _ } } , _header ) do
653
- raise ArgumentError , "Upsert in SQLite3 does not support ON CONSTRAINT"
658
+ defp on_conflict ( { :replace_all , _ , targets } , header ) do
659
+ [ " ON CONFLICT " , conflict_target ( targets ) , "DO " | replace ( header ) ]
654
660
end
655
661
656
- def on_conflict ( { :replace_all , _ , targets } , header ) do
657
- [
658
- " ON CONFLICT " ,
659
- conflict_target ( targets ) ,
660
- "DO " | replace_all ( header )
661
- ]
662
+ defp on_conflict ( { fields , _ , targets } , _header ) when is_list ( fields ) do
663
+ [ " ON CONFLICT " , conflict_target ( targets ) , "DO " | replace ( fields ) ]
662
664
end
663
665
664
- def on_conflict ( { query , _ , targets } , _header ) do
665
- [
666
- " ON CONFLICT " ,
667
- conflict_target ( targets ) ,
668
- "DO " | update_all ( query , "UPDATE SET " )
669
- ]
666
+ defp on_conflict ( { query , _ , targets } , _header ) do
667
+ [ " ON CONFLICT " , conflict_target ( targets ) , "DO " | update_all ( query , "UPDATE SET " ) ]
670
668
end
671
669
672
- def conflict_target ( [ ] ) , do: ""
670
+ defp conflict_target ( [ ] ) , do: ""
673
671
674
- def conflict_target ( targets ) do
672
+ defp conflict_target ( targets ) do
675
673
[ ?( , intersperse_map ( targets , ?, , & quote_name / 1 ) , ?) , ?\s ]
676
674
end
677
675
678
- def replace_all ( header ) do
676
+ defp replace ( fields ) do
679
677
[
680
- "UPDATE SET "
681
- | intersperse_map ( header , ?, , fn field ->
682
- quoted = quote_name ( field )
683
- [ quoted , " = " , "EXCLUDED." | quoted ]
684
- end )
678
+ "UPDATE SET " |
679
+ intersperse_map ( fields , ?, , fn field ->
680
+ quoted = quote_name ( field )
681
+ [ quoted , " = " , "EXCLUDED." | quoted ]
682
+ end )
685
683
]
686
684
end
687
685
@@ -721,6 +719,14 @@ defmodule Ecto.Adapters.SQLite3.Connection do
721
719
end )
722
720
end
723
721
722
+ defp insert_as ( { % { sources: sources } , _ , _ } ) do
723
+ { _expr , name , _schema } = create_name ( sources , 0 , [ ] )
724
+ [ " AS " | name ]
725
+ end
726
+ defp insert_as ( { _ , _ , _ } ) do
727
+ [ ]
728
+ end
729
+
724
730
binary_ops = [
725
731
==: " = " ,
726
732
!=: " != " ,
0 commit comments