Renaming/cleanup
This commit is contained in:
parent
451a5fbd3b
commit
2cb3c20122
1 changed files with 48 additions and 43 deletions
|
|
@ -468,37 +468,42 @@
|
||||||
([connectable sql-params & [opts]]
|
([connectable sql-params & [opts]]
|
||||||
(-execute connectable sql-params opts)))
|
(-execute connectable sql-params opts)))
|
||||||
|
|
||||||
(defn query
|
(defn- into-map [row] (into {} row))
|
||||||
""
|
|
||||||
[connectable sql-params & [opts]]
|
|
||||||
(into []
|
|
||||||
(map (or (:row-fn opts) (partial into {})))
|
|
||||||
(execute! connectable sql-params opts)))
|
|
||||||
|
|
||||||
(defn query-one
|
(defn execute!
|
||||||
""
|
""
|
||||||
[connectable sql-params & [opts]]
|
([connectable sql-params] (execute! connectable sql-params {}))
|
||||||
(reduce (fn [_ row] (reduced ((or (:row-fn opts) (partial into {})) row)))
|
([connectable sql-params opts]
|
||||||
nil
|
(into []
|
||||||
(execute! connectable sql-params opts)))
|
(map (:row-fn opts into-map))
|
||||||
|
(reducible! connectable sql-params opts))))
|
||||||
|
|
||||||
(defn command!
|
(defn execute-one!
|
||||||
""
|
""
|
||||||
[connectable sql-params & [opts]]
|
([connectable sql-params] (execute-one! connectable sql-params {}))
|
||||||
(reduce + 0 (execute! connectable sql-params opts)))
|
([connectable sql-params opts]
|
||||||
|
(reduce (fn [_ row] (reduced ((:row-fn opts into-map) row)))
|
||||||
|
nil
|
||||||
|
(reducible! connectable sql-params opts))))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(def db-spec {:dbtype "h2:mem" :dbname "perf"})
|
(def db-spec {:dbtype "h2:mem" :dbname "perf"})
|
||||||
|
(def db-spec {:dbtype "derby" :dbname "perf" :create true})
|
||||||
|
(def db-spec {:dbtype "mysql" :dbname "worldsingles" :user "root" :password "visual"})
|
||||||
(def con db-spec)
|
(def con db-spec)
|
||||||
(def con (get-datasource db-spec))
|
(def con (get-datasource db-spec))
|
||||||
(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 {}))
|
||||||
(command! con ["DROP TABLE fruit"])
|
(execute! con ["DROP TABLE fruit"])
|
||||||
(command! con ["CREATE TABLE fruit (id int default 0, name varchar(32) primary key, appearance varchar(32), cost int, grade real)"])
|
;; h2
|
||||||
(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)"])
|
(execute! con ["CREATE TABLE fruit (id int default 0, name varchar(32) primary key, appearance varchar(32), cost int, grade real)"])
|
||||||
|
;; mysql
|
||||||
|
(execute! con ["CREATE TABLE fruit (id int auto_increment, name varchar(32), appearance varchar(32), cost int, grade real, primary key (id))"])
|
||||||
|
(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)"])
|
||||||
|
(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)"]
|
||||||
|
{:return-keys true})
|
||||||
|
|
||||||
(println con)
|
|
||||||
(.close con)
|
(.close con)
|
||||||
|
|
||||||
(require '[criterium.core :refer [bench quick-bench]])
|
(require '[criterium.core :refer [bench quick-bench]])
|
||||||
|
|
@ -509,7 +514,7 @@
|
||||||
;; raw java
|
;; raw java
|
||||||
(defn select* [^Connection con]
|
(defn select* [^Connection con]
|
||||||
(let [ps (doto (.prepareStatement con "SELECT * FROM fruit WHERE appearance = ?")
|
(let [ps (doto (.prepareStatement con "SELECT * FROM fruit WHERE appearance = ?")
|
||||||
(.setObject 1 "red"))
|
(.setObject 1 "red"))
|
||||||
rs (.executeQuery ps)
|
rs (.executeQuery ps)
|
||||||
_ (.next rs)
|
_ (.next rs)
|
||||||
value (.getObject rs "name")]
|
value (.getObject rs "name")]
|
||||||
|
|
@ -521,59 +526,59 @@
|
||||||
(quick-bench
|
(quick-bench
|
||||||
(reduce (fn [rs m] (reduced (:name m)))
|
(reduce (fn [rs m] (reduced (:name m)))
|
||||||
nil
|
nil
|
||||||
(execute! con ["select * from fruit where appearance = ?" "red"])))
|
(reducible! con ["select * from fruit where appearance = ?" "red"])))
|
||||||
(quick-bench
|
(quick-bench
|
||||||
(query-one con
|
(execute-one! con
|
||||||
["select * from fruit where appearance = ?" "red"]
|
["select * from fruit where appearance = ?" "red"]
|
||||||
{:row-fn :name}))
|
{:row-fn :name}))
|
||||||
|
|
||||||
;; simple query
|
;; simple query
|
||||||
(quick-bench
|
(quick-bench
|
||||||
(query con ["select * from fruit where appearance = ?" "red"]))
|
(execute! con ["select * from fruit where appearance = ?" "red"]))
|
||||||
|
|
||||||
;; with a prepopulated prepared statement
|
;; with a prepopulated prepared statement
|
||||||
(with-open [ps (prepare con ["select * from fruit where appearance = ?" "red"] {})]
|
(with-open [ps (prepare con ["select * from fruit where appearance = ?" "red"] {})]
|
||||||
(quick-bench
|
(quick-bench
|
||||||
[(reduce (fn [_ row] (reduced (:name row)))
|
[(reduce (fn [_ row] (reduced (:name row)))
|
||||||
nil
|
nil
|
||||||
(execute! ps))]))
|
(reducible! ps))]))
|
||||||
|
|
||||||
;; same as above but setting parameters inside the benchmark
|
;; same as above but setting parameters inside the benchmark
|
||||||
(with-open [ps (prepare con ["select * from fruit where appearance = ?"] {})]
|
(with-open [ps (prepare con ["select * from fruit where appearance = ?"] {})]
|
||||||
(quick-bench
|
(quick-bench
|
||||||
[(reduce (fn [_ row] (reduced (:name row)))
|
[(reduce (fn [_ row] (reduced (:name row)))
|
||||||
nil
|
nil
|
||||||
(execute! (set-parameters ps ["red"])))]))
|
(reducible! (set-parameters ps ["red"])))]))
|
||||||
|
|
||||||
;; this takes more than twice the time of the one above which seems strange
|
;; this takes more than twice the time of the one above which seems strange
|
||||||
(with-open [ps (prepare con ["select * from fruit where appearance = ?"] {})]
|
(with-open [ps (prepare con ["select * from fruit where appearance = ?"] {})]
|
||||||
(quick-bench
|
(quick-bench
|
||||||
[(reduce (fn [_ row] (reduced (:name row)))
|
[(reduce (fn [_ row] (reduced (:name row)))
|
||||||
nil
|
nil
|
||||||
(execute! (set-parameters ps ["red"])))
|
(reducible! (set-parameters ps ["red"])))
|
||||||
(reduce (fn [_ row] (reduced (:name row)))
|
(reduce (fn [_ row] (reduced (:name row)))
|
||||||
nil
|
nil
|
||||||
(execute! (set-parameters ps ["fuzzy"])))]))
|
(reducible! (set-parameters ps ["fuzzy"])))]))
|
||||||
|
|
||||||
;; full first row
|
;; full first row
|
||||||
(quick-bench
|
(quick-bench
|
||||||
(query-one con ["select * from fruit where appearance = ?" "red"]))
|
(execute-one! con ["select * from fruit where appearance = ?" "red"]))
|
||||||
|
|
||||||
;; test assoc works
|
;; test assoc works
|
||||||
(query-one con
|
(execute-one! con
|
||||||
["select * from fruit where appearance = ?" "red"]
|
["select * from fruit where appearance = ?" "red"]
|
||||||
{:row-fn #(assoc % :test :value)})
|
{:row-fn #(assoc % :test :value)})
|
||||||
;; test assoc works
|
;; test assoc works
|
||||||
(query con
|
(execute! con
|
||||||
["select * from fruit where appearance = ?" "red"]
|
["select * from fruit where appearance = ?" "red"]
|
||||||
{:row-fn #(assoc % :test :value)})
|
{:row-fn #(assoc % :test :value)})
|
||||||
|
|
||||||
(in-transaction [t con {:rollback-only? true}]
|
(with-transaction [t con {:rollback-only? true}]
|
||||||
(command! t ["INSERT INTO fruit (id,name,appearance,cost,grade) VALUES (5,'Pear','green',49,47)"])
|
(execute! t ["INSERT INTO fruit (id,name,appearance,cost,grade) VALUES (5,'Pear','green',49,47)"])
|
||||||
(query t ["select * from fruit where name = ?" "Pear"]))
|
(execute! t ["select * from fruit where name = ?" "Pear"]))
|
||||||
(query con ["select * from fruit where name = ?" "Pear"])
|
(execute! con ["select * from fruit where name = ?" "Pear"])
|
||||||
|
|
||||||
(in-transaction [t con {:rollback-only? false}]
|
|
||||||
(command! t ["INSERT INTO fruit (id,name,appearance,cost,grade) VALUES (5,'Pear','green',49,47)"])
|
(require '[clojure.java.jdbc :as j])
|
||||||
(query t ["select * from fruit where name = ?" "Pear"]))
|
(j/insert! db-spec :fruit {:id 6, :name "Kiwi", :appearance "Hairy", :cost 99, :grade 66.6})
|
||||||
(query con ["select * from fruit where name = ?" "Pear"]))
|
(require '[compliment.core]))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue