From f6c79a76fb6464ad05ea1dc4c5785181a256db6d Mon Sep 17 00:00:00 2001 From: quephird Date: Tue, 21 Feb 2017 10:54:27 -0500 Subject: [PATCH] Added new helper for with/CTE clause and supporting test. --- src/honeysql/helpers.cljc | 3 +++ test/honeysql/core_test.cljc | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/honeysql/helpers.cljc b/src/honeysql/helpers.cljc index 1d3b517..a0348dc 100644 --- a/src/honeysql/helpers.cljc +++ b/src/honeysql/helpers.cljc @@ -36,6 +36,9 @@ (defn collify [x] (if (coll? x) x [x])) +(defhelper with [m ctes] + (assoc m :with (collify ctes))) + (defhelper select [m fields] (assoc m :select (collify fields))) diff --git a/test/honeysql/core_test.cljc b/test/honeysql/core_test.cljc index 8a87a45..0f473a2 100644 --- a/test/honeysql/core_test.cljc +++ b/test/honeysql/core_test.cljc @@ -6,7 +6,7 @@ [honeysql.helpers :refer [select modifiers from join left-join right-join full-join where group having order-by limit offset values columns - insert-into]] + insert-into with]] honeysql.format-test)) ;; TODO: more tests @@ -72,6 +72,21 @@ (sql/format (assoc m1 :lock {:mode :update}) {:param1 "gabba" :param2 2})))))) +(deftest test-with + (let [expected-sql (clojure.string/join " " + ["WITH f AS (SELECT foo.* FROM foo)," + "b AS (SELECT bar.* FROM bar)" + "SELECT f.baz, b.quux" + "FROM f" + "INNER JOIN b ON f.id = b.id"])] + (is (= [expected-sql] + (-> (with [:f (-> (select :foo.* )(from :foo))] + [:b (-> (select :bar.* )(from :bar))]) + (select :f.baz :b.quux) + (from :f) + (join :b [:= :f.id :b.id]) + sql/format))))) + (deftest test-cast (is (= ["SELECT foo, CAST(bar AS integer)"] (sql/format {:select [:foo (sql/call :cast :bar :integer)]})))