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.

345 lines
11 KiB

7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
6 years ago
7 years ago
​
7 years ago
​
​
​
​
7 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. m_bIsConnected = false;
  73. if( m_pDBLink != NULL ){
  74. mysql_close( m_pDBLink );
  75. };
  76. m_pDBLink = mysql_init(NULL);
  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. m_bIsConnected = false;
  96. if( m_pDBLink != NULL ){
  97. mysql_close( m_pDBLink );
  98. };
  99. m_pDBLink = mysql_init(NULL);
  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. m_bIsConnected = false;
  132. if( m_pDBLink != NULL ){
  133. mysql_close( m_pDBLink );
  134. };
  135. m_pDBLink = mysql_init(NULL);
  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. char temp[256];
  159. m_mAccessMutex.lock();
  160. #ifdef DEBUG_MYSQL_QUERIES
  161. LOG( QS );
  162. #endif
  163. /*
  164. if( mysql_query( m_pDBLink, QS ) ){
  165. LOG_ERROR( mysql_error( m_pDBLink ));
  166. char temp[256];
  167. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  168. LOG_ERROR( temp );
  169. switch( mysql_errno( m_pDBLink ) ){
  170. case 1156:
  171. case 2006:
  172. case 2013:{// Server Gone
  173. if( ReConnect() ){
  174. if ( !mysql_query( m_pDBLink, QS ) ){
  175. Result = mysql_store_result( m_pDBLink );
  176. } else {
  177. LOG_ERROR( mysql_error( m_pDBLink ));
  178. sprintf( temp, "Reconnect Error Code: %d", mysql_errno( m_pDBLink ));
  179. LOG_ERROR( temp );
  180. };
  181. };
  182. break;
  183. };
  184. };
  185. } else {
  186. Result = mysql_store_result( m_pDBLink );
  187. };*/
  188. while ( mysql_query( m_pDBLink, QS ) ) {
  189. LOG_ERROR( mysql_error( m_pDBLink ));
  190. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  191. LOG_ERROR( temp );
  192. switch( mysql_errno( m_pDBLink ) ) {
  193. case 1156:
  194. case 2006:
  195. case 2013:{
  196. ReConnect();
  197. continue;
  198. };
  199. };
  200. break;
  201. };
  202. Result = mysql_store_result( m_pDBLink );
  203. if( Result!= NULL ){
  204. if ( mysql_num_rows( Result ) ) {
  205. RetVal = new AAssoc();
  206. num_Fields = mysql_num_fields( Result );
  207. MYSQL_FIELD * Fields;
  208. MYSQL_ROW Row;
  209. Fields = mysql_fetch_fields( Result );
  210. while ( ( Row = mysql_fetch_row( Result ) ) != NULL ){
  211. tElement = new Assoc();
  212. string *Key;
  213. string *Val;
  214. for( int i = 0; i < num_Fields; i++ ){
  215. Key = NULL;
  216. Val = NULL;
  217. Key = new string( Fields[ i ].name );
  218. if( Key!= NULL ) {
  219. if( Row[ i ] != NULL ){
  220. Val = new string( Row[ i ] );
  221. } else {
  222. Val = new string( "0" );
  223. };
  224. if( Val != NULL ){
  225. //(*tElement).insert( pair< string, string > ( *Key, *Val ) );
  226. (*tElement)[*Key]=*Val;
  227. } else {
  228. LOG_ERROR( "Val Alloc Error" );
  229. };
  230. } else {
  231. LOG_ERROR( "Key Alloc Error" );
  232. };
  233. };
  234. /*for (map<string, string>::iterator p = (*tElement).begin( ); p != (*tElement).end( ); ++p ) {
  235. cout << "Field: " << p->first<< ", #: " << p->second << endl;
  236. } */
  237. RetVal->push_back( tElement );
  238. };
  239. } else {
  240. #ifdef DEBUG_MYSQL_QUERIES
  241. LOG( "No Rows" );
  242. #endif
  243. RetVal = NULL;
  244. };
  245. mysql_free_result( Result );
  246. Result = NULL;
  247. } else {
  248. #ifdef DEBUG_MYSQL_QUERIES
  249. LOG( "Empty Result" );
  250. #endif
  251. RetVal = NULL;
  252. };
  253. m_mAccessMutex.unlock();
  254. return RetVal;
  255. };
  256. CSTR MySQL_Connector::escapeString( CSTR str ){
  257. CSTR temp;
  258. m_mAccessMutex.lock();
  259. temp = ( CSTR ) malloc( strlen( str ) * 2 + 1 );
  260. mysql_real_escape_string( m_pDBLink, temp, str, strlen( str ) );
  261. m_mAccessMutex.unlock();
  262. return temp;
  263. };
  264. uint64_t MySQL_Connector::QueryID( CSTR QS ){
  265. uint64_t RetVal = 0;
  266. char temp[256];
  267. m_mAccessMutex.lock();
  268. #ifdef DEBUG_MYSQL_QUERIES
  269. LOG( QS );
  270. #endif
  271. while ( mysql_query( m_pDBLink, QS ) ) {
  272. LOG_ERROR( mysql_error( m_pDBLink ));
  273. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  274. LOG_ERROR( temp );
  275. switch( mysql_errno( m_pDBLink ) ) {
  276. case 1156:
  277. case 2006:
  278. case 2013:{
  279. ReConnect();
  280. continue;
  281. };
  282. };
  283. break;
  284. };
  285. RetVal = ( uint64_t ) mysql_insert_id( m_pDBLink );
  286. m_mAccessMutex.unlock();
  287. return RetVal;
  288. };
  289. void MySQL_Connector::SilentQuery( CSTR QS ){
  290. char temp[256];
  291. m_mAccessMutex.lock();
  292. #ifdef DEBUG_MYSQL_QUERIES
  293. LOG( QS );
  294. #endif
  295. while ( mysql_query( m_pDBLink, QS ) ) {
  296. LOG_ERROR( mysql_error( m_pDBLink ));
  297. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  298. LOG_ERROR( temp );
  299. switch( mysql_errno( m_pDBLink ) ) {
  300. case 1156:
  301. case 2006:
  302. case 2013:{
  303. ReConnect();
  304. continue;
  305. };
  306. };
  307. break;
  308. };
  309. m_mAccessMutex.unlock();
  310. };
  311. void deleteResult( AAssoc *R ){
  312. if (R) {
  313. //while( R->size() ){
  314. // delete( (Assoc *) R->at(0) );
  315. // R->erase( R->begin());
  316. //};
  317. R->clear();
  318. delete( R );
  319. R = NULL;
  320. };
  321. };
  322. #undef LOG
  323. #undef LOG_ERROR