CDBConnect.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "CDBConnect.h"
  2. #include <string.h>
  3. namespace YADB
  4. {
  5. CDBConnect::CDBConnect( bool IsTemp )
  6. {
  7. __pConn = 0;
  8. __IsTemp = IsTemp;
  9. __pstmt = 0;
  10. }
  11. CDBConnect::~CDBConnect()
  12. {
  13. Close();
  14. }
  15. bool CDBConnect::Connect( const _DB_CONN_SETTING_ & DBSetting, std::string & Error )
  16. {
  17. //创建
  18. __pConn = mysql_init( NULL );
  19. //超时设置
  20. if ( mysql_options( __pConn, MYSQL_OPT_CONNECT_TIMEOUT, &(DBSetting.TimeOut) ) != 0 )
  21. {
  22. Error = "Failed to call mysql_options,";
  23. Error += " LastError=";
  24. Error += GetLastError();
  25. return false;
  26. }
  27. //设置字符集
  28. if ( mysql_set_character_set( __pConn, DBSetting.CharSet.c_str() ) != 0 )
  29. {
  30. Error = "Failed to call mysql_set_character_set,";
  31. Error += " LastError=";
  32. Error += GetLastError();
  33. return false;
  34. }
  35. //连接数据库
  36. if ( !mysql_real_connect( __pConn, DBSetting.Host.c_str(), DBSetting.User.c_str(), DBSetting.PWD.c_str(), DBSetting.DBName.c_str(), 0, NULL, 0 ) )
  37. {
  38. Error = "Failed to connect database,";
  39. Error += " LastError=";
  40. Error += GetLastError();
  41. return false;
  42. }
  43. //依靠 mysql_ping 无法做到重连
  44. char value = 1;
  45. mysql_options(__pConn, MYSQL_OPT_RECONNECT, &value);
  46. return true;
  47. }
  48. void CDBConnect::Close()
  49. {
  50. stmtClose();
  51. if ( __pConn )
  52. {
  53. mysql_close( __pConn );
  54. __pConn = 0;
  55. }
  56. }
  57. int CDBConnect::ConnctionTest( std::string& Error )
  58. {
  59. int nRet = -1;
  60. if ( 0 == __pConn )
  61. {
  62. Error = "Error,not connected to database!";
  63. nRet = DB_ERR_NOT_CONNECT_DB;
  64. }
  65. else
  66. {
  67. nRet = mysql_ping( __pConn );
  68. if ( nRet != 0 )
  69. {
  70. Error = "Failed to mysql_ping,";
  71. Error += " LastError=";
  72. Error += GetLastError();
  73. }
  74. }
  75. return nRet;
  76. }
  77. int CDBConnect::AutoCommit( bool Mode, std::string& Error )
  78. {
  79. if ( 0 == __pConn )
  80. {
  81. Error = "Error,not connected to database!";
  82. return DB_ERR_NOT_CONNECT_DB;
  83. }
  84. return mysql_autocommit( __pConn, Mode );
  85. }
  86. int CDBConnect::Commit( std::string& Error )
  87. {
  88. if ( 0 == __pConn )
  89. {
  90. Error = "Error,not connected to database!";
  91. return DB_ERR_NOT_CONNECT_DB;
  92. }
  93. return mysql_commit( __pConn );
  94. }
  95. int CDBConnect::RollBack( std::string& Error )
  96. {
  97. if ( 0 == __pConn )
  98. {
  99. Error = "Error,not connected to database!";
  100. return DB_ERR_NOT_CONNECT_DB;
  101. }
  102. return mysql_rollback( __pConn );
  103. }
  104. CDBResultSet * CDBConnect::ExecuteQuery( const char * szSql, std::string & Error )
  105. {
  106. if ( 0 == __pConn )
  107. {
  108. Error = "Error,not connected to database!";
  109. return 0;
  110. }
  111. if ( mysql_query( __pConn, szSql ) )
  112. {
  113. Error = "Failed to execute SQL!";
  114. Error += " LastError=";
  115. Error += GetLastError();
  116. return 0;
  117. }
  118. if ( !__RessultSet.Bind( mysql_store_result( __pConn ), Error ) )
  119. {
  120. return 0;
  121. }
  122. return &__RessultSet;
  123. }
  124. MYSQL_RES* CDBConnect::Query( const char *szSql, std::string& Error )
  125. {
  126. if ( 0 == __pConn )
  127. {
  128. Error = "Error,not connected to database!";
  129. return 0;
  130. }
  131. if ( mysql_query( __pConn, szSql ) )
  132. {
  133. Error = "Failed to execute SQL!";
  134. Error += " LastError=";
  135. Error += GetLastError();
  136. return 0;
  137. }
  138. return mysql_store_result( __pConn );
  139. }
  140. my_ulonglong CDBConnect::ExecuteSql( const char * szSql, std::string& Error )
  141. {
  142. if ( 0 == __pConn )
  143. {
  144. Error = "Error,not connected to database!";
  145. return DB_ERR_NOT_CONNECT_DB;
  146. }
  147. if ( mysql_query( __pConn, szSql ) )
  148. {
  149. Error = "Failed to execute SQL!";
  150. Error += " LastError=";
  151. Error += GetLastError();
  152. return DB_ERR_EXCUTE_QUERY;
  153. }
  154. return mysql_affected_rows( __pConn );
  155. }
  156. my_ulonglong CDBConnect::ExecuteRealSql( const char * szSql, std::string & Error )
  157. {
  158. if ( 0 == __pConn )
  159. {
  160. Error = "Error,not connected to database!";
  161. return DB_ERR_NOT_CONNECT_DB;
  162. }
  163. unsigned long uSqlLen = strlen( szSql );
  164. if ( mysql_real_query( __pConn, szSql, uSqlLen ) )
  165. {
  166. Error = "Failed to execute SQL,";
  167. Error += " LastError=";
  168. Error += GetLastError();
  169. return DB_ERR_EXCUTE_QUERY;
  170. }
  171. return mysql_affected_rows( __pConn );
  172. }
  173. const char * CDBConnect::GetLastError()
  174. {
  175. if ( 0 == __pConn )
  176. {
  177. return "";
  178. }
  179. return mysql_error( __pConn );
  180. }
  181. my_ulonglong CDBConnect::GetLastInsertID( std::string& Error )
  182. {
  183. if ( 0 == __pConn )
  184. {
  185. Error = "Error,not connected to database!";
  186. return DB_ERR_NOT_CONNECT_DB;
  187. }
  188. return mysql_insert_id( __pConn );
  189. }
  190. bool CDBConnect::IsTemp()
  191. {
  192. return __IsTemp;
  193. }
  194. bool CDBConnect::Preparestmt( const char *szSql, std::string & Error )
  195. {
  196. if ( 0 == __pConn )
  197. {
  198. Error = "Error,not connected to database!";
  199. return DB_ERR_NOT_CONNECT_DB;
  200. }
  201. stmtClose();
  202. __pstmt = mysql_stmt_init( __pConn );
  203. if ( mysql_stmt_prepare( __pstmt, szSql, strlen( szSql ) ) != 0 )
  204. {
  205. Error = "Error, failed to mysql_stmt_prepare!";
  206. Error += " LastError=";
  207. Error += GetLastError();
  208. return false;
  209. }
  210. return true;
  211. }
  212. bool CDBConnect::stmtExcute( MYSQL_BIND *stBinds, uint64_t * piId, std::string & Error )
  213. {
  214. if ( __pstmt == NULL )
  215. {
  216. Error = "Bind error, not called Preparestmt function!";
  217. return false;
  218. }
  219. if ( mysql_stmt_bind_param( __pstmt, stBinds ) != 0 )
  220. {
  221. Error = "mysql_stmt_bind_param error,";
  222. Error += mysql_stmt_error( __pstmt );
  223. return false;
  224. }
  225. if ( mysql_stmt_execute( __pstmt ) != 0 )
  226. {
  227. Error = "mysql_stmt_execute error,";
  228. Error += mysql_stmt_error( __pstmt );
  229. return false;
  230. }
  231. if ( piId )
  232. {
  233. *piId = mysql_stmt_insert_id( __pstmt );
  234. }
  235. return true;
  236. }
  237. void CDBConnect::stmtClose()
  238. {
  239. if ( __pstmt )
  240. {
  241. mysql_stmt_close( __pstmt );
  242. __pstmt = 0;
  243. }
  244. }
  245. }