adamc@421
|
1 con colMeta = fn t_formT :: (Type * Type) => {
|
adamc@421
|
2 Nam : string,
|
adamc@421
|
3 Show : t_formT.1 -> xbody,
|
adamc@421
|
4 Widget : nm :: Name -> xml form [] [nm = t_formT.2],
|
adamc@421
|
5 WidgetPopulated : nm :: Name -> t_formT.1 -> xml form [] [nm = t_formT.2],
|
adamc@421
|
6 Parse : t_formT.2 -> t_formT.1,
|
adamc@421
|
7 Inject : sql_injectable t_formT.1
|
adamc@421
|
8 }
|
adamc@622
|
9 con colsMeta = fn cols :: {(Type * Type)} => $(map colMeta cols)
|
adamc@421
|
10
|
adamc@421
|
11 fun default (t ::: Type) (sh : show t) (rd : read t) (inj : sql_injectable t)
|
adamc@421
|
12 name : colMeta (t, string) =
|
adamc@421
|
13 {Nam = name,
|
adamc@421
|
14 Show = txt,
|
adamc@421
|
15 Widget = fn nm :: Name => <xml><textbox{nm}/></xml>,
|
adamc@421
|
16 WidgetPopulated = fn (nm :: Name) n =>
|
adamc@421
|
17 <xml><textbox{nm} value={show n}/></xml>,
|
adamc@421
|
18 Parse = readError,
|
adamc@421
|
19 Inject = _}
|
adamc@421
|
20
|
adamc@421
|
21 val int = default
|
adamc@421
|
22 val float = default
|
adamc@421
|
23 val string = default
|
adamc@421
|
24
|
adamc@421
|
25 fun bool name = {Nam = name,
|
adamc@421
|
26 Show = txt,
|
adamc@421
|
27 Widget = fn nm :: Name => <xml><checkbox{nm}/></xml>,
|
adamc@421
|
28 WidgetPopulated = fn (nm :: Name) b =>
|
adamc@421
|
29 <xml><checkbox{nm} checked={b}/></xml>,
|
adamc@421
|
30 Parse = fn x => x,
|
adamc@421
|
31 Inject = _}
|
adamc@421
|
32
|
adamc@421
|
33 functor Make(M : sig
|
adamc@421
|
34 con cols :: {(Type * Type)}
|
adamc@421
|
35 constraint [Id] ~ cols
|
adamc@632
|
36 val fl : folder cols
|
adamc@632
|
37
|
adamc@622
|
38 val tab : sql_table ([Id = int] ++ map fstTT cols)
|
adamc@421
|
39
|
adamc@421
|
40 val title : string
|
adamc@421
|
41
|
adamc@421
|
42 val cols : colsMeta cols
|
adamc@421
|
43 end) = struct
|
adamc@421
|
44
|
adamc@421
|
45 open constraints M
|
adamc@421
|
46 val tab = M.tab
|
adamc@421
|
47
|
adamc@421
|
48 sequence seq
|
adamc@421
|
49
|
adamc@421
|
50 fun list () =
|
adamc@421
|
51 rows <- queryX (SELECT * FROM tab AS T)
|
adamc@622
|
52 (fn (fs : {T : $([Id = int] ++ map fstTT M.cols)}) => <xml>
|
adamc@421
|
53 <tr>
|
adamc@421
|
54 <td>{[fs.T.Id]}</td>
|
adamc@632
|
55 {foldRX2 [fstTT] [colMeta] [tr]
|
adamc@632
|
56 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@632
|
57 [[nm] ~ rest] v col => <xml>
|
adamc@632
|
58 <td>{col.Show v}</td>
|
adamc@632
|
59 </xml>)
|
adamc@632
|
60 [M.cols] M.fl (fs.T -- #Id) M.cols}
|
adamc@421
|
61 <td>
|
adamc@421
|
62 <a link={upd fs.T.Id}>[Update]</a>
|
adamc@421
|
63 <a link={confirm fs.T.Id}>[Delete]</a>
|
adamc@421
|
64 </td>
|
adamc@421
|
65 </tr>
|
adamc@421
|
66 </xml>);
|
adamc@421
|
67 return <xml>
|
adamc@421
|
68 <table border={1}>
|
adamc@421
|
69 <tr>
|
adamc@421
|
70 <th>ID</th>
|
adamc@632
|
71 {foldRX [colMeta] [tr]
|
adamc@421
|
72 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@421
|
73 [[nm] ~ rest] col => <xml>
|
adamc@421
|
74 <th>{cdata col.Nam}</th>
|
adamc@421
|
75 </xml>)
|
adamc@632
|
76 [M.cols] M.fl M.cols}
|
adamc@421
|
77 </tr>
|
adamc@421
|
78 {rows}
|
adamc@421
|
79 </table>
|
adamc@421
|
80
|
adamc@421
|
81 <br/><hr/><br/>
|
adamc@421
|
82
|
adamc@421
|
83 <form>
|
adamc@632
|
84 {foldR [colMeta] [fn cols :: {(Type * Type)} => xml form [] (map sndTT cols)]
|
adamc@632
|
85 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@632
|
86 [[nm] ~ rest] (col : colMeta t) (acc : xml form [] (map sndTT rest)) => <xml>
|
adamc@632
|
87 <li> {cdata col.Nam}: {col.Widget [nm]}</li>
|
adamc@632
|
88 {useMore acc}
|
adamc@632
|
89 </xml>)
|
adamc@421
|
90 <xml/>
|
adamc@632
|
91 [M.cols] M.fl M.cols}
|
adamc@421
|
92
|
adamc@421
|
93 <submit action={create}/>
|
adamc@421
|
94 </form>
|
adamc@421
|
95 </xml>
|
adamc@421
|
96
|
adamc@622
|
97 and create (inputs : $(map sndTT M.cols)) =
|
adamc@421
|
98 id <- nextval seq;
|
adamc@434
|
99 dml (insert tab
|
adamc@632
|
100 (foldR2 [sndTT] [colMeta]
|
adamc@632
|
101 [fn cols => $(map (fn t :: (Type * Type) =>
|
adamc@632
|
102 sql_exp [] [] [] t.1) cols)]
|
adamc@632
|
103 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@632
|
104 [[nm] ~ rest] =>
|
adamc@632
|
105 fn input col acc => acc ++ {nm = @sql_inject col.Inject (col.Parse input)})
|
adamc@632
|
106 {} [M.cols] M.fl inputs M.cols
|
adamc@471
|
107 ++ {Id = (SQL {[id]})}));
|
adamc@421
|
108 ls <- list ();
|
adamc@421
|
109 return <xml><body>
|
adamc@421
|
110 <p>Inserted with ID {[id]}.</p>
|
adamc@421
|
111
|
adamc@421
|
112 {ls}
|
adamc@421
|
113 </body></xml>
|
adamc@421
|
114
|
adamc@499
|
115 and upd (id : int) =
|
adamc@499
|
116 let
|
adamc@622
|
117 fun save (inputs : $(map sndTT M.cols)) =
|
adamc@632
|
118 dml (update [map fstTT M.cols] !
|
adamc@632
|
119 (foldR2 [sndTT] [colMeta]
|
adamc@632
|
120 [fn cols => $(map (fn t :: (Type * Type) =>
|
adamc@632
|
121 sql_exp [T = [Id = int]
|
adamc@632
|
122 ++ map fstTT M.cols]
|
adamc@632
|
123 [] [] t.1) cols)]
|
adamc@632
|
124 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@632
|
125 [[nm] ~ rest] =>
|
adamc@632
|
126 fn input col acc => acc ++ {nm =
|
adamc@632
|
127 @sql_inject col.Inject (col.Parse input)})
|
adamc@632
|
128 {} [M.cols] M.fl inputs M.cols)
|
adamc@499
|
129 tab (WHERE T.Id = {[id]}));
|
adamc@499
|
130 ls <- list ();
|
adamc@499
|
131 return <xml><body>
|
adamc@499
|
132 <p>Saved!</p>
|
adamc@421
|
133
|
adamc@499
|
134 {ls}
|
adamc@499
|
135 </body></xml>
|
adamc@499
|
136 in
|
adamc@622
|
137 fso <- oneOrNoRows (SELECT tab.{{map fstTT M.cols}} FROM tab WHERE tab.Id = {[id]});
|
adamc@622
|
138 case fso : (Basis.option {Tab : $(map fstTT M.cols)}) of
|
adamc@499
|
139 None => return <xml><body>Not found!</body></xml>
|
adamc@499
|
140 | Some fs => return <xml><body><form>
|
adamc@632
|
141 {foldR2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (map sndTT cols)]
|
adamc@632
|
142 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
|
adamc@632
|
143 [[nm] ~ rest] (v : t.1) (col : colMeta t)
|
adamc@632
|
144 (acc : xml form [] (map sndTT rest)) =>
|
adamc@632
|
145 <xml>
|
adamc@632
|
146 <li> {cdata col.Nam}: {col.WidgetPopulated [nm] v}</li>
|
adamc@632
|
147 {useMore acc}
|
adamc@632
|
148 </xml>)
|
adamc@632
|
149 <xml/>
|
adamc@632
|
150 [M.cols] M.fl fs.Tab M.cols}
|
adamc@421
|
151
|
adamc@499
|
152 <submit action={save}/>
|
adamc@499
|
153 </form></body></xml>
|
adamc@499
|
154 end
|
adamc@421
|
155
|
adamc@499
|
156 and confirm (id : int) =
|
adamc@499
|
157 let
|
adamc@499
|
158 fun delete () =
|
adamc@499
|
159 dml (DELETE FROM tab WHERE Id = {[id]});
|
adamc@499
|
160 ls <- list ();
|
adamc@499
|
161 return <xml><body>
|
adamc@499
|
162 <p>The deed is done.</p>
|
adamc@499
|
163
|
adamc@499
|
164 {ls}
|
adamc@499
|
165 </body></xml>
|
adamc@499
|
166 in
|
adamc@499
|
167 return <xml><body>
|
adamc@499
|
168 <p>Are you sure you want to delete ID #{[id]}?</p>
|
adamc@499
|
169
|
adamc@499
|
170 <p><a link={delete ()}>I was born sure!</a></p>
|
adamc@499
|
171 </body></xml>
|
adamc@499
|
172 end
|
adamc@421
|
173
|
adamc@421
|
174 and main () =
|
adamc@421
|
175 ls <- list ();
|
adamc@421
|
176 return <xml><head>
|
adamc@421
|
177 <title>{cdata M.title}</title>
|
adamc@421
|
178 </head><body>
|
adamc@421
|
179
|
adamc@421
|
180 <h1>{cdata M.title}</h1>
|
adamc@421
|
181
|
adamc@421
|
182 {ls}
|
adamc@421
|
183 </body></xml>
|
adamc@421
|
184
|
adamc@421
|
185 end
|