2020-09-21 02:17:37 +00:00
;; copyright (c) sean corfield, all rights reserved
(ns honey.sql-test
2020-09-23 07:14:25 +00:00
(:refer-clojure :exclude [format])
2020-09-21 02:17:37 +00:00
(:require #?(:clj [clojure.test :refer [deftest is testing]]
:cljs [cljs.test :refer-macros [deftest is testing]])
2020-09-23 07:14:25 +00:00
[honey.sql :as sut :refer [format]]))
2020-09-21 02:17:37 +00:00
(deftest mysql-tests
(is (= ["SELECT * FROM `table` WHERE `id` = ?" 1]
2020-09-24 01:15:20 +00:00
(sut/format {:select [:*] :from [:table] :where [:= :id 1]}
{:dialect :mysql}))))
2020-09-21 05:25:28 +00:00
(deftest expr-tests
2020-09-25 22:31:11 +00:00
(is (= ["id IS NULL"]
(sut/format-expr [:= :id nil])))
(is (= ["id IS NULL"]
(sut/format-expr [:is :id nil])))
(is (= ["id IS NOT NULL"]
(sut/format-expr [:<> :id nil])))
(is (= ["id IS NOT NULL"]
(sut/format-expr [:!= :id nil])))
(is (= ["id IS NOT NULL"]
(sut/format-expr [:is-not :id nil])))
;; degenerate cases:
(is (= ["NULL IS NULL"]
(sut/format-expr [:= nil nil])))
(is (= ["NULL IS NOT NULL"]
(sut/format-expr [:<> nil nil])))
2020-09-21 05:25:28 +00:00
(is (= ["id = ?" 1]
2020-09-24 05:25:13 +00:00
(sut/format-expr [:= :id 1])))
2020-09-21 05:25:28 +00:00
(is (= ["id + ?" 1]
2020-09-24 05:25:13 +00:00
(sut/format-expr [:+ :id 1])))
2020-09-21 05:25:28 +00:00
(is (= ["? + (? + quux)" 1 1]
2020-09-24 05:25:13 +00:00
(sut/format-expr [:+ 1 [:+ 1 :quux]])))
2020-09-25 23:40:15 +00:00
(is (= ["? + ? + quux" 1 1]
(sut/format-expr [:+ 1 1 :quux])))
2020-09-21 05:25:28 +00:00
(is (= ["FOO(BAR(? + G(abc)), F(?, quux))" 2 1]
2020-09-24 05:25:13 +00:00
(sut/format-expr [:foo [:bar [:+ 2 [:g :abc]]] [:f 1 :quux]])))
2020-09-21 05:25:28 +00:00
(is (= ["id"]
2020-09-24 05:25:13 +00:00
(sut/format-expr :id)))
2020-09-21 05:25:28 +00:00
(is (= ["?" 1]
2020-09-24 05:25:13 +00:00
(sut/format-expr 1)))
2020-09-21 05:25:28 +00:00
(is (= ["INTERVAL ? DAYS" 30]
2020-09-24 05:25:13 +00:00
(sut/format-expr [:interval 30 :days]))))
2020-09-21 05:25:28 +00:00
(deftest where-test
2020-09-23 07:14:25 +00:00
(is (= ["WHERE id = ?" 1]
(#'sut/format-on-expr :where [:= :id 1]))))
2020-09-21 05:25:28 +00:00
(deftest general-tests
(is (= ["SELECT * FROM \"table\" WHERE \"id\" = ?" 1]
2020-09-24 01:15:20 +00:00
(sut/format {:select [:*] :from [:table] :where [:= :id 1]} {:quoted true})))
2020-09-23 07:14:25 +00:00
;; temporarily remove AS from alias here
2020-09-26 07:17:31 +00:00
(is (= ["SELECT \"t\".* FROM \"table\" AS \"t\" WHERE \"id\" = ?" 1]
2020-09-24 01:15:20 +00:00
(sut/format {:select [:t.*] :from [[:table :t]] :where [:= :id 1]} {:quoted true})))
2020-09-21 05:25:28 +00:00
(is (= ["SELECT * FROM \"table\" GROUP BY \"foo\", \"bar\""]
2020-09-24 01:15:20 +00:00
(sut/format {:select [:*] :from [:table] :group-by [:foo :bar]} {:quoted true})))
2020-09-21 05:25:28 +00:00
(is (= ["SELECT * FROM \"table\" GROUP BY DATE(\"bar\")"]
2020-09-24 01:15:20 +00:00
(sut/format {:select [:*] :from [:table] :group-by [[:date :bar]]} {:quoted true})))
2020-09-21 05:25:28 +00:00
(is (= ["SELECT * FROM \"table\" ORDER BY \"foo\" DESC, \"bar\" ASC"]
2020-09-24 01:15:20 +00:00
(sut/format {:select [:*] :from [:table] :order-by [[:foo :desc] :bar]} {:quoted true})))
2020-09-21 05:25:28 +00:00
(is (= ["SELECT * FROM \"table\" ORDER BY DATE(\"expiry\") DESC, \"bar\" ASC"]
2020-09-24 01:15:20 +00:00
(sut/format {:select [:*] :from [:table] :order-by [[[:date :expiry] :desc] :bar]} {:quoted true})))
2020-09-21 05:25:28 +00:00
(is (= ["SELECT * FROM \"table\" WHERE DATE_ADD(\"expiry\", INTERVAL ? DAYS) < NOW()" 30]
2020-09-24 01:15:20 +00:00
(sut/format {:select [:*] :from [:table] :where [:< [:date_add :expiry [:interval 30 :days]] [:now]]} {:quoted true})))
2020-09-21 05:25:28 +00:00
(is (= ["SELECT * FROM `table` WHERE `id` = ?" 1]
2020-09-24 01:15:20 +00:00
(sut/format {:select [:*] :from [:table] :where [:= :id 1]} {:dialect :mysql})))
(is (= ["SELECT * FROM \"table\" WHERE \"id\" IN (?, ?, ?, ?)" 1 2 3 4]
(sut/format {:select [:*] :from [:table] :where [:in :id [1 2 3 4]]} {:quoted true}))))
2020-09-23 07:14:25 +00:00
2020-09-26 07:17:31 +00:00
;; issue-based tests
(deftest subquery-alias-263
(is (= ["SELECT type FROM (SELECT address AS field-alias FROM Candidate) AS sub-q-alias"]
(sut/format {:select [:type]
:from [[{:select [[:address :field-alias]]
:from [:Candidate]} :sub-q-alias]]})))
(is (= ["SELECT type FROM (SELECT address field-alias FROM Candidate) sub-q-alias"]
(sut/format {:select [:type]
:from [[{:select [[:address :field-alias]]
:from [:Candidate]} :sub-q-alias]]}
{:dialect :oracle :quoted false}))))
2020-09-23 07:14:25 +00:00
2020-09-26 07:17:31 +00:00
;; tests lifted from HoneySQL v1 to check for compatibility
2020-09-23 07:14:25 +00:00
(deftest alias-splitting
(is (= ["SELECT `aa`.`c` AS `a.c`, `bb`.`c` AS `b.c`, `cc`.`c` AS `c.c`"]
(format {:select [[:aa.c "a.c"]
[:bb.c :b.c]
[:cc.c 'c.c]]}
{:dialect :mysql}))
"aliases containing \".\" are quoted as necessary but not split"))
(deftest values-alias
2020-09-26 07:17:31 +00:00
(is (= ["SELECT vals.a FROM (VALUES (?, ?, ?)) AS vals (a, b, c)" 1 2 3]
2020-09-23 07:14:25 +00:00
(format {:select [:vals.a]
:from [[{:values [[1 2 3]]} [:vals {:columns [:a :b :c]}]]]}))))
(deftest test-cte
(is (= (format {:with [[:query {:select [:foo] :from [:bar]}]]})
2020-09-25 23:38:38 +00:00
["WITH query AS (SELECT foo FROM bar)"]))
2020-09-23 07:14:25 +00:00
(is (= (format {:with-recursive [[:query {:select [:foo] :from [:bar]}]]})
2020-09-25 23:38:38 +00:00
["WITH RECURSIVE query AS (SELECT foo FROM bar)"]))
2020-09-23 07:14:25 +00:00
(is (= (format {:with [[[:static {:columns [:a :b :c]}] {:values [[1 2 3] [4 5 6]]}]]})
["WITH static (a, b, c) AS (VALUES (?, ?, ?), (?, ?, ?))" 1 2 3 4 5 6]))
(is (= (format
{:with [[[:static {:columns [:a :b :c]}]
{:values [[1 2 3] [4 5 6]]}]]
:select [:*]
:from [:static]})
["WITH static (a, b, c) AS (VALUES (?, ?, ?), (?, ?, ?)) SELECT * FROM static" 1 2 3 4 5 6])))
(deftest insert-into
(is (= (format {:insert-into :foo})
["INSERT INTO foo"]))
(is (= (format {:insert-into [:foo {:select [:bar] :from [:baz]}]})
["INSERT INTO foo SELECT bar FROM baz"]))
(is (= (format {:insert-into [[:foo [:a :b :c]] {:select [:d :e :f] :from [:baz]}]})
["INSERT INTO foo (a, b, c) SELECT d, e, f FROM baz"]))
(is (= (format {:insert-into [[:foo [:a :b :c]] {:select [:d :e :f] :from [:baz]}]})
["INSERT INTO foo (a, b, c) SELECT d, e, f FROM baz"])))
(deftest insert-into-namespaced
;; un-namespaced: works as expected:
(is (= (format {:insert-into :foo :values [{:foo/id 1}]})
["INSERT INTO foo (id) VALUES (?)" 1]))
(is (= (format {:insert-into :foo :columns [:foo/id] :values [[2]]})
["INSERT INTO foo (id) VALUES (?)" 2]))
(is (= (format {:insert-into :foo :values [{:foo/id 1}]}
{:namespace-as-table? true})
["INSERT INTO foo (id) VALUES (?)" 1]))
(is (= (format {:insert-into :foo :columns [:foo/id] :values [[2]]}
{:namespace-as-table? true})
["INSERT INTO foo (id) VALUES (?)" 2])))
(deftest exists-test
2020-09-24 01:15:20 +00:00
;; EXISTS should never have been implemented as SQL syntax: it's an operator!
#_(is (= (format {:exists {:select [:a] :from [:foo]}})
["EXISTS (SELECT a FROM foo)"]))
2020-09-25 02:07:32 +00:00
;; select function call with an alias:
2020-09-24 01:15:20 +00:00
(is (= (format {:select [[[:exists {:select [:a] :from [:foo]}] :x]]})
["SELECT EXISTS (SELECT a FROM foo) AS x"]))
2020-09-25 02:07:32 +00:00
;; select function call with no alias required:
(is (= (format {:select [[[:exists {:select [:a] :from [:foo]}]]]})
["SELECT EXISTS (SELECT a FROM foo)"]))
2020-09-23 07:14:25 +00:00
(is (= (format {:select [:id]
:from [:foo]
:where [:exists {:select [1]
:from [:bar]
:where :deleted}]})
["SELECT id FROM foo WHERE EXISTS (SELECT ? FROM bar WHERE deleted)" 1])))
(deftest array-test
2020-09-24 05:25:13 +00:00
(is (= (format {:insert-into :foo
:columns [:baz]
:values [[[:array [1 2 3 4]]]]})
["INSERT INTO foo (baz) VALUES (ARRAY[?, ?, ?, ?])" 1 2 3 4]))
(is (= (format {:insert-into :foo
:columns [:baz]
:values [[[:array ["one" "two" "three"]]]]})
["INSERT INTO foo (baz) VALUES (ARRAY[?, ?, ?])" "one" "two" "three"])))
2020-09-23 07:14:25 +00:00
(deftest union-test
;; UNION and INTERSECT subexpressions should not be parenthesized.
;; If you need to add more complex expressions, use a subquery like this:
;; SELECT foo FROM bar1
;; UNION
;; SELECT foo FROM (SELECT foo FROM bar2 ORDER BY baz LIMIT 2)
;; ORDER BY foo ASC
(is (= (format {:union [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]})
2020-09-24 01:15:20 +00:00
["SELECT foo FROM bar1 UNION SELECT foo FROM bar2"]))
(testing "union complex values"
(is (= (format {:union [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]
:with [[[:bar {:columns [:spam :eggs]}]
{:values [[1 2] [3 4] [5 6]]}]]})
["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) SELECT foo FROM bar1 UNION SELECT foo FROM bar2"
1 2 3 4 5 6]))))
2020-09-23 07:14:25 +00:00
(deftest union-all-test
(is (= (format {:union-all [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]})
["SELECT foo FROM bar1 UNION ALL SELECT foo FROM bar2"])))
(deftest intersect-test
(is (= (format {:intersect [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]})
["SELECT foo FROM bar1 INTERSECT SELECT foo FROM bar2"])))
(deftest except-test
(is (= (format {:except [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]})
["SELECT foo FROM bar1 EXCEPT SELECT foo FROM bar2"])))
(deftest inner-parts-test
(testing "The correct way to apply ORDER BY to various parts of a UNION"
(is (= (format
{:union
[{:select [:amount :id :created_on]
:from [:transactions]}
{:select [:amount :id :created_on]
:from [{:select [:amount :id :created_on]
:from [:other_transactions]
:order-by [[:amount :desc]]
:limit 5}]}]
:order-by [[:amount :asc]]})
["SELECT amount, id, created_on FROM transactions UNION SELECT amount, id, created_on FROM (SELECT amount, id, created_on FROM other_transactions ORDER BY amount DESC LIMIT ?) ORDER BY amount ASC" 5]))))
(deftest compare-expressions-test
(testing "Sequences should be fns when in value/comparison spots"
2020-09-24 01:15:20 +00:00
(is (= ["SELECT foo FROM bar WHERE (col1 MOD ?) = (col2 + ?)" 4 4]
2020-09-23 07:14:25 +00:00
(format {:select [:foo]
:from [:bar]
:where [:= [:mod :col1 4] [:+ :col2 4]]}))))
(testing "Value context only applies to sequences in value/comparison spots"
(let [sub {:select [:%sum.amount]
:from [:bar]
:where [:in :id ["id-1" "id-2"]]}]
2020-09-24 01:15:20 +00:00
(is (= ["SELECT total FROM foo WHERE (SELECT sum(amount) FROM bar WHERE id IN (?, ?)) = total" "id-1" "id-2"]
2020-09-23 07:14:25 +00:00
(format {:select [:total]
:from [:foo]
:where [:= sub :total]})))
2020-09-24 01:15:20 +00:00
(is (= ["WITH t AS (SELECT sum(amount) FROM bar WHERE id IN (?, ?)) SELECT total FROM foo WHERE total = t" "id-1" "id-2"]
2020-09-23 07:14:25 +00:00
(format {:with [[:t sub]]
:select [:total]
:from [:foo]
:where [:= :total :t]}))))))
(deftest union-with-cte
(is (= (format {:union [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]
:with [[[:bar {:columns [:spam :eggs]}]
{:values [[1 2] [3 4] [5 6]]}]]})
["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) SELECT foo FROM bar1 UNION SELECT foo FROM bar2" 1 2 3 4 5 6])))
(deftest union-all-with-cte
(is (= (format {:union-all [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]
:with [[[:bar {:columns [:spam :eggs]}]
{:values [[1 2] [3 4] [5 6]]}]]})
["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) SELECT foo FROM bar1 UNION ALL SELECT foo FROM bar2" 1 2 3 4 5 6])))
(deftest parameterizer-none
2020-09-25 23:39:50 +00:00
(testing "array parameter -- fail: parameterizer"
2020-09-24 05:25:13 +00:00
(is (= (format {:insert-into :foo
:columns [:baz]
:values [[[:array [1 2 3 4]]]]}
{:parameterizer :none})
["INSERT INTO foo (baz) VALUES (ARRAY[1, 2, 3, 4])"])))
2020-09-23 07:14:25 +00:00
2020-09-25 23:39:50 +00:00
(testing "union complex values -- fail: parameterizer"
2020-09-23 07:14:25 +00:00
(is (= (format {:union [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]
:with [[[:bar {:columns [:spam :eggs]}]
{:values [[1 2] [3 4] [5 6]]}]]}
{:parameterizer :none})
["WITH bar (spam, eggs) AS (VALUES (1, 2), (3, 4), (5, 6)) SELECT foo FROM bar1 UNION SELECT foo FROM bar2"]))))
2020-09-25 23:39:50 +00:00
(deftest inline-was-parameterizer-none
(testing "array parameter"
(is (= (format {:insert-into :foo
:columns [:baz]
:values [[[:array (mapv vector
(repeat :inline)
[1 2 3 4])]]]})
["INSERT INTO foo (baz) VALUES (ARRAY[1, 2, 3, 4])"])))
2020-09-23 07:14:25 +00:00
2020-09-25 23:39:50 +00:00
(testing "union complex values"
(is (= (format {:union [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]
:with [[[:bar {:columns [:spam :eggs]}]
{:values (mapv #(mapv vector (repeat :inline) %)
[[1 2] [3 4] [5 6]])}]]})
["WITH bar (spam, eggs) AS (VALUES (1, 2), (3, 4), (5, 6)) SELECT foo FROM bar1 UNION SELECT foo FROM bar2"]))))
2020-09-23 07:14:25 +00:00
#_(defmethod parameterize :single-quote [_ value pname] (str \' value \'))
#_(defmethod parameterize :mysql-fill [_ value pname] "?")
2020-09-25 23:39:50 +00:00
(deftest former-parameterizer-tests-where-and
(testing "should ignore a nil predicate -- fail: postgresql parameterizer"
(is (= (format {:where [:and
[:= :foo "foo"]
[:= :bar "bar"]
nil
[:= :quux "quux"]]}
{:parameterizer :postgresql})
["WHERE (foo = ?) AND (bar = $2) AND (quux = $3)" "foo" "bar" "quux"])))
;; this is _almost_ what :inline should be doing:
#_(testing "should fill param with single quote"
(is (= (format {:where [:and
[:= :foo "foo"]
[:= :bar "bar"]
nil
[:= :quux "quux"]]}
{:parameterizer :single-quote})
["WHERE (foo = 'foo') AND (bar = 'bar') AND (quux = 'quux')" "foo" "bar" "quux"])))
(testing "should inline params with single quote"
(is (= (format {:where [:and
[:= :foo [:inline "foo"]]
[:= :bar [:inline "bar"]]
nil
[:= :quux [:inline "quux"]]]})
["WHERE (foo = 'foo') AND (bar = 'bar') AND (quux = 'quux')"])))
;; this is the normal behavior -- not a custom parameterizer!
2020-09-23 07:14:25 +00:00
(testing "should fill param with ?"
2020-09-25 23:39:50 +00:00
(is (= (format {:where [:and
[:= :foo "foo"]
[:= :bar "bar"]
nil
[:= :quux "quux"]]}
;; this never did anything useful:
#_{:parameterizer :mysql-fill})
["WHERE (foo = ?) AND (bar = ?) AND (quux = ?)" "foo" "bar" "quux"]))))
2020-09-23 07:14:25 +00:00
(deftest set-before-from ; issue 235
(is (=
2020-09-26 07:17:31 +00:00
["UPDATE \"films\" \"f\" SET \"kind\" = \"c\".\"test\" FROM (SELECT \"b\".\"test\" FROM \"bar\" AS \"b\" WHERE \"b\".\"id\" = ?) AS \"c\" WHERE \"f\".\"kind\" = ?" 1 "drama"]
2020-09-23 07:14:25 +00:00
(->
{:update [:films :f]
2020-09-23 19:55:02 +00:00
:set {:kind :c.test}
2020-09-23 07:14:25 +00:00
:from [[{:select [:b.test]
:from [[:bar :b]]
:where [:= :b.id 1]} :c]]
:where [:= :f.kind "drama"]}
2020-09-24 01:15:20 +00:00
(format {:quoted true})))))
2020-09-23 07:14:25 +00:00
(deftest set-after-join
(is (=
["UPDATE `foo` INNER JOIN `bar` ON `bar`.`id` = `foo`.`bar_id` SET `a` = ? WHERE `bar`.`b` = ?" 1 42]
(->
{:update :foo
:join [:bar [:= :bar.id :foo.bar_id]]
:set {:a 1}
:where [:= :bar.b 42]}
(format {:dialect :mysql})))))
(deftest delete-from-test
(is (= ["DELETE FROM `foo` WHERE `foo`.`id` = ?" 42]
(-> {:delete-from :foo
:where [:= :foo.id 42]}
(format {:dialect :mysql})))))
(deftest delete-test
2020-09-26 07:17:31 +00:00
(is (= ["DELETE `t1`, `t2` FROM `table1` AS `t1` INNER JOIN `table2` AS `t2` ON `t1`.`fk` = `t2`.`id` WHERE `t1`.`bar` = ?" 42]
2020-09-23 07:14:25 +00:00
(-> {:delete [:t1 :t2]
:from [[:table1 :t1]]
:join [[:table2 :t2] [:= :t1.fk :t2.id]]
:where [:= :t1.bar 42]}
(format {:dialect :mysql})))))
(deftest truncate-test
(is (= ["TRUNCATE `foo`"]
(-> {:truncate :foo}
(format {:dialect :mysql})))))
(deftest inlined-values-are-stringified-correctly
2020-09-25 23:38:11 +00:00
(is (= ["SELECT 'foo', 'It''s a quote!', bar, NULL"]
2020-09-24 05:52:57 +00:00
(format {:select [[[:inline "foo"]]
2020-09-25 23:38:11 +00:00
[[:inline "It's a quote!"]]
2020-09-24 05:52:57 +00:00
[[:inline :bar]]
[[:inline nil]]]}))))
2020-09-23 07:14:25 +00:00
;; Make sure if Locale is Turkish we're not generating queries like İNNER JOIN (dot over the I) because
;; `string/upper-case` is converting things to upper-case using the default Locale. Generated query should be the same
;; regardless of system Locale. See #236
#?(:clj
(deftest statements-generated-correctly-with-turkish-locale
(let [format-with-locale (fn [^String language-tag]
(let [original-locale (java.util.Locale/getDefault)]
(try
(java.util.Locale/setDefault (java.util.Locale/forLanguageTag language-tag))
(format {:select [:t2.name]
:from [[:table1 :t1]]
:join [[:table2 :t2] [:= :t1.fk :t2.id]]
:where [:= :t1.id 1]})
(finally
(java.util.Locale/setDefault original-locale)))))]
(is (= (format-with-locale "en")
(format-with-locale "tr"))))))
(deftest join-on-true-253
;; used to work on honeysql 0.9.2; broke in 0.9.3
2020-09-26 07:17:31 +00:00
(is (= ["SELECT foo FROM bar INNER JOIN table AS t ON TRUE"]
2020-09-23 07:14:25 +00:00
(format {:select [:foo]
:from [:bar]
:join [[:table :t] true]}))))
(deftest cross-join-test
(is (= ["SELECT * FROM foo CROSS JOIN bar"]
(format {:select [:*]
:from [:foo]
:cross-join [:bar]})))
2020-09-26 07:17:31 +00:00
(is (= ["SELECT * FROM foo AS f CROSS JOIN bar b"]
2020-09-23 07:14:25 +00:00
(format {:select [:*]
:from [[:foo :f]]
:cross-join [[:bar :b]]}))))
2020-09-26 22:16:12 +00:00
2020-09-28 18:49:29 +00:00
(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}))))