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