implement get-in #532

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2024-11-23 13:16:23 -08:00
parent 559e71205d
commit 8320571c4d
No known key found for this signature in database
2 changed files with 28 additions and 0 deletions

View file

@ -1933,6 +1933,23 @@
[k [x]]
[(str (sql-kw k) " " (inline-map x "(" ")"))])
(defn- get-in-navigation
"[:get-in expr key-or-index1 key-or-index2 ...]"
[_ [expr & kix]]
(let [[sql & params] (format-expr expr)
[sqls params']
(reduce-sql (map #(cond (number? %)
[(str "[" % "]")]
(ident? %)
[(str "." (format-entity %))]
:else
(let [[sql' & params'] (format-expr %)]
(cons (str "[" sql' "]") params')))
kix))]
(-> [(str "(" sql ")" (join "" sqls))]
(into params)
(into params'))))
(defn ignore-respect-nulls [k [x]]
(let [[sql & params] (format-expr x)]
(into [(str sql " " (sql-kw k))] params)))
@ -2023,6 +2040,7 @@
(into params-p)
(into params-e))))
:filter expr-clause-pairs
:get-in #'get-in-navigation
:ignore-nulls ignore-respect-nulls
:inline
(fn [_ xs]

View file

@ -108,3 +108,13 @@
(testing "inline map literal"
(is (= ["SELECT {_id: 1, name: 'foo'}"]
(sql/format {:select [[[:inline {:_id 1 :name "foo"}]]]})))))
(deftest navigation-dot-index
(is (= ["SELECT (a.b).c[1].d"]
(sql/format '{select (((get-in a.b c 1 d)))})))
(is (= ["SELECT (a.b).c[?].d" 1]
(sql/format '{select (((get-in a.b c (lift 1) d)))})))
(is (= ["SELECT (a.b).c[?].d" 1]
(sql/format '{select (((get-in (. a b) c (lift 1) d)))})))
(is (= ["SELECT (OBJECT (_id: 1, b: 'thing').b).c[?].d" 1]
(sql/format '{select (((get-in (. (object {_id 1 b "thing"}) b) c (lift 1) d)))}))))