DBMS database creation
authorAdam Chlipala <adamc@hcoop.net>
Fri, 22 Dec 2006 00:07:54 +0000 (00:07 +0000)
committerAdam Chlipala <adamc@hcoop.net>
Fri, 22 Dec 2006 00:07:54 +0000 (00:07 +0000)
configDefault/postgres.cfg
configDefault/postgres.csg
src/dbms.sig
src/dbms.sml
src/main-dbtool.sml
src/main.sig
src/main.sml
src/msg.sml
src/msgTypes.sml
src/plugins/postgres.sml

index dd00d3d..3205fcd 100644 (file)
@@ -1,5 +1,6 @@
 structure Postgres :> POSTGRES_CONFIG = struct
 
 val adduser = "/usr/bin/sudo /usr/local/sbin/domtool-postgres adduser "
+val createdb = "/usr/bin/sudo /usr/local/sbin/domtool-postgres createdb "
 
 end
index b4e6c09..e09637a 100644 (file)
@@ -1,5 +1,6 @@
 signature POSTGRES_CONFIG = sig
 
 val adduser : string
+val createdb : string
 
 end
index 10bc467..b2a74c0 100644 (file)
 
 signature DBMS = sig
 
-    type handler = {adduser : string -> string option}
+    val validDbname : string -> bool
+
+    type handler = {adduser : string -> string option,
+                   createdb : {user : string, dbname : string} -> string option}
 
     val register : string * handler -> unit
     val lookup : string -> handler option
index 6f7bb9d..caf504a 100644 (file)
@@ -22,7 +22,10 @@ structure Dbms :> DBMS = struct
 
 open DataStructures
 
-type handler = {adduser : string -> string option}
+val validDbname = CharVector.all Char.isAlpha
+
+type handler = {adduser : string -> string option,
+               createdb : {user : string, dbname : string} -> string option}
               
 val dbmses : handler StringMap.map ref = ref StringMap.empty
                  
index 98e6e03..73a02a2 100644 (file)
@@ -27,4 +27,9 @@ val _ =
          | _ =>
            case rest of
                ["adduser"] => Main.requestDbUser dbtype
+             | ["createdb", dbname] =>
+               if Dbms.validDbname dbname then
+                   Main.requestDbTable {dbtype = dbtype, dbname = dbname}
+               else
+                   print ("Invalid database name " ^ dbname ^ ".\n")
              | _ => print "Invalid command-line arguments\n"
index d6c64c5..7e2746a 100644 (file)
@@ -49,5 +49,6 @@ signature MAIN = sig
     val autodocBasis : string -> unit
 
     val requestDbUser : string -> unit
+    val requestDbTable : {dbtype : string, dbname : string} -> unit
 
 end
index f2a45c7..5f2038d 100644 (file)
@@ -401,6 +401,21 @@ fun requestDbUser dbtype =
        OpenSSL.close bio
     end
 
+fun requestDbTable p =
+    let
+       val (user, bio) = requestBio (fn () => ())
+    in
+       Msg.send (bio, MsgCreateDbTable p);
+       case Msg.recv bio of
+           NONE => print "Server closed connection unexpectedly.\n"
+         | SOME m =>
+           case m of
+               MsgOk => print ("Your database " ^ user ^ "_" ^ #dbname p ^ " has been created.\n")
+             | MsgError s => print ("Creation failed: " ^ s ^ "\n")
+             | _ => print "Unexpected server reply.\n";
+       OpenSSL.close bio
+    end
+
 fun regenerate context =
     let
        val b = basis ()
@@ -730,6 +745,44 @@ fun service () =
                                                      handle OpenSSL.OpenSSL _ => ();
                                                      loop ()))
 
+                             | MsgCreateDbTable {dbtype, dbname} =>
+                               if Dbms.validDbname dbname then
+                                   (case Dbms.lookup dbtype of
+                                        NONE => ((Msg.send (bio, MsgError ("Unknown database type " ^ dbtype));
+                                                  print ("Database creation request with unknown datatype type " ^ dbtype);
+                                                  ignore (OpenSSL.readChar bio))
+                                                 handle OpenSSL.OpenSSL _ => ();
+                                                 OpenSSL.close bio
+                                                 handle OpenSSL.OpenSSL _ => ();
+                                                 loop ())
+                                      | SOME handler =>
+                                        case #createdb handler {user = user, dbname = dbname} of
+                                            NONE => ((Msg.send (bio, MsgOk);
+                                                      print ("Created database " ^ user ^ "_" ^ dbname ^ ".\n"))
+                                                     handle OpenSSL.OpenSSL s =>
+                                                            (print "OpenSSL error\n";
+                                                             Msg.send (bio,
+                                                                       MsgError
+                                                                           ("Error during creation: "
+                                                                            ^ s)));
+                                                     (ignore (OpenSSL.readChar bio);
+                                                      OpenSSL.close bio)
+                                                     handle OpenSSL.OpenSSL _ => ();
+                                                     loop ())
+                                          | SOME msg => ((Msg.send (bio, MsgError ("Error creating database: " ^ msg));
+                                                          print ("Error creating database " ^ user ^ "_" ^ dbname ^ ": " ^ msg ^ "\n");
+                                                          ignore (OpenSSL.readChar bio);
+                                                          OpenSSL.close bio)
+                                                         handle OpenSSL.OpenSSL _ => ();
+                                                         loop ()))
+                               else
+                                   ((Msg.send (bio, MsgError ("Invalid database name " ^ dbname));
+                                     print ("Invalid database name " ^ user ^ "_" ^ dbname ^ "\n");
+                                     ignore (OpenSSL.readChar bio);
+                                     OpenSSL.close bio)
+                                    handle OpenSSL.OpenSSL _ => ();
+                                    loop ())
+
                              | _ =>
                                (Msg.send (bio, MsgError "Unexpected command")
                                 handle OpenSSL.OpenSSL _ => ();
index 2b6cd20..23e8ac2 100644 (file)
@@ -100,6 +100,9 @@ fun send (bio, m) =
                          OpenSSL.writeString (bio, dom))
       | MsgCreateDbUser s => (OpenSSL.writeInt (bio, 16);
                              OpenSSL.writeString (bio, s))
+      | MsgCreateDbTable {dbtype, dbname} => (OpenSSL.writeInt (bio, 17);
+                                             OpenSSL.writeString (bio, dbtype);
+                                             OpenSSL.writeString (bio, dbname))
 
 fun checkIt v =
     case v of
@@ -151,6 +154,10 @@ fun recv bio =
                   | 14 => SOME MsgRegenerate
                   | 15 => Option.map MsgRmuser (OpenSSL.readString bio)
                   | 16 => Option.map MsgCreateDbUser (OpenSSL.readString bio)
+                  | 17 => (case (OpenSSL.readString bio, OpenSSL.readString bio) of
+                               (SOME dbtype, SOME dbname) =>
+                               SOME (MsgCreateDbTable {dbtype = dbtype, dbname = dbname})
+                             | _ => NONE)
                   | _ => NONE)
         
 end
index 0c7e5fe..adf1bba 100644 (file)
@@ -56,5 +56,7 @@ datatype msg =
        * that user and no one else has rights. *)
        | MsgCreateDbUser of string
        (* Request creation of a user for the named DBMS type *)
+       | MsgCreateDbTable of {dbtype : string, dbname : string}
+       (* Request creation of a DBMS table *)
 
 end
index a267e4f..87ba252 100644 (file)
@@ -26,6 +26,13 @@ fun adduser user =
     else
        SOME "Error executing CREATE USER script"
 
-val _ = Dbms.register ("postgres", {adduser = adduser})
+fun createdb {user, dbname} =
+    if Slave.shell [Config.Postgres.createdb, user, " ", dbname] then
+       NONE
+    else
+       SOME "Error executing CREATE DATABASE script"
+
+val _ = Dbms.register ("postgres", {adduser = adduser,
+                                   createdb = createdb})
 
 end