annotate demo/crud.ur @ 619:f38e009009bb

Time to start thinking about guards
author Adam Chlipala <adamc@hcoop.net>
date Sat, 21 Feb 2009 13:22:30 -0500
parents 12d163bb856f
children d64533157f40
rev   line source
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@421 9 con colsMeta = fn cols :: {(Type * Type)} => $(Top.mapT2T 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@421 36 val tab : sql_table ([Id = int] ++ mapT2T fstTT cols)
adamc@421 37
adamc@421 38 val title : string
adamc@421 39
adamc@421 40 val cols : colsMeta cols
adamc@421 41 end) = struct
adamc@421 42
adamc@421 43 open constraints M
adamc@421 44 val tab = M.tab
adamc@421 45
adamc@421 46 sequence seq
adamc@421 47
adamc@421 48 fun list () =
adamc@421 49 rows <- queryX (SELECT * FROM tab AS T)
adamc@421 50 (fn (fs : {T : $([Id = int] ++ mapT2T fstTT M.cols)}) => <xml>
adamc@421 51 <tr>
adamc@421 52 <td>{[fs.T.Id]}</td>
adamc@421 53 {foldT2RX2 [fstTT] [colMeta] [tr]
adamc@421 54 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@421 55 [[nm] ~ rest] v col => <xml>
adamc@421 56 <td>{col.Show v}</td>
adamc@421 57 </xml>)
adamc@421 58 [M.cols] (fs.T -- #Id) M.cols}
adamc@421 59 <td>
adamc@421 60 <a link={upd fs.T.Id}>[Update]</a>
adamc@421 61 <a link={confirm fs.T.Id}>[Delete]</a>
adamc@421 62 </td>
adamc@421 63 </tr>
adamc@421 64 </xml>);
adamc@421 65 return <xml>
adamc@421 66 <table border={1}>
adamc@421 67 <tr>
adamc@421 68 <th>ID</th>
adamc@421 69 {foldT2RX [colMeta] [tr]
adamc@421 70 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@421 71 [[nm] ~ rest] col => <xml>
adamc@421 72 <th>{cdata col.Nam}</th>
adamc@421 73 </xml>)
adamc@421 74 [M.cols] M.cols}
adamc@421 75 </tr>
adamc@421 76 {rows}
adamc@421 77 </table>
adamc@421 78
adamc@421 79 <br/><hr/><br/>
adamc@421 80
adamc@421 81 <form>
adamc@421 82 {foldT2R [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)]
adamc@421 83 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@421 84 [[nm] ~ rest] (col : colMeta t) (acc : xml form [] (mapT2T sndTT rest)) => <xml>
adamc@421 85 <li> {cdata col.Nam}: {col.Widget [nm]}</li>
adamc@421 86 {useMore acc}
adamc@421 87 </xml>)
adamc@421 88 <xml/>
adamc@421 89 [M.cols] M.cols}
adamc@421 90
adamc@421 91 <submit action={create}/>
adamc@421 92 </form>
adamc@421 93 </xml>
adamc@421 94
adamc@421 95 and create (inputs : $(mapT2T sndTT M.cols)) =
adamc@421 96 id <- nextval seq;
adamc@434 97 dml (insert tab
adamc@434 98 (foldT2R2 [sndTT] [colMeta]
adamc@434 99 [fn cols => $(mapT2T (fn t :: (Type * Type) =>
adamc@434 100 sql_exp [] [] [] t.1) cols)]
adamc@434 101 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@434 102 [[nm] ~ rest] =>
adamc@455 103 fn input col acc => acc ++ {nm = @sql_inject col.Inject (col.Parse input)})
adamc@434 104 {} [M.cols] inputs M.cols
adamc@471 105 ++ {Id = (SQL {[id]})}));
adamc@421 106 ls <- list ();
adamc@421 107 return <xml><body>
adamc@421 108 <p>Inserted with ID {[id]}.</p>
adamc@421 109
adamc@421 110 {ls}
adamc@421 111 </body></xml>
adamc@421 112
adamc@499 113 and upd (id : int) =
adamc@499 114 let
adamc@499 115 fun save (inputs : $(mapT2T sndTT M.cols)) =
adamc@499 116 dml (update [mapT2T fstTT M.cols]
adamc@499 117 (foldT2R2 [sndTT] [colMeta]
adamc@499 118 [fn cols => $(mapT2T (fn t :: (Type * Type) =>
adamc@499 119 sql_exp [T = [Id = int]
adamc@499 120 ++ mapT2T fstTT M.cols]
adamc@499 121 [] [] t.1) cols)]
adamc@499 122 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@499 123 [[nm] ~ rest] =>
adamc@499 124 fn input col acc => acc ++ {nm =
adamc@499 125 @sql_inject col.Inject (col.Parse input)})
adamc@499 126 {} [M.cols] inputs M.cols)
adamc@499 127 tab (WHERE T.Id = {[id]}));
adamc@499 128 ls <- list ();
adamc@499 129 return <xml><body>
adamc@499 130 <p>Saved!</p>
adamc@421 131
adamc@499 132 {ls}
adamc@499 133 </body></xml>
adamc@499 134 in
adamc@499 135 fso <- oneOrNoRows (SELECT tab.{{mapT2T fstTT M.cols}} FROM tab WHERE tab.Id = {[id]});
adamc@499 136 case fso : (Basis.option {Tab : $(mapT2T fstTT M.cols)}) of
adamc@499 137 None => return <xml><body>Not found!</body></xml>
adamc@499 138 | Some fs => return <xml><body><form>
adamc@499 139 {foldT2R2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)]
adamc@499 140 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@499 141 [[nm] ~ rest] (v : t.1) (col : colMeta t)
adamc@499 142 (acc : xml form [] (mapT2T sndTT rest)) =>
adamc@499 143 <xml>
adamc@499 144 <li> {cdata col.Nam}: {col.WidgetPopulated [nm] v}</li>
adamc@499 145 {useMore acc}
adamc@499 146 </xml>)
adamc@499 147 <xml/>
adamc@499 148 [M.cols] fs.Tab M.cols}
adamc@421 149
adamc@499 150 <submit action={save}/>
adamc@499 151 </form></body></xml>
adamc@499 152 end
adamc@421 153
adamc@499 154 and confirm (id : int) =
adamc@499 155 let
adamc@499 156 fun delete () =
adamc@499 157 dml (DELETE FROM tab WHERE Id = {[id]});
adamc@499 158 ls <- list ();
adamc@499 159 return <xml><body>
adamc@499 160 <p>The deed is done.</p>
adamc@499 161
adamc@499 162 {ls}
adamc@499 163 </body></xml>
adamc@499 164 in
adamc@499 165 return <xml><body>
adamc@499 166 <p>Are you sure you want to delete ID #{[id]}?</p>
adamc@499 167
adamc@499 168 <p><a link={delete ()}>I was born sure!</a></p>
adamc@499 169 </body></xml>
adamc@499 170 end
adamc@421 171
adamc@421 172 and main () =
adamc@421 173 ls <- list ();
adamc@421 174 return <xml><head>
adamc@421 175 <title>{cdata M.title}</title>
adamc@421 176 </head><body>
adamc@421 177
adamc@421 178 <h1>{cdata M.title}</h1>
adamc@421 179
adamc@421 180 {ls}
adamc@421 181 </body></xml>
adamc@421 182
adamc@421 183 end