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@622: con colsMeta = fn cols :: {(Type * Type)} => $(map 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@632: val fl : folder cols adamc@632: adamc@622: val tab : sql_table ([Id = int] ++ map 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@622: (fn (fs : {T : $([Id = int] ++ map fstTT M.cols)}) => adamc@421: adamc@421: {[fs.T.Id]} adamc@632: {foldRX2 [fstTT] [colMeta] [tr] adamc@632: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@632: [[nm] ~ rest] v col => adamc@632: {col.Show v} adamc@632: ) adamc@632: [M.cols] M.fl (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@632: {foldRX [colMeta] [tr] adamc@421: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@421: [[nm] ~ rest] col => adamc@421: adamc@421: ) adamc@632: [M.cols] M.fl M.cols} adamc@421: adamc@421: {rows} adamc@421:
ID{cdata col.Nam}
adamc@421: adamc@421:


adamc@421: adamc@421:
adamc@632: {foldR [colMeta] [fn cols :: {(Type * Type)} => xml form [] (map sndTT cols)] adamc@632: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@632: [[nm] ~ rest] (col : colMeta t) (acc : xml form [] (map sndTT rest)) => adamc@632:
  • {cdata col.Nam}: {col.Widget [nm]}
  • adamc@632: {useMore acc} adamc@632:
    ) adamc@421: adamc@632: [M.cols] M.fl M.cols} adamc@421: adamc@421: adamc@421: adamc@421:
    adamc@421: adamc@622: and create (inputs : $(map sndTT M.cols)) = adamc@421: id <- nextval seq; adamc@434: dml (insert tab adamc@632: (foldR2 [sndTT] [colMeta] adamc@632: [fn cols => $(map (fn t :: (Type * Type) => adamc@632: sql_exp [] [] [] t.1) cols)] adamc@632: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@632: [[nm] ~ rest] => adamc@632: fn input col acc => acc ++ {nm = @sql_inject col.Inject (col.Parse input)}) adamc@632: {} [M.cols] M.fl 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@622: fun save (inputs : $(map sndTT M.cols)) = adamc@632: dml (update [map fstTT M.cols] ! adamc@632: (foldR2 [sndTT] [colMeta] adamc@632: [fn cols => $(map (fn t :: (Type * Type) => adamc@632: sql_exp [T = [Id = int] adamc@632: ++ map fstTT M.cols] adamc@632: [] [] t.1) cols)] adamc@632: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@632: [[nm] ~ rest] => adamc@632: fn input col acc => acc ++ {nm = adamc@632: @sql_inject col.Inject (col.Parse input)}) adamc@632: {} [M.cols] M.fl 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@622: fso <- oneOrNoRows (SELECT tab.{{map fstTT M.cols}} FROM tab WHERE tab.Id = {[id]}); adamc@622: case fso : (Basis.option {Tab : $(map fstTT M.cols)}) of adamc@499: None => return Not found! adamc@499: | Some fs => return
    adamc@632: {foldR2 [fstTT] [colMeta] [fn cols :: {(Type * Type)} => xml form [] (map sndTT cols)] adamc@632: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) adamc@632: [[nm] ~ rest] (v : t.1) (col : colMeta t) adamc@632: (acc : xml form [] (map sndTT rest)) => adamc@632: adamc@632:
  • {cdata col.Nam}: {col.WidgetPopulated [nm] v}
  • adamc@632: {useMore acc} adamc@632:
    ) adamc@632: adamc@632: [M.cols] M.fl 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