Fork me on GitHub
#honeysql
<
2021-08-07
>
snorremd14:08:55

Hi. I'm trying to figure out a way to get the containment operator from Postgres registered with honeysql in a good way. The @> operator does not easily represent as a keyword in clojure because of the @ character.

snorremd14:08:47

So while I could do (honey.sql/register-op! (keyword "@>")), I would then have to refer to the operator via (keyword "@>") everywhere I want to use it. 😕

seancorfield15:08:41

@snorremd I would just define a global somewhere with an mnemonic name for it:

(def at> (keyword "@>"))
...
(sql/register-op! at>)
...
(sql/format ... at> ...)

seancorfield15:08:47

(given Clojure's restrictions on using @ there's really not much else you can do -- this applies to a number of PostgreSQL "enhancements" that use weird characters)

snorremd15:08:23

Thanks! Did not think about just defining a global. I made a function instead with infix notation:

(defn- contains-formatter [_f args]
  (let [[sql & params] (hsql/format-expr-list args)]
    (into [(str (first sql) " @> " (second sql))]
          (flatten params))))

(hsql/register-fn! :at> contains-formatter)
This seems to work reasonably well, but yours is simpler. 🙂