honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
2019-05-10T21:38:52.003800Z

Not much activity here but I’ll give it a shot anyway. I’m trying to write a PostgreSQL ORDER BY clause that orders by some data in a JSON column:

order by selections #>> array[?,'user-data','text']

2019-05-10T21:40:44.005400Z

I tried this but it doesn’t work at all:

:order-by [[:#>> :selections (honeysql.types/array [selectable-id "user-data" "text"])] :asc]

seancorfield 2019-05-10T21:41:37.006500Z

There's quite a bit of activity here -- but the free plan we're on limits visibility to 10,000 messages and there's usually well over 15,000 a week so you only ever see a few days of messages in any channel.

seancorfield 2019-05-10T21:42:18.007100Z

@jkrasnay Are you using the PostgreSQL-specific extensions library for HoneySQL, or just bare HoneySQL?

seancorfield 2019-05-10T21:43:33.008Z

I suspect you'll need to use #sql/raw (`sql` = honeysql.types).

2019-05-10T21:52:37.009500Z

Thanks for the quick response! Just using HoneySQL. I did get the following working:

:order-by [[(sql/raw ["selections #>> array[" (sql/param selectable-id) ",'user-data','text']"])]]

2019-05-10T21:54:18.010900Z

Sadly, selectable-id is a string so I have to supply it separately to format instead of it being carried around in the SQL map. Is there some way to lift that parameter out of the raw in the same way, say, an integer would be?

seancorfield 2019-05-10T21:56:51.011200Z

Not yet. It's an open issue.

seancorfield 2019-05-10T21:58:00.012100Z

I'm not very happy with sql/raw but I haven't yet figured out a better solution that deals with the corner cases, since I took over the library.

2019-05-10T22:24:42.012500Z

OK, thanks for your time

2019-05-10T22:36:29.012900Z

@jkrasnay Does this work for you?

=> (defmethod sqlf/fn-handler "#>>"
     [_ obj path]
     (format "%s#>>%s" (sqlf/to-sql obj) (sqlf/to-sql (sqlt/array path))))

=> (sql/format {:order-by [[(sql/call :#>> :payload ["a" "b"]) :asc]]})
["ORDER BY payload#>>ARRAY[?, ?] ASC" "a" "b"]

2019-05-10T22:37:26.013400Z

sqlf = honeysql.format, sql = honeysql.core, sqlt = honeysql.types