add record/object special syntax #532

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2024-11-23 12:17:43 -08:00
parent 1bac4352e3
commit 559e71205d
No known key found for this signature in database
2 changed files with 24 additions and 3 deletions

View file

@ -369,14 +369,14 @@
(keyword (name s)))
s))
(defn- inline-map [x]
(str "{"
(defn- inline-map [x & [open close]]
(str (or open "{")
(join ", " (map (fn [[k v]]
(str (format-entity k)
": "
(p/sqlize v))))
x)
"}"))
(or close "}")))
(extend-protocol p/InlineValue
nil
@ -1929,6 +1929,10 @@
(into params-a)
(into params-b))))
(defn- object-record-literal
[k [x]]
[(str (sql-kw k) " " (inline-map x "(" ")"))])
(defn ignore-respect-nulls [k [x]]
(let [[sql & params] (format-expr x)]
(into [(str sql " " (sql-kw k))] params)))
@ -2057,6 +2061,8 @@
(fn [_ [x]]
(let [[sql & params] (format-expr x {:nested true})]
(into [(str "NOT " sql)] params)))
:object #'object-record-literal
:record #'object-record-literal
:order-by
(fn [k [e & qs]]
(let [[sql-e & params-e] (format-expr e)

View file

@ -93,3 +93,18 @@
(sql/format {:insert-into [:foo
{:records [{:_id 1 :name "cat"}
{:_id 2 :name "dog"}]}]})))))
(deftest object-record-expr
(testing "object literal"
(is (= ["SELECT OBJECT (_id: 1, name: 'foo')"]
(sql/format {:select [[[:object {:_id 1 :name "foo"}]]]})))
(is (= ["SELECT OBJECT (_id: 1, name: 'foo')"]
(sql/format '{select (((:object {:_id 1 :name "foo"})))}))))
(testing "record literal"
(is (= ["SELECT RECORD (_id: 1, name: 'foo')"]
(sql/format {:select [[[:record {:_id 1 :name "foo"}]]]})))
(is (= ["SELECT RECORD (_id: 1, name: 'foo')"]
(sql/format '{select (((:record {:_id 1 :name "foo"})))}))))
(testing "inline map literal"
(is (= ["SELECT {_id: 1, name: 'foo'}"]
(sql/format {:select [[[:inline {:_id 1 :name "foo"}]]]})))))