Skip to content

Support for Postgres JSON operators? #16

@xiongtx

Description

@xiongtx

Is there any interest in adding Postgres JSON functions and operators?

E.g. I've been using the following in my own code:

(require '[clojure.string :as str])
(require '[honeysql.format :as h-format])

(defmethod h-format/fn-handler "->" [_ val index]
  (let [[val index] (map h-format/to-sql [val index])]
    (if (number? index)
      (format "%s->%s" val index)
      (format "%s->'%s'" val index))))

(defmethod h-format/fn-handler "->" [_ val index]
  (let [[val index] (map h-format/to-sql [val index])]
    (if (number? index)
      (format "%s->>%s" val index)
      (format "%s->>'%s'" val index))))

(defmethod h-format/fn-handler "#>" [_ val path]
  (format "%s#>'{%s}'"
          (h-format/to-sql val)
          (->> path
               (map h-format/to-sql)
               (str/join ","))))

This allows us to write e.g.:

(-> (h/select [(sql/call :#> :value [:a :b]) :value])
    (h/from :my_table))

to produce the query SELECT value#>'{a,b}' AS value FROM my_table.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions