comparison demo/broadcast.ur @ 699:4e260887d8f2

Chat demo
author Adam Chlipala <adamc@hcoop.net>
date Sun, 05 Apr 2009 11:48:55 -0400
parents
children 1a317a707d71
comparison
equal deleted inserted replaced
698:9b29ce0babb8 699:4e260887d8f2
1 functor Make(M : sig type t end) = struct
2 sequence s
3 table t : {Id : int, Client : client, Channel : channel M.t}
4
5 type topic = int
6
7 val inj : sql_injectable topic = _
8
9 val create = nextval s
10
11 fun subscribe id =
12 cli <- self;
13 ro <- oneOrNoRows (SELECT t.Channel FROM t WHERE t.Id = {[id]} AND t.Client = {[cli]});
14 case ro of
15 None =>
16 ch <- channel;
17 dml (INSERT INTO t (Id, Client, Channel) VALUES ({[id]}, {[cli]}, {[ch]}));
18 return ch
19 | Some r => return r.T.Channel
20
21 fun send id msg =
22 queryI (SELECT t.Channel FROM t WHERE t.Id = {[id]})
23 (fn r => Basis.send r.T.Channel msg)
24
25 fun subscribers id =
26 r <- oneRow (SELECT COUNT( * ) AS N FROM t WHERE t.Id = {[id]});
27 return r.N
28 end