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.

463 lines
9.9 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
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
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
4 years ago
4 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
​
​
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() :
  6. m_mAccessMutex()
  7. {
  8. m_pLog = basicLogCollection::getInstance();
  9. m_pDBLink = NULL;
  10. m_pDBName = NULL;
  11. m_pDBHost = NULL;
  12. m_pDBUser = NULL;
  13. m_pDBPassword = NULL;
  14. m_nPort = 0;
  15. m_bIsConnected = false;
  16. }
  17. MySQL_Connector::MySQL_Connector(CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port) :
  18. m_mAccessMutex()
  19. {
  20. m_pLog = basicLogCollection::getInstance();
  21. m_pDBLink = mysql_init(NULL);
  22. m_pDBName = NULL;
  23. m_pDBHost = (CSTR) malloc(strlen((const char *) DBHost) + 1);
  24. strcpy(m_pDBHost, DBHost);
  25. m_pDBUser = (CSTR) malloc(strlen((const char *) DBUser) + 1);
  26. strcpy(m_pDBUser, DBUser);
  27. m_pDBPassword = (CSTR) malloc(strlen((const char *) DBPassword) + 1);
  28. strcpy(m_pDBPassword, DBPassword);
  29. m_nPort = Port;
  30. m_bIsConnected = false;
  31. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  32. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  33. {
  34. LOG_ERROR(mysql_error(m_pDBLink));
  35. }
  36. else
  37. {
  38. m_bIsConnected = true;
  39. }
  40. }
  41. MySQL_Connector::MySQL_Connector(CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port) :
  42. m_mAccessMutex()
  43. {
  44. m_pLog = basicLogCollection::getInstance();
  45. m_pDBLink = mysql_init(NULL);
  46. m_pDBName = (CSTR) malloc(strlen((const char *) DBName) + 1);
  47. strcpy(m_pDBName, DBName);
  48. m_pDBHost = (CSTR) malloc(strlen((const char *) DBHost) + 1);
  49. strcpy(m_pDBHost, DBHost);
  50. m_pDBUser = (CSTR) malloc(strlen((const char *) DBUser) + 1);
  51. strcpy(m_pDBUser, DBUser);
  52. m_pDBPassword = (CSTR) malloc(strlen((const char *) DBPassword) + 1);
  53. strcpy(m_pDBPassword, DBPassword);
  54. m_nPort = Port;
  55. m_bIsConnected = false;
  56. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  57. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  58. {
  59. LOG_ERROR(mysql_error(m_pDBLink));
  60. }
  61. else
  62. {
  63. if (mysql_select_db(m_pDBLink, m_pDBName) == 0)
  64. {
  65. m_bIsConnected = true;
  66. #ifdef DEBUG
  67. char temp[256];
  68. sprintf(temp, "Connected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort);
  69. LOG(temp);
  70. #endif
  71. }
  72. else
  73. {
  74. LOG_ERROR(mysql_error(m_pDBLink));
  75. };
  76. };
  77. }
  78. /* default Destructor */
  79. MySQL_Connector::~MySQL_Connector()
  80. {
  81. if (m_pDBName != NULL)
  82. {
  83. delete(m_pDBName);
  84. }
  85. if (m_pDBHost != NULL)
  86. {
  87. delete(m_pDBHost);
  88. }
  89. if (m_pDBUser != NULL)
  90. {
  91. delete(m_pDBUser);
  92. }
  93. if (m_pDBPassword != NULL)
  94. {
  95. delete(m_pDBPassword);
  96. }
  97. if (m_pDBLink != NULL)
  98. {
  99. mysql_close(m_pDBLink);
  100. }
  101. mysql_library_end();
  102. m_bIsConnected = false;
  103. }
  104. bool MySQL_Connector::Connect(CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port)
  105. {
  106. m_bIsConnected = false;
  107. if (m_pDBLink != NULL)
  108. {
  109. mysql_close(m_pDBLink);
  110. };
  111. m_pDBLink = mysql_init(NULL);
  112. if (m_pDBName != NULL)
  113. {
  114. delete(m_pDBName);
  115. }
  116. if (m_pDBHost != NULL)
  117. {
  118. delete(m_pDBHost);
  119. }
  120. if (m_pDBUser != NULL)
  121. {
  122. delete(m_pDBUser);
  123. }
  124. if (m_pDBPassword != NULL)
  125. {
  126. delete(m_pDBPassword);
  127. }
  128. m_pDBHost = (CSTR) malloc(strlen((const char *) DBHost) + 1);
  129. strcpy(m_pDBHost, DBHost);
  130. m_pDBUser = (CSTR) malloc(strlen((const char *) DBUser) + 1);
  131. strcpy(m_pDBUser, DBUser);
  132. m_pDBPassword = (CSTR) malloc(strlen((const char *) DBPassword) + 1);
  133. strcpy(m_pDBPassword, DBPassword);
  134. m_nPort = Port;
  135. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  136. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  137. {
  138. LOG_ERROR(mysql_error(m_pDBLink));
  139. }
  140. else
  141. {
  142. m_bIsConnected = true;
  143. }
  144. return m_bIsConnected;
  145. }
  146. bool MySQL_Connector::Connect(CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, U16 Port)
  147. {
  148. m_bIsConnected = false;
  149. if (m_pDBLink != NULL)
  150. {
  151. mysql_close(m_pDBLink);
  152. };
  153. m_pDBLink = mysql_init(NULL);
  154. if (m_pDBName != NULL)
  155. {
  156. delete(m_pDBName);
  157. }
  158. if (m_pDBHost != NULL)
  159. {
  160. delete(m_pDBHost);
  161. }
  162. if (m_pDBUser != NULL)
  163. {
  164. delete(m_pDBUser);
  165. }
  166. if (m_pDBPassword != NULL)
  167. {
  168. delete(m_pDBPassword);
  169. }
  170. m_pDBName = (CSTR) malloc(strlen((const char *) DBName) + 1);
  171. strcpy(m_pDBName, DBName);
  172. m_pDBHost = (CSTR) malloc(strlen((const char *) DBHost) + 1);
  173. strcpy(m_pDBHost, DBHost);
  174. m_pDBUser = (CSTR) malloc(strlen((const char *) DBUser) + 1);
  175. strcpy(m_pDBUser, DBUser);
  176. m_pDBPassword = (CSTR) malloc(strlen((const char *) DBPassword) + 1);
  177. strcpy(m_pDBPassword, DBPassword);
  178. m_nPort = Port;
  179. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  180. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  181. {
  182. LOG_ERROR(mysql_error(m_pDBLink));
  183. }
  184. else
  185. {
  186. if (mysql_select_db(m_pDBLink, m_pDBName) == 0)
  187. {
  188. m_bIsConnected = true;
  189. #ifdef DEBUG
  190. char temp[256];
  191. sprintf(temp, "Connected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort);
  192. LOG(temp);
  193. #endif
  194. }
  195. else
  196. {
  197. LOG_ERROR(mysql_error(m_pDBLink));
  198. };
  199. };
  200. return m_bIsConnected;
  201. }
  202. bool MySQL_Connector::ReConnect(void)
  203. {
  204. m_bIsConnected = false;
  205. if (m_pDBLink != NULL)
  206. {
  207. mysql_close(m_pDBLink);
  208. };
  209. m_pDBLink = mysql_init(NULL);
  210. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  211. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  212. {
  213. LOG_ERROR(mysql_error(m_pDBLink));
  214. }
  215. else
  216. {
  217. if (mysql_select_db(m_pDBLink, m_pDBName) == 0)
  218. {
  219. m_bIsConnected = true;
  220. #ifdef DEBUG
  221. char temp[256];
  222. sprintf(temp, "ReConnected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort);
  223. LOG(temp);
  224. #endif
  225. }
  226. else
  227. {
  228. LOG_ERROR(mysql_error(m_pDBLink));
  229. };
  230. };
  231. return m_bIsConnected;
  232. }
  233. AAssoc * MySQL_Connector::Query(CSTR QS, bool useCache)
  234. {
  235. AAssoc * RetVal = NULL;
  236. Assoc * tElement;
  237. unsigned int num_Fields;
  238. MYSQL_RES * Result = NULL;
  239. char temp[256];
  240. if (useCache) useCache = true;
  241. #ifdef DEBUG_MYSQL_QUERIES
  242. LOG(QS);
  243. #endif
  244. m_mAccessMutex.lock();
  245. /*
  246. if( mysql_query( m_pDBLink, QS ) ){
  247. LOG_ERROR( mysql_error( m_pDBLink ));
  248. char temp[256];
  249. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  250. LOG_ERROR( temp );
  251. switch( mysql_errno( m_pDBLink ) ){
  252. case 1156:
  253. case 2006:
  254. case 2013
  255. case 2014:{// Server Gone
  256. if( ReConnect() ){
  257. if ( !mysql_query( m_pDBLink, QS ) ){
  258. Result = mysql_store_result( m_pDBLink );
  259. } else {
  260. LOG_ERROR( mysql_error( m_pDBLink ));
  261. sprintf( temp, "Reconnect Error Code: %d", mysql_errno( m_pDBLink ));
  262. LOG_ERROR( temp );
  263. };
  264. };
  265. break;
  266. };
  267. };
  268. } else {
  269. Result = mysql_store_result( m_pDBLink );
  270. };*/
  271. while (mysql_query(m_pDBLink, QS))
  272. {
  273. LOG_ERROR(mysql_error(m_pDBLink));
  274. sprintf(temp, "Error Code: %d", mysql_errno(m_pDBLink));
  275. LOG_ERROR(temp);
  276. switch (mysql_errno(m_pDBLink))
  277. {
  278. case 1156:
  279. case 2006:
  280. case 2013:
  281. case 2014:
  282. {
  283. ReConnect();
  284. continue;
  285. };
  286. };
  287. break;
  288. };
  289. Result = mysql_store_result(m_pDBLink);
  290. if (Result != NULL)
  291. {
  292. if (mysql_num_rows(Result))
  293. {
  294. RetVal = new AAssoc();
  295. num_Fields = mysql_num_fields(Result);
  296. MYSQL_FIELD * Fields;
  297. MYSQL_ROW Row;
  298. Fields = mysql_fetch_fields(Result);
  299. while ((Row = mysql_fetch_row(Result)) != NULL)
  300. {
  301. tElement = new Assoc();
  302. string Key;
  303. string Val;
  304. for (uint32_t i = 0; i < num_Fields; i++)
  305. {
  306. Key = string(Fields[i].name);
  307. if (Row[i] != NULL)
  308. {
  309. Val = string(Row[i]);
  310. }
  311. else
  312. {
  313. Val = string("0");
  314. };
  315. (*tElement)[Key] = Val;
  316. };
  317. /*for (map<string, string>::iterator p = (*tElement).begin( ); p != (*tElement).end( ); ++p ) {
  318. cout << "Field: " << p->first<< ", #: " << p->second << endl;
  319. } */
  320. RetVal->push_back(tElement);
  321. };
  322. }
  323. else
  324. {
  325. #ifdef DEBUG_MYSQL_QUERIES
  326. LOG("No Rows");
  327. #endif
  328. RetVal = NULL;
  329. };
  330. mysql_free_result(Result);
  331. Result = NULL;
  332. }
  333. else
  334. {
  335. #ifdef DEBUG_MYSQL_QUERIES
  336. LOG("Empty Result");
  337. #endif
  338. RetVal = NULL;
  339. };
  340. m_mAccessMutex.unlock();
  341. return RetVal;
  342. }
  343. CSTR MySQL_Connector::escapeString(CSTR str)
  344. {
  345. int len = strlen(str);
  346. CSTR temp = (CSTR) malloc(len * 2 + 1);
  347. if (temp)
  348. {
  349. m_mAccessMutex.lock();
  350. mysql_real_escape_string(m_pDBLink, temp, str, len);
  351. m_mAccessMutex.unlock();
  352. };
  353. return temp;
  354. }
  355. CSTR MySQL_Connector::escapeBinary(uint8_t * Data, uint32_t Size)
  356. {
  357. CSTR temp = NULL;
  358. if (Data && Size)
  359. {
  360. temp = (CSTR) malloc(Size * 2 + 1);
  361. if (temp)
  362. {
  363. m_mAccessMutex.lock();
  364. mysql_real_escape_string(m_pDBLink, temp, (const char *) Data, Size);
  365. m_mAccessMutex.unlock();
  366. };
  367. };
  368. return temp;
  369. }
  370. uint64_t MySQL_Connector::QueryID(CSTR QS)
  371. {
  372. uint64_t RetVal = 0;
  373. char temp[256];
  374. m_mAccessMutex.lock();
  375. #ifdef DEBUG_MYSQL_QUERIES
  376. LOG(QS);
  377. #endif
  378. while (mysql_query(m_pDBLink, QS))
  379. {
  380. LOG_ERROR(mysql_error(m_pDBLink));
  381. sprintf(temp, "Error Code: %d", mysql_errno(m_pDBLink));
  382. LOG_ERROR(temp);
  383. switch (mysql_errno(m_pDBLink))
  384. {
  385. case 1156:
  386. case 2006:
  387. case 2013:
  388. case 2014:
  389. {
  390. ReConnect();
  391. continue;
  392. };
  393. };
  394. break;
  395. };
  396. RetVal = (uint64_t) mysql_insert_id(m_pDBLink);
  397. m_mAccessMutex.unlock();
  398. return RetVal;
  399. }
  400. void MySQL_Connector::SilentQuery(CSTR QS)
  401. {
  402. char temp[256];
  403. m_mAccessMutex.lock();
  404. #ifdef DEBUG_MYSQL_QUERIES
  405. LOG(QS);
  406. #endif
  407. while (mysql_query(m_pDBLink, QS))
  408. {
  409. LOG_ERROR(mysql_error(m_pDBLink));
  410. sprintf(temp, "Error Code: %d", mysql_errno(m_pDBLink));
  411. LOG_ERROR(temp);
  412. switch (mysql_errno(m_pDBLink))
  413. {
  414. case 1156:
  415. case 2006:
  416. case 2013:
  417. case 2014:
  418. {
  419. ReConnect();
  420. continue;
  421. };
  422. };
  423. break;
  424. };
  425. m_mAccessMutex.unlock();
  426. }
  427. void deleteResult(AAssoc *& R)
  428. {
  429. if (R)
  430. {
  431. while (R->size())
  432. {
  433. delete((Assoc *) R->at(0));
  434. R->erase(R->begin());
  435. };
  436. R->clear();
  437. delete(R);
  438. R = NULL;
  439. };
  440. }
  441. #undef LOG
  442. #undef LOG_ERROR