开发者

Using Clojure do-template to set prepared-statement columns

开发者 https://www.devze.com 2023-02-04 12:13 出处:网络
I have defined the following code to allow me to set column values in a java.sql.PreparedStatement. Is this code reasonable/idiomatic? How could it be improved?

I have defined the following code to allow me to set column values in a java.sql.PreparedStatement. Is this code reasonable/idiomatic? How could it be improved?

(use '(clojure.template :only [do-template]))
; (import all java types not in java.lang)

(defprotocol SetPreparedStatement
  (set-prepared-statement [this prepared-statement index]))

(do-template [type-name set-name]
  (extend-type type-name
    SetPreparedStatement
    (set-prepared-statement [this prepared-statement index]
      (set-name prepared-statement index this)))

  BigDecimal .setBigDecimal
  Boolean .setBoolean
  Byte .setByte
  Date .setDate
  Doubl开发者_如何学运维e .setDouble
  Float .setFloat
  Integer .setInt
  Long .setLong
  Object .setObject
  Short .setShort
  Time .setTime
  Timestamp .setTimestamp)

; Sample use
(set-prepared-statement 42 some-prepared-statement 1)


Your example looks as close to idiomatic Clojure as I can tell :) It could perhaps benefit from abstracting the type mapping out if you have situations where you will be creating more than one template though if your creating just this one then this looks like excellent clojure to me.

0

精彩评论

暂无评论...
验证码 换一张
取 消