Allow for row-build to return metadata

Instead of using `with-meta`, use `vary-meta` with `assoc` to _add_ 
metadata.
This commit is contained in:
Sean Corfield 2020-09-25 15:45:19 -07:00
parent 9cc0577214
commit 65ec940ccc

View file

@ -571,16 +571,17 @@
(let [row (try (.getRow rs) (catch Throwable t t)) (let [row (try (.getRow rs) (catch Throwable t t))
cols (try (:cols @builder) (catch Throwable t t)) cols (try (:cols @builder) (catch Throwable t t))
meta (try (d/datafy (.getMetaData rs)) (catch Throwable t t))] meta (try (d/datafy (.getMetaData rs)) (catch Throwable t t))]
(with-meta (vary-meta
(row-builder @builder) (row-builder @builder)
{`core-p/datafy assoc
(navize-row connectable opts) `core-p/datafy
`row-number (navize-row connectable opts)
(fn [_] (if (instance? Throwable row) (throw row) row)) `row-number
`column-names (fn [_] (if (instance? Throwable row) (throw row) row))
(fn [_] (if (instance? Throwable cols) (throw cols) cols)) `column-names
`metadata (fn [_] (if (instance? Throwable cols) (throw cols) cols))
(fn [_] (if (instance? Throwable meta) (throw meta) meta))}))) `metadata
(fn [_] (if (instance? Throwable meta) (throw meta) meta)))))
(toString [_] (toString [_]
(try (try
@ -604,8 +605,10 @@
;; in reality, this is going to be over-optimistic and will like cause `nav` ;; in reality, this is going to be over-optimistic and will like cause `nav`
;; to fail on attempts to navigate into result sets that are not hash maps ;; to fail on attempts to navigate into result sets that are not hash maps
(datafiable-row [this connectable opts] (datafiable-row [this connectable opts]
(with-meta this (vary-meta
{`core-p/datafy (navize-row connectable opts)}))) this
assoc
`core-p/datafy (navize-row connectable opts))))
(defn datafiable-result-set (defn datafiable-result-set
"Given a ResultSet, a connectable, and an options hash map, return a fully "Given a ResultSet, a connectable, and an options hash map, return a fully
@ -1029,27 +1032,29 @@
assumed foreign key column name." assumed foreign key column name."
[connectable opts] [connectable opts]
(fn [row] (fn [row]
(with-meta row (vary-meta
{`core-p/nav (fn [_ k v] row
(try assoc
(let [[table fk cardinality] `core-p/nav (fn [_ k v]
(expand-schema k (or (get-in opts [:schema k]) (try
(default-schema k)))] (let [[table fk cardinality]
(if fk (expand-schema k (or (get-in opts [:schema k])
(let [entity-fn (:table-fn opts identity) (default-schema k)))]
exec-fn! (if (= :many cardinality) (if fk
p/-execute-all (let [entity-fn (:table-fn opts identity)
p/-execute-one)] exec-fn! (if (= :many cardinality)
(exec-fn! connectable p/-execute-all
[(str "SELECT * FROM " p/-execute-one)]
(entity-fn (name table)) (exec-fn! connectable
" WHERE " [(str "SELECT * FROM "
(entity-fn (name fk)) (entity-fn (name table))
" = ?") " WHERE "
v] (entity-fn (name fk))
opts)) " = ?")
v)) v]
(catch Exception _ opts))
;; assume an exception means we just cannot v))
;; navigate anywhere, so return just the value (catch Exception _
v)))}))) ;; assume an exception means we just cannot
;; navigate anywhere, so return just the value
v))))))