honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
borkdude 2021-05-17T16:36:00.307600Z

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

borkdude 2021-05-17T16:38:40.307900Z

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

borkdude 2021-05-17T16:41:49.308300Z

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

borkdude 2021-05-17T16:42:27.308800Z

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

borkdude 2021-05-17T16:49:29.309400Z

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

borkdude 2021-05-17T16:52:58.309700Z

I did this:

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

seancorfield 2021-05-17T17:04:09.311300Z

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?

borkdude 2021-05-17T17:31:31.311600Z

@seancorfield this is an array operator in postgresql.

borkdude 2021-05-17T17:31:57.312200Z

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

borkdude 2021-05-17T17:32:26.312700Z

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

seancorfield 2021-05-17T17:36:16.313300Z

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

seancorfield 2021-05-17T17:38:12.314200Z

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.

borkdude 2021-05-17T17:43:30.314500Z

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

borkdude 2021-05-17T17:43:42.314700Z

We can use them side by side anyway

seancorfield 2021-05-17T17:44:12.315300Z

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

borkdude 2021-05-17T18:46:19.316Z

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

borkdude 2021-05-17T18:49:03.316300Z

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

borkdude 2021-05-17T18:53:31.316800Z

hacky and ugly, but it works:

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

borkdude 2021-05-17T18:55:29.317100Z

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

borkdude 2021-05-17T18:59:28.317400Z

The :inline feature is great for debugging

seancorfield 2021-05-17T19:54:27.318400Z

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.

borkdude 2021-05-17T20:13:41.318900Z

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