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
​
​
​
​
​
​
​
​
​
5 years ago
5 years ago
​
5 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. #ifdef DEBUG_MYSQL_QUERIES
  241. LOG(QS);
  242. #endif
  243. m_mAccessMutex.lock();
  244. /*
  245. if( mysql_query( m_pDBLink, QS ) ){
  246. LOG_ERROR( mysql_error( m_pDBLink ));
  247. char temp[256];
  248. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  249. LOG_ERROR( temp );
  250. switch( mysql_errno( m_pDBLink ) ){
  251. case 1156:
  252. case 2006:
  253. case 2013
  254. case 2014:{// Server Gone
  255. if( ReConnect() ){
  256. if ( !mysql_query( m_pDBLink, QS ) ){
  257. Result = mysql_store_result( m_pDBLink );
  258. } else {
  259. LOG_ERROR( mysql_error( m_pDBLink ));
  260. sprintf( temp, "Reconnect Error Code: %d", mysql_errno( m_pDBLink ));
  261. LOG_ERROR( temp );
  262. };
  263. };
  264. break;
  265. };
  266. };
  267. } else {
  268. Result = mysql_store_result( m_pDBLink );
  269. };*/
  270. while (mysql_query(m_pDBLink, QS))
  271. {
  272. LOG_ERROR(mysql_error(m_pDBLink));
  273. sprintf(temp, "Error Code: %d", mysql_errno(m_pDBLink));
  274. LOG_ERROR(temp);
  275. switch (mysql_errno(m_pDBLink))
  276. {
  277. case 1156:
  278. case 2006:
  279. case 2013:
  280. case 2014:
  281. {
  282. ReConnect();
  283. continue;
  284. };
  285. };
  286. break;
  287. };
  288. Result = mysql_store_result(m_pDBLink);
  289. if (Result != NULL)
  290. {
  291. if (mysql_num_rows(Result))
  292. {
  293. RetVal = new AAssoc();
  294. num_Fields = mysql_num_fields(Result);
  295. MYSQL_FIELD * Fields;
  296. MYSQL_ROW Row;
  297. Fields = mysql_fetch_fields(Result);
  298. while ((Row = mysql_fetch_row(Result)) != NULL)
  299. {
  300. tElement = new Assoc();
  301. string Key;
  302. string Val;
  303. for (int i = 0; i < num_Fields; i++)
  304. {
  305. Key = string(Fields[i].name);
  306. if (Row[i] != NULL)
  307. {
  308. Val = string(Row[i]);
  309. }
  310. else
  311. {
  312. Val = string("0");
  313. };
  314. (*tElement)[Key] = Val;
  315. };
  316. /*for (map<string, string>::iterator p = (*tElement).begin( ); p != (*tElement).end( ); ++p ) {
  317. cout << "Field: " << p->first<< ", #: " << p->second << endl;
  318. } */
  319. RetVal->push_back(tElement);
  320. };
  321. }
  322. else
  323. {
  324. #ifdef DEBUG_MYSQL_QUERIES
  325. LOG("No Rows");
  326. #endif
  327. RetVal = NULL;
  328. };
  329. mysql_free_result(Result);
  330. Result = NULL;
  331. }
  332. else
  333. {
  334. #ifdef DEBUG_MYSQL_QUERIES
  335. LOG("Empty Result");
  336. #endif
  337. RetVal = NULL;
  338. };
  339. m_mAccessMutex.unlock();
  340. return RetVal;
  341. }
  342. CSTR MySQL_Connector::escapeString(CSTR str)
  343. {
  344. int len = strlen(str);
  345. CSTR temp = (CSTR) malloc(len * 2 + 1);
  346. if (temp)
  347. {
  348. m_mAccessMutex.lock();
  349. mysql_real_escape_string(m_pDBLink, temp, str, len);
  350. m_mAccessMutex.unlock();
  351. };
  352. return temp;
  353. }
  354. CSTR MySQL_Connector::escapeBinary(uint8_t * Data, uint32_t Size)
  355. {
  356. CSTR temp = NULL;
  357. if (Data && Size)
  358. {
  359. temp = (CSTR) malloc(Size * 2 + 1);
  360. if (temp)
  361. {
  362. m_mAccessMutex.lock();
  363. mysql_real_escape_string(m_pDBLink, temp, (const char *) Data, Size);
  364. m_mAccessMutex.unlock();
  365. };
  366. };
  367. return temp;
  368. }
  369. uint64_t MySQL_Connector::QueryID(CSTR QS)
  370. {
  371. uint64_t RetVal = 0;
  372. char temp[256];
  373. m_mAccessMutex.lock();
  374. #ifdef DEBUG_MYSQL_QUERIES
  375. LOG(QS);
  376. #endif
  377. while (mysql_query(m_pDBLink, QS))
  378. {
  379. LOG_ERROR(mysql_error(m_pDBLink));
  380. sprintf(temp, "Error Code: %d", mysql_errno(m_pDBLink));
  381. LOG_ERROR(temp);
  382. switch (mysql_errno(m_pDBLink))
  383. {
  384. case 1156:
  385. case 2006:
  386. case 2013:
  387. case 2014:
  388. {
  389. ReConnect();
  390. continue;
  391. };
  392. };
  393. break;
  394. };
  395. RetVal = (uint64_t) mysql_insert_id(m_pDBLink);
  396. m_mAccessMutex.unlock();
  397. return RetVal;
  398. }
  399. void MySQL_Connector::SilentQuery(CSTR QS)
  400. {
  401. char temp[256];
  402. m_mAccessMutex.lock();
  403. #ifdef DEBUG_MYSQL_QUERIES
  404. LOG(QS);
  405. #endif
  406. while (mysql_query(m_pDBLink, QS))
  407. {
  408. LOG_ERROR(mysql_error(m_pDBLink));
  409. sprintf(temp, "Error Code: %d", mysql_errno(m_pDBLink));
  410. LOG_ERROR(temp);
  411. switch (mysql_errno(m_pDBLink))
  412. {
  413. case 1156:
  414. case 2006:
  415. case 2013:
  416. case 2014:
  417. {
  418. ReConnect();
  419. continue;
  420. };
  421. };
  422. break;
  423. };
  424. m_mAccessMutex.unlock();
  425. }
  426. void deleteResult(AAssoc *& R)
  427. {
  428. if (R)
  429. {
  430. while (R->size())
  431. {
  432. delete((Assoc *) R->at(0));
  433. R->erase(R->begin());
  434. };
  435. R->clear();
  436. delete(R);
  437. R = NULL;
  438. };
  439. }
  440. #undef LOG
  441. #undef LOG_ERROR