Address #31 by improving string representation of reified objects

At least this should give a hint as to what you did wrong...
This commit is contained in:
Sean Corfield 2019-06-11 16:47:58 -07:00
parent a09612cebe
commit e1b42b1804
5 changed files with 24 additions and 5 deletions

View file

@ -6,6 +6,7 @@ Only accretive/fixative changes will be made from now on.
The following changes have been committed to the **master** branch and will be in the 1.0.0 release: The following changes have been committed to the **master** branch and will be in the 1.0.0 release:
* Address #31 by making `reify`'d objects produce a more informative string representation if they are printed (e.g., misusing `plan` by not reducing it or not mapping an operation over the rows).
* Fix #26 by exposing `next.jdbc.result-set/datafiable-result-set` so that various `java.sql.DatabaseMetaData` methods that return result metadata information in `ResultSet`s can be easily turned into a fully realized result set. * Fix #26 by exposing `next.jdbc.result-set/datafiable-result-set` so that various `java.sql.DatabaseMetaData` methods that return result metadata information in `ResultSet`s can be easily turned into a fully realized result set.
## Stable Builds ## Stable Builds

View file

@ -147,7 +147,8 @@
(get-driver-connection url (get-driver-connection url
(assoc etc (assoc etc
:user username :user username
:password password))))) :password password)))
(toString [_] url)))
(defn- make-connection (defn- make-connection
"Given a `DataSource` and a map of options, get a connection and update it "Given a `DataSource` and a map of options, get a connection and update it

View file

@ -343,7 +343,9 @@
(datafiable-row [this connectable opts] (datafiable-row [this connectable opts]
(with-meta (with-meta
(row-builder @builder) (row-builder @builder)
{`core-p/datafy (navize-row connectable opts)}))))) {`core-p/datafy (navize-row connectable opts)}))
(toString [_] "{row} from `plan` -- missing `map` or `reduce`?"))))
(extend-protocol (extend-protocol
DatafiableRow DatafiableRow
@ -411,7 +413,8 @@
(first sql-params) (first sql-params)
(rest sql-params) (rest sql-params)
opts)] opts)]
(reduce-stmt stmt f init opts))))) (reduce-stmt stmt f init opts)))
(toString [_] "`IReduceInit` from `plan` -- missing reduction?")))
(-execute-one [this sql-params opts] (-execute-one [this sql-params opts]
(with-open [stmt (prepare/create this (with-open [stmt (prepare/create this
(first sql-params) (first sql-params)
@ -441,7 +444,8 @@
(first sql-params) (first sql-params)
(rest sql-params) (rest sql-params)
opts)] opts)]
(reduce-stmt stmt f init opts)))))) (reduce-stmt stmt f init opts))))
(toString [_] "`IReduceInit` from `plan` -- missing reduction?")))
(-execute-one [this sql-params opts] (-execute-one [this sql-params opts]
(with-open [con (p/get-connection this opts)] (with-open [con (p/get-connection this opts)]
(with-open [stmt (prepare/create con (with-open [stmt (prepare/create con
@ -471,7 +475,8 @@
(-execute [this _ opts] (-execute [this _ opts]
(reify clojure.lang.IReduceInit (reify clojure.lang.IReduceInit
(reduce [_ f init] (reduce [_ f init]
(reduce-stmt this f init (assoc opts :return-keys true))))) (reduce-stmt this f init (assoc opts :return-keys true)))
(toString [_] "`IReduceInit` from `plan` -- missing reduction?")))
(-execute-one [this _ opts] (-execute-one [this _ opts]
(if-let [rs (stmt->result-set this (assoc opts :return-keys true))] (if-let [rs (stmt->result-set this (assoc opts :return-keys true))]
(let [builder-fn (get opts :builder-fn as-maps) (let [builder-fn (get opts :builder-fn as-maps)

View file

@ -66,6 +66,7 @@
(testing "datasource via Associative" (testing "datasource via Associative"
(let [ds (p/get-datasource db)] (let [ds (p/get-datasource db)]
(is (instance? javax.sql.DataSource ds)) (is (instance? javax.sql.DataSource ds))
(is (str/index-of (pr-str ds) (str "jdbc:" (:dbtype db))))
;; checks get-datasource on a DataSource is identity ;; checks get-datasource on a DataSource is identity
(is (identical? ds (p/get-datasource ds))) (is (identical? ds (p/get-datasource ds)))
(with-open [con (p/get-connection ds {})] (with-open [con (p/get-connection ds {})]
@ -74,6 +75,7 @@
(let [[url _] (#'c/spec->url+etc db) (let [[url _] (#'c/spec->url+etc db)
ds (p/get-datasource url)] ds (p/get-datasource url)]
(is (instance? javax.sql.DataSource ds)) (is (instance? javax.sql.DataSource ds))
(is (str/index-of (pr-str ds) url))
(with-open [con (p/get-connection ds {})] (with-open [con (p/get-connection ds {})]
(is (instance? java.sql.Connection con))))) (is (instance? java.sql.Connection con)))))
(testing "connection via map (Object)" (testing "connection via map (Object)"

View file

@ -118,3 +118,13 @@ INSERT INTO fruit (name, appearance, cost, grade)
VALUES ('Pear', 'green', 49, 47) VALUES ('Pear', 'green', 49, 47)
"])))) "]))))
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))) (is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))))
(deftest plan-misuse
(let [s (pr-str (jdbc/plan (ds) ["select * from fruit"]))]
(is (re-find #"missing reduction" s)))
(let [s (pr-str (into [] (jdbc/plan (ds) ["select * from fruit"])))]
(is (re-find #"missing `map` or `reduce`" s)))
(let [s (pr-str (into [] (take 3) (jdbc/plan (ds) ["select * from fruit"])))]
(is (re-find #"missing `map` or `reduce`" s)))
(is (thrown? IllegalArgumentException
(doall (take 3 (jdbc/plan (ds) ["select * from fruit"]))))))