Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

MJDatabaseConnection.cc

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------//
00002 //bb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nu//
00003 //                                                                           //
00004 //                         MAJORANA Simulation                               //
00005 //                                                                           //
00006 //      This code implementation is the intellectual property of the         //
00007 //      MAJORANA Collaboration. It is based on Geant4, an intellectual       //
00008 //      property of the RD44 GEANT4 collaboration.                           //
00009 //                                                                           //
00010 //                        *********************                              //
00011 //                                                                           //
00012 //    Neither the authors of this software system, nor their employing       //
00013 //    institutes, nor the agencies providing financial support for this      //
00014 //    work  make  any representation or  warranty, express or implied,       //
00015 //    regarding this software system or assume any liability for its use.    //
00016 //    By copying, distributing or modifying the Program (or any work based   //
00017 //    on on the Program) you indicate your acceptance of this statement,     //
00018 //    and all its terms.                                                     //
00019 //                                                                           //
00020 //bb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nubb0nu//
00021 //---------------------------------------------------------------------------//
00022 //                                                          
00023 // $Id: MJDatabaseConnection.cc,v 1.1.1.1 2004/11/08 17:53:42 gast Exp $ 
00024 //      
00025 // CLASS IMPLEMENTATION:  MJDatabaseConnection.cc
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"         //Present MJ Class Headers 
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 //  fconninfo = conninfo.c_str();
00118 // a test 
00119   fconninfo="hostaddr=128.3.11.122 dbname=majorana user=akbarm password= port=5432";
00120 
00121   //the version of postgres that is used
00122   //this is important in reading data from database
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   /* Start a transaction block */
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   /* end the transaction */
00155   res = query("END");
00156   PQclear(res);
00157   
00158   /* close the connection to the database and cleanup */
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   /* Make a connection to the database */
00181   fconn = PQconnectdb(fconninfo);
00182 
00183   
00184   /* Check to see that the backend connection was successfully made */
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 

Generated on Mon Nov 29 16:58:49 2004 for Majorana Simulation by  doxygen 1.3.9.1