switch asserts to validation / exceptions

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2024-12-01 13:44:38 -08:00
parent ecd950d009
commit 585a7120a9
No known key found for this signature in database
3 changed files with 30 additions and 24 deletions

View file

@ -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))))))
@ -978,7 +981,7 @@
java.sql.Statement
(-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")
(reify
clojure.lang.IReduceInit
@ -990,7 +993,7 @@
(.getConnection this) opts))
(toString [_] "`IReduceInit` from `plan` -- missing reduction?")))
(-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")
(if-let [rs (stmt-sql->result-set this (first sql-params))]
(let [builder-fn (get opts :builder-fn as-maps)
@ -1000,7 +1003,7 @@
(.getConnection this) opts)))
{:next.jdbc/update-count (.getUpdateCount this)}))
(-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")
(if (:multi-rs opts)
(loop [go (.execute this (first sql-params)) acc []]

View file

@ -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))
(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))))

View file

@ -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}
{}