summaryrefslogtreecommitdiff
path: root/src/clojure_sql/core.clj
blob: 4f88a535fb0cc37e2ab0239072a92ba4eb44721a (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(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"))))