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

    Saved!

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

    The deed is done.

    adamc@499: adamc@499: {ls} adamc@499:
    adamc@499: in adamc@499: return adamc@499:

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

    adamc@499: adamc@499:

    I was born sure!

    adamc@499:
    adamc@499: end 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