From 8320571c4d1860265191404ed9a1c5d0d49ed03e Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Sat, 23 Nov 2024 13:16:23 -0800 Subject: [PATCH] implement get-in #532 Signed-off-by: Sean Corfield --- src/honey/sql.cljc | 18 ++++++++++++++++++ test/honey/sql/xtdb_test.cljc | 10 ++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index cf76263..0102a35 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -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] diff --git a/test/honey/sql/xtdb_test.cljc b/test/honey/sql/xtdb_test.cljc index 458c018..541431f 100644 --- a/test/honey/sql/xtdb_test.cljc +++ b/test/honey/sql/xtdb_test.cljc @@ -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)))}))))