diff --git a/src/next/jdbc/result_set.clj b/src/next/jdbc/result_set.clj index e709cce..42dcae0 100644 --- a/src/next/jdbc/result_set.clj +++ b/src/next/jdbc/result_set.clj @@ -58,6 +58,9 @@ (mapv (fn [^Integer i] (keyword (.getColumnLabel rsmeta i))) (range 1 (inc (if rsmeta (.getColumnCount rsmeta) 0))))) +(defn- validate [expr ^String msg] + (when-not expr (throw (IllegalArgumentException. msg)))) + (defn get-modified-column-names "Given `ResultSetMetaData`, return a vector of modified column names, each qualified by the table from which it came. @@ -66,8 +69,8 @@ [^ResultSetMetaData rsmeta opts] (let [qf (:qualifier-fn opts) lf (:label-fn opts)] - (assert qf ":qualifier-fn is required") - (assert lf ":label-fn is required") + (validate qf ":qualifier-fn is required") + (validate lf ":label-fn is required") (mapv (fn [^Integer i] (if-let [q (some-> (get-table-name rsmeta i) (qf) (not-empty))] (keyword q (-> (.getColumnLabel rsmeta i) (lf))) @@ -81,7 +84,7 @@ Requires the `:label-fn` option." [^ResultSetMetaData rsmeta opts] (let [lf (:label-fn opts)] - (assert lf ":label-fn is required") + (validate lf ":label-fn is required") (mapv (fn [^Integer i] (keyword (lf (.getColumnLabel rsmeta i)))) (range 1 (inc (if rsmeta (.getColumnCount rsmeta) 0)))))) @@ -906,7 +909,7 @@ (first sql-params) (rest sql-params) opts)] - (reduce-stmt stmt f init opts))) + (reduce-stmt stmt f init opts))) r/CollFold (coll-fold [_ n combinef reducef] (with-open [con (p/get-connection this opts) @@ -922,12 +925,12 @@ (first sql-params) (rest sql-params) opts)] - (if-let [rs (stmt->result-set stmt opts)] - (let [builder-fn (get opts :builder-fn as-maps) - builder (builder-fn rs opts)] - (when (.next rs) - (datafiable-row (row-builder builder) this opts))) - {:next.jdbc/update-count (.getUpdateCount stmt)}))) + (if-let [rs (stmt->result-set stmt opts)] + (let [builder-fn (get opts :builder-fn as-maps) + builder (builder-fn rs opts)] + (when (.next rs) + (datafiable-row (row-builder builder) this opts))) + {:next.jdbc/update-count (.getUpdateCount stmt)}))) (-execute-all [this sql-params opts] (with-open [con (p/get-connection this opts) stmt (prepare/create con @@ -978,8 +981,8 @@ java.sql.Statement (-execute [this sql-params opts] - (assert (= 1 (count sql-params)) - "Parameters cannot be provided when executing a non-prepared Statement") + (validate (= 1 (count sql-params)) + "Parameters cannot be provided when executing a non-prepared Statement") (reify clojure.lang.IReduceInit (reduce [_ f init] @@ -990,8 +993,8 @@ (.getConnection this) opts)) (toString [_] "`IReduceInit` from `plan` -- missing reduction?"))) (-execute-one [this sql-params opts] - (assert (= 1 (count sql-params)) - "Parameters cannot be provided when executing a non-prepared Statement") + (validate (= 1 (count sql-params)) + "Parameters cannot be provided when executing a non-prepared Statement") (if-let [rs (stmt-sql->result-set this (first sql-params))] (let [builder-fn (get opts :builder-fn as-maps) builder (builder-fn rs opts)] @@ -1000,8 +1003,8 @@ (.getConnection this) opts))) {:next.jdbc/update-count (.getUpdateCount this)})) (-execute-all [this sql-params opts] - (assert (= 1 (count sql-params)) - "Parameters cannot be provided when executing a non-prepared Statement") + (validate (= 1 (count sql-params)) + "Parameters cannot be provided when executing a non-prepared Statement") (if (:multi-rs opts) (loop [go (.execute this (first sql-params)) acc []] (if-let [rs (stmt->result-set-update-count diff --git a/src/next/jdbc/sql/builder.clj b/src/next/jdbc/sql/builder.clj index f42c3b2..802597c 100644 --- a/src/next/jdbc/sql/builder.clj +++ b/src/next/jdbc/sql/builder.clj @@ -70,6 +70,9 @@ [key-map opts] (as-cols (keys key-map) opts)) +(defn- validate [expr ^String msg] + (when-not expr (throw (IllegalArgumentException. msg)))) + (defn by-keys "Given a hash map of column names and values and a clause type (`:set`, `:where`), return a vector of a SQL clause and its parameters. @@ -84,7 +87,7 @@ [(conj conds (str e " = ?")) (conj params v)]))) [[] []] key-map)] - (assert (seq where) "key-map may not be empty") + (validate (seq where) "key-map may not be empty") (into [(str (str/upper-case (safe-name clause)) " " (str/join (if (= :where clause) " AND " ", ") where))] params))) @@ -122,7 +125,7 @@ (let [entity-fn (:table-fn opts identity) params (as-keys key-map opts) places (as-? key-map opts)] - (assert (seq key-map) "key-map may not be empty") + (validate (seq key-map) "key-map may not be empty") (into [(str "INSERT INTO " (entity-fn (safe-name table)) " (" params ")" " VALUES (" places ")" @@ -144,11 +147,11 @@ If `:suffix` is provided in `opts`, that string is appended to the `INSERT ...` statement." [table cols rows opts] - (assert (apply = (count cols) (map count rows)) - "column counts are not consistent across cols and rows") + (validate (apply = (count cols) (map count rows)) + "column counts are not consistent across cols and rows") ;; to avoid generating bad SQL - (assert (seq cols) "cols may not be empty") - (assert (seq rows) "rows may not be empty") + (validate (seq cols) "cols may not be empty") + (validate (seq rows) "rows may not be empty") (let [table-fn (:table-fn opts identity) batch? (:batch opts) params (as-cols cols opts) @@ -195,7 +198,7 @@ [order-by opts] (when-not (vector? order-by) (throw (IllegalArgumentException. ":order-by must be a vector"))) - (assert (seq order-by) ":order-by may not be empty") + (validate (seq order-by) ":order-by may not be empty") (str "ORDER BY " (str/join ", " (map #(for-order-col % opts) order-by)))) diff --git a/test/next/jdbc/sql/builder_test.clj b/test/next/jdbc/sql/builder_test.clj index 38e60d1..1d577bf 100644 --- a/test/next/jdbc/sql/builder_test.clj +++ b/test/next/jdbc/sql/builder_test.clj @@ -158,7 +158,7 @@ (deftest test-for-update (testing "empty example (would be a SQL error)" - (is (thrown? AssertionError ; changed in #44 + (is (thrown? IllegalArgumentException (builder/for-update :user {:status 42} {}