adamc@421: con colMeta = fn t_formT :: (Type * Type) => { adamc@421: Nam : string, adamc@421: Show : t_formT.1 -> xbody, adamc@421: Widget : nm :: Name -> xml form [] [nm = t_formT.2], adamc@421: WidgetPopulated : nm :: Name -> t_formT.1 -> xml form [] [nm = t_formT.2], adamc@421: Parse : t_formT.2 -> t_formT.1, adamc@421: Inject : sql_injectable t_formT.1 adamc@421: } adamc@421: con colsMeta = fn cols :: {(Type * Type)} => $(Top.mapT2T colMeta cols) adamc@421: adamc@421: fun default (t ::: Type) (sh : show t) (rd : read t) (inj : sql_injectable t) adamc@421: name : colMeta (t, string) = adamc@421: {Nam = name, adamc@421: Show = txt, adamc@421: Widget = fn nm :: Name => , adamc@421: WidgetPopulated = fn (nm :: Name) n => adamc@421: , adamc@421: Parse = readError, adamc@421: Inject = _} adamc@421: adamc@421: val int = default adamc@421: val float = default adamc@421: val string = default adamc@421: adamc@421: fun bool name = {Nam = name, adamc@421: Show = txt, adamc@421: Widget = fn nm :: Name => , adamc@421: WidgetPopulated = fn (nm :: Name) b => adamc@421: , adamc@421: Parse = fn x => x, adamc@421: Inject = _} adamc@421: adamc@421: functor Make(M : sig adamc@421: con cols :: {(Type * Type)} adamc@421: constraint [Id] ~ cols adamc@421: val tab : sql_table ([Id = int] ++ mapT2T fstTT cols) adamc@421: adamc@421: val title : string adamc@421: adamc@421: val cols : colsMeta cols adamc@421: end) = struct adamc@421: adamc@421: open constraints M adamc@421: val tab = M.tab adamc@421: adamc@421: sequence seq adamc@421: adamc@421: fun list () = adamc@421: rows <- queryX (SELECT * FROM tab AS T) adamc@421: (fn (fs : {T : $([Id = int] ++ mapT2T fstTT M.cols)}) => adamc@421: adamc@421: {[fs.T.Id]} adamc@421: {foldT2RX2 [fstTT] [colMeta] [tr] adamc@421: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@421: [[nm] ~ rest] v col => adamc@421: {col.Show v} adamc@421: ) adamc@421: [M.cols] (fs.T -- #Id) M.cols} adamc@421: adamc@421: [Update] adamc@421: [Delete] adamc@421: adamc@421: adamc@421: ); adamc@421: return adamc@421: adamc@421: adamc@421: adamc@421: {foldT2RX [colMeta] [tr] adamc@421: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@421: [[nm] ~ rest] col => adamc@421: adamc@421: ) adamc@421: [M.cols] M.cols} adamc@421: adamc@421: {rows} adamc@421:
ID{cdata col.Nam}
adamc@421: adamc@421:


adamc@421: adamc@421:
adamc@421: {foldT2R [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)] adamc@421: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@421: [[nm] ~ rest] (col : colMeta t) (acc : xml form [] (mapT2T sndTT rest)) => adamc@421:
  • {cdata col.Nam}: {col.Widget [nm]}
  • adamc@421: {useMore acc} adamc@421:
    ) adamc@421: adamc@421: [M.cols] M.cols} adamc@421: adamc@421: adamc@421: adamc@421:
    adamc@421: adamc@421: and create (inputs : $(mapT2T sndTT M.cols)) = adamc@421: id <- nextval seq; adamc@434: dml (insert tab adamc@434: (foldT2R2 [sndTT] [colMeta] adamc@434: [fn cols => $(mapT2T (fn t :: (Type * Type) => adamc@434: sql_exp [] [] [] t.1) cols)] adamc@434: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@434: [[nm] ~ rest] => adamc@434: fn input col acc => acc with nm = @sql_inject col.Inject (col.Parse input)) adamc@434: {} [M.cols] inputs M.cols adamc@434: with #Id = (SQL {id}))); adamc@421: ls <- list (); adamc@421: return adamc@421:

    Inserted with ID {[id]}.

    adamc@421: adamc@421: {ls} adamc@421:
    adamc@421: adamc@421: and save (id : int) (inputs : $(mapT2T sndTT M.cols)) = adamc@434: dml (update [mapT2T fstTT M.cols] adamc@434: (foldT2R2 [sndTT] [colMeta] adamc@434: [fn cols => $(mapT2T (fn t :: (Type * Type) => adamc@434: sql_exp [T = [Id = int] adamc@434: ++ mapT2T fstTT M.cols] adamc@434: [] [] t.1) cols)] adamc@434: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@434: [[nm] ~ rest] => adamc@434: fn input col acc => acc with nm = adamc@434: @sql_inject col.Inject (col.Parse input)) adamc@434: {} [M.cols] inputs M.cols) adamc@434: tab (WHERE T.Id = {id})); adamc@421: ls <- list (); adamc@421: return adamc@421:

    Saved!

    adamc@421: adamc@421: {ls} adamc@421:
    adamc@421: adamc@421: and upd (id : int) = adamc@421: fso <- oneOrNoRows (SELECT tab.{{mapT2T fstTT M.cols}} FROM tab WHERE tab.Id = {id}); adamc@421: case fso : (Basis.option {Tab : $(mapT2T fstTT M.cols)}) of adamc@421: None => return Not found! adamc@421: | Some fs => return
    adamc@421: {foldT2R2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)] adamc@421: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@421: [[nm] ~ rest] (v : t.1) (col : colMeta t) adamc@421: (acc : xml form [] (mapT2T sndTT rest)) => adamc@421: adamc@421:
  • {cdata col.Nam}: {col.WidgetPopulated [nm] v}
  • adamc@421: {useMore acc} adamc@421:
    ) adamc@421: adamc@421: [M.cols] fs.Tab M.cols} adamc@421: adamc@421: adamc@421:
    adamc@421: adamc@421: and delete (id : int) = adamc@434: dml (DELETE FROM tab WHERE Id = {id}); adamc@421: ls <- list (); adamc@421: return adamc@421:

    The deed is done.

    adamc@421: adamc@421: {ls} adamc@421:
    adamc@421: adamc@421: and confirm (id : int) = return adamc@421:

    Are you sure you want to delete ID #{[id]}?

    adamc@421: adamc@421:

    I was born sure!

    adamc@421:
    adamc@421: adamc@421: and main () = adamc@421: ls <- list (); adamc@421: return adamc@421: {cdata M.title} adamc@421: adamc@421: adamc@421:

    {cdata M.title}

    adamc@421: adamc@421: {ls} adamc@421:
    adamc@421: adamc@421: end