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

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

    adamc@337: adamc@337:

    I was born sure!

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

    {cdata M.title}

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