summaryrefslogtreecommitdiff
path: root/demo/refFun.ur
blob: a090b2975c6be019329d4a523dccd2aa50ff5e10 (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
functor Make(M : sig
                 type data
                 val inj : sql_injectable data
             end) = struct

    type ref = int

    sequence s
    table t : { Id : int, Data : M.data }

    fun new d =
        id <- nextval s;
        () <- dml (INSERT INTO t (Id, Data) VALUES ({id}, {d}));
        return id

    fun read r =
        o <- oneOrNoRows (SELECT t.Data FROM t WHERE t.Id = {r});
        return (case o of
            None => error <xml>You already deleted that ref!</xml>
          | Some r => r.T.Data)

    fun write r d =
        dml (UPDATE t SET Data = {d} WHERE Id = {r})

    fun delete r =
        dml (DELETE FROM t WHERE Id = {r})

end