Skip to content

Commit

Permalink
Merge pull request #563 from krevedkokun/expressions-in-with
Browse files Browse the repository at this point in the history
Support expressions in WITH clauses
  • Loading branch information
seancorfield authored Jan 16, 2025
2 parents 8e0d698 + f05c705 commit 2c793ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
21 changes: 20 additions & 1 deletion test/honey/sql_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit 2c793ce

Please sign in to comment.