diff --git a/CHANGES.md b/CHANGES.md index e14ee68..883bda5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ ## 1.0.next in progress * Fix #259 so column names are always unqualified in inserts. (@jrdoane) +* Fix #257 by adding support for `cross-join` / `merge-cross-join` / `:cross-join`. (@dcj) * Switch dev/test pipeline to use CLI/`deps.edn` instead of Leiningen. Also add CI vi both CircleCI and GitHub Actions. * 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/core.cljc b/src/honeysql/core.cljc index 609ba70..fcb1541 100644 --- a/src/honeysql/core.cljc +++ b/src/honeysql/core.cljc @@ -43,6 +43,7 @@ :left-join, :merge-left-join :right-join, :merge-right-join :full-join, :merge-full-join + :cross-join, :merge-cross-join :where, :merge-where :group-by, :merge-group-by :having, :merge-having diff --git a/src/honeysql/format.cljc b/src/honeysql/format.cljc index 38f7ec5..2eb9c43 100644 --- a/src/honeysql/format.cljc +++ b/src/honeysql/format.cljc @@ -241,6 +241,7 @@ :left-join 130 :right-join 140 :full-join 150 + :cross-join 152 :set 155 :set1 156 ; high-priority set clause (synonym for :set) :where 160 @@ -567,6 +568,9 @@ (space-join (map #(apply format-join :full %) (partition 2 join-groups)))) +(defmethod format-clause :cross-join [[_ join-groups] _] + (space-join (map #(format-join :cross % nil) join-groups))) + (defmethod format-clause :group-by [[_ fields] _] (str "GROUP BY " (comma-join (map to-sql fields)))) diff --git a/src/honeysql/helpers.cljc b/src/honeysql/helpers.cljc index b3cdf4e..5b5854c 100644 --- a/src/honeysql/helpers.cljc +++ b/src/honeysql/helpers.cljc @@ -122,6 +122,12 @@ (defhelper merge-full-join [m clauses] (update-in m [:full-join] concat clauses)) +(defhelper cross-join [m clauses] + (assoc m :cross-join clauses)) + +(defhelper merge-cross-join [m clauses] + (update-in m [:cross-join] concat clauses)) + (defmethod build-clause :group-by [_ m fields] (assoc m :group-by (collify fields))) diff --git a/test/honeysql/core_test.cljc b/test/honeysql/core_test.cljc index 0a80c66..4247f1b 100644 --- a/test/honeysql/core_test.cljc +++ b/test/honeysql/core_test.cljc @@ -4,7 +4,8 @@ :cljs [cljs.test :refer-macros]) [deftest testing is]] [honeysql.core :as sql] [honeysql.helpers :refer [select modifiers from join left-join - right-join full-join where group having + right-join full-join cross-join + where group having order-by limit offset values columns insert-into with merge-where]] honeysql.format-test)) @@ -254,4 +255,16 @@ (where nil nil nil nil) sql/format))))) +(deftest cross-join-test + (is (= ["SELECT * FROM foo CROSS JOIN bar"] + (-> (select :*) + (from :foo) + (cross-join :bar) + sql/format))) + (is (= ["SELECT * FROM foo f CROSS JOIN bar b"] + (-> (select :*) + (from [:foo :f]) + (cross-join [:bar :b]) + sql/format)))) + #?(:cljs (cljs.test/run-all-tests)) diff --git a/test/honeysql/format_test.cljc b/test/honeysql/format_test.cljc index 935a15f..d7fa8c7 100644 --- a/test/honeysql/format_test.cljc +++ b/test/honeysql/format_test.cljc @@ -310,3 +310,13 @@ (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]}))) + (is (= ["SELECT * FROM foo f CROSS JOIN bar b"] + (format {:select [:*] + :from [[:foo :f]] + :cross-join [[:bar :b]]}))))