From a6c1f98b71741afd2c4f3e59f4a961c1e0d513a6 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Fri, 25 Sep 2020 16:38:38 -0700 Subject: [PATCH] Fix CTE syntax Should always be wrapped in parens --- src/honey/sql.cljc | 6 ++---- test/honey/sql_test.cljc | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) 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