File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -106,6 +106,13 @@ struct UserQuery < Interro::QueryBuilder(User)
106106 insert! email: email, name: name
107107 end
108108
109+ def upsert (email : String , name : String )
110+ data = {email: email, name: name}
111+ insert data,
112+ on_conflict: Interro ::ConflictHandler .new " email" ,
113+ do: Interro ::Update .new(set: {name: name, updated_at: Time .utc})
114+ end
115+
109116 def destroy (user : User )
110117 self
111118 .where(id: user.id)
@@ -362,6 +369,15 @@ describe Interro do
362369 user.name.should eq " Foo"
363370 end
364371
372+ it " can upsert a row" do
373+ email = " foo-#{ UUID .random} @example.com"
374+ user = UserQuery .new.create(email: email, name: " Foo" )
375+
376+ upserted = UserQuery .new.upsert(email: email, name: " Bar" )
377+
378+ UserQuery .new.find!(id: user.id).name.should eq " Bar"
379+ end
380+
365381 it " can find a row" do
366382 email = " finduser-#{ UUID .random} @example.com"
367383 created_user = UserQuery .new.create(email: email, name: " Find User" )
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ module Interro
4444 .values
4545 .map { |value | Interro ::Any .new(value) }
4646 .to_a
47- sql = generate_query query.sql_table_name, params,
47+ sql = generate_query query.sql_table_name, params, args,
4848 on_conflict: conflict_handler,
4949 returning: - > (io : IO ) { query.select_columns io }
5050
@@ -57,7 +57,7 @@ module Interro
5757 .values
5858 .map { |value | Interro ::Any .new(value) }
5959 .to_a
60- sql = generate_query query.sql_table_name, params,
60+ sql = generate_query query.sql_table_name, params, args,
6161 on_conflict: conflict_handler,
6262 returning: nil
6363
@@ -67,6 +67,7 @@ module Interro
6767 protected def generate_query (
6868 table_name : String ,
6969 params,
70+ args,
7071 on_conflict conflict_handler : ConflictHandler ?,
7172 returning returning_clause,
7273 )
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ module Interro
1313
1414 record Any , value : Value do
1515 def self.new (values : Array )
16- new values.map { |value | Any . new(value) }
16+ new values.map { |value | new(value) }
1717 end
1818
1919 def self.new (any : self )
You can’t perform that action at this time.
0 commit comments