part of #570 -- colon path selection

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2025-03-12 13:36:49 -07:00
parent 675c94b294
commit d70e89ae3b
No known key found for this signature in database
4 changed files with 69 additions and 10 deletions

View file

@ -1,5 +1,8 @@
# Changes
* 2.7.next in progress
* Address #570 by adding `:.:.` as special syntax for Snowflake's JSON path syntax.
* 2.6.1281 -- 2025-03-06
* Address [#568](https://github.com/seancorfield/honeysql/issues/568) by adding `honey.sql/semicolon` to merge multiple SQL+params vectors into one (with semicolons separating the SQL statements).
* Address [#567](https://github.com/seancorfield/honeysql/issues/567) by adding support for `ASSERT` clause.

View file

@ -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

View file

@ -1953,6 +1953,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
@ -1973,12 +1979,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)])

View file

@ -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)
)