diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 72f1ef4..0dd9e73 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -193,10 +193,8 @@ (map (fn [[x expr]] (let [[sql & params] (format-with-part x) [sql' & params'] (format-dsl expr)] - (cond-> [(str sql " AS " - (if (seq params') - (str "(" sql' ")") - sql'))] + ;; according to docs, CTE should _always_ be wrapped: + (cond-> [(str sql " AS " (str "(" sql' ")"))] params (into params) params' (into params')))) xs))] diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index 793e033..1dd95ad 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -84,9 +84,9 @@ :from [[{:values [[1 2 3]]} [:vals {:columns [:a :b :c]}]]]})))) (deftest test-cte (is (= (format {:with [[:query {:select [:foo] :from [:bar]}]]}) - ["WITH query AS SELECT foo FROM bar"])) + ["WITH query AS (SELECT foo FROM bar)"])) (is (= (format {:with-recursive [[:query {:select [:foo] :from [:bar]}]]}) - ["WITH RECURSIVE query AS SELECT foo FROM bar"])) + ["WITH RECURSIVE query AS (SELECT foo FROM bar)"])) (is (= (format {:with [[[:static {:columns [:a :b :c]}] {:values [[1 2 3] [4 5 6]]}]]}) ["WITH static (a, b, c) AS (VALUES (?, ?, ?), (?, ?, ?))" 1 2 3 4 5 6])) (is (= (format