changeset 440:19d7f79cd584

Time MIN/MAX
author Adam Chlipala <adamc@hcoop.net>
date Thu, 30 Oct 2008 15:16:37 -0400
parents 322c8620bbdf
children c5335613f31e
files lib/top.ur lib/top.urs tests/time.ur
diffstat 3 files changed, 21 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/lib/top.ur	Thu Oct 30 15:11:37 2008 -0400
+++ b/lib/top.ur	Thu Oct 30 15:16:37 2008 -0400
@@ -157,3 +157,11 @@
     query q
           (fn fs _ => return (Some fs))
           None
+
+fun oneRow (tables ::: {{Type}}) (exps ::: {Type})
+                (q : sql_query tables exps) [tables ~ exps] =
+    o <- oneOrNoRows q;
+    return (case o of
+                None => error <xml>Query returned no rows</xml>
+              | Some r => r)
+
--- a/lib/top.urs	Thu Oct 30 15:11:37 2008 -0400
+++ b/lib/top.urs	Thu Oct 30 15:16:37 2008 -0400
@@ -116,3 +116,13 @@
                                                       [[nm] ~ acc] =>
                                                    [nm = $fields] ++ acc)
                                                    [] tables))
+
+val oneRow : tables ::: {{Type}} -> exps ::: {Type}
+             -> sql_query tables exps
+             -> fn [tables ~ exps] =>
+                   transaction
+                       $(exps
+                             ++ fold (fn nm (fields :: {Type}) acc
+                                            [[nm] ~ acc] =>
+                                         [nm = $fields] ++ acc)
+                                         [] tables)
--- a/tests/time.ur	Thu Oct 30 15:11:37 2008 -0400
+++ b/tests/time.ur	Thu Oct 30 15:16:37 2008 -0400
@@ -7,7 +7,9 @@
     dml (INSERT INTO t (Id, Time) VALUES (42, {now}));
     xml <- queryX (SELECT * FROM t)
            (fn r => <xml>{[r.T.Id]}: {[r.T.Time]}<br/></xml>);
+    minMax <- oneRow (SELECT MIN(t.Time) AS Min, MAX(t.Time) AS Max FROM t);
     return <xml><body>
       {xml}
-      {[now]}, {[now = now]}, {[now = later]}, {[later < now]}, {[now < later]}
+      {[now]}, {[now = now]}, {[now = later]}, {[later < now]}, {[now < later]}<br/>
+      {[minMax.Min]}, {[minMax.Max]}
     </body></xml>