From 860298943c218b8e53ff4a4071fb615ecfab1f56 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Fri, 22 May 2020 17:11:36 -0700 Subject: [PATCH] Addresses #110 by adding row-number, column-names This is a quick sketch that doesn't break the existing tests. Documentation and tests coming soon! --- src/next/jdbc/result_set.clj | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/next/jdbc/result_set.clj b/src/next/jdbc/result_set.clj index a87a12d..52e790e 100644 --- a/src/next/jdbc/result_set.clj +++ b/src/next/jdbc/result_set.clj @@ -385,6 +385,13 @@ (definterface MapifiedResultSet) +(defprotocol InspectableMapifiedResultSet :extend-via-metadata true + "" + (row-number [this] + "") + (column-names [this] + "")) + (defn- mapify-result-set "Given a `ResultSet`, return an object that wraps the current row as a hash map. Note that a result set is mutable and the current row will change behind @@ -403,6 +410,10 @@ MapifiedResultSet ;; marker, just for printing resolution + InspectableMapifiedResultSet + (row-number [this] (.getRow rs)) + (column-names [this] (:cols @builder)) + clojure.lang.IPersistentMap (assoc [this k v] (assoc (row-builder @builder) k v)) @@ -458,9 +469,18 @@ DatafiableRow (datafiable-row [this connectable opts] - (with-meta - (row-builder @builder) - {`core-p/datafy (navize-row connectable opts)})) + ;; since we have to call these eagerly, we trap any exceptions so + ;; that they can be thrown when the actual functions are called + (let [row (try (.getRow rs) (catch Throwable t t)) + cols (try (:cols @builder) (catch Throwable t t))] + (with-meta + (row-builder @builder) + {`core-p/datafy + (navize-row connectable opts) + `row-number + (fn [_] (if (instance? Throwable row) (throw row) row)) + `column-names + (fn [_] (if (instance? Throwable cols) (throw cols) cols))}))) (toString [_] (try