From eebf9fff0c1a9f5d8e048576fcc6e924f9ff2c32 Mon Sep 17 00:00:00 2001 From: Brandon Adams Date: Mon, 12 Oct 2015 14:31:55 -0500 Subject: [PATCH] Support column names in :with clauses --- src/honeysql/format.clj | 6 ++++-- test/honeysql/format_test.clj | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index c879fa7..86f80f6 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -507,10 +507,12 @@ (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))) + (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] _] (str "WITH " (comma-join (map cte->sql ctes)))) diff --git a/test/honeysql/format_test.clj b/test/honeysql/format_test.clj index 26f90bc..bc2c776 100644 --- a/test/honeysql/format_test.clj +++ b/test/honeysql/format_test.clj @@ -24,7 +24,10 @@ "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)"))) (deftest insert-into (is (= (format-clause (first {:insert-into :foo}) nil)