annotate tests/crud.ur @ 364:7c58dc323683

Fix XML indent not to take two tab-tries
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Oct 2008 12:55:16 -0400
parents 24a31b35e08f
children 28d3d7210687
rev   line source
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@362 15 Widget = fn nm :: Name => <xml><textbox{nm}/></xml>,
adamc@362 16 WidgetPopulated = fn (nm :: Name) n =>
adamc@362 17 <xml><textbox{nm} value={show _ n}/></xml>,
adamc@362 18 Parse = readError _,
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@362 27 Widget = fn nm :: Name => <xml><checkbox{nm}/></xml>,
adamc@362 28 WidgetPopulated = fn (nm :: Name) b =>
adamc@362 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@339 49 id <- nextval seq;
adamc@339 50 () <- dml (insert tab (foldT2R2 [sndTT] [colMeta]
adamc@339 51 [fn cols => $(mapT2T (fn t :: (Type * Type) =>
adamc@342 52 sql_exp [] [] [] t.1) cols)]
adamc@360 53 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@360 54 [[nm] ~ rest] =>
adamc@360 55 fn input col acc => acc with nm = sql_inject col.Inject (col.Parse input))
adamc@339 56 {} [M.cols] inputs M.cols
adamc@339 57 with #Id = (SQL {id})));
adamc@360 58 return <xml><body>
adamc@339 59 Inserted with ID {txt _ id}.
adamc@360 60 </body></xml>
adamc@339 61
adamc@342 62 fun save (id : int) (inputs : $(mapT2T sndTT M.cols)) =
adamc@342 63 () <- dml (update [mapT2T fstTT M.cols] (foldT2R2 [sndTT] [colMeta]
adamc@342 64 [fn cols => $(mapT2T (fn t :: (Type * Type) =>
adamc@342 65 sql_exp [T = [Id = int] ++ mapT2T fstTT M.cols] [] [] t.1) cols)]
adamc@360 66 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@360 67 [[nm] ~ rest] =>
adamc@360 68 fn input col acc => acc with nm = sql_inject col.Inject (col.Parse input))
adamc@342 69 {} [M.cols] inputs M.cols)
adamc@342 70 tab (WHERE T.Id = {id}));
adamc@360 71 return <xml><body>
adamc@342 72 Saved!
adamc@360 73 </body></xml>
adamc@341 74
adamc@341 75 fun update (id : int) =
adamc@341 76 fso <- oneOrNoRows (SELECT tab.{{mapT2T fstTT M.cols}} FROM tab WHERE tab.Id = {id});
adamc@341 77 case fso : (Basis.option {Tab : $(mapT2T fstTT M.cols)}) of
adamc@360 78 None => return <xml><body>Not found!</body></xml>
adamc@361 79 | Some fs => return <xml><body><form>
adamc@341 80 {foldT2R2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)]
adamc@360 81 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@360 82 [[nm] ~ rest] (v : t.1) (col : colMeta t)
adamc@360 83 (acc : xml form [] (mapT2T sndTT rest)) =>
adamc@360 84 <xml>
adamc@360 85 <li> {cdata col.Nam}: {col.WidgetPopulated [nm] v}</li>
adamc@360 86 {useMore acc}
adamc@360 87 </xml>)
adamc@360 88 <xml/>
adamc@341 89 [M.cols] fs.Tab M.cols}
adamc@341 90
adamc@341 91 <submit action={save id}/>
adamc@361 92 </form></body></xml>
adamc@341 93
adamc@337 94 fun delete (id : int) =
adamc@337 95 () <- dml (DELETE FROM tab WHERE Id = {id});
adamc@360 96 return <xml><body>
adamc@337 97 The deed is done.
adamc@360 98 </body></xml>
adamc@337 99
adamc@360 100 fun confirm (id : int) = return <xml><body>
adamc@337 101 <p>Are you sure you want to delete ID #{txt _ id}?</p>
adamc@337 102
adamc@337 103 <p><a link={delete id}>I was born sure!</a></p>
adamc@360 104 </body></xml>
adamc@337 105
adamc@346 106 fun main () =
adamc@334 107 rows <- queryX (SELECT * FROM tab AS T)
adamc@360 108 (fn (fs : {T : $([Id = int] ++ mapT2T fstTT M.cols)}) => <xml>
adamc@326 109 <tr>
adamc@326 110 <td>{txt _ fs.T.Id}</td>
adamc@339 111 {foldT2RX2 [fstTT] [colMeta] [tr]
adamc@360 112 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@360 113 [[nm] ~ rest] v col => <xml>
adamc@336 114 <td>{col.Show v}</td>
adamc@360 115 </xml>)
adamc@326 116 [M.cols] (fs.T -- #Id) M.cols}
adamc@341 117 <td><a link={update fs.T.Id}>[Update]</a> <a link={confirm fs.T.Id}>[Delete]</a></td>
adamc@326 118 </tr>
adamc@360 119 </xml>);
adamc@360 120 return <xml><head>
adamc@334 121 <title>{cdata M.title}</title>
adamc@325 122
adamc@325 123 </head><body>
adamc@325 124
adamc@334 125 <h1>{cdata M.title}</h1>
adamc@325 126
adamc@325 127 <table border={1}>
adamc@336 128 <tr>
adamc@336 129 <th>ID</th>
adamc@339 130 {foldT2RX [colMeta] [tr]
adamc@360 131 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@360 132 [[nm] ~ rest] col => <xml>
adamc@336 133 <th>{cdata col.Nam}</th>
adamc@360 134 </xml>)
adamc@336 135 [M.cols] M.cols}
adamc@336 136 </tr>
adamc@325 137 {rows}
adamc@325 138 </table>
adamc@339 139
adamc@339 140 <br/>
adamc@339 141
adamc@361 142 <form>
adamc@339 143 {foldT2R [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)]
adamc@360 144 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@360 145 [[nm] ~ rest] (col : colMeta t) (acc : xml form [] (mapT2T sndTT rest)) => <xml>
adamc@339 146 <li> {cdata col.Nam}: {col.Widget [nm]}</li>
adamc@339 147 {useMore acc}
adamc@360 148 </xml>)
adamc@360 149 <xml/>
adamc@339 150 [M.cols] M.cols}
adamc@339 151
adamc@339 152 <submit action={create}/>
adamc@361 153 </form>
adamc@360 154 </body></xml>
adamc@325 155
adamc@325 156 end