Drop options; add query-one/command! conveniences
This commit is contained in:
parent
781a6d19e1
commit
c1e8b351a2
1 changed files with 39 additions and 35 deletions
|
|
@ -42,8 +42,6 @@
|
||||||
(-execute ^clojure.lang.IReduceInit [this sql-params opts]))
|
(-execute ^clojure.lang.IReduceInit [this sql-params opts]))
|
||||||
(defprotocol Preparable
|
(defprotocol Preparable
|
||||||
(prepare ^PreparedStatement [this sql-params opts]))
|
(prepare ^PreparedStatement [this sql-params opts]))
|
||||||
(defprotocol WithOptions
|
|
||||||
(get-options [this]))
|
|
||||||
|
|
||||||
(defn execute!
|
(defn execute!
|
||||||
"General SQL execution function.
|
"General SQL execution function.
|
||||||
|
|
@ -53,13 +51,6 @@
|
||||||
([connectable sql-params & [opts]]
|
([connectable sql-params & [opts]]
|
||||||
(-execute connectable sql-params opts)))
|
(-execute connectable sql-params opts)))
|
||||||
|
|
||||||
(extend-protocol
|
|
||||||
WithOptions
|
|
||||||
Object
|
|
||||||
(get-options [_] {})
|
|
||||||
nil
|
|
||||||
(get-options [_] {}))
|
|
||||||
|
|
||||||
(defn set-parameters
|
(defn set-parameters
|
||||||
""
|
""
|
||||||
^PreparedStatement
|
^PreparedStatement
|
||||||
|
|
@ -297,9 +288,7 @@
|
||||||
(get-driver-connection url
|
(get-driver-connection url
|
||||||
(assoc etc
|
(assoc etc
|
||||||
:username username
|
:username username
|
||||||
:password password)))
|
:password password)))))
|
||||||
WithOptions
|
|
||||||
(get-options [_] opts)))
|
|
||||||
|
|
||||||
(extend-protocol Sourceable
|
(extend-protocol Sourceable
|
||||||
clojure.lang.Associative
|
clojure.lang.Associative
|
||||||
|
|
@ -311,9 +300,7 @@
|
||||||
(getConnection [_]
|
(getConnection [_]
|
||||||
(.getConnection this))
|
(.getConnection this))
|
||||||
(getConnection [_ username password]
|
(getConnection [_ username password]
|
||||||
(.getConnection this username password))
|
(.getConnection this username password))))
|
||||||
WithOptions
|
|
||||||
(get-options [_] opts)))
|
|
||||||
String
|
String
|
||||||
(get-datasource [this opts]
|
(get-datasource [this opts]
|
||||||
(url+etc->datasource (string->url+etc this) opts)))
|
(url+etc->datasource (string->url+etc this) opts)))
|
||||||
|
|
@ -330,9 +317,7 @@
|
||||||
(prepare* this sql-params opts))
|
(prepare* this sql-params opts))
|
||||||
DataSource
|
DataSource
|
||||||
(prepare [this sql-params opts]
|
(prepare [this sql-params opts]
|
||||||
(prepare (.getConnection this)
|
(prepare (.getConnection this) sql-params opts))
|
||||||
sql-params
|
|
||||||
(merge (get-options this) opts)))
|
|
||||||
Object
|
Object
|
||||||
(prepare [this sql-params opts]
|
(prepare [this sql-params opts]
|
||||||
(prepare (get-datasource this opts) sql-params opts)))
|
(prepare (get-datasource this opts) sql-params opts)))
|
||||||
|
|
@ -420,12 +405,11 @@
|
||||||
(reduce-stmt stmt f init)))))
|
(reduce-stmt stmt f init)))))
|
||||||
DataSource
|
DataSource
|
||||||
(-execute [this sql-params opts]
|
(-execute [this sql-params opts]
|
||||||
(let [opts (merge (get-options this) opts)]
|
|
||||||
(reify clojure.lang.IReduceInit
|
(reify clojure.lang.IReduceInit
|
||||||
(reduce [_ f init]
|
(reduce [_ f init]
|
||||||
(with-open [con (get-connection this)]
|
(with-open [con (get-connection this)]
|
||||||
(with-open [stmt (prepare con sql-params opts)]
|
(with-open [stmt (prepare con sql-params opts)]
|
||||||
(reduce-stmt stmt f init)))))))
|
(reduce-stmt stmt f init))))))
|
||||||
PreparedStatement
|
PreparedStatement
|
||||||
(-execute [this _ _]
|
(-execute [this _ _]
|
||||||
(reify clojure.lang.IReduceInit
|
(reify clojure.lang.IReduceInit
|
||||||
|
|
@ -437,7 +421,21 @@
|
||||||
(defn query
|
(defn query
|
||||||
""
|
""
|
||||||
[connectable sql-params & [opts]]
|
[connectable sql-params & [opts]]
|
||||||
(into [] (map (partial into {})) (execute! connectable sql-params opts)))
|
(into []
|
||||||
|
(map (or (:row-fn opts) (partial into {})))
|
||||||
|
(execute! connectable sql-params opts)))
|
||||||
|
|
||||||
|
(defn query-one
|
||||||
|
""
|
||||||
|
[connectable sql-params & [opts]]
|
||||||
|
(reduce (fn [_ row] (reduced ((or (:row-fn opts) (partial into {})) row)))
|
||||||
|
nil
|
||||||
|
(execute! connectable sql-params opts)))
|
||||||
|
|
||||||
|
(defn command!
|
||||||
|
""
|
||||||
|
[connectable sql-params & [opts]]
|
||||||
|
(reduce + 0 (execute! connectable sql-params opts)))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(def db-spec {:dbtype "h2:mem" :dbname "perf"})
|
(def db-spec {:dbtype "h2:mem" :dbname "perf"})
|
||||||
|
|
@ -446,12 +444,12 @@
|
||||||
(get-connection con)
|
(get-connection con)
|
||||||
(def con (get-connection (get-datasource db-spec {})))
|
(def con (get-connection (get-datasource db-spec {})))
|
||||||
(def con (get-connection db-spec))
|
(def con (get-connection db-spec))
|
||||||
(reduce + 0 (execute! con ["DROP TABLE fruit"]))
|
(command! con ["DROP TABLE fruit"])
|
||||||
(reduce + 0 (execute! con ["CREATE TABLE fruit (id int default 0, name varchar(32) primary key, appearance varchar(32), cost int, grade real)"]))
|
(command! con ["CREATE TABLE fruit (id int default 0, name varchar(32) primary key, appearance varchar(32), cost int, grade real)"])
|
||||||
(reduce + 0 (execute! con ["INSERT INTO fruit (id,name,appearance,cost,grade) VALUES (1,'Apple','red',59,87), (2,'Banana','yellow',29,92.2), (3,'Peach','fuzzy',139,90.0), (4,'Orange','juicy',89,88.6)"]))
|
(command! con ["INSERT INTO fruit (id,name,appearance,cost,grade) VALUES (1,'Apple','red',59,87), (2,'Banana','yellow',29,92.2), (3,'Peach','fuzzy',139,90.0), (4,'Orange','juicy',89,88.6)"])
|
||||||
|
|
||||||
(println con)
|
(println con)
|
||||||
(close con)
|
(.close con)
|
||||||
|
|
||||||
(require '[criterium.core :refer [bench quick-bench]])
|
(require '[criterium.core :refer [bench quick-bench]])
|
||||||
|
|
||||||
|
|
@ -463,6 +461,10 @@
|
||||||
(reduce (fn [rs m] (reduced (:name m)))
|
(reduce (fn [rs m] (reduced (:name m)))
|
||||||
nil
|
nil
|
||||||
(execute! con ["select * from fruit where appearance = ?" "red"])))
|
(execute! con ["select * from fruit where appearance = ?" "red"])))
|
||||||
|
(quick-bench
|
||||||
|
(query-one con
|
||||||
|
["select * from fruit where appearance = ?" "red"]
|
||||||
|
{:row-fn :name}))
|
||||||
|
|
||||||
;; simple query
|
;; simple query
|
||||||
(quick-bench
|
(quick-bench
|
||||||
|
|
@ -494,11 +496,13 @@
|
||||||
|
|
||||||
;; full first row
|
;; full first row
|
||||||
(quick-bench
|
(quick-bench
|
||||||
(reduce (fn [rs m] (reduced (into {} m)))
|
(query-one con ["select * from fruit where appearance = ?" "red"]))
|
||||||
nil
|
|
||||||
(execute! con ["select * from fruit where appearance = ?" "red"])))
|
|
||||||
|
|
||||||
;; test assoc works
|
;; test assoc works
|
||||||
(reduce (fn [rs m] (reduced (assoc m :test :value)))
|
(query-one con
|
||||||
nil
|
["select * from fruit where appearance = ?" "red"]
|
||||||
(execute! con ["select * from fruit where appearance = ?" "red"])))
|
{:row-fn #(assoc % :test :value)})
|
||||||
|
;; test assoc works
|
||||||
|
(query con
|
||||||
|
["select * from fruit where appearance = ?" "red"]
|
||||||
|
{:row-fn #(assoc % :test :value)}))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue