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.

309 lines
9.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "database_class.h"
  2. #define LOG( X ) m_pLog->Log( "MySQL_Connector", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "MySQL_Connector", 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. m_bIsConnected = false;
  67. };
  68. bool MySQL_Connector::Connect( CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port ){
  69. if( !m_bIsConnected ){
  70. m_pDBLink = mysql_init(NULL);
  71. } else {
  72. m_bIsConnected = false;
  73. };
  74. if( m_pDBName != NULL ) delete( m_pDBName );
  75. if( m_pDBHost != NULL ) delete( m_pDBHost );
  76. if( m_pDBUser != NULL ) delete( m_pDBUser );
  77. if( m_pDBPassword != NULL ) delete( m_pDBPassword );
  78. m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 );
  79. strcpy( m_pDBHost, DBHost );
  80. m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 );
  81. strcpy( m_pDBUser, DBUser );
  82. m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 );
  83. strcpy( m_pDBPassword, DBPassword );
  84. m_nPort = Port;
  85. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  86. if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) {
  87. LOG_ERROR( mysql_error( m_pDBLink ) );
  88. } else m_bIsConnected = true;
  89. return m_bIsConnected;
  90. };
  91. bool MySQL_Connector::Connect( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, U16 Port ){
  92. if( !m_bIsConnected ){
  93. m_pDBLink = mysql_init(NULL);
  94. } else {
  95. m_bIsConnected = false;
  96. };
  97. if( m_pDBName != NULL ) delete( m_pDBName );
  98. if( m_pDBHost != NULL ) delete( m_pDBHost );
  99. if( m_pDBUser != NULL ) delete( m_pDBUser );
  100. if( m_pDBPassword != NULL ) delete( m_pDBPassword );
  101. m_pDBName = (CSTR) malloc( strlen( ( const char * ) DBName ) + 1 );
  102. strcpy( m_pDBName, DBName );
  103. m_pDBHost = (CSTR) malloc( strlen( ( const char * ) DBHost ) + 1 );
  104. strcpy( m_pDBHost, DBHost );
  105. m_pDBUser = (CSTR) malloc( strlen( ( const char * ) DBUser ) + 1 );
  106. strcpy( m_pDBUser, DBUser );
  107. m_pDBPassword = (CSTR) malloc( strlen( ( const char * ) DBPassword ) + 1 );
  108. strcpy( m_pDBPassword, DBPassword );
  109. m_nPort = Port;
  110. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  111. if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) {
  112. LOG_ERROR( mysql_error( m_pDBLink ) );
  113. } else {
  114. if( mysql_select_db( m_pDBLink, m_pDBName ) == 0 ){
  115. m_bIsConnected = true;
  116. #ifdef DEBUG
  117. char temp[256];
  118. sprintf( temp, "Connected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort );
  119. LOG( temp );
  120. #endif
  121. } else {
  122. LOG_ERROR( mysql_error( m_pDBLink ) );
  123. };
  124. };
  125. return m_bIsConnected;
  126. };
  127. bool MySQL_Connector::ReConnect( void ){
  128. if( !m_bIsConnected ){
  129. m_pDBLink = mysql_init(NULL);
  130. } else {
  131. m_bIsConnected = false;
  132. };
  133. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  134. if ( !mysql_real_connect( m_pDBLink , m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS ) ) {
  135. LOG_ERROR( mysql_error( m_pDBLink ) );
  136. } else {
  137. if( mysql_select_db( m_pDBLink, m_pDBName ) == 0 ){
  138. m_bIsConnected = true;
  139. #ifdef DEBUG
  140. char temp[256];
  141. sprintf( temp, "ReConnected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort );
  142. LOG( temp );
  143. #endif
  144. } else {
  145. LOG_ERROR( mysql_error( m_pDBLink ) );
  146. };
  147. };
  148. return m_bIsConnected;
  149. };
  150. AAssoc *MySQL_Connector::Query( CSTR QS, bool useCache ){
  151. AAssoc * RetVal = NULL;
  152. Assoc * tElement;
  153. unsigned int i;
  154. unsigned int num_Fields;
  155. MYSQL_RES * Result;
  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( Row[ i ] != NULL ){
  199. Val = new string( Row[ i ] );
  200. } else {
  201. Val = new string( "0" );//Row[ i ] );
  202. };
  203. if( Key != NULL && Val != NULL ){
  204. //(*tElement).insert( pair< string, string > ( *Key, *Val ) );
  205. (*tElement)[*Key]=*Val;
  206. } else {
  207. LOG_ERROR( "Alloc Error" );
  208. };
  209. };
  210. /*for (map<string, string>::iterator p = (*tElement).begin( ); p != (*tElement).end( ); ++p ) {
  211. cout << "Field: " << p->first<< ", #: " << p->second << endl;
  212. } */
  213. RetVal->push_back( tElement );
  214. };
  215. } else {
  216. #ifdef DEBUG_MYSQL_QUERIES
  217. LOG( "No Rows" );
  218. #endif
  219. RetVal = NULL;
  220. };
  221. mysql_free_result( Result );
  222. Result = NULL;
  223. } else {
  224. #ifdef DEBUG_MYSQL_QUERIES
  225. LOG( "Empty Result" );
  226. #endif
  227. RetVal = NULL;
  228. };
  229. m_mAccessMutex.unlock();
  230. return RetVal;
  231. };
  232. CSTR MySQL_Connector::escapeString( CSTR str ){
  233. CSTR temp;
  234. m_mAccessMutex.lock();
  235. temp = ( CSTR ) malloc( strlen( str ) * 2 + 1 );
  236. mysql_real_escape_string( m_pDBLink, temp, str, strlen( str ) );
  237. m_mAccessMutex.unlock();
  238. return temp;
  239. };
  240. uint64_t MySQL_Connector::QueryID( CSTR QS ){
  241. MYSQL_RES * Result;
  242. uint64_t RetVal = 0;
  243. m_mAccessMutex.lock();
  244. #ifdef DEBUG_MYSQL_QUERIES
  245. LOG( QS );
  246. #endif
  247. if ( mysql_query( m_pDBLink, QS ) ) {
  248. LOG_ERROR( mysql_error( m_pDBLink ));
  249. char temp[256];
  250. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  251. LOG_ERROR( temp );
  252. switch( mysql_errno( m_pDBLink ) ){
  253. case 2006:{// Server Gone
  254. if( ReConnect() ){
  255. if ( !mysql_query( m_pDBLink, QS ) ){
  256. Result = mysql_store_result( m_pDBLink );
  257. } else {
  258. Result = NULL;
  259. };
  260. } else {
  261. Result = NULL;
  262. };
  263. break;
  264. };
  265. };
  266. RetVal = 0;
  267. } else {
  268. RetVal = ( uint64_t ) mysql_insert_id( m_pDBLink );
  269. };
  270. m_mAccessMutex.unlock();
  271. return RetVal;
  272. };
  273. void MySQL_Connector::SilentQuery( CSTR QS ){
  274. m_mAccessMutex.lock();
  275. #ifdef DEBUG_MYSQL_QUERIES
  276. LOG( QS );
  277. #endif
  278. while ( mysql_query( m_pDBLink, QS ) ) {
  279. LOG_ERROR( mysql_error( m_pDBLink ));
  280. char temp[256];
  281. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  282. LOG_ERROR( temp );
  283. if( mysql_errno( m_pDBLink ) == 2013 || mysql_errno( m_pDBLink ) == 2006 ) {
  284. ReConnect();
  285. } else break;
  286. };
  287. //Result = NULL;
  288. m_mAccessMutex.unlock();
  289. };
  290. #undef LOG
  291. #undef LOG_ERROR