Amogh Talpallikar
Amogh Talpallikar

Reputation: 12184

How to pass clojure vector in a prepared statement when running raw sql using Korma?

My query is something like

(def list [1,2,3,4])

(def result (exec-raw
   ["SELECT * from table_name where table_id in (?)"],[list] :results))

I keep getting this error that Clojure can't infer SQL type for persistent vector, use setObject to tell the type, now how do I use setObject with CLojure?

Upvotes: 0

Views: 888

Answers (2)

edbond
edbond

Reputation: 3951

If you want a raw sql you'll need to concatenate '?':

(def list ["AAPL" "GOOG"])
(def questions
  (->> (repeat (count list) "?")
       (interpose ",")
       (apply str)))

(def q (str "SELECT * FROM ta_indicators WHERE ticker IN ("
            questions ")"))

(println (exec-raw [q list] :results))

It looks korma does the same thing and works:

(defentity ta_indicators)
(-> (select* ta_indicators)
    (fields :ticker)
    (where {:ticker [in ["GOOG" "TSLA"]]})
    (as-sql))

;; "SELECT \"ta_indicators\".\"ticker\" FROM \"ta_indicators\" WHERE (\"ta_indicators\".\"ticker\" IN (?, ?))"

Upvotes: 3

xsc
xsc

Reputation: 6073

Shouldn't the function be called like so:

(exec-raw ["select ..." list] ...)

You might have list in the wrong place.

Upvotes: 1

Related Questions