allow for inline hash maps (record syntax)

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2024-11-22 23:46:56 -08:00
parent 09fa8afefe
commit b716d077c4
No known key found for this signature in database
2 changed files with 17 additions and 3 deletions

View file

@ -379,6 +379,14 @@
(sqlize [x] (sql-kw x))
#?(:cljs PersistentVector :default clojure.lang.IPersistentVector)
(sqlize [x] (str "[" (join ", " (map p/sqlize) x) "]"))
#?(:cljs PersistentHashMap :default clojure.lang.IPersistentMap)
(sqlize [x] (str "{"
(join ", " (map (fn [[k v]]
(str (format-entity k)
": "
(p/sqlize v))))
x)
"}"))
#?@(:clj [java.util.UUID
;; issue 385: quoted UUIDs for PostgreSQL/ANSI
(sqlize [x] (str \' x \'))])
@ -1991,7 +1999,7 @@
:inline
(fn [_ xs]
(binding [*inline* true]
[(join " " (mapcat format-expr) xs)]))
[(join " " (mapcat #(format-expr % {:record true})) xs)]))
:interval format-interval
:join
(fn [_ [e & js]]
@ -2130,11 +2138,11 @@
This is intended to be used when writing your own formatters to
extend the DSL supported by HoneySQL."
([expr] (format-expr expr {}))
([expr {:keys [nested] :as opts}]
([expr {:keys [nested record] :as opts}]
(cond (ident? expr)
(format-var expr opts)
(map? expr)
(and (map? expr) (not record))
(format-dsl expr (assoc opts :nested true))
(sequential? expr)

View file

@ -68,3 +68,9 @@
(-> {:erase-from :foo
:where [:= :foo.id 42]}
(sql/format)))))
(deftest inline-record-body
(is (= ["{_id: 1, name: 'foo', info: {contact: [{loc: 'home', tel: '123'}, {loc: 'work', tel: '456'}]}}"]
(sql/format [:inline {:_id 1 :name "foo"
:info {:contact [{:loc "home" :tel "123"}
{:loc "work" :tel "456"}]}}]))))