adamc@339
|
1 con colMeta = fn t_formT :: (Type * Type) => {
|
adamc@339
|
2 Nam : string,
|
adamc@339
|
3 Show : t_formT.1 -> xbody,
|
adamc@339
|
4 Widget : nm :: Name -> xml form [] [nm = t_formT.2],
|
adamc@341
|
5 WidgetPopulated : nm :: Name -> t_formT.1 -> xml form [] [nm = t_formT.2],
|
adamc@339
|
6 Parse : t_formT.2 -> t_formT.1,
|
adamc@339
|
7 Inject : sql_injectable t_formT.1
|
adamc@339
|
8 }
|
adamc@339
|
9 con colsMeta = fn cols :: {(Type * Type)} => $(Top.mapT2T colMeta cols)
|
adamc@326
|
10
|
adamc@325
|
11 functor Make(M : sig
|
adamc@339
|
12 con cols :: {(Type * Type)}
|
adamc@325
|
13 constraint [Id] ~ cols
|
adamc@339
|
14 val tab : sql_table ([Id = int] ++ mapT2T fstTT cols)
|
adamc@325
|
15
|
adamc@325
|
16 val title : string
|
adamc@325
|
17
|
adamc@339
|
18 val cols : colsMeta cols
|
adamc@325
|
19 end) = struct
|
adamc@325
|
20
|
adamc@325
|
21 open constraints M
|
adamc@325
|
22 val tab = M.tab
|
adamc@325
|
23
|
adamc@339
|
24 sequence seq
|
adamc@339
|
25
|
adamc@339
|
26 fun create (inputs : $(mapT2T sndTT M.cols)) =
|
adamc@339
|
27 id <- nextval seq;
|
adamc@339
|
28 () <- dml (insert tab (foldT2R2 [sndTT] [colMeta]
|
adamc@339
|
29 [fn cols => $(mapT2T (fn t :: (Type * Type) =>
|
adamc@342
|
30 sql_exp [] [] [] t.1) cols)]
|
adamc@360
|
31 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@360
|
32 [[nm] ~ rest] =>
|
adamc@360
|
33 fn input col acc => acc with nm = sql_inject col.Inject (col.Parse input))
|
adamc@339
|
34 {} [M.cols] inputs M.cols
|
adamc@339
|
35 with #Id = (SQL {id})));
|
adamc@360
|
36 return <xml><body>
|
adamc@339
|
37 Inserted with ID {txt _ id}.
|
adamc@360
|
38 </body></xml>
|
adamc@339
|
39
|
adamc@342
|
40 fun save (id : int) (inputs : $(mapT2T sndTT M.cols)) =
|
adamc@342
|
41 () <- dml (update [mapT2T fstTT M.cols] (foldT2R2 [sndTT] [colMeta]
|
adamc@342
|
42 [fn cols => $(mapT2T (fn t :: (Type * Type) =>
|
adamc@342
|
43 sql_exp [T = [Id = int] ++ mapT2T fstTT M.cols] [] [] t.1) cols)]
|
adamc@360
|
44 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@360
|
45 [[nm] ~ rest] =>
|
adamc@360
|
46 fn input col acc => acc with nm = sql_inject col.Inject (col.Parse input))
|
adamc@342
|
47 {} [M.cols] inputs M.cols)
|
adamc@342
|
48 tab (WHERE T.Id = {id}));
|
adamc@360
|
49 return <xml><body>
|
adamc@342
|
50 Saved!
|
adamc@360
|
51 </body></xml>
|
adamc@341
|
52
|
adamc@341
|
53 fun update (id : int) =
|
adamc@341
|
54 fso <- oneOrNoRows (SELECT tab.{{mapT2T fstTT M.cols}} FROM tab WHERE tab.Id = {id});
|
adamc@341
|
55 case fso : (Basis.option {Tab : $(mapT2T fstTT M.cols)}) of
|
adamc@360
|
56 None => return <xml><body>Not found!</body></xml>
|
adamc@361
|
57 | Some fs => return <xml><body><form>
|
adamc@341
|
58 {foldT2R2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)]
|
adamc@360
|
59 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@360
|
60 [[nm] ~ rest] (v : t.1) (col : colMeta t)
|
adamc@360
|
61 (acc : xml form [] (mapT2T sndTT rest)) =>
|
adamc@360
|
62 <xml>
|
adamc@360
|
63 <li> {cdata col.Nam}: {col.WidgetPopulated [nm] v}</li>
|
adamc@360
|
64 {useMore acc}
|
adamc@360
|
65 </xml>)
|
adamc@360
|
66 <xml/>
|
adamc@341
|
67 [M.cols] fs.Tab M.cols}
|
adamc@341
|
68
|
adamc@341
|
69 <submit action={save id}/>
|
adamc@361
|
70 </form></body></xml>
|
adamc@341
|
71
|
adamc@337
|
72 fun delete (id : int) =
|
adamc@337
|
73 () <- dml (DELETE FROM tab WHERE Id = {id});
|
adamc@360
|
74 return <xml><body>
|
adamc@337
|
75 The deed is done.
|
adamc@360
|
76 </body></xml>
|
adamc@337
|
77
|
adamc@360
|
78 fun confirm (id : int) = return <xml><body>
|
adamc@337
|
79 <p>Are you sure you want to delete ID #{txt _ id}?</p>
|
adamc@337
|
80
|
adamc@337
|
81 <p><a link={delete id}>I was born sure!</a></p>
|
adamc@360
|
82 </body></xml>
|
adamc@337
|
83
|
adamc@346
|
84 fun main () =
|
adamc@334
|
85 rows <- queryX (SELECT * FROM tab AS T)
|
adamc@360
|
86 (fn (fs : {T : $([Id = int] ++ mapT2T fstTT M.cols)}) => <xml>
|
adamc@326
|
87 <tr>
|
adamc@326
|
88 <td>{txt _ fs.T.Id}</td>
|
adamc@339
|
89 {foldT2RX2 [fstTT] [colMeta] [tr]
|
adamc@360
|
90 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@360
|
91 [[nm] ~ rest] v col => <xml>
|
adamc@336
|
92 <td>{col.Show v}</td>
|
adamc@360
|
93 </xml>)
|
adamc@326
|
94 [M.cols] (fs.T -- #Id) M.cols}
|
adamc@341
|
95 <td><a link={update fs.T.Id}>[Update]</a> <a link={confirm fs.T.Id}>[Delete]</a></td>
|
adamc@326
|
96 </tr>
|
adamc@360
|
97 </xml>);
|
adamc@360
|
98 return <xml><head>
|
adamc@334
|
99 <title>{cdata M.title}</title>
|
adamc@325
|
100
|
adamc@325
|
101 </head><body>
|
adamc@325
|
102
|
adamc@334
|
103 <h1>{cdata M.title}</h1>
|
adamc@325
|
104
|
adamc@325
|
105 <table border={1}>
|
adamc@336
|
106 <tr>
|
adamc@336
|
107 <th>ID</th>
|
adamc@339
|
108 {foldT2RX [colMeta] [tr]
|
adamc@360
|
109 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@360
|
110 [[nm] ~ rest] col => <xml>
|
adamc@336
|
111 <th>{cdata col.Nam}</th>
|
adamc@360
|
112 </xml>)
|
adamc@336
|
113 [M.cols] M.cols}
|
adamc@336
|
114 </tr>
|
adamc@325
|
115 {rows}
|
adamc@325
|
116 </table>
|
adamc@339
|
117
|
adamc@339
|
118 <br/>
|
adamc@339
|
119
|
adamc@361
|
120 <form>
|
adamc@339
|
121 {foldT2R [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)]
|
adamc@360
|
122 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@360
|
123 [[nm] ~ rest] (col : colMeta t) (acc : xml form [] (mapT2T sndTT rest)) => <xml>
|
adamc@339
|
124 <li> {cdata col.Nam}: {col.Widget [nm]}</li>
|
adamc@339
|
125 {useMore acc}
|
adamc@360
|
126 </xml>)
|
adamc@360
|
127 <xml/>
|
adamc@339
|
128 [M.cols] M.cols}
|
adamc@339
|
129
|
adamc@339
|
130 <submit action={create}/>
|
adamc@361
|
131 </form>
|
adamc@360
|
132 </body></xml>
|
adamc@325
|
133
|
adamc@325
|
134 end
|