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.

364 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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 = NULL;
  218. Val = NULL;
  219. Key = new string( Fields[ i ].name );
  220. if( Key!= NULL ) {
  221. if( Row[ i ] != NULL ){
  222. Val = new string( Row[ i ] );
  223. } else {
  224. Val = new string( "0" );
  225. };
  226. if( Val != NULL ){
  227. //(*tElement).insert( pair< string, string > ( *Key, *Val ) );
  228. (*tElement)[*Key]=*Val;
  229. } else {
  230. LOG_ERROR( "Val Alloc Error" );
  231. };
  232. } else {
  233. LOG_ERROR( "Key Alloc Error" );
  234. };
  235. };
  236. /*for (map<string, string>::iterator p = (*tElement).begin( ); p != (*tElement).end( ); ++p ) {
  237. cout << "Field: " << p->first<< ", #: " << p->second << endl;
  238. } */
  239. RetVal->push_back( tElement );
  240. };
  241. } else {
  242. #ifdef DEBUG_MYSQL_QUERIES
  243. LOG( "No Rows" );
  244. #endif
  245. RetVal = NULL;
  246. };
  247. mysql_free_result( Result );
  248. Result = NULL;
  249. } else {
  250. #ifdef DEBUG_MYSQL_QUERIES
  251. LOG( "Empty Result" );
  252. #endif
  253. RetVal = NULL;
  254. };
  255. m_mAccessMutex.unlock();
  256. return RetVal;
  257. };
  258. CSTR MySQL_Connector::escapeString( CSTR str ){
  259. int len = strlen( str );
  260. CSTR temp = ( CSTR ) malloc( len * 2 + 1 );
  261. if ( temp ) {
  262. m_mAccessMutex.lock();
  263. mysql_real_escape_string( m_pDBLink, temp, str, len );
  264. m_mAccessMutex.unlock();
  265. };
  266. return temp;
  267. };
  268. CSTR MySQL_Connector::escapeBinary( uint8_t *Data, uint32_t Size ){
  269. CSTR temp = NULL;
  270. if( Data && Size ){
  271. temp = ( CSTR ) malloc( Size * 2 + 1 );
  272. if ( temp ) {
  273. m_mAccessMutex.lock();
  274. mysql_real_escape_string( m_pDBLink, temp, (const char*) Data, Size );
  275. m_mAccessMutex.unlock();
  276. };
  277. };
  278. return temp;
  279. };
  280. uint64_t MySQL_Connector::QueryID( CSTR QS ){
  281. uint64_t RetVal = 0;
  282. char temp[256];
  283. m_mAccessMutex.lock();
  284. #ifdef DEBUG_MYSQL_QUERIES
  285. LOG( QS );
  286. #endif
  287. while ( mysql_query( m_pDBLink, QS ) ) {
  288. LOG_ERROR( mysql_error( m_pDBLink ));
  289. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  290. LOG_ERROR( temp );
  291. switch( mysql_errno( m_pDBLink ) ) {
  292. case 1156:
  293. case 2006:
  294. case 2013:
  295. case 2014:{
  296. ReConnect();
  297. continue;
  298. };
  299. };
  300. break;
  301. };
  302. RetVal = ( uint64_t ) mysql_insert_id( m_pDBLink );
  303. m_mAccessMutex.unlock();
  304. return RetVal;
  305. };
  306. void MySQL_Connector::SilentQuery( CSTR QS ){
  307. char temp[256];
  308. m_mAccessMutex.lock();
  309. #ifdef DEBUG_MYSQL_QUERIES
  310. LOG( QS );
  311. #endif
  312. while ( mysql_query( m_pDBLink, QS ) ) {
  313. LOG_ERROR( mysql_error( m_pDBLink ));
  314. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  315. LOG_ERROR( temp );
  316. switch( mysql_errno( m_pDBLink ) ) {
  317. case 1156:
  318. case 2006:
  319. case 2013:
  320. case 2014:{
  321. ReConnect();
  322. continue;
  323. };
  324. };
  325. break;
  326. };
  327. m_mAccessMutex.unlock();
  328. };
  329. void deleteResult( AAssoc *&R ){
  330. if( R ){
  331. while( R->size() ){
  332. delete( (Assoc *) R->at(0) );
  333. R->erase( R->begin());
  334. };
  335. R->clear();
  336. delete( R );
  337. R = NULL;
  338. };
  339. };
  340. #undef LOG
  341. #undef LOG_ERROR