From f05c7051e230150d9c93488f269700be37289fda Mon Sep 17 00:00:00 2001 From: Nikita Domnitskii Date: Thu, 16 Jan 2025 18:25:52 +0600 Subject: [PATCH 1/2] Support expressions in WITH clauses --- src/honey/sql.cljc | 2 +- test/honey/sql_test.cljc | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 7030653..d7e5621 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -943,7 +943,7 @@ (map (fn [[x expr & tail :as with]] (let [[sql & params] (format-with-part x) - non-query-expr? (or (ident? expr) (string? expr)) + non-query-expr? (not (map? expr)) [sql' & params'] (if non-query-expr? (format-expr expr) (format-dsl expr)) diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index eca34ab..ba6bce4 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -221,7 +221,26 @@ :from [:hits :stuff] :where [:= :EventDate :ts_upper_bound]}) ["WITH ? AS ts_upper_bound, stuff AS (SELECT * FROM songs) SELECT * FROM hits, stuff WHERE EventDate = ts_upper_bound" - "2019-08-01 15:23:00"])))) + "2019-08-01 15:23:00"]))) + (testing "Use expression in a WITH clause" + (is (= (format + {:with [[:s [:sum :bytes]]] + :select [:s] + :from [:table]}) + ["WITH SUM(bytes) AS s SELECT s FROM table"])) + + (is (= (format + {:with [[:v [:raw "m['k']"]]] + :select [:v] + :from [:table]}) + ["WITH m['k'] AS v SELECT v FROM table"])) + + (is (= (format + {:with [[:cond [:and [:= :a 1] [:= :b 2] [:= :c 3]]]] + :select [:v] + :from [:table] + :where :cond}) + ["WITH (a = ?) AND (b = ?) AND (c = ?) AS cond SELECT v FROM table WHERE cond" 1 2 3])))) (deftest insert-into (is (= (format {:insert-into :foo}) From 6c0c66e371526f46ec79fc9f16973cb51d8e4b70 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Thu, 16 Jan 2025 12:45:24 -0800 Subject: [PATCH 2/2] note pr #563 in change log Signed-off-by: Sean Corfield --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac0257a..33fe9b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.6.next in progress + * Support expressions in `WITH` clauses via PR [#563](https://github.com/seancorfield/honeysql/pull/563) [@krevedkokun](https://github.com/krevedkokun). * More performance optimizations via PR [#560](https://github.com/seancorfield/honeysql/pull/560) [@alexander-yakushev](https://github.com/alexander-yakushev). * Fix two broken links to the [HoneySQL web app](https://john.shaffe.rs/honeysql/) via PR [#559](https://github.com/seancorfield/honeysql/pull/559) [@whatacold](https://github.com/whatacold). * Make SQL Server dialect auto-lift Boolean values to parameters since SQL Server has no `TRUE` / `FALSE` literals.