(ns clojure-sql.core (:refer-clojure :exclude [sort]) (: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 *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* :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))) (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))) (q/set-query-deref-behaviour! run-query) (let [users (-> (table :users) (project [:id :username :password-hash]))] (insert! (-> users (project [:username])) {:username "john"} {:username "not-john"} {:username "bleep bloop"}))