From 2149a808521b429e8a927e559dda9ce64926fb06 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Sat, 3 Feb 2024 12:37:43 -0800 Subject: [PATCH] fix #523 Signed-off-by: Sean Corfield --- CHANGELOG.md | 1 + README.md | 19 +++++++++++++++++-- src/honey/sql.cljc | 2 +- src/honey/sql/helpers.cljc | 2 ++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 011f879..7fc42d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.5.next in progress + * Address [#523](https://github.com/seancorfield/honeysql/issues/523) by expanding examples in README **Functions** to show aliases. * Address [#522](https://github.com/seancorfield/honeysql/issues/522) by supporting metadata on table specifications in `:from` and `:join` clauses to provide index hints (SQL Server). * Address [#520](https://github.com/seancorfield/honeysql/issues/520) by expanding how `:inline` works, to support a sequence of arguments. * Fix [#518](https://github.com/seancorfield/honeysql/issues/518) by moving temporal clause before alias. diff --git a/README.md b/README.md index 4b8a1d0..784e16f 100644 --- a/README.md +++ b/README.md @@ -565,6 +565,11 @@ keywords that begin with `%` are interpreted as SQL function calls: => ["SELECT COUNT(*) FROM foo"] ``` ```clojure +;; with an alias: +(-> (select [:%count.* :total]) (from :foo) sql/format) +=> ["SELECT COUNT(*) AS total FROM foo"] +``` +```clojure (-> (select :%max.id) (from :foo) sql/format) => ["SELECT MAX(id) FROM foo"] ``` @@ -578,6 +583,10 @@ regular function calls in a select: => ["SELECT COUNT(*) FROM foo"] ``` ```clojure +(-> (select [[:count :*] :total]) (from :foo) sql/format) +=> ["SELECT COUNT(*) AS total FROM foo"] +``` +```clojure (-> (select [:%count.*]) (from :foo) sql/format) => ["SELECT COUNT(*) FROM foo"] ;; or even: @@ -587,16 +596,22 @@ regular function calls in a select: ```clojure (-> (select [[:max :id]]) (from :foo) sql/format) => ["SELECT MAX(id) FROM foo"] +(-> (select [[:max :id] :highest]) (from :foo) sql/format) +=> ["SELECT MAX(id) AS highest FROM foo"] ;; the pure data DSL requires an extra level of brackets: (-> {:select [[[:max :id]]], :from [:foo]} sql/format) => ["SELECT MAX(id) FROM foo"] +(-> {:select [[[:max :id] :highest]], :from [:foo]} sql/format) +=> ["SELECT MAX(id) AS highest FROM foo"] ;; the shorthand makes this simpler: (-> {:select [[:%max.id]], :from [:foo]} sql/format) => ["SELECT MAX(id) FROM foo"] -;; or even: +(-> {:select [[:%max.id :highest]], :from [:foo]} sql/format) +=> ["SELECT MAX(id) AS highest FROM foo"] +;; or even (no alias): (-> {:select [:%max.id], :from [:foo]} sql/format) => ["SELECT MAX(id) FROM foo"] -;; or even: +;; or even (no alias, no other columns): (-> {:select :%max.id, :from :foo} sql/format) => ["SELECT MAX(id) FROM foo"] ``` diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 1bad0b5..0f36869 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -195,7 +195,7 @@ (defn upper-case "Upper-case a string in Locale/US to avoid locale-specific capitalization." [^String s] - (.. s toString (toUpperCase (java.util.Locale/US)))) + (.. s toString (toUpperCase java.util.Locale/US))) :cljr (defn upper-case "Upper-case a string in Locale/US to avoid locale-specific capitalization." diff --git a/src/honey/sql/helpers.cljc b/src/honey/sql/helpers.cljc index bc2e2a0..6c597a8 100644 --- a/src/honey/sql/helpers.cljc +++ b/src/honey/sql/helpers.cljc @@ -1185,4 +1185,6 @@ (where [:in (composite :first :second) [(composite 1 2) (composite 2 1)]]) (h/format)) + (-> (select [:%count.* :total]) (from :foo) h/format) + (-> (select [[:count :*] :total]) (from :foo) h/format) )