summaryrefslogtreecommitdiff
path: root/demo/batch.ur
blob: b7007913d1781905b8d854aa8fcf9c8ea4d93319 (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
datatype list t = Nil | Cons of t * list t

table t : {Id : int, A : string}
  PRIMARY KEY Id

fun allRows () =
    query (SELECT * FROM t)
    (fn r acc => return (Cons ((r.T.Id, r.T.A), acc)))
    Nil

fun doBatch ls =
    case ls of
        Nil => return ()
      | Cons ((id, a), ls') =>
        dml (INSERT INTO t (Id, A) VALUES ({[id]}, {[a]}));
        doBatch ls'

fun del id =
    dml (DELETE FROM t WHERE t.Id = {[id]})

fun show withDel lss =
    let
        fun show' ls =
            case ls of
                Nil => <xml/>
              | Cons ((id, a), ls) => <xml>
                <tr><td>{[id]}</td> <td>{[a]}</td> {if withDel then
                                                        <xml><td><button value="Delete" onclick={del id}/></td></xml>
                                                    else
                                                        <xml/>} </tr>
                {show' ls}
              </xml>
    in
        <xml><dyn signal={ls <- signal lss; return <xml><table>
          <tr> <th>Id</th> <th>A</th> </tr>
          {show' ls}
        </table></xml>}/></xml>
    end

fun action () =
    lss <- source Nil;
    batched <- source Nil;

    id <- source "";
    a <- source "";

    let
        fun add () =
            id <- get id;
            a <- get a;
            ls <- get batched;

            set batched (Cons ((readError id, a), ls))

        fun exec () =
            ls <- get batched;

            doBatch ls;
            set batched Nil
    in
        return <xml><body>
          <h2>Rows</h2>

          {show True lss}

          <button value="Update" onclick={ls <- allRows (); set lss ls}/><br/>
          <br/>

          <h2>Batch new rows to add</h2>

          <table>
            <tr> <th>Id:</th> <td><ctextbox source={id}/></td> </tr>
            <tr> <th>A:</th> <td><ctextbox source={a}/></td> </tr>
            <tr> <th/> <td><button value="Batch it" onclick={add ()}/></td> </tr>
          </table>

          <h2>Already batched:</h2>
          {show False batched}
          <button value="Execute" onclick={exec ()}/>
        </body></xml>
    end

fun main () = return <xml><body>
  <form><submit value="Begin demo" action={action}/></form>
</body></xml>