I've put together two functions that handle this for me, and although I haven't looked at recent changes to clojure.contrib.sql (which sound interesting), perhaps there might be some interest in what I've got.
The first function takes a database connection and a SELECT statement, returning a function representing the PreparedStatement. That function can be called with arguments representing the question-marks in the original SELECT statement. (It tweaks any inbound java.util.Date args into java.sql.Date, naively.) (let [query (rl.database/prepare-query (connection) "SELECT * FROM foo WHERE x = ? AND y > ?")] (doseq [foo (query "BAR" 42)] (println foo))) I was going to try to rig up a function in the metadata for the returned function that would close the PreparedStatement but had real work to do with it, so.... The second function builds on this function: it takes SQL with keyword- style placeholders (like ActiveRecord) and returns a query function. This function accepts a map as its argument, and sets the parameters of the PreparedStatement using the values in the map. This works better (or more sanely) for complex queries in which the same value is used for multiple placeholders. Unlike ActiveRecord, however, this uses a PreparedStatement, and so will not accept syntax like "AND some_field IN (:bunch_of_foos)", which I believe (in AR) works because they don't prepare the statement but generate the SQL on the fly. ;; Example of second query with keyword parameters (let [query (rl.sql/prepare-query (connection) "SELECT * FROM foo WHERE (a = :x AND b > :y) OR (a <> :x AND b <= :y)")] (doseq [foo (query {:x "BAR" :y 42})] (println foo))) On Jan 9, 5:05 pm, Greg Harman <ghar...@gmail.com> wrote: > Would someone mind posting an example of a parameterized query using > clojure.contrib.sql? There are examples in the source of non- > parameterized queries, and do-prepared is used to parameterize values > for inserts, but I can't seem to get my form quite right for a query. > > thanks, > Greg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---