(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])) (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)))) (def table #'d/table) (def project #'d/project) (def rename #'d/rename) (def join #'d/join) (def select #'d/select) (def group #'d/group) (def having #'d/having) (def sort #'d/sort) (def take #'d/take) (def drop #'d/drop) (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)