|
5 | 5 | [pretty.core :as pretty] |
6 | 6 | [toucan2.instance :as instance] |
7 | 7 | [toucan2.jdbc.row :as jdbc.row] |
| 8 | + [toucan2.magic-map :as magic-map] |
| 9 | + [toucan2.model :as model] |
8 | 10 | [toucan2.protocols :as protocols] |
9 | 11 | [toucan2.util :as u]) |
10 | 12 | (:import |
|
53 | 55 | [^ResultSetMetaData rsmeta] |
54 | 56 | (range 1 (inc (.getColumnCount rsmeta)))) |
55 | 57 |
|
56 | | -(defn- row-instance [model #_key-xform col-name->thunk] |
| 58 | +(defn- row-instance [model col-name->thunk] |
57 | 59 | (let [row (jdbc.row/row col-name->thunk)] |
58 | 60 | (u/with-debug-result ["Creating new instance of %s, which has key transform fn %s" |
59 | 61 | model |
|
63 | 65 | (defn row-thunk |
64 | 66 | "Return a thunk that when called fetched the current row from the cursor and returns it as a [[row-instance]]." |
65 | 67 | [^Connection conn model ^ResultSet rset] |
66 | | - (let [rsmeta (.getMetaData rset) |
67 | | - key-xform (instance/key-transform-fn model) |
| 68 | + (let [rsmeta (.getMetaData rset) |
| 69 | + ;; do case-insensitive lookup. |
| 70 | + table->namespace (some-> (model/table-name->namespace model) (magic-map/magic-map u/lower-case-en)) |
| 71 | + key-xform (instance/key-transform-fn model) |
68 | 72 | ;; create a set of thunks to read each column. These thunks will call `read-column-thunk` to determine the |
69 | 73 | ;; appropriate column-reading thunk the first time they are used. |
70 | | - col-name->thunk (into {} (for [^Long i (index-range rsmeta) |
71 | | - :let [col-name (key-xform (keyword (.getColumnName rsmeta i))) |
72 | | - ;; TODO -- add test to ensure we only resolve the read-column-thunk |
73 | | - ;; once even with multiple rows. |
74 | | - read-thunk (delay (read-column-thunk conn model rset rsmeta i)) |
75 | | - result-thunk (fn [] |
76 | | - (u/with-debug-result ["Realize column %s %s" i col-name] |
77 | | - (next.jdbc.rs/read-column-by-index (@read-thunk) rsmeta i)))]] |
78 | | - [col-name result-thunk]))] |
| 74 | + col-name->thunk (into {} |
| 75 | + (map (fn [^Long i] |
| 76 | + (let [table-name (.getTableName rsmeta i) |
| 77 | + col-name (.getColumnName rsmeta i) |
| 78 | + table-ns-name (some-> (get table->namespace table-name) name) |
| 79 | + col-key (key-xform (keyword table-ns-name col-name)) |
| 80 | + ;; TODO -- add test to ensure we only resolve the read-column-thunk |
| 81 | + ;; once even with multiple rows. |
| 82 | + read-thunk (delay (read-column-thunk conn model rset rsmeta i)) |
| 83 | + result-thunk (fn [] |
| 84 | + (u/with-debug-result ["Realize column %s %s.%s as %s" |
| 85 | + i table-name col-name col-key] |
| 86 | + (next.jdbc.rs/read-column-by-index (@read-thunk) rsmeta i)))] |
| 87 | + [col-key result-thunk]))) |
| 88 | + (index-range rsmeta))] |
79 | 89 | (fn row-instance-thunk [] |
80 | | - (row-instance model #_key-xform col-name->thunk)))) |
| 90 | + (row-instance model col-name->thunk)))) |
81 | 91 |
|
82 | 92 | (deftype ^:no-doc ReducibleResultSet [^Connection conn model ^ResultSet rset] |
83 | 93 | clojure.lang.IReduceInit |
|
0 commit comments