adamc@1002: con colMeta = fn (db :: Type, widget :: Type) => adamc@1002: {Nam : string, adamc@1002: Show : db -> xbody, adamc@1002: Widget : nm :: Name -> xml form [] [nm = widget], adamc@1002: WidgetPopulated : nm :: Name -> db -> xml form [] [nm = widget], adamc@1002: Parse : widget -> db, adamc@1002: Inject : sql_injectable db} adamc@622: con colsMeta = fn cols :: {(Type * Type)} => $(map colMeta cols) adamc@421: adamc@823: fun default [t] (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@823: Widget = fn [nm :: Name] => , adamc@823: 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@823: Widget = fn [nm :: Name] => , adamc@823: 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@706: table tab : ([Id = int] ++ map fst cols) adamc@421: adamc@421: val title : string adamc@421: adamc@421: val cols : colsMeta cols adamc@421: end) = struct adamc@421: 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@637: (fn (fs : {T : $([Id = int] ++ map fst M.cols)}) => adamc@421: adamc@421: {[fs.T.Id]} adamc@1093: {@foldRX2 [fst] [colMeta] [tr] adamc@1093: (fn [nm :: Name] [t :: (Type * Type)] [rest :: {(Type * Type)}] adamc@1093: [[nm] ~ rest] v col => adamc@1093: {col.Show v} adamc@1093: ) adamc@1093: 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@1093: {@foldRX [colMeta] [tr] adamc@1093: (fn [nm :: Name] [t :: (Type * Type)] [rest :: {(Type * Type)}] adamc@1093: [[nm] ~ rest] col => adamc@1093: adamc@1093: ) adamc@1093: M.fl M.cols} adamc@421: adamc@421: {rows} adamc@421:
ID{cdata col.Nam}
adamc@421: adamc@421:


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