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

View file

@ -68,3 +68,9 @@
(-> {:erase-from :foo (-> {:erase-from :foo
:where [:= :foo.id 42]} :where [:= :foo.id 42]}
(sql/format))))) (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"}]}}]))))