summaryrefslogtreecommitdiff
path: root/demo
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2008-10-21 18:44:52 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2008-10-21 18:44:52 -0400
commitd66bb9f256db65e3487dec361a4a5a9d7ee238b0 (patch)
tree02a7bb0fa4faec1697c91fbc22c46014b3ff49c5 /demo
parentd249f18f65213b5a198f39e254982293ecaa0e10 (diff)
Sql demo
Diffstat (limited to 'demo')
-rw-r--r--demo/prose12
-rw-r--r--demo/sql.ur52
-rw-r--r--demo/sql.urp3
-rw-r--r--demo/sql.urs1
4 files changed, 68 insertions, 0 deletions
diff --git a/demo/prose b/demo/prose
index 5035108c..a863f6c5 100644
--- a/demo/prose
+++ b/demo/prose
@@ -59,3 +59,15 @@ listShop.urp
<p>The <tt>ListFun</tt> module defines a functor for building list editing sub-applications. An argument to the functor <tt>Make</tt> must give the type to be stored in the lists, along with marshaling and unmarshaling functions. In return, the functor returns an entry point function.</p>
<p>The <tt>ListShop</tt> modules ties everything together by instantiating <tt>ListFun.Make</tt> with structures for integers and strings. <tt>show</tt> and <tt>read</tt> can be used for marshaling and unmarshaling in both cases because they are type-class-generic.</p>
+
+sql.urp
+
+<p>We see a simple example of accessing a SQL database. The project file specifies the database to connect to.</p>
+
+<p>A <tt>table</tt> declaration declares a SQL table with rows of a particular record type. We can use embedded SQL syntax in a way that leads to all of our queries and updates being type-checked. Indeed, Ur/Web makes strong guarantees that it is impossible to execute invalid SQL queries or make bad assumptions about the types of tables for marshaling and unmarshaling (which happen implicitly).</p>
+
+<p>The <tt>list</tt> function implements an HTML table view of all rows in the SQL table. The <tt>queryX</tt> function takes two arguments: a SQL query and a function for generating XML fragments from query result rows. The query is run, and the fragments for the rows are concatenated together.</p>
+
+<p>Other functions demonstrate use of the <tt>dml</tt> function, for building a transaction from a SQL DML command. It is easy to insert antiquoted Ur code into queries and DML commands, and the type-checker catches mistakes in the types of the expressions that we insert.</p>
+
+<p>
diff --git a/demo/sql.ur b/demo/sql.ur
new file mode 100644
index 00000000..9e9effff
--- /dev/null
+++ b/demo/sql.ur
@@ -0,0 +1,52 @@
+table t : { A : int, B : float, C : string, D : bool }
+
+fun list () =
+ rows <- queryX (SELECT * FROM t)
+ (fn row => <xml><tr>
+ <td>{[row.T.A]}</td> <td>{[row.T.B]}</td> <td>{[row.T.C]}</td> <td>{[row.T.D]}</td>
+ <td><a link={delete row.T.A}>[delete]</a></td>
+ </tr></xml>);
+ return <xml>
+ <table border=1>
+ <tr> <th>A</th> <th>B</th> <th>C</th> <th>D</th> </tr>
+ {rows}
+ </table>
+
+ <br/><hr/><br/>
+
+ <form>
+ <table>
+ <tr> <th>A:</th> <td><textbox{#A}/></td> </tr>
+ <tr> <th>B:</th> <td><textbox{#B}/></td> </tr>
+ <tr> <th>C:</th> <td><textbox{#C}/></td> </tr>
+ <tr> <th>D:</th> <td><checkbox{#D}/></td> </tr>
+ <tr> <th/> <td><submit action={add} value="Add Row"/></td> </tr>
+ </table>
+ </form>
+ </xml>
+
+and add r =
+ () <- dml (INSERT INTO t (A, B, C, D)
+ VALUES ({readError r.A}, {readError r.B}, {r.C}, {r.D}));
+ xml <- list ();
+ return <xml><body>
+ <p>Row added.</p>
+
+ {xml}
+ </body></xml>
+
+and delete a =
+ () <- dml (DELETE FROM t
+ WHERE t.A = {a});
+ xml <- list ();
+ return <xml><body>
+ <p>Row deleted.</p>
+
+ {xml}
+ </body></xml>
+
+fun main () =
+ xml <- list ();
+ return <xml><body>
+ {xml}
+ </body></xml>
diff --git a/demo/sql.urp b/demo/sql.urp
new file mode 100644
index 00000000..1b8bb5a4
--- /dev/null
+++ b/demo/sql.urp
@@ -0,0 +1,3 @@
+database dbname=test
+
+sql
diff --git a/demo/sql.urs b/demo/sql.urs
new file mode 100644
index 00000000..6ac44e0b
--- /dev/null
+++ b/demo/sql.urs
@@ -0,0 +1 @@
+val main : unit -> transaction page