Support column names in :with clauses

This binds `honeysql.format/*subquery?*` to `false` which turns
on processing of a cte-name like `[:foo {:columns [:a :b :c]}]`
resulting in a string like `WITH foo (a, b, c)`.
This commit is contained in:
Brandon Adams 2015-10-12 14:31:55 -05:00
parent ee2b778d52
commit 10a1565467
2 changed files with 15 additions and 3 deletions

View file

@ -510,7 +510,10 @@
(defn cte->sql
[[cte-name query]]
(str (to-sql cte-name) " AS " (to-sql query)))
(str (binding [*subquery?* false]
(to-sql cte-name))
" AS "
(to-sql query)))
(defmethod format-clause :with [[_ ctes] _]
(str "WITH " (comma-join (map cte->sql ctes))))

View file

@ -24,7 +24,16 @@
"WITH query AS SELECT foo FROM bar"))
(is (= (format-clause
(first {:with-recursive [[:query {:select [:foo] :from [:bar]}]]}) nil)
"WITH RECURSIVE query AS SELECT foo FROM bar")))
"WITH RECURSIVE query AS SELECT foo FROM bar"))
(is (= (format-clause
(first {:with [[[:static {:columns [:a :b :c]}] {:values [[1 2 3] [4 5 6]]}]]}) nil)
"WITH static (a, b, c) AS VALUES (1, 2, 3), (4, 5, 6)"))
(is (= (format
{:with [[[:static {:columns [:a :b :c]}]
{:values [[1 2 3] [4 5 6]]}]]
:select [:*]
:from [:static]})
["WITH static (a, b, c) AS (VALUES (1, 2, 3), (4, 5, 6)) SELECT * FROM static"])))
(deftest insert-into
(is (= (format-clause (first {:insert-into :foo}) nil)