db_bridge/postgresql_db.cpp
changeset 17 b3731a25b9ec
parent 15 5a0ca1f9a2f1
child 18 f3657061ec00
equal deleted inserted replaced
16:2b4ef74a16aa 17:b3731a25b9ec
     6 #include <postgresql_db.h>
     6 #include <postgresql_db.h>
     7 #include <exception>
     7 #include <exception>
     8 
     8 
     9 PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
     9 PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
    10 {
    10 {
    11     server = (srv != "") ? "host=" + srv : "";
    11     server = srv;
    12     user = (usr != "") ? "user=" + usr : "user=" + std::string (getenv ("USER"));
    12     user = (usr != "") ? usr : getenv ("USER");
    13     password = (pwd != "") ? "password=" + pwd : "";
    13     password = pwd;
    14     database = "dbname=" + db;
    14     database = db;
       
    15 
       
    16     conninfo = "user=" + user + "dbname=" + database;
    15 
    17 
    16     if (server != "" && pwd == "") {
    18     if (server != "" && pwd == "") {
    17         std::cerr << "HBA funktioniert nur mit Passwoertern..." << std::endl;
    19         std::cout << "Passwort von " << user << "@" << server << ": ";
       
    20         std::getline (std::cin, password);
       
    21         conninfo += " host=" + server + "password=" + password;
    18     }
    22     }
    19 
       
    20     conninfo = server + " " + user + " " + password + " " + database;
       
    21 }
    23 }
    22 
    24 
    23 PostgreSQL_DB::~PostgreSQL_DB ()
    25 PostgreSQL_DB::~PostgreSQL_DB ()
    24 {
    26 {
    25     if (conn != NULL) {
    27     if (conn != NULL) {
    28     }
    30     }
    29 }
    31 }
    30 
    32 
    31 bool PostgreSQL_DB::connect ()
    33 bool PostgreSQL_DB::connect ()
    32 {
    34 {
       
    35     std::cerr << "Verbindungsparameter:" << " host=" << ((server != "") ? server : "LOCAL")
       
    36         << " user=" << user << " password=" << password << " database=" << database << std::endl;
       
    37 
    33     if ((conn = PQconnectdb (conninfo.c_str ())) == NULL) {
    38     if ((conn = PQconnectdb (conninfo.c_str ())) == NULL) {
    34         std::cerr << server << ": " << "ERROR " << std::endl;
    39         std::cerr << server << ": " << "ERROR " << std::endl;
    35         return false;
    40         return false;
    36     }
    41     }
    37 
    42 
    38     std::cerr << "Verbindungsparameter: " << conninfo << std::endl;
       
    39     return true;
    43     return true;
    40 }
    44 }
    41 
    45 
    42 Abstract_DB::DB_RESULT PostgreSQL_DB::query (std::string sql_string)
    46 Abstract_DB::DB_RESULT PostgreSQL_DB::query (std::string sql_string)
    43 {
    47 {