seancorfield / honeysql

Turn Clojure data structures into SQL
https://cljdoc.org/d/com.github.seancorfield/honeysql/CURRENT
1.75k stars 172 forks source link

Left excluding join #211

Closed AndreaCrotti closed 5 years ago

AndreaCrotti commented 6 years ago

Not sure as a name it make sense but I this is what I'm talking about: http://www.urgenthomework.com/sql-left-excluding-join-homework-help

We have this scenario quite often, and I was wondering if it would make sense to add a helper which

So something like:

(defn exclude-left-join [m table key]
    ...

Having a look at the existing helpers I see that they only modify one key of the map at a time, but the only problem I could see could be if there is also a where helper being used, and how to actually merge the join clause with the others.

Would that be useful/feasible? Thanks

MichaelBlume commented 6 years ago

I'd take a PR, especially if it could be implemented as a helper without changing the formatter (which it sounds like it could?)

AndreaCrotti commented 6 years ago

Yes in theory I think it's possible, however it involves adding the left-join key and updating the where key, adding another condition there. So if where is not there is easy, if there is one condition already we have to wrap both in an :and for example. Is this kind of manipulation done already somewhere else @MichaelBlume ?

AndreaCrotti commented 6 years ago

Ah actually merge-where looks like the right thing to use anyway, so should be relatively easy to implement then thanks!

seancorfield commented 6 years ago

Given that the existing left-join accepts "clauses" which provide the table (and perhaps alias) and then one or more connecting conditions, I'm not sure how you could specify just a table and a key here? It seems that you'd need the current "clauses" and then identify the key that should be added to the "where" clause -- which would duplicate part of one or more "clauses". I'm not convinced this is going to provide much reduction of code over a left-join and a merge-where with the key information given explicitly.

@AndreaCrotti Can you provide a specific example of how you see this working? I'm not convinced this is a worthwhile extension.

seancorfield commented 5 years ago

Closing for lack of response.

AndreaCrotti commented 5 years ago

Yeah I tried a few things but maybe it's not worth since it's relatively easy to do anyway, thanks for the responses though