Search code examples
exceptionclojurecontrol-flow

Class cast exception in clojure


Getting clojure.core$count cannot be cast to java.lang.Number error for defined variable count

(defroutes jobs-routes
      (context "/jobs" []
        (POST "/jobValidation" [name status req]  (insert-job jobName jobStatus req)))

In this method I've define a variable count to store the value of total count from the query

(defn insert-job [jobName jobStatus req]
   (let [count (db/insert-job {:job-name name :status status})])
    (when (> count 0) (println (format "true" )))
    (insert-job req) ; 
)

Query: db/insert-job

-- :name insert-jobWithValidation :? :*
SELECT count(*)  FROM job_history WHERE job_name = :job-name and status = :status

Problem I want to check that whether If I'm getting out value greater than 0 so I can print something any if count is any positive value then I can perform something new


Solution

  • that is probably because of your hugsql query header:

    :? :* - returns the rowset, which is not a number. maybe :? :1 would be more suitable here. Anyway, the query doesn't return a count(*), it rather returns a hash-map (in case of :1) or a list of one hashmap (in case of :*), you would still need to retrieve the value from there.

    Something like that (untested):

    -- :name insert-jobWithValidation :? :1
    SELECT count(*) as amount  FROM job_history WHERE job_name = :job-name and status = :status
    
    (defn insert-job [name status req]
      (let [res (db/insert-job {:job-name name :status status})]
        (when (-> res :amount pos?) 
          (println true))
        (insert-job req)))
    

    UPD: omg, let with no body, indeed, @Martin Půda, thanks )

    i would say, what you want is something like that:

    -- :name count-jobs-by-name-and-status :? :1
    SELECT count(*) as amount  FROM job_history WHERE job_name = :job-name and status = :status
    
    (defn insert-job-if-not-exists [name status req]
      (if (->> {:job-name name :status status}
               db/count-jobs-by-name-and-status
               :amount
               pos?)
        (println "job already exists")
        (insert-job req)))