korma / Korma

Tasty SQL for Clojure.
http://sqlkorma.com
1.48k stars 222 forks source link

How to use sql function in `where` #240

Closed misty000 closed 9 years ago

misty000 commented 10 years ago
(defn query-month [year month]
  (select Table
      (where {(raw "YEAR(create_date)")  year
              (raw "MONTH(create_date)") month})))

In 0.3.0-RC6, it works, but it doesn't work in 0.3.2

Exception in thread "main" java.lang.ClassCastException: clojure.lang.PersistentArrayMap cannot be cast to java.lang.Comparable at clojure.lang.Compiler.load(Compiler.java:7142) at clojure.lang.Compiler.loadFile(Compiler.java:7086) at clojure.main$load_script.invoke(main.clj:274) at clojure.main$script_opt.invoke(main.clj:336) at clojure.main$main.doInvoke(main.clj:420) at clojure.lang.RestFn.invoke(RestFn.java:408) at clojure.lang.Var.invoke(Var.java:379) at clojure.lang.AFn.applyToHelper(AFn.java:154) at clojure.lang.Var.applyTo(Var.java:700) at clojure.main.main(main.java:37) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) Caused by: java.lang.ClassCastException: clojure.lang.PersistentArrayMap cannot be cast to java.lang.Comparable at clojure.lang.Util.compare(Util.java:153) at clojure.lang.APersistentVector.compareTo(APersistentVector.java:424) at clojure.lang.Util.compare(Util.java:153) at clojure.core$compare.invoke(core.clj:799) at clojure.lang.AFunction.compare(AFunction.java:47) at java.util.TimSort.countRunAndMakeAscending(TimSort.java:351) at java.util.TimSort.sort(TimSort.java:216) at java.util.Arrays.sort(Arrays.java:1435) at clojure.core$sort.invoke(core.clj:2829) at clojure.core$sort.invoke(core.clj:2825) at korma.sql.engine$pred_map.invoke(engine.clj:222)

ls4f commented 10 years ago

Personally, I'm using things like

(select :TABLE (where {(sqlfn coalesce :FIELD 0) [in [0 param]]}))

However, I can say that for some reason it works only if that is the only condition in that exact where. You can have multiple wheres and add other conditions, but for some reason if you add another condition to this one - it starts raising an exception.

misty000 commented 10 years ago

@ls4f

 (defn query-month [year month]
  (select Table
      (where {(raw "YEAR(create_date)") year})
      (where {(raw "MONTH(create_date)") month})))

it also works, thank you

ls4f commented 10 years ago

You're welcome :)

immoh commented 10 years ago

Definitely a bug, got broken in 0.3.1. Thanks for the workaround @ls4f.

ls4f commented 10 years ago

So my code was working last year ... I dug a bit through the history and couldn't find a commit that appears to be braking it so I figured my first case was simply dead code @immoh