Add example of extending SettableParameter
This commit is contained in:
parent
8c98f60a30
commit
5c21d17dec
1 changed files with 13 additions and 1 deletions
|
|
@ -27,7 +27,19 @@ If parameters are provided in the vector along with the SQL statement, in the ca
|
||||||
|
|
||||||
* `(set-parameter v ps i)` -- by default this calls `(.setObject ps i v)` (for `nil` and `Object`)
|
* `(set-parameter v ps i)` -- by default this calls `(.setObject ps i v)` (for `nil` and `Object`)
|
||||||
|
|
||||||
This can be extended to any Clojure data type, to provide a customized way to add specific types of values as parameters to any `PreparedStatement`. Note that you can extend this protocol via metadata so you can do it on a per-object basis if you need:
|
This can be extended to any Clojure data type, to provide a customized way to add specific types of values as parameters to any `PreparedStatement`. For example, to have all `java.time.LocalDate` and `java.time.LocalDateTime` objects converted to `java.sql.Timestamp` automatically:
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
(extend-protocol p/SettableParameter
|
||||||
|
java.time.LocalDate
|
||||||
|
(set-parameter [^java.time.LocalDate v ^PreparedStatement s ^long i]
|
||||||
|
(.setTimestamp ps i (java.sql.Timestamp/valueOf (.atStartOfDay v))))
|
||||||
|
java.time.LocalDateTime
|
||||||
|
(set-parameter [^java.time.LocalDateTime v ^PreparedStatement s ^long i]
|
||||||
|
(.setTimestamp ps i (java.sql.Timestamp/valueOf v))))
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that you can extend this protocol via metadata so you can do it on a per-object basis if you need:
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(with-meta obj {'next.jdbc.prepare/set-parameter (fn [v ps i]...)})
|
(with-meta obj {'next.jdbc.prepare/set-parameter (fn [v ps i]...)})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue