Back
datafiable-row (clj)
(source)protocol
(datafiable-row this connectable opts)
Produce a datafiable representation of a row from a `ResultSet`.
Examples
next-jdbc
What's left to be tested:
* ReadableColumn protocol extension point"
(:require [clojure.core.protocols :as core-p]
[clojure.datafy :as d]
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[next.jdbc.protocols :as p]
[next.jdbc.result-set :as rs]
[next.jdbc.specs :as specs]
[next.jdbc.test-fixtures :refer [with-test-db ds column
default-options
derby? mssql? mysql? postgres?]])
(:import (java.sql ResultSet ResultSetMetaData)))
(deftest test-datafy-nav
(testing "default schema"
(let [connectable (ds)
test-row (rs/datafiable-row {:TABLE/FRUIT_ID 1} connectable
(default-options))
data (d/datafy test-row)
v (get data :TABLE/FRUIT_ID)]
;; check datafication is sane
(is (= 1 v))
(let [object (d/nav data :table/fruit_id v)]
;; check nav produces a single map with the expected key/value data
(is (= 1 ((column :FRUIT/ID) object)))
(is (= "Apple" ((column :FRUIT/NAME) object))))))
(testing "custom schema *-to-1"
(let [connectable (ds)
test-row (rs/datafiable-row {:foo/bar 2} connectable
(assoc (default-options)
:schema {:foo/bar :fruit/id}))
data (d/datafy test-row)
v (get data :foo/bar)]
;; check datafication is sane
(is (= 2 v))
(let [object (d/nav data :foo/bar v)]
;; check nav produces a single map with the expected key/value data
(is (= 2 ((column :FRUIT/ID) object)))
(is (= "Banana" ((column :FRUIT/NAME) object))))))
(testing "custom schema *-to-many"
(let [connectable (ds)
test-row (rs/datafiable-row {:foo/bar 3} connectable
(assoc (default-options)
:schema {:foo/bar [:fruit/id]}))
data (d/datafy test-row)
v (get data :foo/bar)]
;; check datafication is sane
(is (= 3 v))
(let [object (d/nav data :foo/bar v)]
;; check nav produces a result set with the expected key/value data
(is (vector? object))
(is (= 3 ((column :FRUIT/ID) (first object))))
(is (= "Peach" ((column :FRUIT/NAME) (first object)))))))
(testing "legacy schema tuples"
(let [connectable (ds)
test-row (rs/datafiable-row {:foo/bar 2} connectable
(assoc (default-options)
:schema {:foo/bar [:fruit :id]}))
data (d/datafy test-row)
v (get data :foo/bar)]
;; check datafication is sane
(is (= 2 v))
(let [object (d/nav data :foo/bar v)]
;; check nav produces a single map with the expected key/value data
(is (= 2 ((column :FRUIT/ID) object)))
(is (= "Banana" ((column :FRUIT/NAME) object)))))
(let [connectable (ds)
test-row (rs/datafiable-row {:foo/bar 3} connectable
(assoc (default-options)
:schema {:foo/bar [:fruit :id :many]}))
data (d/datafy test-row)
v (get data :foo/bar)]
;; check datafication is sane
(is (= 3 v))
(let [object (d/nav data :foo/bar v)]
;; check nav produces a result set with the expected key/value data
(is (vector? object))
(is (= 3 ((column :FRUIT/ID) (first object))))
(is (= "Peach" ((column :FRUIT/NAME) (first object))))))))
(deftest test-row-number
;; two notes here: we use as-arrays as a nod to issue #110 to make
;; sure that actually works; also Apache Derby is the only database
;; (that we test against) to restrict .getRow() calls to scroll cursors
(testing "row-numbers on bare abstraction"
(is (= [1 2 3]
(into [] (map rs/row-number)
(p/-execute (ds) ["select * from fruit where id < ?" 4]
;; we do not need a real builder here...
(cond-> {:builder-fn (constantly nil)}
(derby?)
(assoc :concurrency :read-only
:cursors :close
:result-type :scroll-insensitive)))))))
(testing "row-numbers on realized row"
(is (= [1 2 3]
(into [] (comp (map #(rs/datafiable-row % (ds) {}))
(map rs/row-number))
(p/-execute (ds) ["select * from fruit where id < ?" 4]
;; ...but datafiable-row requires a real builder
(cond-> {:builder-fn rs/as-arrays}
(derby?)
(assoc :concurrency :read-only
:cursors :close
:result-type :scroll-insensitive))))))))
(deftest test-column-names
(testing "column-names on bare abstraction"
(is (= #{"id" "appearance" "grade" "cost" "name"}
(reduce (fn [_ row]
(-> row
(->> (rs/column-names)
(map (comp str/lower-case name))
(set)
(reduced))))
nil
(p/-execute (ds) ["select * from fruit where id < ?" 4]
;; column-names require a real builder
{:builder-fn rs/as-arrays})))))
(testing "column-names on realized row"
(is (= #{"id" "appearance" "grade" "cost" "name"}
(reduce (fn [_ row]
(-> row
(rs/datafiable-row (ds) {})
(->> (rs/column-names)
(map (comp str/lower-case name))
(set)
(reduced))))
nil
(p/-execute (ds) ["select * from fruit where id < ?" 4]
{:builder-fn rs/as-arrays}))))))
(deftest test-over-partition-all
;; this verifies that InspectableMapifiedResultSet survives partition-all
(testing "row-numbers on partitioned rows"
(is (= [[1 2] [3 4]]
(into [] (comp (map #(rs/datafiable-row % (ds) %))
(partition-all 2)
(map #(map rs/row-number %)))
(p/-execute (ds) ["select * from fruit"]
(cond-> {:builder-fn rs/as-arrays}
(derby?)
(assoc :concurrency :read-only
:cursors :close
:result-type :scroll-insensitive))))))))
(deftest test-mapify
(testing "no row builder is used"
(is (= [true]
(into [] (map map?) ; it looks like a real map now
(p/-execute (ds) ["select * from fruit where id = ?" 1]
{:builder-fn (constantly nil)}))))
(is (= ["Apple"]
(into [] (map :name) ; keyword selection works
(p/-execute (ds) ["select * from fruit where id = ?" 1]
{:builder-fn (constantly nil)}))))
(is (= [[2 [:name "Banana"]]]
(into [] (map (juxt #(get % "id") ; get by string key works
#(find % :name))) ; get MapEntry works
(p/-execute (ds) ["select * from fruit where id = ?" 2]
{:builder-fn (constantly nil)}))))
(is (= [{:id 3 :name "Peach"}]
(into [] (map #(select-keys % [:id :name])) ; select-keys works
(p/-execute (ds) ["select * from fruit where id = ?" 3]
{:builder-fn (constantly nil)}))))
(is (= [[:orange 4]]
(into [] (map #(vector (if (contains? % :name) ; contains works
(keyword (str/lower-case (:name %)))
:unnamed)
(get % :id 0))) ; get with not-found works
(p/-execute (ds) ["select * from fruit where id = ?" 4]
{:builder-fn (constantly nil)}))))
(is (= [{}]
(into [] (map empty) ; return empty map without building
(p/-execute (ds) ["select * from fruit where id = ?" 1]
{:builder-fn (constantly nil)})))))
(testing "count does not build a map"
(let [count-builder (fn [_1 _2]
(reify rs/RowBuilder
(column-count [_] 13)))]
(is (= [13]
(into [] (map count) ; count relies on columns, not row fields
(p/-execute (ds) ["select * from fruit where id = ?" 1]
{:builder-fn count-builder}))))))
(testing "assoc, dissoc, cons, seq, and = build maps"
(is (map? (reduce (fn [_ row] (reduced (assoc row :x 1)))
nil
(p/-execute (ds) ["select * from fruit"] {}))))
(is (= 6 (count (reduce (fn [_ row] (reduced (assoc row :x 1)))
nil
(p/-execute (ds) ["select * from fruit"] {})))))
(is (map? (reduce (fn [_ row] (reduced
(dissoc row (column :FRUIT/NAME))))
nil
(p/-execute (ds) ["select * from fruit"]
(default-options)))))
(is (= 4 (count (reduce (fn [_ row] (reduced
(dissoc row (column :FRUIT/NAME))))
nil
(p/-execute (ds) ["select * from fruit"]
(default-options))))))
(is (seq? (reduce (fn [_ row] (reduced (seq row)))
nil
(p/-execute (ds) ["select * from fruit"] {}))))
(is (seq? (reduce (fn [_ row] (reduced (cons :seq row)))
nil
(p/-execute (ds) ["select * from fruit"] {}))))
(is (= :seq (first (reduce (fn [_ row] (reduced (cons :seq row)))
nil
(p/-execute (ds) ["select * from fruit"] {})))))
(is (false? (reduce (fn [_ row] (reduced (= row {})))
nil
(p/-execute (ds) ["select * from fruit"] {}))))
(is (map-entry? (second (reduce (fn [_ row] (reduced (cons :seq row)))
nil
(p/-execute (ds) ["select * from fruit"] {})))))
(is (every? map-entry? (reduce (fn [_ row] (reduced (seq row)))
nil
(p/-execute (ds) ["select * from fruit"] {}))))
(is (map? (reduce (fn [_ row] (reduced (conj row {:a 1})))
nil
(p/-execute (ds) ["select * from fruit"] {}))))
(is (map? (reduce (fn [_ row] (reduced (conj row [:a 1])))
nil
(p/-execute (ds) ["select * from fruit"] {}))))
(is (map? (reduce (fn [_ row] (reduced (conj row {:a 1 :b 2})))
nil
(p/-execute (ds) ["select * from fruit"] {}))))
(is (= 1 (:a (reduce (fn [_ row] (reduced (conj row {:a 1})))
nil
(p/-execute (ds) ["select * from fruit"] {})))))
(is (= 1 (:a (reduce (fn [_ row] (reduced (conj row [:a 1])))
nil
(p/-execute (ds) ["select * from fruit"] {})))))
(is (= 1 (:a (reduce (fn [_ row] (reduced (conj row {:a 1 :b 2})))
nil
(p/-execute (ds) ["select * from fruit"] {})))))
(is (= 2 (:b (reduce (fn [_ row] (reduced (conj row {:a 1 :b 2})))
nil
(p/-execute (ds) ["select * from fruit"] {})))))
(is (vector? (reduce (fn [_ row] (reduced (conj row :a)))
nil
(p/-execute (ds) ["select * from fruit"]
{:builder-fn rs/as-arrays}))))
(is (= :a (peek (reduce (fn [_ row] (reduced (conj row :a)))
nil
(p/-execute (ds) ["select * from fruit"]
{:builder-fn rs/as-arrays})))))
(is (= :b (peek (reduce (fn [_ row] (reduced (conj row :a :b)))
nil
(p/-execute (ds) ["select * from fruit"]
{:builder-fn rs/as-arrays}))))))
(testing "datafiable-row builds map; with metadata"
(is (map? (reduce (fn [_ row] (reduced (rs/datafiable-row row (ds) {})))
nil
(p/-execute (ds) ["select * from fruit"] {}))))
(is (contains? (meta (reduce (fn [_ row] (reduced (rs/datafiable-row row (ds) {})))
nil
(p/-execute (ds) ["select * from fruit"] {})))
`core-p/datafy))))