address #398 (needs docs)

This commit is contained in:
Sean Corfield 2022-03-25 21:48:00 -07:00
parent ccaf29292a
commit af69f12630
4 changed files with 84 additions and 2 deletions

View file

@ -1,6 +1,7 @@
# Changes
* 2.2.next in progress
* Address [#398](https://github.com/seancorfield/honeysql/issues/398) by adding `honey.sql.pg-json` namespace that registers PostgreSQL JSON operators and provides symbolic names for "unwritable" operators (that contain `@`).
* Fix [#387](https://github.com/seancorfield/honeysql/issues/387) again.
* Update CI to reflect Clojure 1.11 release (master -> 1.11; new master is 1.12).
* Update `build-clj` to v0.8.0.

View file

@ -227,9 +227,11 @@
becomes a `KEBAB CASE` (uppercase) string with hyphens replaced
by spaces, e.g., `:insert-into` => `INSERT INTO`.
Any namespace qualifier is ignored."
Any namespace qualifier is ignored.
Any ? is escaped to ??."
[k]
(let [n (name k)]
(let [n (str/replace (name k) "?" "??")]
(if (= \' (first n))
(format-entity (keyword (subs n 1 (count n))))
(-> n (dehyphen) (upper-case)))))

View file

@ -0,0 +1,40 @@
;; copyright (c) 2022 sean corfield, all rights reserved
(ns honey.sql.pg-json
"Register all the PostgreSQL JSON/JSONB operators
and provide convenient Clojure names for those ops
that cannot be written as keywords or symbols directly."
(:require [honey.sql :as sql]))
;; see https://www.postgresql.org/docs/current/functions-json.html
;; :->
;; :->>
;; :#>
;; :#>>
(def at> "The @> operator." (keyword "@>"))
(def <at "The <@ operator." (keyword "<@"))
;; :?
;; :?|
;; :?&
;; :||
;; :-
;; :#-
(def at? "The @? operator." (keyword "@?"))
(def atat "The @@ operator." (keyword "@@"))
(sql/register-op! :->)
(sql/register-op! :->>)
(sql/register-op! :#>)
(sql/register-op! :#>>)
(sql/register-op! at>)
(sql/register-op! <at)
(sql/register-op! :?)
(sql/register-op! :?|)
(sql/register-op! :?&)
;; these are already known operators:
;(sql/register-op! :||)
;(sql/register-op! :-)
(sql/register-op! :#-)
(sql/register-op! at?)
(sql/register-op! atat)

View file

@ -0,0 +1,39 @@
;; copyright (c) 2020-2021 sean corfield, all rights reserved
(ns honey.sql.pg-json-test
(:require [clojure.test :refer [deftest is testing]]
[honey.sql :as sql]
[honey.sql.pg-json :as sut]))
(deftest pg-op-tests
(testing "built-in ops"
(is (= ["SELECT a || b AS x"]
(sql/format {:select [[[:|| :a :b] :x]]})))
(is (= ["SELECT a - b AS x"]
(sql/format {:select [[[:- :a :b] :x]]}))))
(testing "writable ops"
(is (= ["SELECT a -> b AS x"]
(sql/format {:select [[[:-> :a :b] :x]]})))
(is (= ["SELECT a ->> b AS x"]
(sql/format {:select [[[:->> :a :b] :x]]})))
(is (= ["SELECT a #> b AS x"]
(sql/format {:select [[[:#> :a :b] :x]]})))
(is (= ["SELECT a #>> b AS x"]
(sql/format {:select [[[:#>> :a :b] :x]]})))
(is (= ["SELECT a ?? b AS x"]
(sql/format {:select [[[:? :a :b] :x]]})))
(is (= ["SELECT a ??| b AS x"]
(sql/format {:select [[[:?| :a :b] :x]]})))
(is (= ["SELECT a ??& b AS x"]
(sql/format {:select [[[:?& :a :b] :x]]})))
(is (= ["SELECT a #- b AS x"]
(sql/format {:select [[[:#- :a :b] :x]]}))))
(testing "named ops"
(is (= ["SELECT a @> b AS x"]
(sql/format {:select [[[sut/at> :a :b] :x]]})))
(is (= ["SELECT a <@ b AS x"]
(sql/format {:select [[[sut/<at :a :b] :x]]})))
(is (= ["SELECT a @?? b AS x"]
(sql/format {:select [[[sut/at? :a :b] :x]]})))
(is (= ["SELECT a @@ b AS x"]
(sql/format {:select [[[sut/atat :a :b] :x]]})))))