From d187c66987d7e2cbd97e9f2fca851c56472df927 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Fri, 11 Aug 2023 19:06:30 -0700 Subject: [PATCH] add :alias special syntax #497 --- CHANGELOG.md | 1 + src/honey/sql.cljc | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a00de92..f5b1d7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.4.next in progress + * Address [#497](https://github.com/seancorfield/honeysql/issues/497) by adding `:alias` special syntax. Documentation TBD. * Attempt to clarify the formatting behavior of the `:values` clause when used to produce column names. * Update `tools.build` to 0.9.5 (and remove `:java-opts` setting from `build/run-task`) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 1cbfdc1..fce02b2 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -223,7 +223,11 @@ Handles quoting, splitting at / or ., replacing - with _ etc." [e & [{:keys [aliased drop-ns]}]] - (let [col-fn (if (or *quoted* (string? e)) + (let [e (if (and aliased (keyword? e) (= \' (first (name e)))) + ;; #497 quoted alias support (should behave like string) + (subs (name e) 1) + e) + col-fn (if (or *quoted* (string? e)) (if *quoted-snake* name-_ name) name-_) col-e (col-fn e) @@ -1494,6 +1498,8 @@ ;; used in DDL to force rendering as a SQL entity instead ;; of a SQL keyword: :entity (fn [_ [e]] [(format-entity e)]) + ;; #497 used to force rendering as an alias: + :alias (fn [_ [e]] [(format-entity e {:aliased true})]) ;; bigquery column types: :bigquery/array (fn [_ spec] [(str "ARRAY<" @@ -2098,4 +2104,15 @@ (sql/format {:insert-into :foo :output [:inserted.*] :values [{:bar 1}]}) (sql/format {:insert-into :foo :columns [:bar] :output [:inserted.*] :values [[1]]}) + ;; issue-497 + (honey.sql/format {:select [[:column-name "some-alias"]] + :from :b + :order-by [[[:raw "\"some-alias\""]]]}) + (honey.sql/format {:select [[:column-name "some-alias"]] + :from :b + :order-by [[[:alias "some-alias"]]]}) + (honey.sql/format {:select [[:column-name "some-alias"]] + :from :b + :order-by [[[:alias "some-alias"]]]} + {:quoted true}) )