luminus-framework / conman

a helper library for managing database connections
Eclipse Public License 1.0
122 stars 27 forks source link

ClassCastException when doing a request #67

Closed little-dude closed 5 years ago

little-dude commented 5 years ago

Hi,

I'm not 100% sure whether this is an issue with conman or hugsql (or even if something wrong with my code). I have the following sql query:

-- :name get-status
-- :doc fetch the status of a specific message
-- :command :query
-- :return :many
/* :require [clojure.string :as s] */
SELECT
    *
FROM
    message_status
/*~
(let [params (filter (comp some? val) params)]
  (when (not (empty? params))
    (str "WHERE "
      (s/join ", "
              (for [[field _] params]
                (str (name field) " = " field))))))
~*/

The idea is that I want to be able to do

(get-status {:message_id 2 :user_id 1 :status "sent"})
;; or
(get-status {:message_id 2})
;; or
(get-status {:user_id 1})
;; etc.

The query seems ok to me:

myapp.db.core=> (get-status-sqlvec {:message_id 2 :user_id 1})
["SELECT\n    *\nFROM\n    message_status\nWHERE message_id = ?, user_id = ?"
 2
 1]

But when I execute it I get an un-helpful error message, and this is where I'm stuck:

myapp.db.core=> (get-status {:message_id 2})
Execution error (ClassCastException) at (REPL:1).
null

myapp.db.core=> *e
#error {
 :cause nil
 :via
 [{:type java.lang.Exception
   :message "Exception in :get-status"
   :at [conman.core$try_query$fn__12775$fn__12776 invoke "core.clj" 32]}
  {:type java.lang.ClassCastException
   :message nil}]
 :trace
 []}

Do you have any idea about what is wrong with this request?

little-dude commented 5 years ago

So it turns out my query is wrong (I'm an SQL newbie). In the WHERE clause, it needs AND instead of commas, which makes sense. I found this by using cls-postgresql directly instead of conman:

myapp.db.core=> (get-status db2 {:message_id 3 :user_id 1})
Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2468).
ERROR: syntax error at or near ","
  Position: 59

It would be nice if conman was able to report this kind of errors, but that's probably a separate issue.

Sorry for the noise, and thanks for you work on Luminus, it's making me enjoy Clojure and web development :)

yogthos commented 5 years ago

No worries, and glad to hear everything's working. :)