From f4d212ae18b477902f225fffeff371462faae1b4 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Fri, 22 Nov 2024 22:54:42 -0800 Subject: [PATCH] address #532 by supporting exclude/rename in alias position Signed-off-by: Sean Corfield --- src/honey/sql.cljc | 20 ++++++++++++++------ test/honey/sql/xtdb_test.cljc | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 53b10cb..824ef2a 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -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))] @@ -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 ") " ") diff --git a/test/honey/sql/xtdb_test.cljc b/test/honey/sql/xtdb_test.cljc index 0524a1d..aed822c 100644 --- a/test/honey/sql/xtdb_test.cljc +++ b/test/honey/sql/xtdb_test.cljc @@ -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"]