switch asserts to validation / exceptions
Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
parent
ecd950d009
commit
585a7120a9
3 changed files with 30 additions and 24 deletions
|
|
@ -58,6 +58,9 @@
|
||||||
(mapv (fn [^Integer i] (keyword (.getColumnLabel rsmeta i)))
|
(mapv (fn [^Integer i] (keyword (.getColumnLabel rsmeta i)))
|
||||||
(range 1 (inc (if rsmeta (.getColumnCount rsmeta) 0)))))
|
(range 1 (inc (if rsmeta (.getColumnCount rsmeta) 0)))))
|
||||||
|
|
||||||
|
(defn- validate [expr ^String msg]
|
||||||
|
(when-not expr (throw (IllegalArgumentException. msg))))
|
||||||
|
|
||||||
(defn get-modified-column-names
|
(defn get-modified-column-names
|
||||||
"Given `ResultSetMetaData`, return a vector of modified column names, each
|
"Given `ResultSetMetaData`, return a vector of modified column names, each
|
||||||
qualified by the table from which it came.
|
qualified by the table from which it came.
|
||||||
|
|
@ -66,8 +69,8 @@
|
||||||
[^ResultSetMetaData rsmeta opts]
|
[^ResultSetMetaData rsmeta opts]
|
||||||
(let [qf (:qualifier-fn opts)
|
(let [qf (:qualifier-fn opts)
|
||||||
lf (:label-fn opts)]
|
lf (:label-fn opts)]
|
||||||
(assert qf ":qualifier-fn is required")
|
(validate qf ":qualifier-fn is required")
|
||||||
(assert lf ":label-fn is required")
|
(validate lf ":label-fn is required")
|
||||||
(mapv (fn [^Integer i]
|
(mapv (fn [^Integer i]
|
||||||
(if-let [q (some-> (get-table-name rsmeta i) (qf) (not-empty))]
|
(if-let [q (some-> (get-table-name rsmeta i) (qf) (not-empty))]
|
||||||
(keyword q (-> (.getColumnLabel rsmeta i) (lf)))
|
(keyword q (-> (.getColumnLabel rsmeta i) (lf)))
|
||||||
|
|
@ -81,7 +84,7 @@
|
||||||
Requires the `:label-fn` option."
|
Requires the `:label-fn` option."
|
||||||
[^ResultSetMetaData rsmeta opts]
|
[^ResultSetMetaData rsmeta opts]
|
||||||
(let [lf (:label-fn 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))))
|
(mapv (fn [^Integer i] (keyword (lf (.getColumnLabel rsmeta i))))
|
||||||
(range 1 (inc (if rsmeta (.getColumnCount rsmeta) 0))))))
|
(range 1 (inc (if rsmeta (.getColumnCount rsmeta) 0))))))
|
||||||
|
|
||||||
|
|
@ -978,7 +981,7 @@
|
||||||
|
|
||||||
java.sql.Statement
|
java.sql.Statement
|
||||||
(-execute [this sql-params opts]
|
(-execute [this sql-params opts]
|
||||||
(assert (= 1 (count sql-params))
|
(validate (= 1 (count sql-params))
|
||||||
"Parameters cannot be provided when executing a non-prepared Statement")
|
"Parameters cannot be provided when executing a non-prepared Statement")
|
||||||
(reify
|
(reify
|
||||||
clojure.lang.IReduceInit
|
clojure.lang.IReduceInit
|
||||||
|
|
@ -990,7 +993,7 @@
|
||||||
(.getConnection this) opts))
|
(.getConnection this) opts))
|
||||||
(toString [_] "`IReduceInit` from `plan` -- missing reduction?")))
|
(toString [_] "`IReduceInit` from `plan` -- missing reduction?")))
|
||||||
(-execute-one [this sql-params opts]
|
(-execute-one [this sql-params opts]
|
||||||
(assert (= 1 (count sql-params))
|
(validate (= 1 (count sql-params))
|
||||||
"Parameters cannot be provided when executing a non-prepared Statement")
|
"Parameters cannot be provided when executing a non-prepared Statement")
|
||||||
(if-let [rs (stmt-sql->result-set this (first sql-params))]
|
(if-let [rs (stmt-sql->result-set this (first sql-params))]
|
||||||
(let [builder-fn (get opts :builder-fn as-maps)
|
(let [builder-fn (get opts :builder-fn as-maps)
|
||||||
|
|
@ -1000,7 +1003,7 @@
|
||||||
(.getConnection this) opts)))
|
(.getConnection this) opts)))
|
||||||
{:next.jdbc/update-count (.getUpdateCount this)}))
|
{:next.jdbc/update-count (.getUpdateCount this)}))
|
||||||
(-execute-all [this sql-params opts]
|
(-execute-all [this sql-params opts]
|
||||||
(assert (= 1 (count sql-params))
|
(validate (= 1 (count sql-params))
|
||||||
"Parameters cannot be provided when executing a non-prepared Statement")
|
"Parameters cannot be provided when executing a non-prepared Statement")
|
||||||
(if (:multi-rs opts)
|
(if (:multi-rs opts)
|
||||||
(loop [go (.execute this (first sql-params)) acc []]
|
(loop [go (.execute this (first sql-params)) acc []]
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,9 @@
|
||||||
[key-map opts]
|
[key-map opts]
|
||||||
(as-cols (keys key-map) opts))
|
(as-cols (keys key-map) opts))
|
||||||
|
|
||||||
|
(defn- validate [expr ^String msg]
|
||||||
|
(when-not expr (throw (IllegalArgumentException. msg))))
|
||||||
|
|
||||||
(defn by-keys
|
(defn by-keys
|
||||||
"Given a hash map of column names and values and a clause type
|
"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.
|
(`:set`, `:where`), return a vector of a SQL clause and its parameters.
|
||||||
|
|
@ -84,7 +87,7 @@
|
||||||
[(conj conds (str e " = ?")) (conj params v)])))
|
[(conj conds (str e " = ?")) (conj params v)])))
|
||||||
[[] []]
|
[[] []]
|
||||||
key-map)]
|
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)) " "
|
(into [(str (str/upper-case (safe-name clause)) " "
|
||||||
(str/join (if (= :where clause) " AND " ", ") where))]
|
(str/join (if (= :where clause) " AND " ", ") where))]
|
||||||
params)))
|
params)))
|
||||||
|
|
@ -122,7 +125,7 @@
|
||||||
(let [entity-fn (:table-fn opts identity)
|
(let [entity-fn (:table-fn opts identity)
|
||||||
params (as-keys key-map opts)
|
params (as-keys key-map opts)
|
||||||
places (as-? 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))
|
(into [(str "INSERT INTO " (entity-fn (safe-name table))
|
||||||
" (" params ")"
|
" (" params ")"
|
||||||
" VALUES (" places ")"
|
" VALUES (" places ")"
|
||||||
|
|
@ -144,11 +147,11 @@
|
||||||
If `:suffix` is provided in `opts`, that string is appended to the
|
If `:suffix` is provided in `opts`, that string is appended to the
|
||||||
`INSERT ...` statement."
|
`INSERT ...` statement."
|
||||||
[table cols rows opts]
|
[table cols rows opts]
|
||||||
(assert (apply = (count cols) (map count rows))
|
(validate (apply = (count cols) (map count rows))
|
||||||
"column counts are not consistent across cols and rows")
|
"column counts are not consistent across cols and rows")
|
||||||
;; to avoid generating bad SQL
|
;; to avoid generating bad SQL
|
||||||
(assert (seq cols) "cols may not be empty")
|
(validate (seq cols) "cols may not be empty")
|
||||||
(assert (seq rows) "rows may not be empty")
|
(validate (seq rows) "rows may not be empty")
|
||||||
(let [table-fn (:table-fn opts identity)
|
(let [table-fn (:table-fn opts identity)
|
||||||
batch? (:batch opts)
|
batch? (:batch opts)
|
||||||
params (as-cols cols opts)
|
params (as-cols cols opts)
|
||||||
|
|
@ -195,7 +198,7 @@
|
||||||
[order-by opts]
|
[order-by opts]
|
||||||
(when-not (vector? order-by)
|
(when-not (vector? order-by)
|
||||||
(throw (IllegalArgumentException. ":order-by must be a vector")))
|
(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 "ORDER BY "
|
||||||
(str/join ", " (map #(for-order-col % opts) order-by))))
|
(str/join ", " (map #(for-order-col % opts) order-by))))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -158,7 +158,7 @@
|
||||||
|
|
||||||
(deftest test-for-update
|
(deftest test-for-update
|
||||||
(testing "empty example (would be a SQL error)"
|
(testing "empty example (would be a SQL error)"
|
||||||
(is (thrown? AssertionError ; changed in #44
|
(is (thrown? IllegalArgumentException
|
||||||
(builder/for-update :user
|
(builder/for-update :user
|
||||||
{:status 42}
|
{:status 42}
|
||||||
{}
|
{}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue