I have improved the connection info and cleaned something up.
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:09 +0100
changeset 17 b3731a25b9ec
parent 16 2b4ef74a16aa
child 18 f3657061ec00
I have improved the connection info and cleaned something up. committer: Markus Bröker <mbroeker@largo.homelinux.org>
db_bridge/Makefile
db_bridge/include/abstract_db.h
db_bridge/main.cpp
db_bridge/mysql_db.cpp
db_bridge/postgresql_db.cpp
--- a/db_bridge/Makefile
+++ b/db_bridge/Makefile
@@ -24,5 +24,5 @@
 
 clean:
 	rm -f *.o;
-	rm -f *~
+	rm -f *~ include/*~
 	rm -f $(TARGET)
--- a/db_bridge/include/abstract_db.h
+++ b/db_bridge/include/abstract_db.h
@@ -17,7 +17,7 @@
     std::string server;
 
   public:
-    typedef std::vector < std::vector <std::string> >DB_RESULT;
+    typedef std::vector < std::vector<std::string> >DB_RESULT;
     typedef std::vector <std::string> DB_ROW;
 
     virtual bool connect () = 0;
--- a/db_bridge/main.cpp
+++ b/db_bridge/main.cpp
@@ -14,11 +14,11 @@
 void usage (std::string progname)
 {
     std::cout << "Benutzung: " << progname << " [-h] [-r <remotehost>] [-u <user>] [-p <password>] DATABASE\n";
-	std::cout << "\t\t-r <remotehost>\t\tVerbindung zu Fremdrechner <remotehost>\n";
-	std::cout << "\t\t-u <user>\t\tAnmeldung als <user>\n";
-	std::cout << "\t\t-p <password>\t\tAnmelden mit dem Passwort <password>\n";
-	std::cout << "\n";
-	std::cout << "\t\tDATABASE\t\tVerbindung zur Datenbank DATABASE herstellen\n";
+    std::cout << "\t\t-r <remotehost>\t\tVerbindung zu Fremdrechner <remotehost>\n";
+    std::cout << "\t\t-u <user>\t\tAnmeldung als <user>\n";
+    std::cout << "\t\t-p <password>\t\tAnmelden mit dem Passwort <password>\n";
+    std::cout << "\n";
+    std::cout << "\t\tDATABASE\t\tVerbindung zur Datenbank DATABASE herstellen\n";
     std::cout << "\nBerichten Sie Fehler oder Anregungen an mbroeker@largo.homelinux.org" << std::endl;
     exit (0);
 }
@@ -63,7 +63,7 @@
 #endif
 
     if (db->connect () == false) {
-        std::cerr << "Kann mich nicht zur Datenbank verbinden. Passwoerter falsch?" << std::endl;
+        std::cerr << "Kann mich nicht zur Datenbank verbinden." << std::endl;
         return EXIT_FAILURE;
     }
 
--- a/db_bridge/mysql_db.cpp
+++ b/db_bridge/mysql_db.cpp
@@ -8,10 +8,15 @@
 MySQL_DB::MySQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
 {
     server = srv;
-    user = usr;
+    user = (usr != "") ? usr : getenv ("USER");
     password = pwd;
     database = db;
     conn = mysql_init (NULL);
+
+    if (server != "" && pwd == "") {
+        std::cout << "Passwort von " << user << "@" << server << ": ";
+        std::getline (std::cin, password);
+    }
 }
 
 MySQL_DB::~MySQL_DB ()
@@ -24,6 +29,9 @@
 
 bool MySQL_DB::connect ()
 {
+    std::cerr << "Verbindungsparameter:" << " host=" << ((server != "") ? server : "LOCAL")
+        << " user=" << user << " password=" << password << " database=" << database << std::endl;
+
     if (!mysql_real_connect (conn, server.c_str (), user.c_str (), password.c_str (), database.c_str (), 0, NULL, 0)) {
         std::cerr << server << ": " << mysql_error (conn) << std::endl;
         return false;
--- a/db_bridge/postgresql_db.cpp
+++ b/db_bridge/postgresql_db.cpp
@@ -8,16 +8,18 @@
 
 PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
 {
-    server = (srv != "") ? "host=" + srv : "";
-    user = (usr != "") ? "user=" + usr : "user=" + std::string (getenv ("USER"));
-    password = (pwd != "") ? "password=" + pwd : "";
-    database = "dbname=" + db;
+    server = srv;
+    user = (usr != "") ? usr : getenv ("USER");
+    password = pwd;
+    database = db;
+
+    conninfo = "user=" + user + "dbname=" + database;
 
     if (server != "" && pwd == "") {
-        std::cerr << "HBA funktioniert nur mit Passwoertern..." << std::endl;
+        std::cout << "Passwort von " << user << "@" << server << ": ";
+        std::getline (std::cin, password);
+        conninfo += " host=" + server + "password=" + password;
     }
-
-    conninfo = server + " " + user + " " + password + " " + database;
 }
 
 PostgreSQL_DB::~PostgreSQL_DB ()
@@ -30,12 +32,14 @@
 
 bool PostgreSQL_DB::connect ()
 {
+    std::cerr << "Verbindungsparameter:" << " host=" << ((server != "") ? server : "LOCAL")
+        << " user=" << user << " password=" << password << " database=" << database << std::endl;
+
     if ((conn = PQconnectdb (conninfo.c_str ())) == NULL) {
         std::cerr << server << ": " << "ERROR " << std::endl;
         return false;
     }
 
-    std::cerr << "Verbindungsparameter: " << conninfo << std::endl;
     return true;
 }