Fork me on GitHub
#honeysql
<
2022-12-01
>
orestis13:12:16

Are there facilities to, er, query a HoneySQLv2 query map? For example, I want to check if a table is already left-joined. The function is trivial to write myself, but I was wondering if people tend to do this kind of thing.

orestis13:12:48

I've had to also add facilities to join/filter/select using "dynamic" table names so that you would be able to do it more than once.

markaddleman13:12:22

There are SQLisms that make this a difficult prospect in general - particularly aliases. But, if you ignore that, I have had good experience using #CFFTD7R6Z to parse through Honey data structures

orestis14:12:10

I think that given honeysql has a somewhat well defined grammar, the SQLisms are not such big of a deal.

orestis14:12:20

Eg a table name is either a keyword or a vector

markaddleman14:12:32

Grammar isn’t the issue but semantics are. For example:

{:with [[:cte {:select [:a] :from [:t]}]] :select [:a] :from [:s] :join [:cte]}
(edited to fix the syntax)

markaddleman14:12:46

Is :t in the join?

orestis14:12:24

Ah true. I’m not concerned with that though. I’m more interested in not writing functions to get the table names in the join clause.

markaddleman14:12:35

Ah, ok. Then, I’ve had good experience using meander to parse and recursively descend into honey data structures. Meander makes it very straightforward.

orestis14:12:01

I do love how honeysql makes this even possible. We’ve written some terrible looking code that “prunes” an SQL query based on what the API request is. Impossible with string based sql

seancorfield18:12:19

@U7PBP4UVA That would be "trivial" if HoneySQL used sets for :join clauses I think? The only problem would be that JOIN order can be significant (which is why HoneySQL has a specific construct for ordering multiple JOIN clauses as well). Or I guess the join-merging logic could always apply distinct to the sequence of JOINs?

seancorfield18:12:58

Feel free to create a GH issue if you think there's anything HoneySQL could do better in this area to help support your use case.

orestis19:12:28

(->> query :left-join (partition-all 2) (into #{} (map first)))

orestis19:12:11

This works nicely but I think doesn't handle aliasing the joined table as something else. It handles the specific query it was written for though (which assumes uniqueness, hence the cast to the set at the end)

orestis19:12:17

I wouldn't change HoneySQL at all - this is a very much secondary problem that is definitely solvable in user land. I was more wondering if there are other people that are actually querying maps to find information, then some helpers might be in order.