Fork me on GitHub
#honeysql
<
2021-05-17
>
borkdude16:05:00

Any tips on how to express:

SELECT ARRAY[1,2] && foo;
in honeysql (still on v1 in our production app)? The left array is from values from input, the other array foo is from a table

borkdude16:05:40

I guess I can use (h/format (honeysql.types/array [1 2 3]))

borkdude16:05:49

and now the [:&& included-uuids :tzdb.tag_uuids] part: can I generate the && as an operator instead of a function?

borkdude16:05:27

(sorry, this is probblay in the docs somewhere, but I have trouble finding it šŸ˜’ )

borkdude16:05:29

Is there some hook to register a thing as an infix thing?

borkdude16:05:58

I did this:

(defmethod hf/fn-handler "&&" [op & args]
  (let [args (map hf/to-sql args)]
    (hf/paren-wrap (str/join (str " " op " ") args))))

seancorfield17:05:09

Sounds like youā€™ve solved the problem? Which DB is that for? || is a variadic infix operator in V2 for string concatenation so Iā€™m a bit surprised to see && ā€” what does it mean?

borkdude17:05:31

@seancorfield this is an array operator in postgresql.

borkdude17:05:57

array[1,2,3] && array[1,2,3,4]
means all the left elements must occur in the right elements

borkdude17:05:26

perhaps allowing people to say: "this is an infix operator" would be easier

seancorfield17:05:16

Thatā€™s easy in HoneySQL v2 but I think fn-handler is the right approach in v1.

seancorfield17:05:12

In v2 itā€™s (sql/register-op! :&& :variadic true) and youā€™re off to the races. But Iā€™ll go ahead and add && as a built-in for the next v2 version anyway.

borkdude17:05:30

Excellent! I'll try to migrate this one namespace to v2 now

borkdude17:05:42

We can use them side by side anyway

seancorfield17:05:12

Yup. develop has that change. Itā€™ll be in RC 3 or ā€œgoldā€, whichever comes next.

borkdude18:05:19

if I have an array like ARRAY["..."] but I want to generate ARRAY["..."]::uuid[], what's the best option?

borkdude18:05:03

[:cast included-uuids :uuid[]]
doesn't work obviously :)

borkdude18:05:31

hacky and ugly, but it works:

[:&& [:cast included-uuids [:lift (symbol "uuid[]")]]  :tzdb.tag_uuids]
open to improvements :)

borkdude18:05:29

this seems to work: [:&& [:cast included-uuids [:raw "uuid[]"]] :tzdb.tag_uuids]

borkdude18:05:28

The :inline feature is great for debugging

seancorfield19:05:27

Iā€™d probably go with :cast / :raw for a situation like that. Open to suggestions on making that easier ā€” but weā€™re sort of at the mercy of Clojureā€™s reader for that one.

borkdude20:05:41

cast / raw works for me, it just wasn't what I usually write with hugsql