linux版本中间件

JdbcHelper.cpp 8.4KB

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