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
signature POSTGRES_CONFIG = sig
val adduser : string
+val createdb : string
end
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
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
| _ =>
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"
val autodocBasis : string -> unit
val requestDbUser : string -> unit
+ val requestDbTable : {dbtype : string, dbname : string} -> unit
end
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 ()
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 _ => ();
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
| 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
* 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
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