Skip to content

Commit

Permalink
address #532 by supporting exclude/rename in alias position
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <[email protected]>
  • Loading branch information
seancorfield committed Nov 23, 2024
1 parent e2f7991 commit f4d212a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,18 @@
[sql & params] (if (map? selectable)
(format-dsl selectable {:nested true})
(format-expr selectable))
*-qualifier (and (map? alias)
(some #(contains? alias %)
[:exclude :rename
'exclude 'rename]))
[sql' & params'] (when alias
(if (sequential? alias)
(let [[sqls params] (format-expr-list alias {:aliased true})]
(into [(join " " sqls)] params))
(format-selectable-dsl alias {:aliased true})))
(cond (sequential? alias)
(let [[sqls params] (format-expr-list alias {:aliased true})]
(into [(join " " sqls)] params))
*-qualifier
(format-dsl alias)
:else
(format-selectable-dsl alias {:aliased true})))
[sql'' & params''] (when temporal
(format-temporal temporal))]

Expand All @@ -646,8 +653,9 @@
(str " " sql''))
(when sql' ; alias
(str (if as
(if (and (contains? *dialect* :as)
(not (:as *dialect*)))
(if (or *-qualifier
(and (contains? *dialect* :as)
(not (:as *dialect*))))
" "
" AS ")
" ")
Expand Down
14 changes: 14 additions & 0 deletions test/honey/sql/xtdb_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
(sql/format (-> (select :*) (exclude :_id)
(rename [:value :foo_value]
[:a :b])
(from :foo)))))
(is (= ["SELECT * EXCLUDE _id RENAME value AS foo_value, c.x FROM foo"]
(sql/format (-> (select [:* (-> (exclude :_id) (rename [:value :foo_value]))]
:c.x)
(from :foo)))))
(is (= ["SELECT * EXCLUDE (_id, a) RENAME value AS foo_value, c.x FROM foo"]
(sql/format (-> (select [:* (-> (exclude :_id :a) (rename [:value :foo_value]))]
:c.x)
(from :foo)))))
(is (= ["SELECT * EXCLUDE _id RENAME (value AS foo_value, a AS b), c.x FROM foo"]
(sql/format (-> (select [:* (-> (exclude :_id)
(rename [:value :foo_value]
[:a :b]))]
:c.x)
(from :foo))))))
(testing "select, nest_one, nest_many"
(is (= ["SELECT a._id, NEST_ONE (SELECT * FROM foo AS b WHERE b_id = a._id) FROM bar AS a"]
Expand Down

0 comments on commit f4d212a

Please sign in to comment.