comparison demo/more/orm.ur @ 988:d923b47e483d

Basic CRUD operations in Orm
author Adam Chlipala <adamc@hcoop.net>
date Mon, 05 Oct 2009 17:00:04 -0400
parents 6dd122f10c0c
children 0bdc4d538f1c
comparison
equal deleted inserted replaced
987:6dd122f10c0c 988:d923b47e483d
17 Inj = inj} 17 Inj = inj}
18 18
19 sequence s 19 sequence s
20 table t : ([Id = id] ++ M.cols) 20 table t : ([Id = id] ++ M.cols)
21 21
22 type row = $([Id = id] ++ M.cols)
23
24 fun ensql [avail] (r : $M.cols) : $(map (sql_exp avail [] []) M.cols) =
25 map2 [meta] [Top.id] [sql_exp avail [] []]
26 (fn [t] meta v => @sql_inject meta.Inj v)
27 [_] M.folder M.cols r
28
22 fun create (r : $M.cols) = 29 fun create (r : $M.cols) =
23 id <- nextval s; 30 id <- nextval s;
24 dml (insert t ({Id = sql_inject id} 31 dml (insert t ({Id = sql_inject id} ++ ensql r));
25 ++ map2 [meta] [Top.id] [sql_exp [] [] []] 32 return ({Id = id} ++ r)
26 (fn [t ::: Type] (meta : meta t) (v : t) => @sql_inject meta.Inj v) 33
27 [_] M.folder M.cols r)); 34 fun delete r = dml (DELETE FROM t WHERE t.Id = {[r.Id]})
28 return id 35
36 fun save r = dml (update [M.cols] ! (ensql (r -- #Id)) t (WHERE T.Id = {[r.Id]}))
37
38 fun lookup id =
39 ro <- oneOrNoRows (SELECT * FROM t WHERE t.Id = {[id]});
40 return (Option.mp (fn r => r.T) ro)
41
42 val list = query (SELECT * FROM t) (fn r ls => return (r.T :: ls)) []
29 end 43 end