Improve function naming in prepare ns

This commit is contained in:
Sean Corfield 2019-03-31 17:30:10 -07:00
parent 38017d720d
commit 31cbecf411
2 changed files with 16 additions and 16 deletions

View file

@ -44,7 +44,7 @@
[return-keys] [return-keys]
(into-array String return-keys)) (into-array String return-keys))
(defn pre-prepare* (defn ->factory
"Given a some options, return a statement factory -- a function that will "Given a some options, return a statement factory -- a function that will
accept a connection and a SQL string and parameters, and return a accept a connection and a SQL string and parameters, and return a
PreparedStatement representing that." PreparedStatement representing that."
@ -106,7 +106,7 @@
(fn [^Connection con ^String sql] (fn [^Connection con ^String sql]
(.setQueryTimeout ^PreparedStatement (f con sql) timeout))))) (.setQueryTimeout ^PreparedStatement (f con sql) timeout)))))
(defn prepare-fn* (defn create
"Given a connection, a SQL statement, its parameters, and a statement factory, "Given a connection, a SQL statement, its parameters, and a statement factory,
return a PreparedStatement representing that." return a PreparedStatement representing that."
^java.sql.PreparedStatement ^java.sql.PreparedStatement
@ -117,5 +117,5 @@
java.sql.Connection java.sql.Connection
(prepare [this sql-params opts] (prepare [this sql-params opts]
(let [[sql & params] sql-params (let [[sql & params] sql-params
factory (pre-prepare* opts)] factory (->factory opts)]
(set-parameters (factory this sql) params)))) (set-parameters (factory this sql) params))))

View file

@ -96,24 +96,24 @@
(extend-protocol p/Executable (extend-protocol p/Executable
java.sql.Connection java.sql.Connection
(-execute [this sql-params opts] (-execute [this sql-params opts]
(let [factory (prepare/pre-prepare* opts)] (let [factory (prepare/->factory opts)]
(reify clojure.lang.IReduceInit (reify clojure.lang.IReduceInit
(reduce [_ f init] (reduce [_ f init]
(with-open [stmt (prepare/prepare-fn* this (with-open [stmt (prepare/create this
(first sql-params) (first sql-params)
(rest sql-params) (rest sql-params)
factory)] factory)]
(reduce-stmt stmt f init opts)))))) (reduce-stmt stmt f init opts))))))
javax.sql.DataSource javax.sql.DataSource
(-execute [this sql-params opts] (-execute [this sql-params opts]
(let [factory (prepare/pre-prepare* opts)] (let [factory (prepare/->factory opts)]
(reify clojure.lang.IReduceInit (reify clojure.lang.IReduceInit
(reduce [_ f init] (reduce [_ f init]
(with-open [con (p/get-connection this opts)] (with-open [con (p/get-connection this opts)]
(with-open [stmt (prepare/prepare-fn* con (with-open [stmt (prepare/create con
(first sql-params) (first sql-params)
(rest sql-params) (rest sql-params)
factory)] factory)]
(reduce-stmt stmt f init opts))))))) (reduce-stmt stmt f init opts)))))))
java.sql.PreparedStatement java.sql.PreparedStatement
(-execute [this _ opts] (-execute [this _ opts]
@ -129,7 +129,7 @@
(declare navize-row) (declare navize-row)
(defn- row-into-map (defn datafiable-row
[connectable opts] [connectable opts]
(fn [row] (fn [row]
(into (with-meta {} {`core-p/datafy (navize-row connectable opts)}) row))) (into (with-meta {} {`core-p/datafy (navize-row connectable opts)}) row)))
@ -138,13 +138,13 @@
"" ""
[connectable sql-params opts] [connectable sql-params opts]
(into [] (into []
(map (or (:row-fn opts) (row-into-map connectable opts))) (map (or (:row-fn opts) (datafiable-row connectable opts)))
(p/-execute connectable sql-params opts))) (p/-execute connectable sql-params opts)))
(defn execute-one! (defn execute-one!
"" ""
[connectable sql-params opts] [connectable sql-params opts]
(let [row-fn (or (:row-fn opts) (row-into-map connectable opts))] (let [row-fn (or (:row-fn opts) (datafiable-row connectable opts))]
(reduce (fn [_ row] (reduce (fn [_ row]
(reduced (row-fn row))) (reduced (row-fn row)))
nil nil