# HG changeset patch # User Adam Chlipala # Date 1241377980 14400 # Node ID 7b47fc964a0f5becc294079c9a1ec2c407f3efa9 # Parent 87a7702d681d6a51446d1b194992d23443f73ad2 view demo diff -r 87a7702d681d -r 7b47fc964a0f demo/prose --- a/demo/prose Sun May 03 14:57:33 2009 -0400 +++ b/demo/prose Sun May 03 15:13:00 2009 -0400 @@ -139,6 +139,10 @@

SQL outer joins are no problem, as this demo shows. Unlike with SQL, here we have static type inference determining for us which columns may become nullable as a result of an outer join, and the compiler will reject programs that make the wrong assumptions about that process. The details of that nullification don't appear in this example, where the magic of type classes determines both the post-join type of each field and the right pretty-printing and parsing function for each of those types.

+view.urp + +

SQL views are also supported with a special declaration form, analogous to table. A multi-parameter type class fieldsOf is used to characterize places where both tables and views are allowed. For instance, the polymorphic function list shown here lists the contents of any table or view containing just a single int column named A.

+ sum.urp

Metaprogramming is one of the most important facilities of Ur. This example shows how to write a function that is able to sum up the fields of records of integers, no matter which set of fields the particular record has.

diff -r 87a7702d681d -r 7b47fc964a0f demo/view.ur --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/view.ur Sun May 03 15:13:00 2009 -0400 @@ -0,0 +1,25 @@ +table t : { A : int } +view v = SELECT t.A AS A FROM t WHERE t.A > 7 + +fun list (u ::: Type) (_ : fieldsOf u [A = int]) (title : string) (x : u) = + xml <- queryX (SELECT * FROM x) + (fn r : {X : {A : int}} =>
  • {[r.X.A]}
  • ); + return +

    {[title]}

    + +
    + +fun main () = + listT <- list "T" t; + listV <- list "V" v; + return + {listT} + {listV} +
    + +
    Insert: +
    + +and ins r = + dml (INSERT INTO t (A) VALUES ({[readError r.A]})); + main () diff -r 87a7702d681d -r 7b47fc964a0f demo/view.urp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/view.urp Sun May 03 15:13:00 2009 -0400 @@ -0,0 +1,4 @@ +database dbname=test +sql view.sql + +view diff -r 87a7702d681d -r 7b47fc964a0f demo/view.urs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/view.urs Sun May 03 15:13:00 2009 -0400 @@ -0,0 +1,1 @@ +val main : unit -> transaction page