#include "database_class.h" #define LOG( X ) m_pLog->Log( "MYDB", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "MYDB", this, _LOG_ERROR, X ) /* Default Constructor */ MySQL_Connector::MySQL_Connector():m_mAccessMutex(){ m_pLog = basicLogCollection::getInstance(); m_pDBLink = NULL; m_pDBName = NULL; m_pDBHost = NULL; m_pDBUser = NULL; m_pDBPassword = NULL; m_nPort = 0; m_bIsConnected = false; }; MySQL_Connector::MySQL_Connector( CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port ):m_mAccessMutex(){ m_pLog = basicLogCollection::getInstance(); m_pDBLink = mysql_init(NULL); m_pDBName = NULL; m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 ); strcpy( m_pDBHost, DBHost ); m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 ); strcpy( m_pDBUser, DBUser ); m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 ); strcpy( m_pDBPassword, DBPassword ); m_nPort = Port; m_bIsConnected = false; /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */ if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) { LOG_ERROR( mysql_error( m_pDBLink ) ); } else m_bIsConnected = true; }; MySQL_Connector::MySQL_Connector( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port ):m_mAccessMutex(){ m_pLog = basicLogCollection::getInstance(); m_pDBLink = mysql_init(NULL); m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 ); strcpy( m_pDBHost, DBHost ); m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 ); strcpy( m_pDBUser, DBUser ); m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 ); strcpy( m_pDBPassword, DBPassword ); m_nPort = Port; m_bIsConnected = false; /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */ if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) { LOG_ERROR( mysql_error( m_pDBLink ) ); } else { if( mysql_select_db( m_pDBLink, m_pDBName ) == 0 ){ m_bIsConnected = true; #ifdef DEBUG char temp[256]; sprintf( temp, "Connected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort ); LOG( temp ); #endif } else { LOG_ERROR( mysql_error( m_pDBLink ) ); }; }; }; /* default Destructor */ MySQL_Connector::~MySQL_Connector(){ if( m_pDBName != NULL ) delete( m_pDBName ); if( m_pDBHost != NULL ) delete( m_pDBHost ); if( m_pDBUser != NULL ) delete( m_pDBUser ); if( m_pDBPassword != NULL ) delete( m_pDBPassword ); if ( m_pDBLink != NULL ) mysql_close( m_pDBLink ); mysql_library_end(); m_bIsConnected = false; }; bool MySQL_Connector::Connect( CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port ){ if( !m_bIsConnected ){ m_pDBLink = mysql_init(NULL); } else { m_bIsConnected = false; }; if( m_pDBName != NULL ) delete( m_pDBName ); if( m_pDBHost != NULL ) delete( m_pDBHost ); if( m_pDBUser != NULL ) delete( m_pDBUser ); if( m_pDBPassword != NULL ) delete( m_pDBPassword ); m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 ); strcpy( m_pDBHost, DBHost ); m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 ); strcpy( m_pDBUser, DBUser ); m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 ); strcpy( m_pDBPassword, DBPassword ); m_nPort = Port; /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */ if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) { LOG_ERROR( mysql_error( m_pDBLink ) ); } else m_bIsConnected = true; return m_bIsConnected; }; bool MySQL_Connector::Connect( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, U16 Port ){ if( !m_bIsConnected ){ m_pDBLink = mysql_init(NULL); } else { m_bIsConnected = false; }; if( m_pDBName != NULL ) delete( m_pDBName ); if( m_pDBHost != NULL ) delete( m_pDBHost ); if( m_pDBUser != NULL ) delete( m_pDBUser ); if( m_pDBPassword != NULL ) delete( m_pDBPassword ); m_pDBName = (CSTR) malloc( strlen( ( const char * ) DBName ) + 1 ); strcpy( m_pDBName, DBName ); m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 ); strcpy( m_pDBHost, DBHost ); m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 ); strcpy( m_pDBUser, DBUser ); m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 ); strcpy( m_pDBPassword, DBPassword ); m_nPort = Port; /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */ if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) { LOG_ERROR( mysql_error( m_pDBLink ) ); } else { if( mysql_select_db( m_pDBLink, m_pDBName ) == 0 ){ m_bIsConnected = true; #ifdef DEBUG char temp[256]; sprintf( temp, "Connected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort ); LOG( temp ); #endif } else { LOG_ERROR( mysql_error( m_pDBLink ) ); }; }; return m_bIsConnected; }; bool MySQL_Connector::ReConnect( void ){ if( !m_bIsConnected ){ m_pDBLink = mysql_init(NULL); } else { m_bIsConnected = false; }; /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */ if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) { LOG_ERROR( mysql_error( m_pDBLink ) ); } else { if( mysql_select_db( m_pDBLink, m_pDBName ) == 0 ){ m_bIsConnected = true; #ifdef DEBUG char temp[256]; sprintf( temp, "ReConnected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort ); LOG( temp ); #endif } else { LOG_ERROR( mysql_error( m_pDBLink ) ); }; }; return m_bIsConnected; }; AAssoc *MySQL_Connector::Query( CSTR QS, bool useCache ){ AAssoc * RetVal = NULL; Assoc * tElement; unsigned int num_Fields; MYSQL_RES * Result = NULL; m_mAccessMutex.lock(); #ifdef DEBUG_MYSQL_QUERIES LOG( QS ); #endif if ( mysql_query( m_pDBLink, QS ) ) { LOG_ERROR( mysql_error( m_pDBLink )); char temp[256]; sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink )); LOG_ERROR( temp ); switch( mysql_errno( m_pDBLink ) ){ case 2006:{// Server Gone if( ReConnect() ){ if ( !mysql_query( m_pDBLink, QS ) ){ Result = mysql_store_result( m_pDBLink ); } else { Result = NULL; }; } else { Result = NULL; }; break; }; }; RetVal = NULL; } else { Result = mysql_store_result( m_pDBLink ); }; if( Result!= NULL ){ if ( mysql_num_rows( Result ) ) { RetVal = new AAssoc(); num_Fields = mysql_num_fields( Result ); MYSQL_FIELD * Fields; MYSQL_ROW Row; Fields = mysql_fetch_fields( Result ); while ( ( Row = mysql_fetch_row( Result ) ) != NULL ){ tElement = new Assoc(); string *Key; string *Val; for( int i = 0; i < num_Fields; i++ ){ Key = NULL; Val = NULL; Key = new string( Fields[ i ].name ); if( Key!= NULL ) { if( Row[ i ] != NULL ){ Val = new string( Row[ i ] ); } else { Val = new string( "0" ); }; if( Val != NULL ){ //(*tElement).insert( pair< string, string > ( *Key, *Val ) ); (*tElement)[*Key]=*Val; } else { LOG_ERROR( "Val Alloc Error" ); }; } else { LOG_ERROR( "Key Alloc Error" ); }; }; /*for (map::iterator p = (*tElement).begin( ); p != (*tElement).end( ); ++p ) { cout << "Field: " << p->first<< ", #: " << p->second << endl; } */ RetVal->push_back( tElement ); }; } else { #ifdef DEBUG_MYSQL_QUERIES LOG( "No Rows" ); #endif RetVal = NULL; }; mysql_free_result( Result ); Result = NULL; } else { #ifdef DEBUG_MYSQL_QUERIES LOG( "Empty Result" ); #endif RetVal = NULL; }; m_mAccessMutex.unlock(); return RetVal; }; CSTR MySQL_Connector::escapeString( CSTR str ){ CSTR temp; m_mAccessMutex.lock(); temp = ( CSTR ) malloc( strlen( str ) * 2 + 1 ); mysql_real_escape_string( m_pDBLink, temp, str, strlen( str ) ); m_mAccessMutex.unlock(); return temp; }; uint64_t MySQL_Connector::QueryID( CSTR QS ){ uint64_t RetVal = 0; m_mAccessMutex.lock(); #ifdef DEBUG_MYSQL_QUERIES LOG( QS ); #endif while ( mysql_query( m_pDBLink, QS ) ) { LOG_ERROR( mysql_error( m_pDBLink )); char temp[256]; sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink )); LOG_ERROR( temp ); if( mysql_errno( m_pDBLink ) == 2013 || mysql_errno( m_pDBLink ) == 2006 ) { ReConnect(); } else break; }; LOG("Query Issued"); RetVal = ( uint64_t ) 0;//mysql_insert_id( m_pDBLink ); LOG("ID Receved"); m_mAccessMutex.unlock(); LOG("Mutex Unlocked"); return RetVal; }; void MySQL_Connector::SilentQuery( CSTR QS ){ m_mAccessMutex.lock(); #ifdef DEBUG_MYSQL_QUERIES LOG( QS ); #endif while ( mysql_query( m_pDBLink, QS ) ) { LOG_ERROR( mysql_error( m_pDBLink )); char temp[256]; sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink )); LOG_ERROR( temp ); if( mysql_errno( m_pDBLink ) == 2013 || mysql_errno( m_pDBLink ) == 2006 ) { ReConnect(); } else break; }; m_mAccessMutex.unlock(); }; void deleteResult( AAssoc *R ){ if (R) { //while( R->size() ){ // delete( (Assoc *) R->at(0) ); // R->erase( R->begin()); //}; R->clear(); delete( R ); R = NULL; }; }; #undef LOG #undef LOG_ERROR