# HG changeset patch # User Adam Chlipala # Date 1224800331 14400 # Node ID 0767d7ad0c3a09e752983bf803e5df73d2915f97 # Parent 9119a5920106b137e97ee859175ff6c899782ba8 Crud demo diff -r 9119a5920106 -r 0767d7ad0c3a demo/crud.ur --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/crud.ur Thu Oct 23 18:18:51 2008 -0400 @@ -0,0 +1,178 @@ +con colMeta = fn t_formT :: (Type * Type) => { + Nam : string, + Show : t_formT.1 -> xbody, + Widget : nm :: Name -> xml form [] [nm = t_formT.2], + WidgetPopulated : nm :: Name -> t_formT.1 -> xml form [] [nm = t_formT.2], + Parse : t_formT.2 -> t_formT.1, + Inject : sql_injectable t_formT.1 + } +con colsMeta = fn cols :: {(Type * Type)} => $(Top.mapT2T colMeta cols) + +fun default (t ::: Type) (sh : show t) (rd : read t) (inj : sql_injectable t) + name : colMeta (t, string) = + {Nam = name, + Show = txt, + Widget = fn nm :: Name => , + WidgetPopulated = fn (nm :: Name) n => + , + Parse = readError, + Inject = _} + +val int = default +val float = default +val string = default + +fun bool name = {Nam = name, + Show = txt, + Widget = fn nm :: Name => , + WidgetPopulated = fn (nm :: Name) b => + , + Parse = fn x => x, + Inject = _} + +functor Make(M : sig + con cols :: {(Type * Type)} + constraint [Id] ~ cols + val tab : sql_table ([Id = int] ++ mapT2T fstTT cols) + + val title : string + + val cols : colsMeta cols + end) = struct + + open constraints M + val tab = M.tab + + sequence seq + + fun list () = + rows <- queryX (SELECT * FROM tab AS T) + (fn (fs : {T : $([Id = int] ++ mapT2T fstTT M.cols)}) => + + {[fs.T.Id]} + {foldT2RX2 [fstTT] [colMeta] [tr] + (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) + [[nm] ~ rest] v col => + {col.Show v} + ) + [M.cols] (fs.T -- #Id) M.cols} + + [Update] + [Delete] + + + ); + return + + + + {foldT2RX [colMeta] [tr] + (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) + [[nm] ~ rest] col => + + ) + [M.cols] M.cols} + + {rows} +
ID{cdata col.Nam}
+ +


+ +
+ {foldT2R [colMeta] [fn cols :: {(Type * Type)} => xml form [] (mapT2T sndTT cols)] + (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) + [[nm] ~ rest] (col : colMeta t) (acc : xml form [] (mapT2T sndTT rest)) => +
  • {cdata col.Nam}: {col.Widget [nm]}
  • + {useMore acc} +
    ) + + [M.cols] M.cols} + + + +
    + + and create (inputs : $(mapT2T sndTT M.cols)) = + id <- nextval seq; + () <- dml (insert tab + (foldT2R2 [sndTT] [colMeta] + [fn cols => $(mapT2T (fn t :: (Type * Type) => + sql_exp [] [] [] t.1) cols)] + (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) + [[nm] ~ rest] => + fn input col acc => acc with nm = @sql_inject col.Inject (col.Parse input)) + {} [M.cols] inputs M.cols + with #Id = (SQL {id}))); + ls <- list (); + return +

    Inserted with ID {[id]}.

    + + {ls} +
    + + and save (id : int) (inputs : $(mapT2T sndTT M.cols)) = + () <- dml (update [mapT2T fstTT M.cols] + (foldT2R2 [sndTT] [colMeta] + [fn cols => $(mapT2T (fn t :: (Type * Type) => + sql_exp [T = [Id = int] + ++ mapT2T fstTT M.cols] + [] [] t.1) cols)] + (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)}) + [[nm] ~ rest] => + fn input col acc => acc with nm = + @sql_inject col.Inject (col.Parse input)) + {} [M.cols] inputs M.cols) + tab (WHERE T.Id = {id})); + ls <- list (); + return +

    Saved!

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

    The deed is done.

    + + {ls} +
    + + and confirm (id : int) = return +

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

    + +

    I was born sure!

    +
    + + and main () = + ls <- list (); + return + {cdata M.title} + + +

    {cdata M.title}

    + + {ls} +
    + +end diff -r 9119a5920106 -r 0767d7ad0c3a demo/crud.urs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/crud.urs Thu Oct 23 18:18:51 2008 -0400 @@ -0,0 +1,26 @@ +con colMeta = fn t_formT :: (Type * Type) => + {Nam : string, + Show : t_formT.1 -> xbody, + Widget : nm :: Name -> xml form [] [nm = t_formT.2], + WidgetPopulated : nm :: Name -> t_formT.1 + -> xml form [] [nm = t_formT.2], + Parse : t_formT.2 -> t_formT.1, + Inject : sql_injectable t_formT.1} +con colsMeta = fn cols :: {(Type * Type)} => $(mapT2T colMeta cols) + +val int : string -> colMeta (int, string) +val float : string -> colMeta (float, string) +val string : string -> colMeta (string, string) +val bool : string -> colMeta (bool, bool) + +functor Make(M : sig + con cols :: {(Type * Type)} + constraint [Id] ~ cols + val tab : sql_table ([Id = int] ++ mapT2T fstTT cols) + + val title : string + + val cols : colsMeta cols + end) : sig + val main : unit -> transaction page +end diff -r 9119a5920106 -r 0767d7ad0c3a demo/crud1.ur --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/crud1.ur Thu Oct 23 18:18:51 2008 -0400 @@ -0,0 +1,12 @@ +table t1 : {Id : int, A : int, B : string, C : float, D : bool} + +open Crud.Make(struct + val tab = t1 + + val title = "Crud1" + + val cols = {A = Crud.int "A", + B = Crud.string "B", + C = Crud.float "C", + D = Crud.bool "D"} + end) diff -r 9119a5920106 -r 0767d7ad0c3a demo/crud1.urp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/crud1.urp Thu Oct 23 18:18:51 2008 -0400 @@ -0,0 +1,5 @@ +database dbname=test +sql crud1.sql + +crud +crud1 diff -r 9119a5920106 -r 0767d7ad0c3a demo/prose --- a/demo/prose Thu Oct 23 17:52:04 2008 -0400 +++ b/demo/prose Thu Oct 23 18:18:51 2008 -0400 @@ -121,3 +121,34 @@ metaform2.urp

    This example showcases code reuse by applying the same functor as in the last example. The Metaform2 module mixes pages from the functor with some new pages of its own.

    + +crud1.urp + +

    This example pulls together much of what we have seen so far. It involves a generic "admin interface" builder. That is, we have the Crud.Make functor, which takes in a description of a table and outputs a sub-application for viewing and editing that table.

    + +

    The signature of Crud.Make is based around a type function colMeta, which describes which supporting values we need for each column. This function is declared with the keyword con, which stands for "constructor," the general class of "compile-time things" that includes types. An argument to colMeta has kind (Type * Type), which means that it must be a type-level tuple. The first type is how the column is represented in SQL, and the second is how we represent it in HTML forms. In order, the components of the resulting record give: + +

      +
    1. A display name
    2. +
    3. A way of pretty-printing values of the column
    4. +
    5. A way of generating an HTML form widget to input this column
    6. +
    7. A way of generating an HTML form widget with an initial value specified
    8. +
    9. A way of parsing values of the column from strings
    10. +
    11. A type class witness, showing that the SQL representation can really be included in SQL
    12. +

    + +

    The function colsMeta lifts colMeta over type-level records of type pairs. The Crud module also defines reasonable default colMeta values for some primitive types.

    + +

    The functor signature tells us (in order) that an input must contain: + +

      +
    1. A type pair record cols
    2. +
    3. A proof that cols does not contain a field named Id
    4. +
    5. A SQL table tab with an Id field of type int and other fields whose names and types are read off of cols
    6. +
    7. A display title for the admin interface
    8. +
    9. A record of meta-data for the columns
    10. +

    + +

    Looking at crud1.ur, we see that a use of the functor is almost trivial. Only the value components of the argument structure must be provided. The column row type is inferred, and the disjointness constraint is proved automatically.

    + +

    We won't go into detail on the implementation of Crud.Make. The types of the functions used there can be found in the signatures of the built-in Basis module and the Top module from the standard library. The signature of the first and the signature and implementation of the second can be found in the lib directory of the Ur/Web distribution.

    diff -r 9119a5920106 -r 0767d7ad0c3a demo/ref.urp --- a/demo/ref.urp Thu Oct 23 17:52:04 2008 -0400 +++ b/demo/ref.urp Thu Oct 23 18:18:51 2008 -0400 @@ -1,4 +1,5 @@ database dbname=test +sql ref.sql refFun ref diff -r 9119a5920106 -r 0767d7ad0c3a demo/sql.urp --- a/demo/sql.urp Thu Oct 23 17:52:04 2008 -0400 +++ b/demo/sql.urp Thu Oct 23 18:18:51 2008 -0400 @@ -1,3 +1,4 @@ database dbname=test +sql sql.sql sql