linux版本中间件

JdbcHelper.cpp 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #include "JdbcHelper.h"
  2. #include <cppconn/parameter_metadata.h>
  3. //JdbcHelper::JdbcHelper(const std::string& host, const std::string& username, const std::string& passwd, const std::string& database)
  4. //JdbcHelper::JdbcHelper( std::string host, std::string username, std::string passwd, std::string database)
  5. //{
  6. // this->host = host;
  7. // this->username = username;
  8. // this->passwd = passwd;
  9. // this->database = database;
  10. //
  11. // this->conn = NULL;
  12. //
  13. // try {
  14. // this->driver = get_driver_instance();
  15. // }
  16. // catch (sql::SQLException &e) {
  17. // this->printSQLException(e, "get_driver_instance");
  18. // }
  19. //
  20. // if (driver == NULL) {
  21. // std::cout << "driver is null" << std::endl;
  22. // }
  23. //}
  24. JdbcHelper::~JdbcHelper()
  25. {
  26. jdbc_close_connect();
  27. }
  28. bool JdbcHelper::init(std::string host, std::string username, std::string passwd, std::string database)
  29. {
  30. this->host = host;
  31. this->username = username;
  32. this->passwd = passwd;
  33. this->database = database;
  34. this->conn = nullptr;
  35. try {
  36. this->driver = get_driver_instance();
  37. }
  38. catch (sql::SQLException &e) {
  39. this->printSQLException(e, "get_driver_instance");
  40. return false;
  41. }
  42. if (driver == nullptr) {
  43. std::cout << "driver is null" << std::endl;
  44. return false;
  45. }
  46. return true;
  47. }
  48. int JdbcHelper::jdbc_connect(bool enableSSL)
  49. {
  50. if (nullptr != conn) {
  51. return -1;
  52. }
  53. this->m_enableSSL = enableSSL;
  54. try {
  55. sql::ConnectOptionsMap opts;
  56. opts["hostName"] = this->host;
  57. opts["userName"] = this->username;
  58. opts["password"] = this->passwd;
  59. if (enableSSL) {
  60. // 默认使用SSL
  61. }
  62. else {
  63. opts["OPT_SSL_MODE"] = sql::SSL_MODE_DISABLED;
  64. }
  65. opts["OPT_RECONNECT"] = sql::ConnectPropertyVal(true);
  66. this->conn = driver->connect(opts);
  67. if (NULL == conn) {
  68. printf("conn id null\n");
  69. return -2;
  70. }
  71. // 选择数据库
  72. sql::SQLString catalog(this->database);
  73. conn->setSchema(catalog);
  74. return 0;
  75. }
  76. catch (sql::SQLException &e) {
  77. this->printSQLException(e, "connect");
  78. }
  79. return -3;
  80. }
  81. int JdbcHelper::jdbc_close_connect()
  82. {
  83. if (conn) {
  84. conn->close();
  85. delete conn;
  86. conn = nullptr;
  87. return 0;
  88. }
  89. return -1;
  90. }
  91. bool JdbcHelper::jdbc_is_connected()
  92. {
  93. if (conn) {
  94. return (conn->isClosed() == false);
  95. }
  96. else {
  97. this->jdbc_connect(m_enableSSL);
  98. }
  99. return false;
  100. }
  101. int JdbcHelper::jdbc_set_auto_commit(bool auto_commit)
  102. {
  103. if (NULL == conn || conn->isClosed()) {
  104. printf("jdbc conn is closed\n");
  105. return -1;
  106. }
  107. try {
  108. conn->setAutoCommit(auto_commit);
  109. return 0;
  110. }
  111. catch (sql::SQLException &e) {
  112. this->printSQLException(e, "setAutoCommit");
  113. }
  114. return -2;
  115. }
  116. int JdbcHelper::jdbc_commit()
  117. {
  118. if (NULL == conn || conn->isClosed()) {
  119. printf("jdbc conn is closed\n");
  120. return -1;
  121. }
  122. try {
  123. conn->commit();
  124. return 0;
  125. }
  126. catch (sql::SQLException &e) {
  127. this->printSQLException(e, "commit");
  128. }
  129. return -2;
  130. }
  131. sql::Savepoint* JdbcHelper::jdbc_save_point(const std::string& name)
  132. {
  133. if (NULL == conn || conn->isClosed()) {
  134. printf("jdbc conn is closed\n");
  135. return NULL;
  136. }
  137. try {
  138. sql::SQLString savePointName(name);
  139. return conn->setSavepoint(savePointName);
  140. }
  141. catch (sql::SQLException &e) {
  142. this->printSQLException(e, "setSavepoint");
  143. }
  144. return NULL;
  145. }
  146. int JdbcHelper::jdbc_rollback_save_point(sql::Savepoint* point)
  147. {
  148. if (NULL == conn || conn->isClosed()) {
  149. printf("jdbc conn is closed\n");
  150. return -1;
  151. }
  152. if (NULL == point) {
  153. return -2;
  154. }
  155. try {
  156. conn->rollback(point);
  157. return 0;
  158. }
  159. catch (sql::SQLException &e) {
  160. this->printSQLException(e, "rollback");
  161. }
  162. return -3;
  163. }
  164. int JdbcHelper::jdbc_release_save_point(sql::Savepoint* point)
  165. {
  166. if (NULL == conn || conn->isClosed()) {
  167. printf("jdbc conn is closed\n");
  168. return -1;
  169. }
  170. if (NULL == point) {
  171. return -2;
  172. }
  173. int rc = -1;
  174. try {
  175. conn->releaseSavepoint(point);
  176. rc = 0;
  177. }
  178. catch (sql::SQLException &e) {
  179. this->printSQLException(e, "releaseSavepoint");
  180. rc = -3;
  181. }
  182. delete point;
  183. return rc;
  184. }
  185. int JdbcHelper::jdbc_executeUpdate(
  186. const std::string& sql,
  187. std::function<void(sql::PreparedStatement* stmt)> prepCallBack,
  188. std::function<void(sql::SQLException &e)> exceptionCallBack)
  189. {
  190. std::unique_lock<std::mutex> locak(mut);
  191. if (NULL == conn || conn->isClosed()) {
  192. printf("jdbc conn is closed\n");
  193. return -1;
  194. }
  195. int row_affected = -1;
  196. sql::PreparedStatement* stmt = NULL;
  197. try {
  198. do
  199. {
  200. sql::SQLString sqlString(sql);
  201. stmt = conn->prepareStatement(sqlString);
  202. if (NULL == stmt) {
  203. row_affected = -2;
  204. break;
  205. }
  206. sql::ParameterMetaData* paramMetaData = stmt->getParameterMetaData();
  207. if (paramMetaData) {
  208. if (paramMetaData->getParameterCount() > 0) {
  209. if (prepCallBack) {
  210. prepCallBack(stmt);
  211. }
  212. }
  213. }
  214. row_affected = stmt->executeUpdate(); // 插入数据
  215. } while (false);
  216. }
  217. catch (sql::SQLException &e)
  218. {
  219. this->printSQLException(e, sql);
  220. if (exceptionCallBack) {
  221. exceptionCallBack(e);
  222. }
  223. }
  224. if (stmt) {
  225. try {
  226. stmt->close();
  227. }
  228. catch (sql::SQLException &e) {
  229. this->printSQLException(e, "close stmt res");
  230. }
  231. delete stmt;
  232. }
  233. return row_affected;
  234. }
  235. int JdbcHelper::jdbc_executeQuery(
  236. const std::string& sql,
  237. std::function<void(sql::PreparedStatement* stmt)> prepCallBack,
  238. std::function<void(sql::ResultSet* result)> resultCallBack,
  239. std::function<void(sql::SQLException &e)> exceptionCallBack)
  240. {
  241. std::unique_lock<std::mutex> locak(mut);
  242. if (NULL == conn || conn->isClosed()) {
  243. printf("jdbc conn is closed\n");
  244. return -1;
  245. }
  246. int rc = -1;
  247. sql::PreparedStatement* prep_stmt = NULL;
  248. sql::ResultSet* res = NULL;
  249. try {
  250. do
  251. {
  252. // std::cout << "SQL: " << sql << std::endl;
  253. sql::SQLString sqlString(sql);
  254. prep_stmt = conn->prepareStatement(sqlString);
  255. if (NULL == prep_stmt) {
  256. rc = -2;
  257. break;
  258. }
  259. if (prepCallBack) {
  260. sql::ParameterMetaData* paramMetaData = prep_stmt->getParameterMetaData();
  261. if (paramMetaData && paramMetaData->getParameterCount() > 0) {
  262. prepCallBack(prep_stmt);
  263. }
  264. }
  265. res = prep_stmt->executeQuery(); // 查询数据
  266. if (NULL == res) {
  267. rc = -3;
  268. break;
  269. }
  270. if (resultCallBack) {
  271. sql::ResultSetMetaData* metaData = prep_stmt->getMetaData();
  272. if (metaData && metaData->getColumnCount() > 0) {
  273. // printSQLMeta(metaData);
  274. resultCallBack(res);
  275. }
  276. }
  277. rc = 0;
  278. } while (false);
  279. }
  280. catch (sql::SQLException &e)
  281. {
  282. // if (e.getErrorCode() == CR_SERVER_LOST) { } // in errmsg.h of mysqlclient
  283. printSQLException(e, sql);
  284. if (exceptionCallBack) {
  285. exceptionCallBack(e);
  286. }
  287. }
  288. if (prep_stmt) {
  289. try {
  290. prep_stmt->close();
  291. }
  292. catch (sql::SQLException &e) {
  293. this->printSQLException(e, "close stmt");
  294. }
  295. delete prep_stmt;
  296. }
  297. if (res) {
  298. try {
  299. res->close();
  300. }
  301. catch (sql::SQLException &e) {
  302. this->printSQLException(e, "close stmt res");
  303. }
  304. delete res;
  305. }
  306. return rc;
  307. }
  308. void JdbcHelper::printSQLException(sql::SQLException &e, const std::string& sql)
  309. {
  310. std::cout << "message: " << e.what() << std::endl;
  311. std::cout << "code: " << e.getErrorCode() << std::endl;
  312. std::cout << "state: " << e.getSQLState() << std::endl;
  313. std::cout << "sql: " << sql << std::endl;
  314. }
  315. void JdbcHelper::printSQLMeta(sql::ResultSetMetaData* metaData)
  316. {
  317. int columnCount = metaData->getColumnCount();
  318. for (int i = 1; i <= columnCount; i++) {
  319. std::cout << "column: " << i << ", name: " << metaData->getColumnName(i) << ", type: " << metaData->getColumnTypeName(i) << std::endl;
  320. }
  321. }
  322. JdbcHelper JdbcHelper::instance;
  323. //int main(int argc, char* argv[])
  324. //{
  325. // int data = 3;
  326. //
  327. // JdbcHelper jdbc("tcp://192.168.6.80:23306", "root", "123456", "test");
  328. // jdbc.jdbc_connect(true);
  329. //
  330. // std::string sql = "SELECT id, label FROM test WHERE id = ?";
  331. // jdbc.jdbc_executeQuery(sql, [=](sql::PreparedStatement* stmt) {
  332. // stmt->setInt(1, data);
  333. // }, [](sql::ResultSet* result) {
  334. //
  335. // while (result->next())
  336. // {
  337. // int64_t id = result->getInt64(1);
  338. // sql::SQLString label = result->getString(2);
  339. //
  340. // std::cout << "id: " << id << " label: " << label << std::endl;
  341. // }
  342. // }, NULL);
  343. //
  344. //
  345. // sql = "INSERT INTO test(id, label) VALUES(?, ?)";
  346. // int affected_rows = jdbc.jdbc_executeUpdate(sql, [](sql::PreparedStatement* stmt) {
  347. // stmt->setInt64(1, 1234);
  348. // stmt->setString(2, "label_1234");
  349. // }, [sql](sql::SQLException &e) {
  350. // std::cerr << "exception, SQL: " << sql << std::endl;
  351. // });
  352. //
  353. // std::cout << "affected_rows: " << affected_rows << std::endl;
  354. //
  355. // jdbc.jdbc_close_connect();
  356. //
  357. // return 0;
  358. //}