summaryrefslogtreecommitdiff
path: root/src/clojure_sql
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-06-20 17:48:07 +1000
committerCarlo Zancanaro <carlo@clearboxsystems.com.au>2013-06-20 17:48:07 +1000
commitd6c4a295a6458bb9243c2ba8008ad1c0c559ee15 (patch)
treed9cbc097b8c982950e7eed541fbfef588afbf6dd /src/clojure_sql
parentb7669002c55b2615abef4239887d70fb09a3075b (diff)
Make core nicer, pass the "type" of query to executors
Diffstat (limited to 'src/clojure_sql')
-rw-r--r--src/clojure_sql/core.clj40
1 files 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]))]