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:
parent
ee2b778d52
commit
10a1565467
2 changed files with 15 additions and 3 deletions
|
|
@ -507,10 +507,13 @@
|
||||||
|
|
||||||
(defmethod format-clause :delete-from [[_ table] _]
|
(defmethod format-clause :delete-from [[_ table] _]
|
||||||
(str "DELETE FROM " (to-sql table)))
|
(str "DELETE FROM " (to-sql table)))
|
||||||
|
|
||||||
(defn cte->sql
|
(defn cte->sql
|
||||||
[[cte-name query]]
|
[[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] _]
|
(defmethod format-clause :with [[_ ctes] _]
|
||||||
(str "WITH " (comma-join (map cte->sql ctes))))
|
(str "WITH " (comma-join (map cte->sql ctes))))
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,16 @@
|
||||||
"WITH query AS SELECT foo FROM bar"))
|
"WITH query AS SELECT foo FROM bar"))
|
||||||
(is (= (format-clause
|
(is (= (format-clause
|
||||||
(first {:with-recursive [[:query {:select [:foo] :from [:bar]}]]}) nil)
|
(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
|
(deftest insert-into
|
||||||
(is (= (format-clause (first {:insert-into :foo}) nil)
|
(is (= (format-clause (first {:insert-into :foo}) nil)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue