00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "DBInterface.h"
00025 #include "Utilities.h"
00026 #include "GlobalVariables.h"
00027
00028
00029 #include <iostream>
00030 #include <iomanip>
00031 using namespace std;
00032 using namespace mysqlpp;
00033
00034
00035
00036 DBInterface::DBInterface(){
00037 }
00038
00039
00040
00041 DBInterface::DBInterface(const char* host, const char* user, const char* password, const char* database){
00042 Utilities::safeCStringCopy(dbParam.host, host, dbParam.maxParamLength);
00043 Utilities::safeCStringCopy(dbParam.user, user, dbParam.maxParamLength);
00044 Utilities::safeCStringCopy(dbParam.password, password, dbParam.maxParamLength);
00045 Utilities::safeCStringCopy(dbParam.database, database, dbParam.maxParamLength);
00046 }
00047
00048
00049
00050 DBInterface::~DBInterface(){
00051 #ifdef MEMORY_DEBUG
00052 cout<<"DELETING DBINTERFACE. NUMBER OF EXTRA CONNECTIONS: "<<connectionVector.size()<<";"<<endl;
00053 #endif//MEMORY_DEBUG
00054
00055
00056 if(connection->connected()){
00057 connection->close();
00058 }
00059 delete connection;
00060
00061
00062 for(vector<Connection*>::iterator iter = connectionVector.begin(); iter != connectionVector.end(); ++iter){
00063 if((*iter)->connected()){
00064 (*iter)->close();
00065 }
00066 delete *iter;
00067 }
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077 void DBInterface::checkDatabaseConnection(Connection*& conn){
00078
00079
00080 int pingRes = conn->ping();
00081
00082
00083 unsigned int reconnectCounter = 1;
00084 while(pingRes){
00085 cout<<"DBInterface: Trying to ping "<<dbParam.database<<" database."<<endl;
00086
00087
00088 pingRes = conn->ping();
00089
00090
00091 if(pingRes && reconnectCounter <= MAX_NUMBER_RECONNECT_ATTEMPTS)
00092 sleep(1);
00093
00094
00095
00096 else if (reconnectCounter > MAX_NUMBER_RECONNECT_ATTEMPTS){
00097 cout<<"DBInterface. Cannot ping "<<dbParam.database<<" on existing connection. Trying to reconnect to database."<<endl;
00098
00099 if(connectToDatabase(conn))
00100 pingRes = 0;
00101 else
00102 throw ConnectionFailed("DBInterface: Failed to reconnect to the database.");
00103 }
00104
00105
00106 ++reconnectCounter;
00107 }
00108 }
00109
00110
00111
00112 bool DBInterface::connectToDatabase(bool useEx){
00113
00114 useExceptions = useEx;
00115
00116
00117 connection = new Connection(useExceptions);
00118 return connectToDatabase(connection);
00119 }
00120
00121
00122
00123
00124 const DBParameters DBInterface::getDBParameters(){
00125 return dbParam;
00126 }
00127
00128
00129
00130
00131
00132 Connection* DBInterface::getNewConnection(){
00133
00134 Connection *tempConnection = new Connection(useExceptions);
00135
00136
00137 if(!connectToDatabase(tempConnection)){
00138 cerr<<"DBInterface: NEW CONNECTION TO DATABASE FAILED: "<<tempConnection->error()<<endl;
00139 return 0;
00140 }
00141 else{
00142 connectionVector.push_back(tempConnection);
00143 return tempConnection;
00144 }
00145 }
00146
00147
00148
00149
00150 Query DBInterface::getQuery(){
00151
00152
00153
00154
00155 checkDatabaseConnection(connection);
00156
00157
00158 return connection->query();
00159 }
00160
00161
00162
00163
00164
00165
00166
00167 bool DBInterface::connectToDatabase(Connection*& conn){
00168 try{
00169 conn->connect(dbParam.database, dbParam.host, dbParam.user, dbParam.password);
00170 if(!conn->connected()){
00171 cerr<<"DBInterface: CONNECTION TO DATABASE FAILED: "<<connection->error()<<endl;
00172 return false;
00173 }
00174 else{
00175 return true;
00176 }
00177 }
00178 catch (const std::exception er) {
00179 cerr<<"DBInterface: EXCEPTION THROWN CONNECTING TO DATABASE: "<<er.what()<<endl;
00180 return false;
00181 }
00182 }
00183
00184