00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00033
00034
00047
00048
00049 #include <iostream>
00050 #include <fstream>
00051 #include <map>
00052 using namespace std;
00053
00054
00055
00056
00057 #include "database/MJDatabaseConnection.hh"
00058 #include "io/MJLogger.hh"
00059
00060
00061 static void
00062 exit_nicely(PGconn *conn)
00063 {
00064 PQfinish(conn);
00065 exit(1);
00066 }
00067 string MJDatabaseConnection::PG_VERSION="";
00068
00069 MJDatabaseConnection::MJDatabaseConnection()
00070 {
00071 }
00072 MJDatabaseConnection::MJDatabaseConnection(const MJDatabaseConnection & other)
00073 {
00074
00075 }
00076
00077 MJDatabaseConnection::~MJDatabaseConnection()
00078 {
00079 }
00080 void
00081 MJDatabaseConnection::getConnectionInfo(){
00082
00083 int size = 512;
00084 char buff[512];
00085 map<string,string> propMap;
00086 ifstream infile("MJDBProperties.txt", ios::in);
00087 if(!infile){
00088 MJLog(trace) << "Cannot open properties file MJDBProperties.txt"
00089 << endl;
00090 MJLog(trace) << "Make sure you have this file exists in the current directory."
00091 << endl;
00092 }
00093 char* token = "=";
00094 while(!infile.eof()){
00095 infile.getline(buff, size);
00096 while('#' !=buff[0]
00097 && buff[0] != '\n'
00098 && buff[0] != '\0'
00099 && !infile.eof()){
00100 string key(strtok(buff, token));
00101 string value(strtok(0, token));
00102 propMap.insert(make_pair(key, value));
00103 MJLog(trace) << "Key: " << key << " value: "<< value << endl;
00104 infile.getline(buff, size);
00105 }
00106
00107 }
00108 string host = propMap["databaseURL"];
00109 string name = propMap["databaseName"];
00110 string user = propMap["userName"];
00111 string passwd = propMap["password"];
00112 string portNo = propMap["port"];
00113 string conninfo = "hostaddr=" + host + " dbname="+
00114 name + " user=" + user + " password=" + passwd +
00115 " port=" + portNo;
00116
00117
00118
00119 fconninfo="hostaddr=128.3.11.122 dbname=majorana user=akbarm password= port=5432";
00120
00121
00122
00123 PG_VERSION = propMap["pg_version"];
00124
00125 MJLog(trace)<< conninfo << endl;
00126 MJLog(trace)<< fconninfo << endl;
00127 }
00128
00129 PGresult*
00130 MJDatabaseConnection::qResult(string sqlCmd)
00131 {
00132 if(! connect())exit_nicely(fconn);
00133
00134 fres = query("BEGIN");
00135 PQclear(fres);
00136
00137 string command = "DECLARE myportal BINARY CURSOR FOR " + sqlCmd;
00138 fres = query(command);
00139 PQclear(fres);
00140 fres = PQexec(fconn, "FETCH ALL in myportal");
00141 if(PQresultStatus(fres) != PGRES_TUPLES_OK){
00142 MJLog(trace) << "FETCH ALL failed: " << PQerrorMessage(fconn) << endl;
00143 PQclear(fres);
00144 exit_nicely(fconn);
00145 }
00146 return fres;
00147 }
00148 void
00149 MJDatabaseConnection::qDone(PGresult* res)
00150 {
00151 PQclear(res);
00152 res = query("CLOSE myportal");
00153
00154
00155 res = query("END");
00156 PQclear(res);
00157
00158
00159 PQfinish(fconn);
00160
00161 }
00162 PGresult*
00163 MJDatabaseConnection::query(string cmd)
00164 {
00165 PGresult* res;
00166 res = PQexec(fconn, cmd.c_str());
00167 if (PQresultStatus(res) != PGRES_COMMAND_OK){
00168 MJLog(trace)<< cmd << " command failed: " << PQerrorMessage(fconn) << endl;
00169 PQclear(res);
00170 exit_nicely(fconn);
00171 }
00172 return res;
00173
00174 }
00175 bool
00176 MJDatabaseConnection::connect()
00177 {
00178 getConnectionInfo();
00179 bool retVal = true;
00180
00181 fconn = PQconnectdb(fconninfo);
00182
00183
00184
00185 if (PQstatus(fconn) != CONNECTION_OK)
00186 {
00187 MJLog(trace)<<"Connection to database \n"
00188 << PQdb(fconn)<<" failed.\n"<<endl;
00189 MJLog(trace)<<"----------- more information ----------------- \n"<<endl;
00190 MJLog(trace)<<" dbname "<<PQdb(fconn)
00191 <<" username "<<PQuser(fconn)
00192 <<" host "<<PQhost(fconn)
00193 <<" port "<<PQport(fconn)
00194 <<" option "<<PQoptions(fconn)
00195 <<" status "<<PQstatus(fconn)<<"\n"<<endl;
00196 MJLog(trace)<<"----------------------------------------------- \n"<<endl;
00197 MJLog(trace) << PQerrorMessage(fconn)<< endl;
00198 retVal = false;
00199 }
00200 return retVal;
00201 }
00202
00203