comparison demo/batch.ur @ 649:96ebc6bdb5a0

Batch example
author Adam Chlipala <adamc@hcoop.net>
date Tue, 10 Mar 2009 15:17:23 -0400
parents
children 1a317a707d71
comparison
equal deleted inserted replaced
648:3c6d535d3d8b 649:96ebc6bdb5a0
1 datatype list t = Nil | Cons of t * list t
2
3 table t : {Id : int, A : string}
4
5 fun allRows () =
6 query (SELECT * FROM t)
7 (fn r acc => return (Cons ((r.T.Id, r.T.A), acc)))
8 Nil
9
10 fun doBatch ls =
11 case ls of
12 Nil => return ()
13 | Cons ((id, a), ls') =>
14 dml (INSERT INTO t (Id, A) VALUES ({[id]}, {[a]}));
15 doBatch ls'
16
17 fun del id =
18 dml (DELETE FROM t WHERE t.Id = {[id]})
19
20 fun show withDel lss =
21 let
22 fun show' ls =
23 case ls of
24 Nil => <xml/>
25 | Cons ((id, a), ls) => <xml>
26 <tr><td>{[id]}</td> <td>{[a]}</td> {if withDel then
27 <xml><td><button value="Delete" onclick={del id}/></td></xml>
28 else
29 <xml/>} </tr>
30 {show' ls}
31 </xml>
32 in
33 <xml><dyn signal={ls <- signal lss; return <xml><table>
34 <tr> <th>Id</th> <th>A</th> </tr>
35 {show' ls}
36 </table></xml>}/></xml>
37 end
38
39 fun main () =
40 lss <- source Nil;
41 batched <- source Nil;
42
43 id <- source "";
44 a <- source "";
45
46 let
47 fun add () =
48 id <- get id;
49 a <- get a;
50 ls <- get batched;
51
52 set batched (Cons ((readError id, a), ls))
53
54 fun exec () =
55 ls <- get batched;
56
57 doBatch ls;
58 set batched Nil
59 in
60 return <xml><body>
61 <h2>Rows</h2>
62
63 {show True lss}
64
65 <button value="Update" onclick={ls <- allRows (); set lss ls}/><br/>
66 <br/>
67
68 <h2>Batch new rows to add</h2>
69
70 <table>
71 <tr> <th>Id:</th> <td><ctextbox source={id}/></td> </tr>
72 <tr> <th>A:</th> <td><ctextbox source={a}/></td> </tr>
73 <tr> <th/> <td><button value="Batch it" onclick={add ()}/></td> </tr>
74 </table>
75
76 <h2>Already batched:</h2>
77 {show False batched}
78 <button value="Execute" onclick={exec ()}/>
79 </body></xml>
80 end