Merge pull request #95 from emidln/emidln/with_column_names

Support column names in :with clauses
This commit is contained in:
Michael Blume 2015-10-18 11:07:03 -07:00
commit 607ee17cca
2 changed files with 15 additions and 3 deletions

View file

@ -511,10 +511,13 @@
(defmethod format-clause :delete-from [[_ table] _]
(str "DELETE FROM " (to-sql table)))
(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

@ -31,7 +31,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)