adamc@339: con colMeta = fn t_formT :: (Type * Type) => { adamc@339: Nam : string, adamc@339: Show : t_formT.1 -> xbody, adamc@339: Widget : nm :: Name -> xml form [] [nm = t_formT.2], adamc@341: WidgetPopulated : nm :: Name -> t_formT.1 -> xml form [] [nm = t_formT.2], adamc@339: Parse : t_formT.2 -> t_formT.1, adamc@339: Inject : sql_injectable t_formT.1 adamc@339: } adamc@339: con colsMeta = fn cols :: {(Type * Type)} => $(Top.mapT2T colMeta cols) adamc@326: adamc@325: functor Make(M : sig adamc@339: con cols :: {(Type * Type)} adamc@325: constraint [Id] ~ cols adamc@339: val tab : sql_table ([Id = int] ++ mapT2T fstTT cols) adamc@325: adamc@325: val title : string adamc@325: adamc@339: val cols : colsMeta cols adamc@325: end) = struct adamc@325: adamc@325: open constraints M adamc@325: val tab = M.tab adamc@325: adamc@339: sequence seq adamc@339: adamc@339: fun create (inputs : $(mapT2T sndTT M.cols)) = adamc@339: id <- nextval seq; adamc@339: () <- dml (insert tab (foldT2R2 [sndTT] [colMeta] adamc@339: [fn cols => $(mapT2T (fn t :: (Type * Type) => adamc@342: sql_exp [] [] [] t.1) cols)] adamc@339: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) => adamc@339: [[nm] ~ rest] => adamc@339: fn input col acc => acc with nm = sql_inject col.Inject (col.Parse input)) adamc@339: {} [M.cols] inputs M.cols adamc@339: with #Id = (SQL {id}))); adamc@339: return adamc@339: Inserted with ID {txt _ id}. adamc@339: adamc@339: adamc@342: fun save (id : int) (inputs : $(mapT2T sndTT M.cols)) = adamc@342: () <- dml (update [mapT2T fstTT M.cols] (foldT2R2 [sndTT] [colMeta] adamc@342: [fn cols => $(mapT2T (fn t :: (Type * Type) => adamc@342: sql_exp [T = [Id = int] ++ mapT2T fstTT M.cols] [] [] t.1) cols)] adamc@342: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) => adamc@342: [[nm] ~ rest] => adamc@342: fn input col acc => acc with nm = sql_inject col.Inject (col.Parse input)) adamc@342: {} [M.cols] inputs M.cols) adamc@342: tab (WHERE T.Id = {id})); adamc@341: return adamc@342: Saved! adamc@341: adamc@341: adamc@341: fun update (id : int) = adamc@341: fso <- oneOrNoRows (SELECT tab.{{mapT2T fstTT M.cols}} FROM tab WHERE tab.Id = {id}); adamc@341: case fso : (Basis.option {Tab : $(mapT2T fstTT M.cols)}) of adamc@341: None => return Not found! adamc@341: | Some fs => return adamc@341: {foldT2R2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)] adamc@341: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) => adamc@341: [[nm] ~ rest] => adamc@341: fn (v : t.1) (col : colMeta t) (acc : xml form [] (mapT2T sndTT rest)) => adamc@341:
  • {cdata col.Nam}: {col.WidgetPopulated [nm] v}
  • adamc@341: {useMore acc} adamc@341:
    ) adamc@341: adamc@341: [M.cols] fs.Tab M.cols} adamc@341: adamc@341: adamc@341:
    adamc@341: adamc@337: fun delete (id : int) = adamc@337: () <- dml (DELETE FROM tab WHERE Id = {id}); adamc@337: return adamc@337: The deed is done. adamc@337: adamc@337: adamc@337: fun confirm (id : int) = return adamc@337:

    Are you sure you want to delete ID #{txt _ id}?

    adamc@337: adamc@337:

    I was born sure!

    adamc@337: adamc@337: adamc@346: fun main () = adamc@334: rows <- queryX (SELECT * FROM tab AS T) adamc@339: (fn (fs : {T : $([Id = int] ++ mapT2T fstTT M.cols)}) => adamc@326: adamc@326: {txt _ fs.T.Id} adamc@339: {foldT2RX2 [fstTT] [colMeta] [tr] adamc@339: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) => adamc@326: [[nm] ~ rest] => adamc@336: fn v col => adamc@336: {col.Show v} adamc@326: ) adamc@326: [M.cols] (fs.T -- #Id) M.cols} adamc@341: [Update] [Delete] adamc@326: adamc@334: ); adamc@325: return adamc@334: {cdata M.title} adamc@325: adamc@325: adamc@325: adamc@334:

    {cdata M.title}

    adamc@325: adamc@325: adamc@336: adamc@336: adamc@339: {foldT2RX [colMeta] [tr] adamc@339: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) => adamc@336: [[nm] ~ rest] => adamc@336: fn col => adamc@336: adamc@336: ) adamc@336: [M.cols] M.cols} adamc@336: adamc@325: {rows} adamc@325:
    ID
    {cdata col.Nam}
    adamc@339: adamc@339:
    adamc@339: adamc@339: adamc@339: {foldT2R [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)] adamc@339: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) => adamc@339: [[nm] ~ rest] => adamc@339: fn (col : colMeta t) (acc : xml form [] (mapT2T sndTT rest)) => adamc@339:
  • {cdata col.Nam}: {col.Widget [nm]}
  • adamc@339: {useMore acc} adamc@339:
    ) adamc@339: adamc@339: [M.cols] M.cols} adamc@339: adamc@339: adamc@339:
    adamc@325: adamc@325: adamc@325: end