CDBConnect.cpp 5.4 KB

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