From fe513564989d9151a79d5494f2958ae190c20d02 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Tue, 12 Nov 2013 16:58:44 +1100 Subject: Change the query executor model: now it's query local and the interface is managed by a protocol. --- src/clojure_sql/core.clj | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) (limited to 'src/clojure_sql/core.clj') diff --git a/src/clojure_sql/core.clj b/src/clojure_sql/core.clj index 2a9e77f..415c7f4 100644 --- a/src/clojure_sql/core.clj +++ b/src/clojure_sql/core.clj @@ -22,40 +22,27 @@ select group sort take drop - union intersection) - - -(def ^:private ^:dynamic *database-type* nil) -(defn set-database-type! [new-type] - (alter-var-root #'*database-type* (constantly new-type)) - nil) - -(def ^:private ^:dynamic *query-executor* (comp second vector)) -(defn set-query-executor! [exec-fn] - (alter-var-root #'*query-executor* (constantly exec-fn)) - nil) - - + union intersection + set-default-query-executor!) (defmethod print-method clojure_sql.query.Query [query writer] (binding [*out* writer] - (pr (c/compile-select *database-type* query)))) + (pr (c/compile-select nil query)))) (defn run-query "Run a select query. Return value is determined by query executor." [query] - (assert *query-executor* "Cannot execute a query without a query executor") - (*query-executor* :select (c/compile-select *database-type* query))) + (assert (:executor query) "Cannot execute a query without a query executor") + (q/query (:executor query) query)) (defn insert! "Insert a number of records into a table, setting each column to the corresponding value from the record. Return value is determined by query executor." [query & records] - (assert *query-executor* "Cannot execute a query without a query executor") - (let [compiled (apply c/compile-insert *database-type* query records)] - (*query-executor* :insert compiled))) + (assert (:executor query) "Cannot execute a query without a query executor") + (q/insert! (:executor query) query records)) (defn update! "Update everything which would have been selected by the query, @@ -67,16 +54,12 @@ with `project` before calling `update!`." [query partial-record] - (assert *query-executor* "Cannot execute a query without a query executor") - (let [compiled (c/compile-update *database-type* query partial-record)] - (*query-executor* :update compiled))) + (assert (:executor query) "Cannot execute a query without a query executor") + (q/update! (:executor query) query partial-record)) (defn delete! "Delete everything which would have been selected by the query. Return value is determined by query executor." [query] - (assert *query-executor* "Cannot execute a query without a query executor") - (let [compiled (c/compile-delete *database-type* query)] - (*query-executor* :delete compiled))) - -(q/set-query-deref-behaviour! run-query) + (assert (:executor query) "Cannot execute a query without a query executor") + (q/delete! (:executor query) query)) -- cgit v1.2.3