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

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

    adamc@370: adamc@370:

    I was born sure!

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

    {cdata M.title}

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