Support column names in :with clauses

This commit is contained in:
Brandon Adams 2015-10-12 14:31:55 -05:00
parent ee2b778d52
commit eebf9fff0c
2 changed files with 8 additions and 3 deletions

View file

@ -507,10 +507,12 @@
(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))) (if (sequential? cte-name)
(str (to-sql (first cte-name)) " " (to-sql (second cte-name)) " AS " (to-sql query))
(str (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))))

View file

@ -24,7 +24,10 @@
"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)")))
(deftest insert-into (deftest insert-into
(is (= (format-clause (first {:insert-into :foo}) nil) (is (= (format-clause (first {:insert-into :foo}) nil)