changeset 988:d923b47e483d

Basic CRUD operations in Orm
author Adam Chlipala <adamc@hcoop.net>
date Mon, 05 Oct 2009 17:00:04 -0400
parents 6dd122f10c0c
children 0bdc4d538f1c
files CHANGELOG demo/more/orm.ur demo/more/orm.urp demo/more/orm.urs
diffstat 4 files changed, 35 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGELOG	Mon Oct 05 16:36:38 2009 -0400
+++ b/CHANGELOG	Mon Oct 05 17:00:04 2009 -0400
@@ -1,3 +1,11 @@
+========
+Next
+========
+
+- Bug fixes
+- Improvement to choice of line number to cite in record unification error
+  messages
+
 ========
 20090926
 ========
--- a/demo/more/orm.ur	Mon Oct 05 16:36:38 2009 -0400
+++ b/demo/more/orm.ur	Mon Oct 05 17:00:04 2009 -0400
@@ -19,11 +19,25 @@
     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
--- a/demo/more/orm.urp	Mon Oct 05 16:36:38 2009 -0400
+++ b/demo/more/orm.urp	Mon Oct 05 17:00:04 2009 -0400
@@ -1,2 +1,3 @@
 
+$/option
 orm
--- a/demo/more/orm.urs	Mon Oct 05 16:36:38 2009 -0400
+++ b/demo/more/orm.urs	Mon Oct 05 17:00:04 2009 -0400
@@ -15,5 +15,11 @@
     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