You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
9.9 KiB

7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
7 years ago
​
​
​
6 years ago
​
​
7 years ago
6 years ago
​
​
7 years ago
6 years ago
7 years ago
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
  1. #include "database_class.h"
  2. #define LOG( X ) m_pLog->Log( "MYDB", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "MYDB", this, _LOG_ERROR, X )
  4. /* Default Constructor */
  5. MySQL_Connector::MySQL_Connector():m_mAccessMutex(){
  6. m_pLog = basicLogCollection::getInstance();
  7. m_pDBLink = NULL;
  8. m_pDBName = NULL;
  9. m_pDBHost = NULL;
  10. m_pDBUser = NULL;
  11. m_pDBPassword = NULL;
  12. m_nPort = 0;
  13. m_bIsConnected = false;
  14. };
  15. MySQL_Connector::MySQL_Connector( CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port ):m_mAccessMutex(){
  16. m_pLog = basicLogCollection::getInstance();
  17. m_pDBLink = mysql_init(NULL);
  18. m_pDBName = NULL;
  19. m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 );
  20. strcpy( m_pDBHost, DBHost );
  21. m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 );
  22. strcpy( m_pDBUser, DBUser );
  23. m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 );
  24. strcpy( m_pDBPassword, DBPassword );
  25. m_nPort = Port;
  26. m_bIsConnected = false;
  27. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  28. if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) {
  29. LOG_ERROR( mysql_error( m_pDBLink ) );
  30. } else m_bIsConnected = true;
  31. };
  32. MySQL_Connector::MySQL_Connector( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port ):m_mAccessMutex(){
  33. m_pLog = basicLogCollection::getInstance();
  34. m_pDBLink = mysql_init(NULL);
  35. m_pDBName = (CSTR) malloc( strlen( ( const char * ) DBName ) + 1 );
  36. strcpy( m_pDBName, DBName );
  37. m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 );
  38. strcpy( m_pDBHost, DBHost );
  39. m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 );
  40. strcpy( m_pDBUser, DBUser );
  41. m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 );
  42. strcpy( m_pDBPassword, DBPassword );
  43. m_nPort = Port;
  44. m_bIsConnected = false;
  45. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  46. if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) {
  47. LOG_ERROR( mysql_error( m_pDBLink ) );
  48. } else {
  49. if( mysql_select_db( m_pDBLink, m_pDBName ) == 0 ){
  50. m_bIsConnected = true;
  51. #ifdef DEBUG
  52. char temp[256];
  53. sprintf( temp, "Connected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort );
  54. LOG( temp );
  55. #endif
  56. } else {
  57. LOG_ERROR( mysql_error( m_pDBLink ) );
  58. };
  59. };
  60. };
  61. /* default Destructor */
  62. MySQL_Connector::~MySQL_Connector(){
  63. if( m_pDBName != NULL ) delete( m_pDBName );
  64. if( m_pDBHost != NULL ) delete( m_pDBHost );
  65. if( m_pDBUser != NULL ) delete( m_pDBUser );
  66. if( m_pDBPassword != NULL ) delete( m_pDBPassword );
  67. if ( m_pDBLink != NULL ) mysql_close( m_pDBLink );
  68. mysql_library_end();
  69. m_bIsConnected = false;
  70. };
  71. bool MySQL_Connector::Connect( CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port ){
  72. if( !m_bIsConnected ){
  73. m_pDBLink = mysql_init(NULL);
  74. } else {
  75. m_bIsConnected = false;
  76. };
  77. if( m_pDBName != NULL ) delete( m_pDBName );
  78. if( m_pDBHost != NULL ) delete( m_pDBHost );
  79. if( m_pDBUser != NULL ) delete( m_pDBUser );
  80. if( m_pDBPassword != NULL ) delete( m_pDBPassword );
  81. m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 );
  82. strcpy( m_pDBHost, DBHost );
  83. m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 );
  84. strcpy( m_pDBUser, DBUser );
  85. m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 );
  86. strcpy( m_pDBPassword, DBPassword );
  87. m_nPort = Port;
  88. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  89. if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) {
  90. LOG_ERROR( mysql_error( m_pDBLink ) );
  91. } else m_bIsConnected = true;
  92. return m_bIsConnected;
  93. };
  94. bool MySQL_Connector::Connect( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, U16 Port ){
  95. if( !m_bIsConnected ){
  96. m_pDBLink = mysql_init(NULL);
  97. } else {
  98. m_bIsConnected = false;
  99. };
  100. if( m_pDBName != NULL ) delete( m_pDBName );
  101. if( m_pDBHost != NULL ) delete( m_pDBHost );
  102. if( m_pDBUser != NULL ) delete( m_pDBUser );
  103. if( m_pDBPassword != NULL ) delete( m_pDBPassword );
  104. m_pDBName = (CSTR) malloc( strlen( ( const char * ) DBName ) + 1 );
  105. strcpy( m_pDBName, DBName );
  106. m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 );
  107. strcpy( m_pDBHost, DBHost );
  108. m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 );
  109. strcpy( m_pDBUser, DBUser );
  110. m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 );
  111. strcpy( m_pDBPassword, DBPassword );
  112. m_nPort = Port;
  113. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  114. if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) {
  115. LOG_ERROR( mysql_error( m_pDBLink ) );
  116. } else {
  117. if( mysql_select_db( m_pDBLink, m_pDBName ) == 0 ){
  118. m_bIsConnected = true;
  119. #ifdef DEBUG
  120. char temp[256];
  121. sprintf( temp, "Connected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort );
  122. LOG( temp );
  123. #endif
  124. } else {
  125. LOG_ERROR( mysql_error( m_pDBLink ) );
  126. };
  127. };
  128. return m_bIsConnected;
  129. };
  130. bool MySQL_Connector::ReConnect( void ){
  131. if( !m_bIsConnected ){
  132. m_pDBLink = mysql_init(NULL);
  133. } else {
  134. m_bIsConnected = false;
  135. };
  136. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  137. if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) {
  138. LOG_ERROR( mysql_error( m_pDBLink ) );
  139. } else {
  140. if( mysql_select_db( m_pDBLink, m_pDBName ) == 0 ){
  141. m_bIsConnected = true;
  142. #ifdef DEBUG
  143. char temp[256];
  144. sprintf( temp, "ReConnected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort );
  145. LOG( temp );
  146. #endif
  147. } else {
  148. LOG_ERROR( mysql_error( m_pDBLink ) );
  149. };
  150. };
  151. return m_bIsConnected;
  152. };
  153. AAssoc *MySQL_Connector::Query( CSTR QS, bool useCache ){
  154. AAssoc * RetVal = NULL;
  155. Assoc * tElement;
  156. unsigned int num_Fields;
  157. MYSQL_RES * Result = NULL;
  158. m_mAccessMutex.lock();
  159. #ifdef DEBUG_MYSQL_QUERIES
  160. LOG( QS );
  161. #endif
  162. if ( mysql_query( m_pDBLink, QS ) ) {
  163. LOG_ERROR( mysql_error( m_pDBLink ));
  164. char temp[256];
  165. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  166. LOG_ERROR( temp );
  167. switch( mysql_errno( m_pDBLink ) ){
  168. case 2006:{// Server Gone
  169. if( ReConnect() ){
  170. if ( !mysql_query( m_pDBLink, QS ) ){
  171. Result = mysql_store_result( m_pDBLink );
  172. } else {
  173. Result = NULL;
  174. };
  175. } else {
  176. Result = NULL;
  177. };
  178. break;
  179. };
  180. };
  181. RetVal = NULL;
  182. } else {
  183. Result = mysql_store_result( m_pDBLink );
  184. };
  185. if( Result!= NULL ){
  186. if ( mysql_num_rows( Result ) ) {
  187. RetVal = new AAssoc();
  188. num_Fields = mysql_num_fields( Result );
  189. MYSQL_FIELD * Fields;
  190. MYSQL_ROW Row;
  191. Fields = mysql_fetch_fields( Result );
  192. while ( ( Row = mysql_fetch_row( Result ) ) != NULL ){
  193. tElement = new Assoc();
  194. string *Key;
  195. string *Val;
  196. for( int i = 0; i < num_Fields; i++ ){
  197. Key = NULL;
  198. Val = NULL;
  199. Key = new string( Fields[ i ].name );
  200. if( Key!= NULL ) {
  201. if( Row[ i ] != NULL ){
  202. Val = new string( Row[ i ] );
  203. } else {
  204. Val = new string( "0" );
  205. };
  206. if( Val != NULL ){
  207. //(*tElement).insert( pair< string, string > ( *Key, *Val ) );
  208. (*tElement)[*Key]=*Val;
  209. } else {
  210. LOG_ERROR( "Val Alloc Error" );
  211. };
  212. } else {
  213. LOG_ERROR( "Key Alloc Error" );
  214. };
  215. };
  216. /*for (map<string, string>::iterator p = (*tElement).begin( ); p != (*tElement).end( ); ++p ) {
  217. cout << "Field: " << p->first<< ", #: " << p->second << endl;
  218. } */
  219. RetVal->push_back( tElement );
  220. };
  221. } else {
  222. #ifdef DEBUG_MYSQL_QUERIES
  223. LOG( "No Rows" );
  224. #endif
  225. RetVal = NULL;
  226. };
  227. mysql_free_result( Result );
  228. Result = NULL;
  229. } else {
  230. #ifdef DEBUG_MYSQL_QUERIES
  231. LOG( "Empty Result" );
  232. #endif
  233. RetVal = NULL;
  234. };
  235. m_mAccessMutex.unlock();
  236. return RetVal;
  237. };
  238. CSTR MySQL_Connector::escapeString( CSTR str ){
  239. CSTR temp;
  240. m_mAccessMutex.lock();
  241. temp = ( CSTR ) malloc( strlen( str ) * 2 + 1 );
  242. mysql_real_escape_string( m_pDBLink, temp, str, strlen( str ) );
  243. m_mAccessMutex.unlock();
  244. return temp;
  245. };
  246. uint64_t MySQL_Connector::QueryID( CSTR QS ){
  247. uint64_t RetVal = 0;
  248. m_mAccessMutex.lock();
  249. #ifdef DEBUG_MYSQL_QUERIES
  250. LOG( QS );
  251. #endif
  252. while ( mysql_query( m_pDBLink, QS ) ) {
  253. LOG_ERROR( mysql_error( m_pDBLink ));
  254. char temp[256];
  255. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  256. LOG_ERROR( temp );
  257. if( mysql_errno( m_pDBLink ) == 2013 || mysql_errno( m_pDBLink ) == 2006 ) {
  258. ReConnect();
  259. } else break;
  260. };
  261. LOG("Query Issued");
  262. RetVal = ( uint64_t ) 0;//mysql_insert_id( m_pDBLink );
  263. LOG("ID Receved");
  264. m_mAccessMutex.unlock();
  265. LOG("Mutex Unlocked");
  266. return RetVal;
  267. };
  268. void MySQL_Connector::SilentQuery( CSTR QS ){
  269. m_mAccessMutex.lock();
  270. #ifdef DEBUG_MYSQL_QUERIES
  271. LOG( QS );
  272. #endif
  273. while ( mysql_query( m_pDBLink, QS ) ) {
  274. LOG_ERROR( mysql_error( m_pDBLink ));
  275. char temp[256];
  276. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  277. LOG_ERROR( temp );
  278. if( mysql_errno( m_pDBLink ) == 2013 || mysql_errno( m_pDBLink ) == 2006 ) {
  279. ReConnect();
  280. } else break;
  281. };
  282. m_mAccessMutex.unlock();
  283. };
  284. void deleteResult( AAssoc *R ){
  285. if (R) {
  286. //while( R->size() ){
  287. // delete( (Assoc *) R->at(0) );
  288. // R->erase( R->begin());
  289. //};
  290. R->clear();
  291. delete( R );
  292. R = NULL;
  293. };
  294. };
  295. #undef LOG
  296. #undef LOG_ERROR