-
Notifications
You must be signed in to change notification settings - Fork 4
/
sql.clj
284 lines (226 loc) · 6.13 KB
/
sql.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
(ns jaskell.sql
(:require [clojure.string :as str])
(:import (jaskell.script Directive)
(jaskell.sql Query Statement JDBCParameter)))
(def returning :returning)
(def as :as)
(def case :case)
(def when :when)
(def where :where)
(def join :join)
(def left :left)
(def right :right)
(def full :full)
(def cross :cross)
(def inner :inner)
(def outer :outer)
(def on :on)
(def using :using)
(def end :end)
(def do :do)
(def conflict :conflict)
(def nothing :nothing)
(def is :is)
(def null :null)
(def not :not)
(def recursive :recursive)
(def union :union)
(def all :all)
(def limit :limit)
(def offset :offset)
(def group :group)
(def order :order)
(def by :by)
(def having :having)
(defn partition-helper
[]
(let [state (atom 0)
last (atom "")]
(fn [token]
(let [l @last]
(if (and (= token all) (= l union))
(reset! last [:union :all])
(reset! last token))
(if (or (= token as) (= l as) (= token union) (= l union) (and (= l union) (= token all)) (= l [union all]))
@state
(swap! state inc))))))
(defn parse-statement
[tokens]
(.script (apply (first tokens) (rest tokens))))
(declare parse-comma)
(defn parse
[token]
(cond
(keyword? token) (name token)
(instance? Directive token) (.script token)
(vector? token) (parse-comma token)
(instance? String token) token
:else (str token)))
(defn parse-comma
[tokens]
(->> tokens
(partition-by (partition-helper))
(map #(->> %
(map parse)
(str/join " ")))
(str/join ", ")))
(defn extract
[token]
(cond
(instance? Directive token) (-> token (.parameters) vec)
:else []))
(defn select
[& tokens]
(proxy [Query] []
(script []
(str "select " (->> tokens (map parse) (str/join " "))))
(parameters []
(->> tokens (map extract) flatten vec))))
(def from :from)
(def where :where)
(defn by
[& tokens]
(proxy [Directive] []
(script []
(str "by (" (parse-comma tokens) ")"))
(parameters []
(->> tokens (map extract) flatten vec))))
(defn write-helper
[word tokens]
(if (= (-> tokens butlast last) :returning)
(proxy [Query] []
(script []
(str word " " (->> tokens (map parse) (str/join " "))))
(parameters []
(->> tokens (map extract) flatten vec)))
(proxy [Statement] []
(script []
(str word " " (->> tokens (map parse) (str/join " "))))
(parameters []
(->> tokens (map extract) flatten vec)))))
(defn insert
[& tokens]
(write-helper "insert" tokens))
(defn into
[table tokens]
(if tokens
(str "into " (parse table) "(" (str/join ", " (map parse tokens)) ")")
(str "into " (parse table))))
(defn values
[& tokens]
(proxy [Directive] []
(script []
(str "values(" (str/join ", " (map parse tokens)) ")"))
(parameters []
(->> tokens (map extract) flatten vec))))
(defn delete
[& tokens]
(write-helper "delete" tokens))
(defn set
[& tokens]
(proxy [Directive] []
(script []
(str "set " (->> tokens
(map parse)
(partition 3)
(map #(str/join " " %))
(str/join ", "))))
(parameters []
(->> tokens (map extract) flatten vec))))
(defn update
[& tokens]
(write-helper "update" tokens))
(defn with-query?
[tokens]
(let [select? (if (= (first tokens) :recursive)
(nth tokens 2)
(nth tokens 1))
returning? (last (butlast tokens))]
(or (= select select?) (= returning returning?))))
(defn parse-cte
[cte]
(->> cte
(partition-by (partition-helper))
(map #(str/join " " [(parse (first %)) (parse (second %)) (str "(" (parse (last %)) ")")]))
(str/join ", ")))
(defn parse-with
[tokens]
(let [recursive? (= recursive (first tokens))
head (if recursive? "with recursive" "with")
cte (if recursive? (second tokens) (first tokens))
main (if recursive? (drop 2 tokens) (rest tokens))]
(str head " " (parse-cte cte) " " (parse-statement main))))
(defn with
[& tokens]
(if (with-query? tokens)
(proxy [Query] []
(script []
(parse-with tokens))
(parameters []
(->> tokens (map extract) flatten vec)))
(proxy [Statement] []
(script []
(parse-with tokens))
(parameters []
(->> tokens (map extract) flatten vec)))))
(defn in
[& tokens]
(if (not-any? #(= (first tokens) %) [select delete update insert with])
(proxy [Directive] []
(script []
(str "in (" (->> tokens (map parse) (str/join ", ")) ")"))
(parameters []
(->> tokens (map extract) flatten vec)))
(let [^Query sub (apply (first tokens) (rest tokens))]
(proxy [Directive] []
(script []
(str "in (" (.script sub) ")"))
(parameters []
(.parameters sub))))))
(defn br
[& tokens]
(if (not-any? #(= (first tokens) %) [select delete update insert with])
(proxy [Directive] []
(script []
(str "(" (->> tokens (map parse) (str/join " ")) ")"))
(parameters []
(->> tokens (map extract) flatten vec)))
(let [^Query sub (apply (first tokens) (rest tokens))]
(proxy [Directive] []
(script []
(str "(" (.script sub) ")"))
(parameters []
(.parameters sub))))))
(defn t
[^String token]
(str "'" (str/replace token #"'" "''") "'"))
(defn q
[^String token]
(str "\"" (str/replace token #"\"" "\\\"") "\""))
(defn p
([key]
(JDBCParameter. key))
([key cls]
(JDBCParameter. key cls)))
(defn f
([name]
(proxy [Directive] []
(script []
(str (parse name) "()"))
(parameters []
[])))
([name & p]
(proxy [Directive] []
(script []
(str (parse name) "(" (->> p (map parse) (str/join ", ")) ")"))
(parameters []
(->> (concat name p) (map extract) flatten vec)))))
(defn t
[table fields]
(proxy [Directive] []
(parameters []
(-> (concat (extract table) (map extract fields))
flatten
vec))
(script []
(str (parse table) "(" (parse-comma fields) ")"))))