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.

312 lines
9.8 KiB

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