(ns clojure-sql.core (:refer-clojure :exclude [sort-by]) (: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) (q/set-query-deref-behaviour! #(c/compile *database-type* %)) (defmethod print-method clojure_sql.query.Query [query writer] (binding [*out* writer] (pr (c/compile nil query)))) (def table #'d/table) (def project #'d/project) (def rename #'d/rename) (def join #'d/join) (def select #'d/select) (def sort-by #'d/sort-by) (def insert! #'c/insert!) (def update! #'c/update!) (def delete! #'c/delete!) (comment (let [users (-> (table :users) (project [:id :username :password]) (select '(= :deleted false))) people (-> (table :people) (project [:id :fname :sname]) (select '(= :deleted false))) uid-pid-match '(= :uid :pid) is-carlo `(= :fname "Carlo'; SELECT * FROM users --")] (-> (join (-> users (rename {:id :uid})) (join (-> people (rename {:id :pid})) (-> (table {:others :o}) (project {:id :oid})) :on '(= :pid :oid)) :on uid-pid-match) (select is-carlo) (project [:fname :sname :oid]))) (-> (table :users) (project [:username]) (join (table :something-else-with-a-username)) (select '(or (= :username "john") (not (= :username "carlo")))) (project [:username])) (-> (table {:nodes :child}) (project [:parent-id, :name]) (rename {:name :child.name :parent-id :pid}) (join (-> (table {:nodes :parent}) (project [:id, :name]) (rename {:name :parent.name})) :on '(= :pid :id)) (project [:child.name :parent.name])) (-> (table :users) (project [:id]) (join (-> (table :people) (project [:id])))) (-> (table :users) (project [:id :name]) (rename {:id :name :name :id})) (-> (table :users) (project {:id :name :name :id}) (select '(= :id 10))) (-> (table :anotherStack) (project [:anotherNumber]) (join (-> (table :collection) (project [:number])))) (-> (table :anotherStack) (project [:anotherNumber]) (join (-> (table :collection) (project [:number]))) (select '(is-okay 10))) (-> (table :users) (select '(= (left :username 1) "bloo"))))