summaryrefslogtreecommitdiff
path: root/demo
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2009-10-05 17:00:04 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2009-10-05 17:00:04 -0400
commit0462faba5cb84757ef5d3223e025b00da7cf4aad (patch)
tree7dcc49f2bcd888768b554a1673e2032f117ed41b /demo
parent78e557c3b405113b62bc945382415895860a5825 (diff)
Basic CRUD operations in Orm
Diffstat (limited to 'demo')
-rw-r--r--demo/more/orm.ur24
-rw-r--r--demo/more/orm.urp1
-rw-r--r--demo/more/orm.urs8
3 files changed, 27 insertions, 6 deletions
diff --git a/demo/more/orm.ur b/demo/more/orm.ur
index 9abc0234..16b82c84 100644
--- a/demo/more/orm.ur
+++ b/demo/more/orm.ur
@@ -19,11 +19,25 @@ functor Table(M : sig
sequence s
table t : ([Id = id] ++ M.cols)
+ type row = $([Id = id] ++ M.cols)
+
+ fun ensql [avail] (r : $M.cols) : $(map (sql_exp avail [] []) M.cols) =
+ map2 [meta] [Top.id] [sql_exp avail [] []]
+ (fn [t] meta v => @sql_inject meta.Inj v)
+ [_] M.folder M.cols r
+
fun create (r : $M.cols) =
id <- nextval s;
- dml (insert t ({Id = sql_inject id}
- ++ map2 [meta] [Top.id] [sql_exp [] [] []]
- (fn [t ::: Type] (meta : meta t) (v : t) => @sql_inject meta.Inj v)
- [_] M.folder M.cols r));
- return id
+ dml (insert t ({Id = sql_inject id} ++ ensql r));
+ return ({Id = id} ++ r)
+
+ fun delete r = dml (DELETE FROM t WHERE t.Id = {[r.Id]})
+
+ fun save r = dml (update [M.cols] ! (ensql (r -- #Id)) t (WHERE T.Id = {[r.Id]}))
+
+ fun lookup id =
+ ro <- oneOrNoRows (SELECT * FROM t WHERE t.Id = {[id]});
+ return (Option.mp (fn r => r.T) ro)
+
+ val list = query (SELECT * FROM t) (fn r ls => return (r.T :: ls)) []
end
diff --git a/demo/more/orm.urp b/demo/more/orm.urp
index 7380983a..01a50b52 100644
--- a/demo/more/orm.urp
+++ b/demo/more/orm.urp
@@ -1,2 +1,3 @@
+$/option
orm
diff --git a/demo/more/orm.urs b/demo/more/orm.urs
index 8fab3ae8..f25f9ff0 100644
--- a/demo/more/orm.urs
+++ b/demo/more/orm.urs
@@ -15,5 +15,11 @@ functor Table(M : sig
val inj : sql_injectable id
val id : meta id
- val create : $M.cols -> transaction id
+ type row = $([Id = id] ++ M.cols)
+
+ val create : $M.cols -> transaction row
+ val delete : row -> transaction unit
+ val save : row -> transaction unit
+ val lookup : id -> transaction (option row)
+ val list : transaction (list row)
end