db_bridge/postgresql_db.cpp
changeset 12 9f0ce4eaa1ce
child 14 862d63715611
equal deleted inserted replaced
11:0a8be4eac87e 12:9f0ce4eaa1ce
       
     1 /**
       
     2  * db_bridge/postgresql_db.cpp
       
     3  * Copyright 2008 (C) Markus Broeker
       
     4  */
       
     5 
       
     6 #include <postgresql_db.h>
       
     7 
       
     8 PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
       
     9 {
       
    10     server = (srv != "") ? "host=" + srv : "";
       
    11     user = (usr != "") ? "user=" + usr : "user=" + std::string (getenv ("USER"));
       
    12     password = (pwd != "") ? "password=" + pwd : "";
       
    13     database = "dbname=" + db;
       
    14 
       
    15     if (server != "" && pwd == "") {
       
    16         std::cerr << "HBA funktioniert nur mit Passwoertern..." << std::endl;
       
    17     }
       
    18 
       
    19     conninfo = server + " " + user + " " + password + " " + database;
       
    20 }
       
    21 
       
    22 PostgreSQL_DB::~PostgreSQL_DB ()
       
    23 {
       
    24     if (conn != NULL) {
       
    25         PQfinish (conn);
       
    26         std::cerr << "Datenbank wird geschlossen" << std::endl;
       
    27     }
       
    28 }
       
    29 
       
    30 bool PostgreSQL_DB::connect ()
       
    31 {
       
    32     if ((conn = PQconnectdb (conninfo.c_str ())) == NULL) {
       
    33         std::cerr << server << ": " << "ERROR " << std::endl;
       
    34         return false;
       
    35     }
       
    36 
       
    37     std::cerr << "Verbindungsparameter: " << conninfo << std::endl;
       
    38     return true;
       
    39 }
       
    40 
       
    41 std::vector < std::vector<std::string> >PostgreSQL_DB::query (std::string sql_string)
       
    42 {
       
    43     std::vector <std::string> vec;
       
    44     std::vector < std::vector <std::string> >rows;
       
    45     PGresult *res;
       
    46     int i, j, nFields;
       
    47 
       
    48     /*
       
    49      * send SQL query
       
    50      */
       
    51     res = PQexec (conn, sql_string.c_str ());
       
    52 
       
    53     nFields = PQnfields (res);
       
    54 
       
    55     std::cout << "Die Abfrage ergab " << nFields << " Zeilen." << std::endl;
       
    56 
       
    57     /*
       
    58      * push everything into a vector< vector<string> >
       
    59      */
       
    60     for (i = 0; i < PQntuples (res); i++) {
       
    61         for (j = 0; j < nFields; j++) {
       
    62             vec.push_back (PQgetvalue (res, i, j));
       
    63         }
       
    64         rows.push_back (vec);
       
    65         vec.clear ();
       
    66     }
       
    67 
       
    68     if (res != NULL) {
       
    69         PQclear (res);
       
    70     }
       
    71 
       
    72     return rows;
       
    73 }