1
0

CDBConnect.cpp 5.7 KB

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