Hi, is there a way to use boolean expressions in select statements in honeysql? like select (a.numeric_field > 100) AS over_threshold
@bertofer I think you have to use #sql/call
in there, like this:
user=> (require '[honeysql.helpers :refer :all] '[honeysql.core :as h] '[honeysql.types :as sql])
...
nil
user=> (h/format (select [#sql/call [:> :a 100] :threshold]))
["SELECT a > ? AS threshold" 100]
user=>
(without the #sql/call
you get SELECT > AS a AS threshold
which is obviously wrong -- and should at least blow up with an assert in the next version -- but ideally should not treat the expression as an alias in that position either!)
Yes is what I ended up doing, thanks @seancorfield! I was doing it like a fn call though, like (sql/call :> :a 100)
, can you explain what is the difference with #sql/call [:> :a 100]
? Whatβs the meaning of #
here? Iβm a bit new to clojure yet π
@bertofer #
indicates a reader literal so #sql/call [:> :a 100]
says "read the following form -- [:> :a 100]
-- and treat it as a "sql/call" object...
Okei, thanks ! π
The reader doesn't evaluate forms:
user=> #sql/call [:> (keyword "a") (* 10 10)]
#sql/call [:> (keyword "a") (* 10 10)]
user=> (h/format *1)
["keyword(?) > (? * ?)" "a" 10 10]
user=> (sql/call :> (keyword "a") (* 10 10))
#sql/call [:> :a 100]
user=> (h/format *1)
["a > ?" 100]
user=>
There are pros and cons to both approaches.
For example
user=> #sql/call [:> :a (* :b 10)]
#sql/call [:> :a (* :b 10)]
user=> (h/format *1)
["a > (b * ?)" 10]
user=>
lets you lift the 10
out as a parameter and treat :b
as a column name -- so the multiplication is done in SQL.(sql/call :> :a (* :b 10))
will fail trying to evaluate :b
times 10
π