Added new helper for with/CTE clause and supporting test.

This commit is contained in:
quephird 2017-02-21 10:54:27 -05:00
parent 4b79536a75
commit f6c79a76fb
2 changed files with 19 additions and 1 deletions

View file

@ -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)))

View file

@ -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)]})))