summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/top.ur8
-rw-r--r--lib/top.urs10
-rw-r--r--tests/time.ur4
3 files changed, 21 insertions, 1 deletions
diff --git a/lib/top.ur b/lib/top.ur
index 0bc345de..d36af3f3 100644
--- a/lib/top.ur
+++ b/lib/top.ur
@@ -157,3 +157,11 @@ fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
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)
+
diff --git a/lib/top.urs b/lib/top.urs
index 22cebb16..6e9dda4e 100644
--- a/lib/top.urs
+++ b/lib/top.urs
@@ -116,3 +116,13 @@ val oneOrNoRows : tables ::: {{Type}} -> exps ::: {Type}
[[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)
diff --git a/tests/time.ur b/tests/time.ur
index f66004a5..f81c59c3 100644
--- a/tests/time.ur
+++ b/tests/time.ur
@@ -7,7 +7,9 @@ fun main () =
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>