CDBConnect.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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(), DBSetting.Port, NULL, 0 ) )
  37. {
  38. Error = "Failed to connect database,";
  39. Error += " LastError=";
  40. Error += GetLastError();
  41. return false;
  42. }
  43. mysql_query(__pConn, "set names utf8");
  44. //依靠 mysql_ping 无法做到重连
  45. char value = 1;
  46. mysql_options(__pConn, MYSQL_OPT_RECONNECT, &value);
  47. return true;
  48. }
  49. void CDBConnect::Close()
  50. {
  51. stmtClose();
  52. if ( __pConn )
  53. {
  54. mysql_close( __pConn );
  55. __pConn = 0;
  56. }
  57. }
  58. int CDBConnect::ConnctionTest( std::string& Error )
  59. {
  60. int nRet = -1;
  61. if ( 0 == __pConn )
  62. {
  63. Error = "Error,not connected to database!";
  64. nRet = DB_ERR_NOT_CONNECT_DB;
  65. }
  66. else
  67. {
  68. nRet = mysql_ping( __pConn );
  69. if ( nRet != 0 )
  70. {
  71. Error = "Failed to mysql_ping,";
  72. Error += " LastError=";
  73. Error += GetLastError();
  74. }
  75. }
  76. return nRet;
  77. }
  78. int CDBConnect::AutoCommit( bool Mode, std::string& Error )
  79. {
  80. if ( 0 == __pConn )
  81. {
  82. Error = "Error,not connected to database!";
  83. return DB_ERR_NOT_CONNECT_DB;
  84. }
  85. return mysql_autocommit( __pConn, Mode );
  86. }
  87. int CDBConnect::Commit( std::string& Error )
  88. {
  89. if ( 0 == __pConn )
  90. {
  91. Error = "Error,not connected to database!";
  92. return DB_ERR_NOT_CONNECT_DB;
  93. }
  94. return mysql_commit( __pConn );
  95. }
  96. int CDBConnect::RollBack( std::string& Error )
  97. {
  98. if ( 0 == __pConn )
  99. {
  100. Error = "Error,not connected to database!";
  101. return DB_ERR_NOT_CONNECT_DB;
  102. }
  103. return mysql_rollback( __pConn );
  104. }
  105. CDBResultSet * CDBConnect::ExecuteQuery( const char * szSql, std::string & Error )
  106. {
  107. if ( 0 == __pConn )
  108. {
  109. Error = "Error,not connected to database!";
  110. return 0;
  111. }
  112. if ( mysql_query( __pConn, szSql ) )
  113. {
  114. Error = "Failed to execute SQL!";
  115. Error += " LastError=";
  116. Error += GetLastError();
  117. return 0;
  118. }
  119. if ( !__RessultSet.Bind( mysql_store_result( __pConn ), Error ) )
  120. {
  121. return 0;
  122. }
  123. return &__RessultSet;
  124. }
  125. MYSQL_RES* CDBConnect::Query( const char *szSql, std::string& Error )
  126. {
  127. if ( 0 == __pConn )
  128. {
  129. Error = "Error,not connected to database!";
  130. return 0;
  131. }
  132. if ( mysql_query( __pConn, szSql ) )
  133. {
  134. Error = "Failed to execute SQL!";
  135. Error += " LastError=";
  136. Error += GetLastError();
  137. return 0;
  138. }
  139. return mysql_store_result( __pConn );
  140. }
  141. my_ulonglong CDBConnect::ExecuteSql( const char * szSql, std::string& Error )
  142. {
  143. if ( 0 == __pConn )
  144. {
  145. Error = "Error,not connected to database!";
  146. return DB_ERR_NOT_CONNECT_DB;
  147. }
  148. if ( mysql_query( __pConn, szSql ) )
  149. {
  150. Error = "Failed to execute SQL!";
  151. Error += " LastError=";
  152. Error += GetLastError();
  153. return DB_ERR_EXCUTE_QUERY;
  154. }
  155. return mysql_affected_rows( __pConn );
  156. }
  157. my_ulonglong CDBConnect::ExecuteRealSql( const char * szSql, std::string & Error )
  158. {
  159. if ( 0 == __pConn )
  160. {
  161. Error = "Error,not connected to database!";
  162. return DB_ERR_NOT_CONNECT_DB;
  163. }
  164. unsigned long uSqlLen = strlen( szSql );
  165. if ( mysql_real_query( __pConn, szSql, uSqlLen ) )
  166. {
  167. Error = "Failed to execute SQL,";
  168. Error += " LastError=";
  169. Error += GetLastError();
  170. return DB_ERR_EXCUTE_QUERY;
  171. }
  172. return mysql_affected_rows( __pConn );
  173. }
  174. const char * CDBConnect::GetLastError()
  175. {
  176. if ( 0 == __pConn )
  177. {
  178. return "";
  179. }
  180. return mysql_error( __pConn );
  181. }
  182. my_ulonglong CDBConnect::GetLastInsertID( std::string& Error )
  183. {
  184. if ( 0 == __pConn )
  185. {
  186. Error = "Error,not connected to database!";
  187. return DB_ERR_NOT_CONNECT_DB;
  188. }
  189. return mysql_insert_id( __pConn );
  190. }
  191. bool CDBConnect::IsTemp()
  192. {
  193. return __IsTemp;
  194. }
  195. bool CDBConnect::Preparestmt( const char *szSql, std::string & Error )
  196. {
  197. if ( 0 == __pConn )
  198. {
  199. Error = "Error,not connected to database!";
  200. return DB_ERR_NOT_CONNECT_DB;
  201. }
  202. stmtClose();
  203. __pstmt = mysql_stmt_init( __pConn );
  204. if ( mysql_stmt_prepare( __pstmt, szSql, strlen( szSql ) ) != 0 )
  205. {
  206. Error = "Error, failed to mysql_stmt_prepare!";
  207. Error += " LastError=";
  208. Error += GetLastError();
  209. return false;
  210. }
  211. return true;
  212. }
  213. bool CDBConnect::stmtExcute( MYSQL_BIND *stBinds, uint64_t * piId, std::string & Error )
  214. {
  215. if ( __pstmt == NULL )
  216. {
  217. Error = "Bind error, not called Preparestmt function!";
  218. return false;
  219. }
  220. if ( mysql_stmt_bind_param( __pstmt, stBinds ) != 0 )
  221. {
  222. Error = "mysql_stmt_bind_param error,";
  223. Error += mysql_stmt_error( __pstmt );
  224. return false;
  225. }
  226. if ( mysql_stmt_execute( __pstmt ) != 0 )
  227. {
  228. Error = "mysql_stmt_execute error,";
  229. Error += mysql_stmt_error( __pstmt );
  230. return false;
  231. }
  232. if ( piId )
  233. {
  234. *piId = mysql_stmt_insert_id( __pstmt );
  235. }
  236. return true;
  237. }
  238. void CDBConnect::stmtClose()
  239. {
  240. if ( __pstmt )
  241. {
  242. mysql_stmt_close( __pstmt );
  243. __pstmt = 0;
  244. }
  245. }
  246. my_ulonglong CDBConnect::ExecuteSqlID( const char * szSql, std::string & Error )
  247. {
  248. if ( 0 == __pConn )
  249. {
  250. Error = "Error,not connected to database!";
  251. return DB_ERR_NOT_CONNECT_DB;
  252. }
  253. if ( mysql_real_query( __pConn, szSql, strlen( szSql ) ) )
  254. {
  255. Error = "Failed to execute SQL!";
  256. Error += " LastError=";
  257. Error += GetLastError();
  258. return DB_ERR_EXCUTE_QUERY;
  259. }
  260. return mysql_insert_id( __pConn );
  261. }
  262. }