From d6c4a295a6458bb9243c2ba8008ad1c0c559ee15 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Thu, 20 Jun 2013 17:48:07 +1000 Subject: Make core nicer, pass the "type" of query to executors --- src/clojure_sql/core.clj | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/clojure_sql/core.clj b/src/clojure_sql/core.clj index 5380c08..f10d968 100644 --- a/src/clojure_sql/core.clj +++ b/src/clojure_sql/core.clj @@ -12,15 +12,12 @@ (alter-var-root #'*database-type* (constantly new-type)) nil) -(def ^:private ^:dynamic *query-executor* nil) +(def ^:private ^:dynamic *query-executor* (comp second vector)) (defn set-query-executor! [exec-fn] (alter-var-root #'*query-executor* (constantly exec-fn)) nil) -(q/set-query-deref-behaviour! #(let [compiled (c/compile *database-type* %)] - (if *query-executor* - (*query-executor* compiled) - compiled))) + (defmethod print-method clojure_sql.query.Query [query writer] (binding [*out* writer] @@ -40,23 +37,26 @@ ;(def drop #'d/drop) -(def insert! (fn [query & records] - (let [compiled (apply c/insert! *database-type* query records)] - (if *query-executor* - (*query-executor* compiled) - compiled)))) +(defn run-query [query] + (assert *query-executor* "Cannot execute a query without a query executor") + (*query-executor* :query (c/compile *database-type* query))) + +(defn insert! [query & records] + (assert *query-executor* "Cannot execute a query without a query executor") + (let [compiled (apply c/insert! *database-type* query records)] + (*query-executor* :insert compiled))) + +(defn update! [query partial-record] + (assert *query-executor* "Cannot execute a query without a query executor") + (let [compiled (c/update! *database-type* query partial-record)] + (*query-executor* :update compiled))) -(def update! (fn [query partial-record] - (let [compiled (c/update! *database-type* query partial-record)] - (if *query-executor* - (*query-executor* compiled) - compiled)))) +(defn delete! [query] + (assert *query-executor* "Cannot execute a query without a query executor") + (let [compiled (c/delete! *database-type* query)] + (*query-executor* :delete compiled))) -(def delete! (fn [query] - (let [compiled (c/delete! *database-type* query)] - (if *query-executor* - (*query-executor* compiled) - compiled)))) +(q/set-query-deref-behaviour! run-query) (let [users (-> (table :users) (project [:id :username :password-hash]))] -- cgit v1.2.3