(ns clojure-sql.core (:refer-clojure :exclude [sort take drop]) (:require [clojure.set :as set] [clojure-sql.compiler :as c] [clojure-sql.dsl :as d] [clojure-sql.query :as q] [clojure-sql.util :as u] [clojure.walk])) (defmacro pull [ns & vlist] `(do ~@(for [i vlist :let [sym (symbol (name ns) (name i))]] `(def ~(with-meta i (u/map-vals (fn [x] `(quote ~x)) (meta (resolve sym)))) ~sym)) nil)) (pull clojure-sql.dsl table join project rename select group having sort take drop) (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) (defmethod print-method clojure_sql.query.Query [query writer] (binding [*out* writer] (pr (c/compile-select *database-type* query)))) (defn run-query [query] (assert *query-executor* "Cannot execute a query without a query executor") (*query-executor* :select (c/compile-select *database-type* query))) (defn insert! [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))) (defn 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))) (defn delete! [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)