Skip to content

Commit

Permalink
add record/object special syntax #532
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <[email protected]>
  • Loading branch information
seancorfield committed Nov 23, 2024
1 parent 1bac435 commit 559e712
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions test/honey/sql/xtdb_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]]]})))))

0 comments on commit 559e712

Please sign in to comment.