part of #570 -- colon path selection
Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
parent
89f39be55c
commit
67ea477a5c
4 changed files with 67 additions and 10 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
* 2.7.next in progress
|
* 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).
|
* Drop support for Clojure 1.9 [#561](https://github.com/seancorfield/honeysql/issues/561).
|
||||||
|
|
||||||
* 2.6.1281 -- 2025-03-06
|
* 2.6.1281 -- 2025-03-06
|
||||||
|
|
|
||||||
|
|
@ -211,15 +211,23 @@ Accepts a single expression and prefixes it with `DISTINCT `:
|
||||||
;;=> ["SELECT COUNT(DISTINCT status) AS n FROM table"]
|
;;=> ["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
|
```clojure
|
||||||
(sql/format {:select [ [[:. :t :c]] [[:. :s :t :c]] ]})
|
(sql/format {:select [ [[:. :t :c]] [[:. :s :t :c]] ]})
|
||||||
;;=> ["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:
|
Can be used with `:nest` for field selection from composites:
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
|
|
|
||||||
|
|
@ -1946,6 +1946,12 @@
|
||||||
(let [[sql & params] (format-expr x)]
|
(let [[sql & params] (format-expr x)]
|
||||||
(into [(str sql " " (sql-kw k))] params)))
|
(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
|
(def ^:private special-syntax
|
||||||
(atom
|
(atom
|
||||||
{;; these "functions" are mostly used in column
|
{;; these "functions" are mostly used in column
|
||||||
|
|
@ -1966,12 +1972,9 @@
|
||||||
:references #'function-1
|
:references #'function-1
|
||||||
:unique #'function-1-opt
|
:unique #'function-1-opt
|
||||||
;; dynamic dotted name creation:
|
;; dynamic dotted name creation:
|
||||||
:. (fn [_ [expr col subcol]]
|
:. (fn [_ data] (dot-navigation "." data))
|
||||||
(let [[sql & params] (format-expr expr)]
|
;; snowflake variant #570:
|
||||||
(into [(str sql "." (format-entity col)
|
:.:. (fn [_ data] (dot-navigation ":" data))
|
||||||
(when subcol
|
|
||||||
(str "." (format-entity subcol))))]
|
|
||||||
params)))
|
|
||||||
;; used in DDL to force rendering as a SQL entity instead
|
;; used in DDL to force rendering as a SQL entity instead
|
||||||
;; of a SQL keyword:
|
;; of a SQL keyword:
|
||||||
:entity (fn [_ [e]] [(format-entity e)])
|
:entity (fn [_ [e]] [(format-entity e)])
|
||||||
|
|
|
||||||
|
|
@ -1178,9 +1178,10 @@ ORDER BY id = ? DESC
|
||||||
|
|
||||||
(deftest issue-474-dot-selection
|
(deftest issue-474-dot-selection
|
||||||
(testing "basic 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]
|
(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]"]
|
(is (= ["SELECT [a].[b], [c].[d], [a].[d].[x]"]
|
||||||
(let [t :a c :d]
|
(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]]]}
|
||||||
|
|
@ -1194,6 +1195,45 @@ ORDER BY id = ? DESC
|
||||||
(sut/format '{select (((. (nest v) *))
|
(sut/format '{select (((. (nest v) *))
|
||||||
((. (nest w) x))
|
((. (nest w) x))
|
||||||
((. (nest (y z)) *)))}
|
((. (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})))))
|
{:dialect :mysql})))))
|
||||||
|
|
||||||
(deftest issue-476-raw
|
(deftest issue-476-raw
|
||||||
|
|
@ -1451,4 +1491,9 @@ ORDER BY id = ? DESC
|
||||||
:select [:*]
|
:select [:*]
|
||||||
:from [:a-b.b-c.c-d]}
|
:from [:a-b.b-c.c-d]}
|
||||||
(sut/format {:dialect :nrql}))
|
(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)
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue