Mercurial > urweb
comparison demo/crud.ur @ 637:24fd1edfcaa3
Kind-polymorphic [fst] and friends
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 26 Feb 2009 16:16:54 -0500 |
parents | 6c4643880df5 |
children | 1fb318c17546 |
comparison
equal
deleted
inserted
replaced
636:de8333ef1a0c | 637:24fd1edfcaa3 |
---|---|
33 functor Make(M : sig | 33 functor Make(M : sig |
34 con cols :: {(Type * Type)} | 34 con cols :: {(Type * Type)} |
35 constraint [Id] ~ cols | 35 constraint [Id] ~ cols |
36 val fl : folder cols | 36 val fl : folder cols |
37 | 37 |
38 val tab : sql_table ([Id = int] ++ map fstTT cols) | 38 val tab : sql_table ([Id = int] ++ map fst cols) |
39 | 39 |
40 val title : string | 40 val title : string |
41 | 41 |
42 val cols : colsMeta cols | 42 val cols : colsMeta cols |
43 end) = struct | 43 end) = struct |
47 | 47 |
48 sequence seq | 48 sequence seq |
49 | 49 |
50 fun list () = | 50 fun list () = |
51 rows <- queryX (SELECT * FROM tab AS T) | 51 rows <- queryX (SELECT * FROM tab AS T) |
52 (fn (fs : {T : $([Id = int] ++ map fstTT M.cols)}) => <xml> | 52 (fn (fs : {T : $([Id = int] ++ map fst M.cols)}) => <xml> |
53 <tr> | 53 <tr> |
54 <td>{[fs.T.Id]}</td> | 54 <td>{[fs.T.Id]}</td> |
55 {foldRX2 [fstTT] [colMeta] [tr] | 55 {foldRX2 [fst] [colMeta] [tr] |
56 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) | 56 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) |
57 [[nm] ~ rest] v col => <xml> | 57 [[nm] ~ rest] v col => <xml> |
58 <td>{col.Show v}</td> | 58 <td>{col.Show v}</td> |
59 </xml>) | 59 </xml>) |
60 [M.cols] M.fl (fs.T -- #Id) M.cols} | 60 [M.cols] M.fl (fs.T -- #Id) M.cols} |
79 </table> | 79 </table> |
80 | 80 |
81 <br/><hr/><br/> | 81 <br/><hr/><br/> |
82 | 82 |
83 <form> | 83 <form> |
84 {foldR [colMeta] [fn cols :: {(Type * Type)} => xml form [] (map sndTT cols)] | 84 {foldR [colMeta] [fn cols :: {(Type * Type)} => xml form [] (map snd cols)] |
85 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) | 85 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) |
86 [[nm] ~ rest] (col : colMeta t) (acc : xml form [] (map sndTT rest)) => <xml> | 86 [[nm] ~ rest] (col : colMeta t) (acc : xml form [] (map snd rest)) => <xml> |
87 <li> {cdata col.Nam}: {col.Widget [nm]}</li> | 87 <li> {cdata col.Nam}: {col.Widget [nm]}</li> |
88 {useMore acc} | 88 {useMore acc} |
89 </xml>) | 89 </xml>) |
90 <xml/> | 90 <xml/> |
91 [M.cols] M.fl M.cols} | 91 [M.cols] M.fl M.cols} |
92 | 92 |
93 <submit action={create}/> | 93 <submit action={create}/> |
94 </form> | 94 </form> |
95 </xml> | 95 </xml> |
96 | 96 |
97 and create (inputs : $(map sndTT M.cols)) = | 97 and create (inputs : $(map snd M.cols)) = |
98 id <- nextval seq; | 98 id <- nextval seq; |
99 dml (insert tab | 99 dml (insert tab |
100 (foldR2 [sndTT] [colMeta] | 100 (foldR2 [snd] [colMeta] |
101 [fn cols => $(map (fn t :: (Type * Type) => | 101 [fn cols => $(map (fn t :: (Type * Type) => |
102 sql_exp [] [] [] t.1) cols)] | 102 sql_exp [] [] [] t.1) cols)] |
103 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) | 103 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) |
104 [[nm] ~ rest] => | 104 [[nm] ~ rest] => |
105 fn input col acc => acc ++ {nm = @sql_inject col.Inject (col.Parse input)}) | 105 fn input col acc => acc ++ {nm = @sql_inject col.Inject (col.Parse input)}) |
112 {ls} | 112 {ls} |
113 </body></xml> | 113 </body></xml> |
114 | 114 |
115 and upd (id : int) = | 115 and upd (id : int) = |
116 let | 116 let |
117 fun save (inputs : $(map sndTT M.cols)) = | 117 fun save (inputs : $(map snd M.cols)) = |
118 dml (update [map fstTT M.cols] ! | 118 dml (update [map fst M.cols] ! |
119 (foldR2 [sndTT] [colMeta] | 119 (foldR2 [snd] [colMeta] |
120 [fn cols => $(map (fn t :: (Type * Type) => | 120 [fn cols => $(map (fn t :: (Type * Type) => |
121 sql_exp [T = [Id = int] | 121 sql_exp [T = [Id = int] |
122 ++ map fstTT M.cols] | 122 ++ map fst M.cols] |
123 [] [] t.1) cols)] | 123 [] [] t.1) cols)] |
124 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) | 124 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) |
125 [[nm] ~ rest] => | 125 [[nm] ~ rest] => |
126 fn input col acc => acc ++ {nm = | 126 fn input col acc => acc ++ {nm = |
127 @sql_inject col.Inject (col.Parse input)}) | 127 @sql_inject col.Inject (col.Parse input)}) |
132 <p>Saved!</p> | 132 <p>Saved!</p> |
133 | 133 |
134 {ls} | 134 {ls} |
135 </body></xml> | 135 </body></xml> |
136 in | 136 in |
137 fso <- oneOrNoRows (SELECT tab.{{map fstTT M.cols}} FROM tab WHERE tab.Id = {[id]}); | 137 fso <- oneOrNoRows (SELECT tab.{{map fst M.cols}} FROM tab WHERE tab.Id = {[id]}); |
138 case fso : (Basis.option {Tab : $(map fstTT M.cols)}) of | 138 case fso : (Basis.option {Tab : $(map fst M.cols)}) of |
139 None => return <xml><body>Not found!</body></xml> | 139 None => return <xml><body>Not found!</body></xml> |
140 | Some fs => return <xml><body><form> | 140 | Some fs => return <xml><body><form> |
141 {foldR2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (map sndTT cols)] | 141 {foldR2 [fst] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (map snd cols)] |
142 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) | 142 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) |
143 [[nm] ~ rest] (v : t.1) (col : colMeta t) | 143 [[nm] ~ rest] (v : t.1) (col : colMeta t) |
144 (acc : xml form [] (map sndTT rest)) => | 144 (acc : xml form [] (map snd rest)) => |
145 <xml> | 145 <xml> |
146 <li> {cdata col.Nam}: {col.WidgetPopulated [nm] v}</li> | 146 <li> {cdata col.Nam}: {col.WidgetPopulated [nm] v}</li> |
147 {useMore acc} | 147 {useMore acc} |
148 </xml>) | 148 </xml>) |
149 <xml/> | 149 <xml/> |