diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index a258c0e..cf76263 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -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) diff --git a/test/honey/sql/xtdb_test.cljc b/test/honey/sql/xtdb_test.cljc index db02f1b..458c018 100644 --- a/test/honey/sql/xtdb_test.cljc +++ b/test/honey/sql/xtdb_test.cljc @@ -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"}]]]})))))