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.

353 lines
11 KiB

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