-
Notifications
You must be signed in to change notification settings - Fork 16
/
parameterizedQuery.ur
257 lines (226 loc) · 10.1 KB
/
parameterizedQuery.ur
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
open Bootstrap
signature S = sig
con params :: {(Type * Type * Type)}
val widgets : $(map Widget.t' params)
val paramsFl : folder params
val paramLabels : $(map (fn _ => string) params)
val authorized : transaction bool
end
functor Html(M : sig
include S
con results :: {(Type * Type * Type)}
val resultsFl : folder results
val resultLabels : $(map (fn _ => string) results)
val query : $(map fst3 params) -> transaction (sql_query [] [] [] (map fst3 results))
val resultWidgets : $(map Widget.t' results)
con buttons :: {Unit}
val buttonsFl : folder buttons
val onResult : option ($(map fst3 results) -> transaction unit)
end) = struct
open M
(* We put the button-function record in a source to ease server-to-client embedding. *)
type a = {Buttons : source $(mapU ($(map fst3 params) -> $(map fst3 results) -> string (* label *) * url) buttons),
Ids : $(map (fn _ => id) params),
Widgets : $(map snd3 params),
Results : source (list $(map fst3 results)),
RunningAction : source bool}
fun create bs =
bs <- source bs;
ids <- @Monad.mapR0 _ [fn _ => id]
(fn [nm ::_] [p ::_] => fresh)
paramsFl;
ws <- @Monad.mapR _ [Widget.t'] [snd3]
(fn [nm ::_] [p ::_] (w : Widget.t' p) =>
cfg <- @Widget.configure w;
@Widget.create w cfg)
paramsFl widgets;
rs <- (case @Row.isEmpty' [fn r => $(map fst3 r)] paramsFl of
None => return []
| Some cast => q <- query (cast ()); queryL q);
rs <- source rs;
ra <- source False;
return {Buttons = bs,
Ids = ids,
Widgets = ws,
Results = rs,
RunningAction = ra}
fun onload _ = return ()
fun runQuery vs =
b <- authorized;
if b then
q <- query vs;
queryL q
else
error <xml>Access denied</xml>
val runAction =
case onResult of
None => fn _ => return ()
| Some f => fn rs =>
b <- authorized;
if b then
List.app (fn v => case onResult of
None => return ()
| Some f => f v) rs
else
error <xml>Access denied</xml>
fun render _ self = <xml>
<table class="bs-table">
{@mapX4 [fn _ => string] [Widget.t'] [fn _ => id] [snd3] [tabl]
(fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (s : string) (w : Widget.t' p) (id : id) (c : p.2) =>
<xml><tr class="form-group">
<th><label class="control-label" for={id}>{[s]}</label></th>
<td>{@Widget.asWidget w c (Some id)}</td>
</tr></xml>)
paramsFl paramLabels widgets self.Ids self.Widgets}
</table>
{if @Row.isEmpty paramsFl then
<xml></xml>
else <xml>
<button class="btn btn-primary"
value="Search"
onclick={fn _ =>
vs <- @Monad.mapR2 _ [Widget.t'] [snd3] [fst3]
(fn [nm ::_] [p ::_] (w : Widget.t' p) (c : p.2) =>
current (@Widget.value w c))
paramsFl widgets self.Widgets;
rs <- rpc (runQuery vs);
set self.Results rs}/>
</xml>}
<hr/>
{case onResult of
None => <xml></xml>
| _ => <xml>
<dyn signal={rs <- signal self.Results;
case rs of
[] => return <xml></xml>
| _ =>
ra <- signal self.RunningAction;
return (if ra then
<xml>
<button class="btn btn-secondary disabled"
value="Running action..."/>
<hr/>
</xml>
else
<xml>
<button class="btn btn-secondary"
value="Run action on these results"
onclick={fn _ =>
sure <- confirm "Are you sure you want to run the action?";
if sure then
set self.RunningAction True;
rpc (runAction rs);
set self.RunningAction False
else
return ()}/>
<hr/>
</xml>)}/>
</xml>}
<table class="bs-table">
<thead>
<tr>
<dyn signal={bs <- signal self.Buttons;
return (@mapX [fn _ => $(map fst3 params) -> $(map fst3 results) -> string * url] [tr]
(fn [nm ::_] [u ::_] [r ::_] [[nm] ~ r] _ =>
<xml><th/></xml>)
buttonsFl bs)}/>
{@mapX [fn _ => string] [tr]
(fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (s : string) =>
<xml><th>{[s]}</th></xml>)
resultsFl resultLabels}
</tr>
</thead>
<tbody>
<dyn signal={vs <- @Monad.mapR2 _ [Widget.t'] [snd3] [fst3]
(fn [nm ::_] [p ::_] (w : Widget.t' p) (c : p.2) =>
@Widget.value w c)
paramsFl widgets self.Widgets;
bs <- signal self.Buttons;
rs <- signal self.Results;
return (List.mapX (fn row => <xml><tr>
{@mapX [fn _ => $(map fst3 params) -> $(map fst3 results) -> string * url] [tr]
(fn [nm ::_] [u ::_] [r ::_] [[nm] ~ r] f =>
let
val (label, link) = f vs row
in
<xml><td class="col-sm-1"><a class="btn btn-info" href={link}>{[label]}</a></td></xml>
end)
buttonsFl bs}
{@mapX2 [Widget.t'] [fst3] [tr]
(fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (w : Widget.t' p) (v : p.1) =>
<xml><td>{@Widget.asValue w v}</td></xml>)
resultsFl resultWidgets row}
</tr></xml>) rs)}/>
</tbody>
</table>
</xml>
fun notification _ _ = <xml></xml>
fun buttons _ _ = <xml></xml>
type input = _
fun ui bs = {Create = create bs,
Onload = onload,
Render = render,
Notification = notification,
Buttons = buttons}
end
functor Csv(M : sig
include S
con results :: {Type}
val resultsFl : folder results
val resultLabels : $(map (fn _ => string) results)
val query : $(map fst3 params) -> transaction (sql_query [] [] [] results)
val shows : $(map show results)
val filename : string
end) = struct
open M
type a = {Ids : $(map (fn _ => id) params),
Widgets : $(map snd3 params)}
fun generate ps () : transaction page =
q <- query ps;
csv <- @@Csv.buildComputed [results] resultsFl #"," shows resultLabels q;
setHeader (blessResponseHeader "Content-Disposition")
("attachment; filename=" ^ filename);
returnBlob (textBlob csv) (blessMime "text/csv")
val create =
ids <- @Monad.mapR0 _ [fn _ => id]
(fn [nm ::_] [p ::_] => fresh)
paramsFl;
ws <- @Monad.mapR _ [Widget.t'] [snd3]
(fn [nm ::_] [p ::_] (w : Widget.t' p) =>
cfg <- @Widget.configure w;
@Widget.create w cfg)
paramsFl widgets;
return {Ids = ids,
Widgets = ws}
fun onload _ = return ()
fun runQuery vs =
q <- query vs;
queryL q
fun render _ self = <xml>
<table class="bs-table">
{@mapX4 [fn _ => string] [Widget.t'] [fn _ => id] [snd3] [tabl]
(fn [nm ::_] [p ::_] [r ::_] [[nm] ~ r] (s : string) (w : Widget.t' p) (id : id) (c : p.2) =>
<xml><tr class="form-group">
<th><label class="control-label" for={id}>{[s]}</label></th>
<td>{@Widget.asWidget w c (Some id)}</td>
</tr></xml>)
paramsFl paramLabels widgets self.Ids self.Widgets}
</table>
<dyn signal={vs <- @Monad.mapR2 _ [Widget.t'] [snd3] [fst3]
(fn [nm ::_] [p ::_] (w : Widget.t' p) (c : p.2) =>
@Widget.value w c)
paramsFl widgets self.Widgets;
return <xml><form>
<submit class="btn btn-primary"
value="Search"
action={generate vs}/>
</form></xml>}/>
</xml>
fun notification _ _ = <xml></xml>
fun buttons _ _ = <xml></xml>
val ui = {Create = create,
Onload = onload,
Render = render,
Notification = notification,
Buttons = buttons}
end