annotate demo/crud.ur @ 434:c471345f5165

Remove need for '() <-' notation
author Adam Chlipala <adamc@hcoop.net>
date Mon, 27 Oct 2008 08:27:45 -0400
parents 0767d7ad0c3a
children d4a81273d4b1
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@434 103 fn input col acc => acc with nm = @sql_inject col.Inject (col.Parse input))
adamc@434 104 {} [M.cols] inputs M.cols
adamc@434 105 with #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@421 113 and save (id : int) (inputs : $(mapT2T sndTT M.cols)) =
adamc@434 114 dml (update [mapT2T fstTT M.cols]
adamc@434 115 (foldT2R2 [sndTT] [colMeta]
adamc@434 116 [fn cols => $(mapT2T (fn t :: (Type * Type) =>
adamc@434 117 sql_exp [T = [Id = int]
adamc@434 118 ++ mapT2T fstTT M.cols]
adamc@434 119 [] [] t.1) cols)]
adamc@434 120 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@434 121 [[nm] ~ rest] =>
adamc@434 122 fn input col acc => acc with nm =
adamc@434 123 @sql_inject col.Inject (col.Parse input))
adamc@434 124 {} [M.cols] inputs M.cols)
adamc@434 125 tab (WHERE T.Id = {id}));
adamc@421 126 ls <- list ();
adamc@421 127 return <xml><body>
adamc@421 128 <p>Saved!</p>
adamc@421 129
adamc@421 130 {ls}
adamc@421 131 </body></xml>
adamc@421 132
adamc@421 133 and upd (id : int) =
adamc@421 134 fso <- oneOrNoRows (SELECT tab.{{mapT2T fstTT M.cols}} FROM tab WHERE tab.Id = {id});
adamc@421 135 case fso : (Basis.option {Tab : $(mapT2T fstTT M.cols)}) of
adamc@421 136 None => return <xml><body>Not found!</body></xml>
adamc@421 137 | Some fs => return <xml><body><form>
adamc@421 138 {foldT2R2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)]
adamc@421 139 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@421 140 [[nm] ~ rest] (v : t.1) (col : colMeta t)
adamc@421 141 (acc : xml form [] (mapT2T sndTT rest)) =>
adamc@421 142 <xml>
adamc@421 143 <li> {cdata col.Nam}: {col.WidgetPopulated [nm] v}</li>
adamc@421 144 {useMore acc}
adamc@421 145 </xml>)
adamc@421 146 <xml/>
adamc@421 147 [M.cols] fs.Tab M.cols}
adamc@421 148
adamc@421 149 <submit action={save id}/>
adamc@421 150 </form></body></xml>
adamc@421 151
adamc@421 152 and delete (id : int) =
adamc@434 153 dml (DELETE FROM tab WHERE Id = {id});
adamc@421 154 ls <- list ();
adamc@421 155 return <xml><body>
adamc@421 156 <p>The deed is done.</p>
adamc@421 157
adamc@421 158 {ls}
adamc@421 159 </body></xml>
adamc@421 160
adamc@421 161 and confirm (id : int) = return <xml><body>
adamc@421 162 <p>Are you sure you want to delete ID #{[id]}?</p>
adamc@421 163
adamc@421 164 <p><a link={delete id}>I was born sure!</a></p>
adamc@421 165 </body></xml>
adamc@421 166
adamc@421 167 and main () =
adamc@421 168 ls <- list ();
adamc@421 169 return <xml><head>
adamc@421 170 <title>{cdata M.title}</title>
adamc@421 171 </head><body>
adamc@421 172
adamc@421 173 <h1>{cdata M.title}</h1>
adamc@421 174
adamc@421 175 {ls}
adamc@421 176 </body></xml>
adamc@421 177
adamc@421 178 end