diff --git a/src/honeysql/helpers.cljc b/src/honeysql/helpers.cljc index f2d944d..2f6eba6 100644 --- a/src/honeysql/helpers.cljc +++ b/src/honeysql/helpers.cljc @@ -41,6 +41,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 4f10e0c..3328ccc 100644 --- a/test/honeysql/core_test.cljc +++ b/test/honeysql/core_test.cljc @@ -78,6 +78,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)]})))