From a6eb484bd588045d401e61fed64fac553e0e3395 Mon Sep 17 00:00:00 2001
From: Adam Chlipala
Date: Thu, 23 Oct 2008 18:18:51 -0400
Subject: Crud demo
---
demo/crud.ur | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
demo/crud.urs | 26 +++++++++
demo/crud1.ur | 12 ++++
demo/crud1.urp | 5 ++
demo/prose | 31 ++++++++++
demo/ref.urp | 1 +
demo/sql.urp | 1 +
7 files changed, 254 insertions(+)
create mode 100644 demo/crud.ur
create mode 100644 demo/crud.urs
create mode 100644 demo/crud1.ur
create mode 100644 demo/crud1.urp
diff --git a/demo/crud.ur b/demo/crud.ur
new file mode 100644
index 00000000..472de6d4
--- /dev/null
+++ b/demo/crud.ur
@@ -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
+
+
+ ID |
+ {foldT2RX [colMeta] [tr]
+ (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
+ [[nm] ~ rest] col =>
+ {cdata col.Nam} |
+ )
+ [M.cols] M.cols}
+
+ {rows}
+
+
+
+
+
+
+
+ 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
+
+ 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 --git a/demo/crud.urs b/demo/crud.urs
new file mode 100644
index 00000000..33090421
--- /dev/null
+++ b/demo/crud.urs
@@ -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 --git a/demo/crud1.ur b/demo/crud1.ur
new file mode 100644
index 00000000..3849e822
--- /dev/null
+++ b/demo/crud1.ur
@@ -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 --git a/demo/crud1.urp b/demo/crud1.urp
new file mode 100644
index 00000000..bfc2d14e
--- /dev/null
+++ b/demo/crud1.urp
@@ -0,0 +1,5 @@
+database dbname=test
+sql crud1.sql
+
+crud
+crud1
diff --git a/demo/prose b/demo/prose
index e447aee3..6b7ddf29 100644
--- a/demo/prose
+++ b/demo/prose
@@ -121,3 +121,34 @@ metaform1.urp
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:
+
+
+- A display name
+- A way of pretty-printing values of the column
+- A way of generating an HTML form widget to input this column
+- A way of generating an HTML form widget with an initial value specified
+- A way of parsing values of the column from strings
+- A type class witness, showing that the SQL representation can really be included in SQL
+
+
+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:
+
+
+- A type pair record cols
+- A proof that cols does not contain a field named Id
+- A SQL table tab with an Id field of type int and other fields whose names and types are read off of cols
+- A display title for the admin interface
+- A record of meta-data for the columns
+
+
+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 --git a/demo/ref.urp b/demo/ref.urp
index c00e5406..a6bb1de3 100644
--- a/demo/ref.urp
+++ b/demo/ref.urp
@@ -1,4 +1,5 @@
database dbname=test
+sql ref.sql
refFun
ref
diff --git a/demo/sql.urp b/demo/sql.urp
index 1b8bb5a4..7894da95 100644
--- a/demo/sql.urp
+++ b/demo/sql.urp
@@ -1,3 +1,4 @@
database dbname=test
+sql sql.sql
sql
--
cgit v1.2.3