Back

as-maps-adapter (clj)

(source)

function

(as-maps-adapter builder-fn column-reader)
Given a map builder function (e.g., `as-lower-maps`) and a column reading function, return a new builder function that uses that column reading function instead of `.getObject` so you can override the default behavior. This adapter omits SQL NULL values, even if the underlying builder does not. The default column-reader behavior would be equivalent to: (defn default-column-reader [^ResultSet rs ^ResultSetMetaData rsmeta ^Integer i] (.getObject rs i)) Your column-reader can use the result set metadata to determine whether to call `.getObject` or some other method to read the column's value. `read-column-by-index` is still called on the result of that read, if it is not `nil`.

Examples

next-jdbc
(ns next.jdbc.optional-test
  "Test namespace for the optional builder functions."
  (:require [clojure.string :as str]
            [clojure.test :refer [deftest is testing use-fixtures]]
            [next.jdbc.optional :as opt]
            [next.jdbc.protocols :as p]
            [next.jdbc.test-fixtures :refer [with-test-db ds column
                                              default-options]])
  (:import (java.sql ResultSet ResultSetMetaData)))

(deftest test-map-row-adapter
  (testing "default row builder"
    (let [row (p/-execute-one (ds)
                              ["select * from fruit where id = ?" 1]
                              (assoc (default-options)
                                     :builder-fn (opt/as-maps-adapter
                                                  opt/as-maps
                                                  default-column-reader)))]
      (is (map? row))
      (is (not (contains? row (column :FRUIT/GRADE))))
      (is (= 1 ((column :FRUIT/ID) row)))
      (is (= "Apple" ((column :FRUIT/NAME) row)))))
  (testing "unqualified row builder"
    (let [row (p/-execute-one (ds)
                              ["select * from fruit where id = ?" 2]
                              {:builder-fn (opt/as-maps-adapter
                                            opt/as-unqualified-maps
                                            default-column-reader)})]
      (is (map? row))
      (is (not (contains? row (column :COST))))
      (is (= 2 ((column :ID) row)))
      (is (= "Banana" ((column :NAME) row)))))
  (testing "lower-case row builder"
    (let [row (p/-execute-one (ds)
                              ["select * from fruit where id = ?" 3]
                              (assoc (default-options)
                                     :builder-fn (opt/as-maps-adapter
                                                  opt/as-lower-maps
                                                  default-column-reader)))]
      (is (map? row))
      (is (not (contains? row :fruit/appearance)))
      (is (= 3 (:fruit/id row)))
      (is (= "Peach" (:fruit/name row)))))
  (testing "unqualified lower-case row builder"
    (let [row (p/-execute-one (ds)
                              ["select * from fruit where id = ?" 4]
                              {:builder-fn (opt/as-maps-adapter
                                            opt/as-unqualified-lower-maps
                                            default-column-reader)})]
      (is (map? row))
      (is (= 4 (:id row)))
      (is (= "Orange" (:name row)))))
  (testing "custom row builder"
    (let [row (p/-execute-one (ds)
                              ["select * from fruit where id = ?" 3]
                              (assoc (default-options)
                                     :builder-fn (opt/as-maps-adapter
                                                  opt/as-modified-maps
                                                  default-column-reader)
                                     :label-fn str/lower-case
                                     :qualifier-fn identity))]
      (is (map? row))
      (is (not (contains? row (column :FRUIT/appearance))))
      (is (= 3 ((column :FRUIT/id) row)))
      (is (= "Peach" ((column :FRUIT/name) row))))))