diff --git a/CHANGES.md b/CHANGES.md index 2981491..4589ee9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,6 @@ ## 1.0.next in progress +* Fix #259 so column names are always unqualified in inserts. (@jrdoane) * Switch dev/test pipeline to use CLI/`deps.edn` instead of Leiningen. * Remove macrovich dependency as this is no longer needed with modern ClojureScript. * Add mention of `next.jdbc` everywhere `clojure.java.jdbc` was mentioned. diff --git a/src/honeysql/format.cljc b/src/honeysql/format.cljc index 0fafcb5..38f7ec5 100644 --- a/src/honeysql/format.cljc +++ b/src/honeysql/format.cljc @@ -615,13 +615,15 @@ (if (and (sequential? table) (sequential? (first table))) (str "INSERT INTO " (to-sql (ffirst table)) - " (" (comma-join (map to-sql (second (first table)))) ") " + (binding [*namespace-as-table?* false] + (str " (" (comma-join (map to-sql (second (first table)))) ") ")) (binding [*subquery?* false] (to-sql (second table)))) (str "INSERT INTO " (to-sql table)))) (defmethod format-clause :columns [[_ fields] _] - (str "(" (comma-join (map to-sql fields)) ")")) + (binding [*namespace-as-table?* false] + (str "(" (comma-join (map to-sql fields)) ")"))) (defmethod format-clause :composite [[_ fields] _] (comma-join (map to-sql fields))) @@ -632,7 +634,9 @@ (str "(" (comma-join (map to-sql x)) ")")))) (let [cols (keys (first values))] (str - "(" (comma-join (map to-sql cols)) ") VALUES " + (binding [*namespace-as-table?* false] + (str "(" (comma-join (map to-sql cols)) ")")) + " VALUES " (comma-join (for [x values] (str "(" (comma-join (map #(to-sql (get x %)) cols)) ")"))))))) diff --git a/test/honeysql/format_test.cljc b/test/honeysql/format_test.cljc index cf8258c..935a15f 100644 --- a/test/honeysql/format_test.cljc +++ b/test/honeysql/format_test.cljc @@ -85,6 +85,19 @@ (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 (is (= (format {:exists {:select [:a] :from [:foo]}}) ["EXISTS (SELECT a FROM foo)"]))