seancorfield / next-jdbc

A modern low-level Clojure wrapper for JDBC-based access to databases.
https://cljdoc.org/d/com.github.seancorfield/next.jdbc/
Eclipse Public License 1.0
760 stars 90 forks source link

Extend ReadableColumn protocol by adding a 3-arity read-column-by-label that takes a ResultSetMetaData #135

Closed eihli closed 4 years ago

eihli commented 4 years ago

An attempt to address Issue #134

Let's me do something like this.

(extend-protocol result-set/ReadableColumn
  Integer
  (read-column-by-label [x mrs name]
    (let [i (loop [i 0]
              (cond
                (> i (.getColumnCount mrs)) (throw (Exception. "unable to find column with name: " name))
                (= (.getColumnName mrs i) name) i
                :else (recur (inc i))))]
      (if (re-find #"(?i)bool" (.getColumnTypeName mrs i))
        (if (= 1 x) true false)
        x)))
  (read-column-by-index [x mrs i]
    (if (re-find #"(?i)bool" (.getColumnTypeName mrs i))
      (if (= 1 x) true false)
      x)))
seancorfield commented 4 years ago

This would be a breaking change for anyone using ReadableColumn.