funcool / clojure.jdbc

JDBC library for Clojure
http://funcool.github.io/clojure.jdbc/latest/
Apache License 2.0
105 stars 26 forks source link

java.time interop #46

Open maxp opened 5 years ago

maxp commented 5 years ago

Would be great to have a file like

;; clojure.java-time

  (:require
    [java-time]
    [jdbc.proto :refer [ISQLType ISQLResultSetReadColumn]])

; java.time.LocalDate     - java.sql.Date
; java.time.LocalDateTime - java.sql.Timestamp
; java.time.LocalTime     - java.sql.Time

(extend-protocol ISQLType
  ;
  java.time.LocalDate
  (as-sql-type [this conn]
    (java-time/sql-date this))
  (set-stmt-parameter! [this conn stmt index]
    (.setDate stmt index
      (java-time/sql-date this)))

  java.time.LocalTime
  (as-sql-type [this conn]
    (java-time/sql-time this))
  (set-stmt-parameter! [this conn stmt index]
    (.setTime stmt index
      (java-time/sql-time this)))

  java.time.LocalDateTime
  (as-sql-type [this conn]
    (java-time/sql-timestamp this))
  (set-stmt-parameter! [this conn stmt index]
    (.setTimestamp stmt index
      (java-time/sql-timestamp this))))
  ;
;

(extend-protocol ISQLResultSetReadColumn
  ;
  java.sql.Timestamp
  (from-sql-type [this conn metadata index]
    (java-time/local-date-time this))
  ;
  java.sql.Date
  (from-sql-type [this conn metadata index]
    (java-time/local-date this))
  ;
  java.sql.Time
  (from-sql-type [this conn metadata index]
    (java-time/local-time this)))
;

to make java.time support easier.