Mailing list creation requests
[bpt/portal.git] / init.sml
index 390b9a8..722f1b8 100644 (file)
--- a/init.sml
+++ b/init.sml
@@ -6,6 +6,8 @@ structure C = PgClient
 
 exception Access of string
 
+val urlPrefix = "http://users.hcoop.net/portal/"
+
 fun conn () = C.conn "dbname='hcoop'"
 val close = C.close
 
@@ -14,15 +16,25 @@ type user = {id : int, name : string, rname : string, bal : int, joined : C.time
 val db = ref (NONE : C.conn option)
 val user = ref (NONE : user option)
 
+fun fromSql v =
+    if C.isNull v then
+       "NULL"
+    else
+       C.stringFromSql v
+
+fun rowError (tab, vs) = raise Fail ("Bad " ^ tab ^ "row: " ^ makeSet fromSql vs)
+
 fun getDb () = valOf (!db)
 
 fun mkUserRow [id, name, rname, bal, joined] =
     {id = C.intFromSql id, name = C.stringFromSql name, rname = C.stringFromSql rname,
      bal = C.intFromSql bal, joined = C.timestampFromSql joined}
-  | mkUserRow row = raise Fail ("Bad user row : " ^ makeSet id row)
+  | mkUserRow row = rowError ("user", row)
 
 fun init () =
     let
+       val _ = Util.init ()
+
        val c = conn ()
     in
        C.dml c "BEGIN";
@@ -61,7 +73,7 @@ fun listUsers () =
 
 fun nextSeq (db, seq) =
     case C.oneRow db ($`SELECT nextval('^(seq)')`) of
-       [id] => id
+       [id] => C.intFromSql id
       | _ => raise Fail "Bad next sequence val"
 
 fun addUser (name, rname, bal) =
@@ -70,8 +82,8 @@ fun addUser (name, rname, bal) =
        val id = nextSeq (db, "WebUserSeq")
     in
        C.dml db ($`INSERT INTO WebUser (id, name, rname, bal, joined)
-                   VALUES (^id, ^(C.stringToSql name), ^(C.stringToSql rname), ^(C.intToSql bal), CURRENT_TIMESTAMP)`);
-       C.intFromSql id
+                   VALUES (^(C.intToSql id), ^(C.stringToSql name), ^(C.stringToSql rname), ^(C.intToSql bal), CURRENT_TIMESTAMP)`);
+       id
     end
 
 fun modUser (user : user) =