postgres_util.h

Go to the documentation of this file.
00001 
00004 #ifndef __postgres_util_h
00005 #define __postgres_util_h
00006 #include "globals.h"
00007 #include "libpq-fe.h"
00008 #include "model.h"
00009 #include <iomanip>
00010 #include <iostream>
00011 #include <vector>
00012 #include <map>
00013 
00017 class Hazy_Database {
00018  private:
00020   PGconn *conn;
00022   PGresult *res;
00026   void exit_nicely()
00027   {
00028     fprintf(stderr, "problem: %s", PQerrorMessage(conn));
00029     PQfinish(conn);
00030   }
00031   
00032  public:
00034   Hazy_Database(std::string dbname);
00042   int execute_statement_msg(const char* stmt, const char *msg);
00051   int execute_query_msg_text(const char* query, const char* msg, std::string &result);
00052 
00061   int execute_query_msg_double(const char* query, const char* msg, double &result);
00070   int execute_query_msg_int(const char* query, const char* msg, int &result);
00071   
00079   int copy_to_table(std::istream &s, std::string &table_name, std::string &delimiter);
00087   int copy_from_table(std::ostream &s, std::string &table_name, std::string &delimiter);
00088   
00096   template<class T>
00097     int postgresTupleParser(const char* szQuery, int (*tuple_parser)(PGresult *r, int index, T &out), std::vector<T> &_out)
00098     {
00099       res = PQexec(conn, szQuery);
00100       if (!res || (PQresultStatus(res) != PGRES_COMMAND_OK && PQresultStatus(res) != PGRES_TUPLES_OK))
00101         {
00102           fprintf(stderr, "PROBLEM %s: %s\n", "query error in postgresTupleParser", PQerrorMessage(conn));
00103           PQclear(res);
00104           this->exit_nicely();
00105           return -1;
00106         }
00107       int _n = PQntuples(res);
00108       for(int i = 0; i < _n; i ++) {
00109         T _t;
00110         if(tuple_parser(res, i, _t) < 0) {     
00111           fprintf(stderr, "tuple could not be parsed in postgresTupleParser method\n");
00112           return -1;
00113         }
00114         //    int id = atoi(PQgetvalue(res, i, 0));
00115         //string f_vec = PQgetvalue(res, i, 1);
00116         //double eps_val = atof(PQgetvalue(res, i, 2));
00117         _out.push_back(_t);
00118       }
00119       return _n;
00120     }
00121   
00129   template<class K, class T>
00130     int postgresMapParser(const char* szQuery, int (*tuple_parser)(PGresult *r, int index, K &k, T &v), std::map<K,T> &_out)
00131   {
00132     res = PQexec(conn, szQuery);
00133     if (!res || (PQresultStatus(res) != PGRES_COMMAND_OK && PQresultStatus(res) != PGRES_TUPLES_OK))
00134       {
00135         fprintf(stderr, "PROBLEM %s: %s\n", "query error in postgresTupleParser", PQerrorMessage(conn));
00136         PQclear(res);
00137         exit_nicely();
00138         return -1;
00139       }
00140     int _n = PQntuples(res);
00141     for(int i = 0; i < _n; i ++) {
00142       K _k;
00143       T _t;
00144       if(tuple_parser(res, i, _k, _t) < 0) {     
00145         fprintf(stderr, "tuple could not be parsed in postgresTupleParser method\n");
00146         return -1;
00147       }
00148       _out[_k] = _t;
00149     }
00150     return _n;
00151   }
00155   void closeConnection();
00156 };
00157 
00158 
00159 // Helper functions
00165 void svector_to_dbstring(const SVector& s, std::string& feature_vector_str);
00171 void fvector_to_dbstring(const FVector& v, std::string &feature_values);
00178 void model_to_dbstring(const struct model &m, std::string &model_string, std::string &bias_string);
00184 void checkQueryReturnValue(int retVal, std::string query);
00185 
00186 #endif

Generated on Wed Dec 15 10:46:15 2010 for Hazy_System by  doxygen 1.4.7