-
-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional WHERE
clauses are hard to write and read
#466
Comments
The issue with multiple clauses is exacerbated when you have clauses that should follow {:select [[[:array_agg :x]]]
:from [:t]
:where [:and
(when filter-by-x? [:< :x 5])
(when filter-by-y? [:> :y 0])]
:group-by [:y]
:order-by [:y]} to what I have to write currently: (cond-> {:select [[[:array_agg :x]]]
:from [:t]
:group-by [:y]
:order-by [:y]}
filter-by-x? (hh/where [:< :x 5])
filter-by-y? (hh/where [:> :y 0])) |
It's done as a safety measure, so you don't accidentally select your entire table and delete or update it. I think at one point, it used to allow it and that caught someone out badly, so now you have to be explicit. |
Hmm, not sure I would've done the same TBH. We don't have But if you think it's a reasonable trade-off in the case of HoneySQL, how about having a flag that allows that? Alternatively, now that I have slept for a bit, I realize that (defmacro cond-where [& clauses]
(assert (even? (count clauses)))
`(let [where# (cond-> [:and]
~@(mapcat (fn [[cond expr]]
[cond `(conj ~expr)])
(partition 2 clauses)))]
(if (= (count where#) 1)
[]
where#))) Of course I can just copy it across my projects. But it feels that the need that this issue outlines should be a common one, so maybe the macro or something like it is worth introducing it into HoneySQL? |
Another 5c (keep noticing stuff only because I'm rewriting a decently sized code base from v1 to v2): dynamic {:select [:*]
:from [:t]
:where (into [:and]
(map cond->sql-cond)
conditions)} Of course, it's easy to wrap that |
Oh, maybe this is what you were talking about: #413 |
While you can argue for But you could do I could special case |
I've updated develop to collapse |
I would say so, yes! |
A single conditional clause
If I have just one conditional clause, I have to write
or something like
IMO it would be better if it were possible to write
But it's not possible -
format
throws:Multiple conditional clauses
With more than one condition, I'm practically forced to use the helper function because otherwise I'd have to figure out myself when I need
[:and ...]
:I think it would be better to be able to write
But if none of the conditions is there,
format
throws:The text was updated successfully, but these errors were encountered: