mysql.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*Software License
  2. Copyright(C) 2024[liuyingjie]
  3. License Terms
  4. Usage Rights
  5. Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code.
  6. If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license.
  7. Future Versions Updates
  8. From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version.
  9. Users must comply with the terms of the new license issued in future releases.
  10. Liability and Disclaimer
  11. This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
  12. Contact Information
  13. If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com.
  14. */
  15. #include "db/mysql.h"
  16. #if defined(_WIN64) || defined(__linux__)
  17. #include "cppconn/driver.h"
  18. #include "cppconn/connection.h"
  19. #include "cppconn/statement.h"
  20. #include "cppconn/prepared_statement.h"
  21. #include "cppconn/resultset.h"
  22. #include "cppconn/metadata.h"
  23. #include "cppconn/resultset_metadata.h"
  24. #include "cppconn/exception.h"
  25. #include "cppconn/warning.h"
  26. #include "util/strutils.h"
  27. #define CHECK_SQL_CONNECTION if(m_handle == nullptr){throw ylib::exception("current connection is invalid");}
  28. #define CHECK_SQL_PPST if(m_handle == nullptr){throw ylib::exception("current preparedstatement is invalid");}
  29. #define CHECK_SQL_RESULT if(m_handle == nullptr){throw ylib::exception("current resultset is invalid");}
  30. #define CONNECTION ((sql::Connection*)m_handle)
  31. #define PREPARE_STATEMENT ((sql::PreparedStatement*)m_handle)
  32. #define RESULT_SET ((sql::ResultSet*)m_handle)
  33. #define OPTS (*(sql::ConnectOptionsMap*)m_opts)
  34. #ifdef _DEBUG
  35. #define DEBUG_LOG_SETSQL 1
  36. #define DEBUG_LOG_PPST_SET 1
  37. #else
  38. #define DEBUG_LOG_SETSQL 0
  39. #define DEBUG_LOG_PPST_SET 0
  40. #endif
  41. ylib::mysql::conn::conn()
  42. {
  43. m_handle = nullptr;
  44. m_ppst = nullptr;
  45. }
  46. ylib::mysql::conn::~conn()
  47. {
  48. close();
  49. }
  50. EXAMPLE_START_RESULT ylib::mysql::conn::start(const mysql_conn_info &info)
  51. {
  52. if (info.ipaddress.empty())
  53. {
  54. m_lastErrorDesc = "address is empty";
  55. return SR_FAILED;
  56. }
  57. if (info.username.empty())
  58. {
  59. m_lastErrorDesc = "username is empty";
  60. return SR_FAILED;
  61. }
  62. if (info.password.empty())
  63. {
  64. m_lastErrorDesc = "password is empty";
  65. return SR_FAILED;
  66. }
  67. if (info.charset.empty())
  68. {
  69. m_lastErrorDesc = "charset is empty";
  70. return SR_FAILED;
  71. }
  72. m_info = info;
  73. sql::Driver* driver = get_driver_instance();
  74. // Configure Connect info
  75. sql::ConnectOptionsMap opts;
  76. opts["hostName"] = m_info.ipaddress.c_str();
  77. opts["userName"] = m_info.username.c_str();
  78. opts["password"] = m_info.password.c_str();
  79. if(m_info.database.empty() == false)
  80. opts["schema"] = m_info.database.c_str();
  81. opts["port"] = (int)m_info.port;
  82. opts["OPT_CONNECT_TIMEOUT"] = 3;
  83. opts["OPT_CHARSET_NAME"] = m_info.charset.c_str();
  84. opts["OPT_SSL_MODE"] = sql::SSL_MODE_DISABLED;
  85. // opts["OPT_RECONNECT"] = sql::ConnectPropertyVal(true);
  86. sql::Connection* conn = nullptr;
  87. try
  88. {
  89. conn = driver->connect(opts);
  90. m_handle = conn;
  91. return EXAMPLE_START_RESULT::SR_SUCCESS;
  92. }
  93. catch (const sql::SQLException& e)
  94. {
  95. if (conn != nullptr)
  96. delete conn;
  97. m_lastErrorDesc = e.what();
  98. }
  99. if (m_lastErrorDesc.find("timeout") == -1)
  100. return EXAMPLE_START_RESULT::SR_FAILED;
  101. else
  102. return EXAMPLE_START_RESULT::SR_TIMEOUT;
  103. }
  104. void ylib::mysql::conn::close()
  105. {
  106. clear();
  107. if (CONNECTION == nullptr)
  108. return;
  109. try
  110. {
  111. CONNECTION->close();
  112. }
  113. catch (const std::exception& e)
  114. {
  115. std::cout << e.what() << std::endl;
  116. }
  117. delete CONNECTION;
  118. m_handle = nullptr;
  119. }
  120. void ylib::mysql::conn::recover()
  121. {
  122. if (m_sw == 1)
  123. {
  124. rollback();
  125. }
  126. clear();
  127. }
  128. void ylib::mysql::conn::task_out()
  129. {
  130. try
  131. {
  132. if(m_sw != 0 || CONNECTION->isValid() == false) {
  133. close();
  134. if (start(m_info) != SR_SUCCESS)
  135. {
  136. throw ylib::exception(this->last_error());
  137. }
  138. m_sw = 0;
  139. }
  140. }
  141. catch (const sql::SQLException & e)
  142. {
  143. throw ylib::exception(e.what());
  144. }
  145. }
  146. void ylib::mysql::conn::clear()
  147. {
  148. if(this->m_ppst != nullptr)
  149. delete this->m_ppst;
  150. this->m_ppst = nullptr;
  151. }
  152. ylib::mysql::prepare_statement* ylib::mysql::conn::setsql(const std::string &sql)
  153. {
  154. #if DEBUG_LOG_SETSQL == 1
  155. std::cout << "setsql : " << sql << std::endl;
  156. #endif
  157. CHECK_SQL_CONNECTION;
  158. clear();
  159. m_ppst = new prepare_statement;
  160. try
  161. {
  162. m_ppst->m_handle = CONNECTION->prepareStatement(sql.c_str());
  163. }
  164. catch (const sql::SQLException & e)
  165. {
  166. throw ylib::exception(e.what());
  167. }
  168. return m_ppst;
  169. }
  170. uint64 ylib::mysql::conn::insert_id()
  171. {
  172. CHECK_SQL_CONNECTION;
  173. clear();
  174. m_ppst = new prepare_statement;
  175. uint64 last_insert_id = 0;
  176. try
  177. {
  178. m_ppst->m_handle = CONNECTION->prepareStatement("SELECT LAST_INSERT_ID()");
  179. auto result = m_ppst->query();
  180. result->next();
  181. last_insert_id = result->get_uint64(1);
  182. }
  183. catch (const sql::SQLException& e)
  184. {
  185. throw ylib::exception(e.what());
  186. }
  187. return last_insert_id;
  188. }
  189. void ylib::mysql::conn::begin(bool autocommit)
  190. {
  191. try
  192. {
  193. CONNECTION->setAutoCommit(autocommit);
  194. m_sw = 1;
  195. }
  196. catch (const sql::SQLException & e)
  197. {
  198. throw ylib::exception(e.what());
  199. //std::cout<<e.what()<<std::endl;
  200. }
  201. }
  202. void ylib::mysql::conn::commit()
  203. {
  204. try
  205. {
  206. CONNECTION->commit();
  207. m_sw = 2;
  208. }
  209. catch (const sql::SQLException & e)
  210. {
  211. throw ylib::exception(e.what());
  212. //std::cout<<e.what()<<std::endl;
  213. }
  214. }
  215. void ylib::mysql::conn::rollback()
  216. {
  217. try
  218. {
  219. CONNECTION->rollback();
  220. m_sw = 2;
  221. }
  222. catch (const sql::SQLException & e)
  223. {
  224. throw ylib::exception(e.what());
  225. //std::cout<<e.what()<<std::endl;
  226. }
  227. }
  228. void ylib::mysql::conn::setDatabase(const std::string& name)
  229. {
  230. try
  231. {
  232. CONNECTION->setSchema(name);
  233. }
  234. catch (const sql::SQLException& e)
  235. {
  236. throw ylib::exception(e.what());
  237. }
  238. }
  239. ylib::mysql::prepare_statement::prepare_statement()
  240. {
  241. m_result = nullptr;
  242. m_handle = nullptr;
  243. }
  244. ylib::mysql::prepare_statement::~prepare_statement()
  245. {
  246. clear();
  247. if(PREPARE_STATEMENT != nullptr)
  248. {
  249. try
  250. {
  251. while (PREPARE_STATEMENT->getMoreResults()) {
  252. PREPARE_STATEMENT->getResultSet()->close();
  253. }
  254. PREPARE_STATEMENT->close();
  255. delete PREPARE_STATEMENT;
  256. }
  257. catch (const sql::SQLException & e)
  258. {
  259. std::cout<<e.what()<<std::endl;
  260. }
  261. }
  262. }
  263. #if 0
  264. #define PRINT_DEBUG_SET std::cout<<__FUNCTION__<<"["<<index<<"]: "<<value<<std::endl
  265. #define PRINT_DEBUG_SET_std::string std::cout<<__FUNCTION__<<"["<<index<<"]: "<<value.c_str()<<std::endl
  266. #else
  267. #define PRINT_DEBUG_SET
  268. #define PRINT_DEBUG_SET_NSTRING
  269. #endif
  270. void ylib::mysql::prepare_statement::set_bigint(uint32 index, const std::string &value)
  271. {
  272. CHECK_SQL_PPST;
  273. PRINT_DEBUG_SET_NSTRING;
  274. PREPARE_STATEMENT->setBigInt(index,value.c_str());
  275. #if DEBUG_LOG_PPST_SET == 1
  276. std::cout << "set " << index << " = " << value << std::endl;
  277. #endif
  278. //PREPARE_STATEMENT->insert
  279. }
  280. void ylib::mysql::prepare_statement::set_boolean(uint32 index, bool value)
  281. {
  282. CHECK_SQL_PPST;
  283. PRINT_DEBUG_SET;
  284. PREPARE_STATEMENT->setBoolean(index,value);
  285. #if DEBUG_LOG_PPST_SET == 1
  286. std::cout << "set " << index << " = " << value << std::endl;
  287. #endif
  288. }
  289. void ylib::mysql::prepare_statement::set_datetime(uint32 index, const std::string &value)
  290. {
  291. CHECK_SQL_PPST;
  292. PRINT_DEBUG_SET_NSTRING;
  293. if (value.empty())
  294. {
  295. PREPARE_STATEMENT->setNull(index, sql::DataType::DATE);
  296. }
  297. else
  298. {
  299. PREPARE_STATEMENT->setDateTime(index, value.c_str());
  300. }
  301. #if DEBUG_LOG_PPST_SET == 1
  302. std::cout << "set " << index << " = " << value << std::endl;
  303. #endif
  304. }
  305. void ylib::mysql::prepare_statement::set_double(uint32 index, double value)
  306. {
  307. CHECK_SQL_PPST;
  308. PRINT_DEBUG_SET;
  309. PREPARE_STATEMENT->setDouble(index,value);
  310. #if DEBUG_LOG_PPST_SET == 1
  311. std::cout << "set " << index << " = " << value << std::endl;
  312. #endif
  313. }
  314. void ylib::mysql::prepare_statement::set_int32(uint32 index, int32 value)
  315. {
  316. CHECK_SQL_PPST;
  317. PRINT_DEBUG_SET;
  318. PREPARE_STATEMENT->setInt(index,value);
  319. #if DEBUG_LOG_PPST_SET == 1
  320. std::cout << "set " << index << " = " << value << std::endl;
  321. #endif
  322. }
  323. void ylib::mysql::prepare_statement::set_uint32(uint32 index, uint32 value)
  324. {
  325. CHECK_SQL_PPST;
  326. PRINT_DEBUG_SET;
  327. PREPARE_STATEMENT->setUInt(index,value);
  328. #if DEBUG_LOG_PPST_SET == 1
  329. std::cout << "set " << index << " = " << value << std::endl;
  330. #endif
  331. }
  332. void ylib::mysql::prepare_statement::set_int64(uint32 index, int64 value)
  333. {
  334. CHECK_SQL_PPST;
  335. PRINT_DEBUG_SET;
  336. PREPARE_STATEMENT->setInt64(index,value);
  337. #if DEBUG_LOG_PPST_SET == 1
  338. std::cout << "set " << index << " = " << value << std::endl;
  339. #endif
  340. }
  341. void ylib::mysql::prepare_statement::set_uint64(uint32 index, uint64 value)
  342. {
  343. CHECK_SQL_PPST;
  344. PRINT_DEBUG_SET;
  345. PREPARE_STATEMENT->setUInt64(index,value);
  346. #if DEBUG_LOG_PPST_SET == 1
  347. std::cout << "set " << index << " = " << value << std::endl;
  348. #endif
  349. }
  350. void ylib::mysql::prepare_statement::set_null(uint32 index)
  351. {
  352. throw ylib::exception("Not supported temporarily");
  353. //PREPARE_STATEMENT->setNull(index,value);
  354. }
  355. void ylib::mysql::prepare_statement::set_string(uint32 index, const std::string &value)
  356. {
  357. set_string(index, value.c_str(), value.length());
  358. }
  359. void ylib::mysql::prepare_statement::set_string(uint32 index, const char* data, int size)
  360. {
  361. CHECK_SQL_PPST;
  362. PRINT_DEBUG_SET_NSTRING;
  363. PREPARE_STATEMENT->setString(index,sql::SQLString(data,size));
  364. #if DEBUG_LOG_PPST_SET == 1
  365. std::cout << "set " << index << " = " << std::string_view(data,size) << std::endl;
  366. #endif
  367. }
  368. void ylib::mysql::prepare_statement::set_blob(uint32 index, const char* data, int size)
  369. {
  370. CHECK_SQL_PPST;
  371. PRINT_DEBUG_SET_NSTRING;
  372. auto data_ptr = std::make_shared<std::istringstream>(std::string(data,size));
  373. m_blobs.push(data_ptr);
  374. PREPARE_STATEMENT->setBlob(index, data_ptr.get());
  375. #if DEBUG_LOG_PPST_SET == 1
  376. std::cout << "set " << index << " = " << size<<"(BLOB)" << std::endl;
  377. #endif
  378. }
  379. void ylib::mysql::prepare_statement::clear()
  380. {
  381. if(m_result != nullptr)
  382. {
  383. delete m_result;
  384. }
  385. m_result = nullptr;
  386. }
  387. uint64 ylib::mysql::prepare_statement::update()
  388. {
  389. CHECK_SQL_PPST;
  390. clear();
  391. try
  392. {
  393. return PREPARE_STATEMENT->executeUpdate();
  394. }
  395. catch (const sql::SQLException & e)
  396. {
  397. throw ylib::exception(e.what());
  398. }
  399. }
  400. ylib::mysql::result *ylib::mysql::prepare_statement::query()
  401. {
  402. CHECK_SQL_PPST;
  403. clear();
  404. try
  405. {
  406. m_result = new ylib::mysql::result(PREPARE_STATEMENT->executeQuery());
  407. }
  408. catch (const sql::SQLException & e)
  409. {
  410. throw ylib::exception(e.what());
  411. }
  412. return m_result;
  413. }
  414. ylib::mysql::result::result(void* handle):m_handle(handle)
  415. {
  416. for (uint32 i = 0; i < RESULT_SET->getMetaData()->getColumnCount(); i++)
  417. {
  418. ylib::mysql::field field;
  419. field.name = RESULT_SET->getMetaData()->getColumnLabel(i + 1);
  420. field.type_name = strutils::change_case(RESULT_SET->getMetaData()->getColumnTypeName(i + 1).c_str(), false);
  421. field.index = i;
  422. m_fields.push_back(field);
  423. }
  424. }
  425. ylib::mysql::result::~result()
  426. {
  427. if(RESULT_SET != nullptr)
  428. {
  429. try
  430. {
  431. while (RESULT_SET->next())
  432. RESULT_SET->close();
  433. }
  434. catch(const std::exception& e)
  435. {
  436. std::cout << e.what() << std::endl;
  437. }
  438. delete RESULT_SET;
  439. }
  440. }
  441. std::string ylib::mysql::result::field_name(uint32 index)
  442. {
  443. if (index>m_fields.size() || index < 1)
  444. return "";
  445. return m_fields[index-1].name;
  446. }
  447. std::string ylib::mysql::result::field_type(uint32 index)
  448. {
  449. if (index > m_fields.size() || index < 1)
  450. return "";
  451. return m_fields[index-1].type_name;
  452. }
  453. std::string ylib::mysql::result::field_type(const std::string& name)
  454. {
  455. for (size_t i = 0; i < m_fields.size(); i++)
  456. {
  457. if (m_fields[i].name == name)
  458. return m_fields[i].type_name;
  459. }
  460. throw ylib::exception("not found field: "+name);
  461. }
  462. uint32 ylib::mysql::result::field_count()
  463. {
  464. CHECK_SQL_PPST;
  465. try
  466. {
  467. return RESULT_SET->getMetaData()->getColumnCount();
  468. }
  469. catch (const sql::SQLException & e)
  470. {
  471. throw ylib::exception(e.what());
  472. }
  473. }
  474. size_t ylib::mysql::result::row_count()
  475. {
  476. CHECK_SQL_PPST;
  477. try
  478. {
  479. return RESULT_SET->rowsCount();
  480. }
  481. catch (const sql::SQLException & e)
  482. {
  483. throw ylib::exception(e.what());
  484. }
  485. }
  486. bool ylib::mysql::result::next()
  487. {
  488. CHECK_SQL_PPST;
  489. try
  490. {
  491. return RESULT_SET->next();
  492. }
  493. catch (const sql::SQLException & e)
  494. {
  495. throw ylib::exception(e.what());
  496. }
  497. }
  498. int32 ylib::mysql::result::get_int32(uint32 index)
  499. {
  500. CHECK_SQL_PPST;
  501. try
  502. {
  503. return RESULT_SET->getInt(index);
  504. }
  505. catch (const sql::SQLException & e)
  506. {
  507. throw ylib::exception(e.what());
  508. }
  509. }
  510. int32 ylib::mysql::result::get_int32(const std::string &name)
  511. {
  512. CHECK_SQL_PPST;
  513. try
  514. {
  515. return RESULT_SET->getInt(name.c_str());
  516. }
  517. catch (const sql::SQLException & e)
  518. {
  519. throw ylib::exception(e.what());
  520. }
  521. }
  522. uint32 ylib::mysql::result::get_uint32(uint32 index)
  523. {
  524. CHECK_SQL_PPST;
  525. try
  526. {
  527. return RESULT_SET->getUInt(index);
  528. }
  529. catch (const sql::SQLException & e)
  530. {
  531. throw ylib::exception(e.what());
  532. }
  533. }
  534. uint32 ylib::mysql::result::get_uint32(const std::string &name)
  535. {
  536. CHECK_SQL_PPST;
  537. try
  538. {
  539. return RESULT_SET->getUInt(name.c_str());
  540. }
  541. catch (const sql::SQLException & e)
  542. {
  543. throw ylib::exception(e.what());
  544. }
  545. }
  546. int64 ylib::mysql::result::get_int64(uint32 index)
  547. {
  548. CHECK_SQL_PPST;
  549. try
  550. {
  551. return RESULT_SET->getInt64(index);
  552. }
  553. catch (const sql::SQLException & e)
  554. {
  555. throw ylib::exception(e.what());
  556. }
  557. }
  558. int64 ylib::mysql::result::get_int64(const std::string &name)
  559. {
  560. CHECK_SQL_PPST;
  561. try
  562. {
  563. return RESULT_SET->getInt64(name.c_str());
  564. }
  565. catch (const sql::SQLException & e)
  566. {
  567. throw ylib::exception(e.what());
  568. }
  569. }
  570. uint64 ylib::mysql::result::get_uint64(uint32 index)
  571. {
  572. CHECK_SQL_PPST;
  573. try
  574. {
  575. return RESULT_SET->getUInt64(index);
  576. }
  577. catch (const sql::SQLException & e)
  578. {
  579. throw ylib::exception(e.what());
  580. }
  581. }
  582. uint64 ylib::mysql::result::get_uint64(const std::string &name)
  583. {
  584. CHECK_SQL_PPST;
  585. try
  586. {
  587. return RESULT_SET->getUInt64(name.c_str());
  588. }
  589. catch (const sql::SQLException & e)
  590. {
  591. throw ylib::exception(e.what());
  592. }
  593. }
  594. std::string ylib::mysql::result::get_string(uint32 index)
  595. {
  596. CHECK_SQL_PPST;
  597. try
  598. {
  599. return RESULT_SET->getString(index).c_str();
  600. }
  601. catch (const sql::SQLException & e)
  602. {
  603. throw ylib::exception(e.what());
  604. }
  605. }
  606. std::string ylib::mysql::result::get_string(const std::string &name)
  607. {
  608. CHECK_SQL_PPST;
  609. try
  610. {
  611. return RESULT_SET->getString(name.c_str()).c_str();
  612. }
  613. catch (const sql::SQLException & e)
  614. {
  615. throw ylib::exception(e.what());
  616. }
  617. }
  618. bool ylib::mysql::result::get_boolean(uint32 index)
  619. {
  620. CHECK_SQL_PPST;
  621. try
  622. {
  623. return RESULT_SET->getBoolean(index);
  624. }
  625. catch (const sql::SQLException & e)
  626. {
  627. throw ylib::exception(e.what());
  628. }
  629. }
  630. bool ylib::mysql::result::get_boolean(const std::string &name)
  631. {
  632. CHECK_SQL_PPST;
  633. try
  634. {
  635. return RESULT_SET->getBoolean(name.c_str());
  636. }
  637. catch (const sql::SQLException & e)
  638. {
  639. throw ylib::exception(e.what());
  640. }
  641. }
  642. double ylib::mysql::result::get_double(uint32 index)
  643. {
  644. CHECK_SQL_PPST;
  645. try
  646. {
  647. return RESULT_SET->getDouble(index);
  648. }
  649. catch (const sql::SQLException & e)
  650. {
  651. throw ylib::exception(e.what());
  652. }
  653. }
  654. double ylib::mysql::result::get_double(const std::string &name)
  655. {
  656. CHECK_SQL_PPST;
  657. try
  658. {
  659. return RESULT_SET->getDouble(name.c_str());
  660. }
  661. catch (const sql::SQLException & e)
  662. {
  663. throw ylib::exception(e.what());
  664. }
  665. }
  666. ylib::buffer ylib::mysql::result::get_blob(uint32 index)
  667. {
  668. CHECK_SQL_PPST;
  669. try
  670. {
  671. auto stream = RESULT_SET->getBlob(index);
  672. ylib::buffer result;
  673. char ch;
  674. while (stream->get(ch)) {
  675. result.append<char>(ch);
  676. }
  677. return result;
  678. }
  679. catch (const sql::SQLException& e)
  680. {
  681. throw ylib::exception(e.what());
  682. }
  683. }
  684. ylib::buffer ylib::mysql::result::get_blob(const std::string& name)
  685. {
  686. CHECK_SQL_PPST;
  687. try
  688. {
  689. auto stream = RESULT_SET->getBlob(name);
  690. ylib::buffer result;
  691. char ch;
  692. while (stream->get(ch)) {
  693. result.append<char>(ch);
  694. }
  695. return result;
  696. }
  697. catch (const sql::SQLException& e)
  698. {
  699. throw ylib::exception(e.what());
  700. }
  701. }
  702. #endif