Would be convenient to have set-parameters public. I'm experimenting with a 
couple of fns to insert into oracle and oracle's sequences for generated 
keys are a headache. What's below is not fully tested but should give you 
an idea of the sql I'm dealing with. Any pointers on how to leaverage 
java.jdbc more with less custom code would be appreciated.

(defn- oracle-insert-sql [table pk-col-name pk-seq-name ks]
  (let [cols (apply str (interpose \, (map jdbc/as-identifier ks)))
        n (count ks)
        qmarks (apply str (interpose \, (repeat n \?)))]
    (str "insert into " (jdbc/as-identifier table)
         \( (jdbc/as-identifier pk-col-name) \, cols ") values ("
         (jdbc/as-identifier pk-seq-name) ".nextval," qmarks \))))

(defn oracle-insert-record [table pk-col-name pk-seq-name record]
  (let [sql (oracle-insert-sql table pk-col-name pk-seq-name (keys record))]
    (with-open [^PreparedStatement pstmt (.prepareStatement 
(jdbc/connection) sql,
                                                            (into-array 
[(jdbc/as-identifier pk-col-name)]))]
      (set-parameters pstmt (vals record))
      (jdbc/transaction
       (.executeUpdate pstmt)
       (vec (ijdbc/resultset-seq* (.getGeneratedKeys pstmt)))))))

(defn oracle-insert-records [table pk-col-name pk-seq-name & records]
  (when-let [record (first records)]
    (let [ks (keys record)
          sql (oracle-insert-sql table pk-col-name pk-seq-name (keys 
record))          
          value-groups (map #(map (partial get %) ks) records)]
      (apply jdbc/do-prepared sql value-groups))))

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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

Reply via email to