From 67ea477a5cf28f89490508483b82bfdf6be61b0b Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Wed, 12 Mar 2025 13:36:49 -0700 Subject: [PATCH] part of #570 -- colon path selection Signed-off-by: Sean Corfield --- CHANGELOG.md | 1 + doc/special-syntax.md | 12 ++++++++-- src/honey/sql.cljc | 15 +++++++----- test/honey/sql_test.cljc | 49 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04b27e7..23aaaeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.7.next in progress + * Address #570 by adding `:.:.` as special syntax for Snowflake's JSON path syntax. * Drop support for Clojure 1.9 [#561](https://github.com/seancorfield/honeysql/issues/561). * 2.6.1281 -- 2025-03-06 diff --git a/doc/special-syntax.md b/doc/special-syntax.md index 97d872c..5f109e0 100644 --- a/doc/special-syntax.md +++ b/doc/special-syntax.md @@ -211,15 +211,23 @@ Accepts a single expression and prefixes it with `DISTINCT `: ;;=> ["SELECT COUNT(DISTINCT status) AS n FROM table"] ``` -## dot . +## dot . .:. -Accepts an expression and a field (or column) selection: +Accepts an expression and one or more fields (or columns). Plain dot produces +plain dotted selection: ```clojure (sql/format {:select [ [[:. :t :c]] [[:. :s :t :c]] ]}) ;;=> ["SELECT t.c, s.t.c"] ``` +Dot colon dot produces Snowflake-style dotted selection: + +```clojure +(sql/format {:select [ [[:.:. :t :c]] [[:.:. :s :t :c]] ]}) +;;=> ["SELECT t:c, s:t.c"] +``` + Can be used with `:nest` for field selection from composites: ```clojure diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 44fe883..9cb1fa6 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -1946,6 +1946,12 @@ (let [[sql & params] (format-expr x)] (into [(str sql " " (sql-kw k))] params))) +(defn dot-navigation [sep [expr col & subcols]] + (let [[sql & params] (format-expr expr)] + (into [(str sql sep (format-entity col) + (when (seq subcols) + (str "." (join "." (map format-entity subcols)))))] + params))) (def ^:private special-syntax (atom {;; these "functions" are mostly used in column @@ -1966,12 +1972,9 @@ :references #'function-1 :unique #'function-1-opt ;; dynamic dotted name creation: - :. (fn [_ [expr col subcol]] - (let [[sql & params] (format-expr expr)] - (into [(str sql "." (format-entity col) - (when subcol - (str "." (format-entity subcol))))] - params))) + :. (fn [_ data] (dot-navigation "." data)) + ;; snowflake variant #570: + :.:. (fn [_ data] (dot-navigation ":" data)) ;; used in DDL to force rendering as a SQL entity instead ;; of a SQL keyword: :entity (fn [_ [e]] [(format-entity e)]) diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index ba6bce4..f5c4301 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -1178,9 +1178,10 @@ ORDER BY id = ? DESC (deftest issue-474-dot-selection (testing "basic dot selection" - (is (= ["SELECT a.b, c.d, a.d.x"] + (is (= ["SELECT a.b, c.d, a.d.x, a.d.x.y"] (let [t :a c :d] - (sut/format {:select [[[:. t :b]] [[:. :c c]] [[:. t c :x]]]})))) + (sut/format {:select [[[:. t :b]] [[:. :c c]] + [[:. t c :x]] [[:. t c :x :y]]]})))) (is (= ["SELECT [a].[b], [c].[d], [a].[d].[x]"] (let [t :a c :d] (sut/format {:select [[[:. t :b]] [[:. :c c]] [[:. t c :x]]]} @@ -1194,6 +1195,45 @@ ORDER BY id = ? DESC (sut/format '{select (((. (nest v) *)) ((. (nest w) x)) ((. (nest (y z)) *)))} + {:dialect :mysql}))) + (is (= ["SELECT (v).*, (w).x, (Y(z)).*"] + (sut/format '{select (((get-in v *)) + ((get-in w x)) + ((get-in (y z) *)))}))) + (is (= ["SELECT (`v`).*, (`w`).`x`, (Y(`z`)).*"] + (sut/format '{select (((get-in v *)) + ((get-in w x)) + ((get-in (y z) *)))} + {:dialect :mysql}))))) + +(deftest issue-570-snowflake-dot-selection + (testing "basic colon selection" + (is (= ["SELECT a:b, c:d, a:d.x, a:d.x.y"] + (let [t :a c :d] + (sut/format {:select [[[:.:. t :b]] [[:.:. :c c]] + [[:.:. t c :x]] [[:.:. t c :x :y]]]})))) + (is (= ["SELECT [a]:[b], [c]:[d], [a]:[d].[x]"] + (let [t :a c :d] + (sut/format {:select [[[:.:. t :b]] [[:.:. :c c]] [[:.:. t c :x]]]} + {:dialect :sqlserver}))))) + (testing "basic field selection from composite" + (is (= ["SELECT (v):*, (w):x, (Y(z)):*"] + (sut/format '{select (((.:. (nest v) *)) + ((.:. (nest w) x)) + ((.:. (nest (y z)) *)))}))) + (is (= ["SELECT (`v`):*, (`w`):`x`, (Y(`z`)):*"] + (sut/format '{select (((.:. (nest v) *)) + ((.:. (nest w) x)) + ((.:. (nest (y z)) *)))} + {:dialect :mysql}))) + (is (= ["SELECT (v):*, (w):x, (Y(z)):*"] + (sut/format '{select (((get-in v *)) + ((get-in w x)) + ((get-in (y z) *)))}))) + (is (= ["SELECT (`v`):*, (`w`):`x`, (Y(`z`)):*"] + (sut/format '{select (((get-in v *)) + ((get-in w x)) + ((get-in (y z) *)))} {:dialect :mysql}))))) (deftest issue-476-raw @@ -1451,4 +1491,9 @@ ORDER BY id = ? DESC :select [:*] :from [:a-b.b-c.c-d]} (sut/format {:dialect :nrql})) + (sut/format {:select :a:b.c}) ; quotes a:b + (sut/format [:. :a :b :c]) ; a.b.c + (sut/format [:. :a :b :c :d]) ; drops d ; a.b.c + (sut/format [:.:. :a :b :c]) ; .(a, b, c) + (sut/format '(.:. a b c)) ; .(a, b, c) )