-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.clj
65 lines (59 loc) · 2.48 KB
/
builder.clj
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
(ns ez-database.builder
(:require [ez-database.query :as query]
[honeysql.helpers :as sql.helpers]))
(defn- build-equality [constraint data]
(let [[eq col k] (if (= (count constraint) 3)
constraint
(into [:=] constraint))
[k col] (cond (nil? k)
[col col]
(nil? col)
[k k]
:else
[k col])
d (get data k)]
(if d
[eq col (get data k)]
nil)))
(defn- build-where [constraints data]
(reduce (fn [out constraint]
(if-let [eq (build-equality constraint data)]
(conj out eq)
out))
[:and] constraints))
(defn- get-base-query [{:keys [operation table values value select order-by] :or {select [:*]}}]
(case operation
:delete {:delete-from table}
:insert {:insert-into table :values values}
:update {:update table :set value}
:select (merge {:select select :from [table]}
(if order-by
{:order-by order-by}))
(throw (ex-info "Unknown operation" {:operation operation}))))
(defn- select? [composition]
(= :select (:operation composition)))
(defn- insert? [composition]
(= :insert (:operation composition)))
(defn simple-query-builder [composition]
(let [{:keys [constraints]} composition]
(assert (set? constraints) "constraints needs to be a set of tuples of keywords")
(fn [data]
(let [base-query (get-base-query (merge composition data))
where-constraints (if (not (insert? composition))
(build-where constraints data))]
(-> base-query
(query/swap (not= [:and] where-constraints)
(sql.helpers/where where-constraints))
(query/swap (and (contains? data :limit)
(select? composition))
(sql.helpers/limit (get data :limit)))
(query/swap (and (contains? data :offset)
(select? composition))
(sql.helpers/offset (get data :offset)))
(query/swap (and (contains? data :order-by)
(select? composition))
((fn [q]
(apply sql.helpers/order-by q (get data :order-by)))))
(query/clean))))))
(defmacro defsimplequery [query-name composition]
`(def ~query-name (simple-query-builder ~composition)))