diff --git a/CHANGELOG.md b/CHANGELOG.md index 323f403..00c85b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: +* 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. ## Stable Builds diff --git a/src/next/jdbc/connection.clj b/src/next/jdbc/connection.clj index f318126..065cfe4 100644 --- a/src/next/jdbc/connection.clj +++ b/src/next/jdbc/connection.clj @@ -147,7 +147,8 @@ (get-driver-connection url (assoc etc :user username - :password password))))) + :password password))) + (toString [_] url))) (defn- make-connection "Given a `DataSource` and a map of options, get a connection and update it diff --git a/src/next/jdbc/result_set.clj b/src/next/jdbc/result_set.clj index 06bf02d..8f9d9f6 100644 --- a/src/next/jdbc/result_set.clj +++ b/src/next/jdbc/result_set.clj @@ -343,7 +343,9 @@ (datafiable-row [this connectable opts] (with-meta (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 DatafiableRow @@ -411,7 +413,8 @@ (first sql-params) (rest sql-params) 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] (with-open [stmt (prepare/create this (first sql-params) @@ -441,7 +444,8 @@ (first sql-params) (rest sql-params) 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] (with-open [con (p/get-connection this opts)] (with-open [stmt (prepare/create con @@ -471,7 +475,8 @@ (-execute [this _ opts] (reify clojure.lang.IReduceInit (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] (if-let [rs (stmt->result-set this (assoc opts :return-keys true))] (let [builder-fn (get opts :builder-fn as-maps) diff --git a/test/next/jdbc/connection_test.clj b/test/next/jdbc/connection_test.clj index 4b36862..f38f482 100644 --- a/test/next/jdbc/connection_test.clj +++ b/test/next/jdbc/connection_test.clj @@ -66,6 +66,7 @@ (testing "datasource via Associative" (let [ds (p/get-datasource db)] (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 (is (identical? ds (p/get-datasource ds))) (with-open [con (p/get-connection ds {})] @@ -74,6 +75,7 @@ (let [[url _] (#'c/spec->url+etc db) ds (p/get-datasource url)] (is (instance? javax.sql.DataSource ds)) + (is (str/index-of (pr-str ds) url)) (with-open [con (p/get-connection ds {})] (is (instance? java.sql.Connection con))))) (testing "connection via map (Object)" diff --git a/test/next/jdbc_test.clj b/test/next/jdbc_test.clj index ac0baff..d00f6ef 100644 --- a/test/next/jdbc_test.clj +++ b/test/next/jdbc_test.clj @@ -118,3 +118,13 @@ INSERT INTO fruit (name, appearance, cost, grade) VALUES ('Pear', 'green', 49, 47) "])))) (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"]))))))