diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 1e30c26..dbe29f3 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -308,8 +308,7 @@ (if (keyword? x) (let [e (format-entity x {:drop-ns? true})] [(str (sql-kw k) " " e " = EXCLUDED." e)]) - (let [[sql & params] (format-set-exprs :set x)] - (into [(str (sql-kw k) " " sql)] params)))) + (format-set-exprs k x))) (def ^:private base-clause-order "The (base) order for known clauses. Can have items added and removed. diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index 1401019..fe741d0 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -399,6 +399,44 @@ :from [[:foo :f]] :cross-join [[:bar :b]]})))) -(comment ; make on conflict tests, based on nilenso repo and PG docs - (format {:insert-into :foo, :values [[1 2 3]], :on-conflict :e :do-nothing true}) - (format {:insert-into :foo, :values [[1 2 3]], :on-conflict {:on-constraint :foo_key} :do-update-set {:a 42 :b :excluded.b}})) +(deftest on-conflict-tests + ;; these examples are taken from https://www.postgresqltutorial.com/postgresql-upsert/ + (is (= [" +INSERT INTO customers +(name, email) +VALUES ('Microsoft', 'hotline@microsoft.com') +ON CONFLICT ON CONSTRAINT customers_name_key +DO NOTHING +"] + (format {:insert-into :customers + :columns [:name :email] + :values [[[:inline "Microsoft"], [:inline "hotline@microsoft.com"]]] + :on-conflict {:on-constraint :customers_name_key} + :do-nothing true} + {:pretty? true}))) + (is (= [" +INSERT INTO customers +(name, email) +VALUES ('Microsoft', 'hotline@microsoft.com') +ON CONFLICT (name) +DO NOTHING +"] + (format {:insert-into :customers + :columns [:name :email] + :values [[[:inline "Microsoft"], [:inline "hotline@microsoft.com"]]] + :on-conflict :name + :do-nothing true} + {:pretty? true}))) + (is (= [" +INSERT INTO customers +(name, email) +VALUES ('Microsoft', 'hotline@microsoft.com') +ON CONFLICT (name) +DO UPDATE SET email = EXCLUDED.email || ';' || customers.email +"] + (format {:insert-into :customers + :columns [:name :email] + :values [[[:inline "Microsoft"], [:inline "hotline@microsoft.com"]]] + :on-conflict :name + :do-update-set {:email [:|| :EXCLUDED.email [:inline ";"] :customers.email]}} + {:pretty? true}))))