annotate tests/crud.ur @ 344:3c0feecd057d

Force existence of bin and clib directories
author Adam Chlipala <adamc@hcoop.net>
date Thu, 18 Sep 2008 15:01:01 -0400
parents f55034419a07
children a94a79820d49
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@325 11 functor Make(M : sig
adamc@339 12 con cols :: {(Type * Type)}
adamc@325 13 constraint [Id] ~ cols
adamc@339 14 val tab : sql_table ([Id = int] ++ mapT2T fstTT cols)
adamc@325 15
adamc@325 16 val title : string
adamc@325 17
adamc@339 18 val cols : colsMeta cols
adamc@325 19 end) = struct
adamc@325 20
adamc@325 21 open constraints M
adamc@325 22 val tab = M.tab
adamc@325 23
adamc@339 24 sequence seq
adamc@339 25
adamc@339 26 fun create (inputs : $(mapT2T sndTT M.cols)) =
adamc@339 27 id <- nextval seq;
adamc@339 28 () <- dml (insert tab (foldT2R2 [sndTT] [colMeta]
adamc@339 29 [fn cols => $(mapT2T (fn t :: (Type * Type) =>
adamc@342 30 sql_exp [] [] [] t.1) cols)]
adamc@339 31 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) =>
adamc@339 32 [[nm] ~ rest] =>
adamc@339 33 fn input col acc => acc with nm = sql_inject col.Inject (col.Parse input))
adamc@339 34 {} [M.cols] inputs M.cols
adamc@339 35 with #Id = (SQL {id})));
adamc@339 36 return <html><body>
adamc@339 37 Inserted with ID {txt _ id}.
adamc@339 38 </body></html>
adamc@339 39
adamc@342 40 fun save (id : int) (inputs : $(mapT2T sndTT M.cols)) =
adamc@342 41 () <- dml (update [mapT2T fstTT M.cols] (foldT2R2 [sndTT] [colMeta]
adamc@342 42 [fn cols => $(mapT2T (fn t :: (Type * Type) =>
adamc@342 43 sql_exp [T = [Id = int] ++ mapT2T fstTT M.cols] [] [] t.1) cols)]
adamc@342 44 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) =>
adamc@342 45 [[nm] ~ rest] =>
adamc@342 46 fn input col acc => acc with nm = sql_inject col.Inject (col.Parse input))
adamc@342 47 {} [M.cols] inputs M.cols)
adamc@342 48 tab (WHERE T.Id = {id}));
adamc@341 49 return <html><body>
adamc@342 50 Saved!
adamc@341 51 </body></html>
adamc@341 52
adamc@341 53 fun update (id : int) =
adamc@341 54 fso <- oneOrNoRows (SELECT tab.{{mapT2T fstTT M.cols}} FROM tab WHERE tab.Id = {id});
adamc@341 55 case fso : (Basis.option {Tab : $(mapT2T fstTT M.cols)}) of
adamc@341 56 None => return <html><body>Not found!</body></html>
adamc@341 57 | Some fs => return <html><body><lform>
adamc@341 58 {foldT2R2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)]
adamc@341 59 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) =>
adamc@341 60 [[nm] ~ rest] =>
adamc@341 61 fn (v : t.1) (col : colMeta t) (acc : xml form [] (mapT2T sndTT rest)) => <lform>
adamc@341 62 <li> {cdata col.Nam}: {col.WidgetPopulated [nm] v}</li>
adamc@341 63 {useMore acc}
adamc@341 64 </lform>)
adamc@341 65 <lform></lform>
adamc@341 66 [M.cols] fs.Tab M.cols}
adamc@341 67
adamc@341 68 <submit action={save id}/>
adamc@341 69 </lform></body></html>
adamc@341 70
adamc@337 71 fun delete (id : int) =
adamc@337 72 () <- dml (DELETE FROM tab WHERE Id = {id});
adamc@337 73 return <html><body>
adamc@337 74 The deed is done.
adamc@337 75 </body></html>
adamc@337 76
adamc@337 77 fun confirm (id : int) = return <html><body>
adamc@337 78 <p>Are you sure you want to delete ID #{txt _ id}?</p>
adamc@337 79
adamc@337 80 <p><a link={delete id}>I was born sure!</a></p>
adamc@337 81 </body></html>
adamc@337 82
adamc@334 83 fun main () : transaction page =
adamc@334 84 rows <- queryX (SELECT * FROM tab AS T)
adamc@339 85 (fn (fs : {T : $([Id = int] ++ mapT2T fstTT M.cols)}) => <body>
adamc@326 86 <tr>
adamc@326 87 <td>{txt _ fs.T.Id}</td>
adamc@339 88 {foldT2RX2 [fstTT] [colMeta] [tr]
adamc@339 89 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) =>
adamc@326 90 [[nm] ~ rest] =>
adamc@336 91 fn v col => <tr>
adamc@336 92 <td>{col.Show v}</td>
adamc@326 93 </tr>)
adamc@326 94 [M.cols] (fs.T -- #Id) M.cols}
adamc@341 95 <td><a link={update fs.T.Id}>[Update]</a> <a link={confirm fs.T.Id}>[Delete]</a></td>
adamc@326 96 </tr>
adamc@334 97 </body>);
adamc@325 98 return <html><head>
adamc@334 99 <title>{cdata M.title}</title>
adamc@325 100
adamc@325 101 </head><body>
adamc@325 102
adamc@334 103 <h1>{cdata M.title}</h1>
adamc@325 104
adamc@325 105 <table border={1}>
adamc@336 106 <tr>
adamc@336 107 <th>ID</th>
adamc@339 108 {foldT2RX [colMeta] [tr]
adamc@339 109 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) =>
adamc@336 110 [[nm] ~ rest] =>
adamc@336 111 fn col => <tr>
adamc@336 112 <th>{cdata col.Nam}</th>
adamc@336 113 </tr>)
adamc@336 114 [M.cols] M.cols}
adamc@336 115 </tr>
adamc@325 116 {rows}
adamc@325 117 </table>
adamc@339 118
adamc@339 119 <br/>
adamc@339 120
adamc@339 121 <lform>
adamc@339 122 {foldT2R [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)]
adamc@339 123 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) =>
adamc@339 124 [[nm] ~ rest] =>
adamc@339 125 fn (col : colMeta t) (acc : xml form [] (mapT2T sndTT rest)) => <lform>
adamc@339 126 <li> {cdata col.Nam}: {col.Widget [nm]}</li>
adamc@339 127 {useMore acc}
adamc@339 128 </lform>)
adamc@339 129 <lform></lform>
adamc@339 130 [M.cols] M.cols}
adamc@339 131
adamc@339 132 <submit action={create}/>
adamc@339 133 </lform>
adamc@325 134 </body></html>
adamc@325 135
adamc@325 136 end