mysql.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #include "mysql.h"
  2. #include "util/time.h"
  3. module::select::select(ylib::mysql::conn *conn)
  4. {
  5. m_select = std::make_shared<ylib::select>(conn);
  6. }
  7. module::select::~select()
  8. {
  9. }
  10. module::select& module::select::where_i32(const std::string& name, const std::string& expression, int32 value)
  11. {
  12. m_select->where(name,expression,value);
  13. return *this;
  14. }
  15. module::select& module::select::where_i64(const std::string& name, const std::string& expression, int64 value)
  16. {
  17. m_select->where(name, expression, value);
  18. return *this;
  19. }
  20. module::select& module::select::where_dob(const std::string& name, const std::string& expression, double value)
  21. {
  22. m_select->where(name, expression, value);
  23. return *this;
  24. }
  25. module::select& module::select::where_str(const std::string& name, const std::string& expression, const std::string& value)
  26. {
  27. m_select->where(name, expression, value);
  28. return *this;
  29. }
  30. module::select& module::select::where_expression(const std::string& expression)
  31. {
  32. m_select->where(expression);
  33. return *this;
  34. }
  35. module::select& module::select::where_like(const std::string& name, const std::string& value)
  36. {
  37. m_select->where_like(name, value);
  38. return *this;
  39. }
  40. module::select& module::select::table(const std::string& table_name)
  41. {
  42. m_select->table(table_name);
  43. return *this;
  44. }
  45. module::select& module::select::field(sol::table table)
  46. {
  47. std::vector<std::string> strings;
  48. for (auto& pair : table) {
  49. if (pair.second.get_type() != sol::type::string)
  50. {
  51. throw ylib::exception("module::select::field: attempting to obtain field name that is not a string");
  52. }
  53. strings.push_back(pair.second.as<std::string>());
  54. }
  55. m_select->field(strings);
  56. return *this;
  57. }
  58. module::select& module::select::page(uint32 page, uint32 count)
  59. {
  60. m_select->page(page,count);
  61. return *this;
  62. }
  63. module::select& module::select::limit(uint32 start, uint32 count)
  64. {
  65. m_select->limit(start,count);
  66. return *this;
  67. }
  68. module::select& module::select::orderby(const std::string& field, int sort)
  69. {
  70. m_select->orderby(field,(ylib::sort)sort);
  71. return *this;
  72. }
  73. void module::select::clear()
  74. {
  75. m_select->clear();
  76. }
  77. std::shared_ptr<module::mysql_result> module::select::query()
  78. {
  79. return std::make_shared<module::mysql_result>(m_select->query());
  80. }
  81. uint64 module::select::count()
  82. {
  83. return m_select->count();
  84. }
  85. void module::select::regist(sol::state& lua)
  86. {
  87. lua.new_usertype<module::select>("mysql_builder_select",
  88. "new", sol::constructors<module::select(ylib::mysql::conn*)>(),
  89. "count", &module::select::count,
  90. "field", &module::select::field,
  91. "limit", &module::select::limit,
  92. "orderby", &module::select::orderby,
  93. "page", &module::select::page,
  94. "table", &module::select::table,
  95. "where_dob", &module::select::where_dob,
  96. "where_expression", &module::select::where_expression,
  97. "where_i32", &module::select::where_i32,
  98. "where_i64", &module::select::where_i64,
  99. "where_str", &module::select::where_str,
  100. "query", &module::select::query,
  101. "clear", &module::select::clear,
  102. "where_like", &module::select::where_like,
  103. "clear", &module::select::clear
  104. );
  105. }
  106. module::update::update(ylib::mysql::conn* conn)
  107. {
  108. m_update = std::make_shared<ylib::update>(conn);
  109. }
  110. module::update::~update()
  111. {
  112. }
  113. module::update& module::update::table(const std::string& table_name)
  114. {
  115. m_update->table(table_name);
  116. return *this;
  117. }
  118. module::update& module::update::set_i32(const std::string& name, int32 value)
  119. {
  120. m_update->set(name, value);
  121. return *this;
  122. }
  123. module::update& module::update::set_i64(const std::string& name, int64 value)
  124. {
  125. m_update->set(name, value);
  126. return *this;
  127. }
  128. module::update& module::update::set_dob(const std::string& name, double value)
  129. {
  130. m_update->set(name, value);
  131. return *this;
  132. }
  133. module::update& module::update::set_str(const std::string& name, const std::string& value)
  134. {
  135. m_update->set(name, value);
  136. return *this;
  137. }
  138. module::update& module::update::set(const std::string& expression)
  139. {
  140. m_update->set(expression);
  141. return *this;
  142. }
  143. module::update& module::update::where_i32(const std::string& name, const std::string& expression, int32 value)
  144. {
  145. m_update->where(name,expression,value);
  146. return *this;
  147. }
  148. module::update& module::update::where_i64(const std::string& name, const std::string& expression, int64 value)
  149. {
  150. m_update->where(name, expression, value);
  151. return *this;
  152. }
  153. module::update& module::update::where_dob(const std::string& name, const std::string& expression, double value)
  154. {
  155. m_update->where(name, expression, value);
  156. return *this;
  157. }
  158. module::update& module::update::where_str(const std::string& name, const std::string& expression, const std::string& value)
  159. {
  160. m_update->where(name, expression, value);
  161. return *this;
  162. }
  163. module::update& module::update::where_expression(const std::string& expression)
  164. {
  165. m_update->where(expression);
  166. return *this;
  167. }
  168. module::update& module::update::page(uint32 page, uint32 count)
  169. {
  170. m_update->page(page,count);
  171. return *this;
  172. }
  173. module::update& module::update::limit(uint32 start, uint32 count)
  174. {
  175. m_update->limit(start, count);
  176. return *this;
  177. }
  178. module::update& module::update::orderby(const std::string& field, int sort)
  179. {
  180. m_update->orderby(field,(ylib::sort)sort);
  181. return *this;
  182. }
  183. uint64 module::update::exec()
  184. {
  185. return m_update->exec();
  186. }
  187. void module::update::clear()
  188. {
  189. m_update->clear();
  190. }
  191. void module::update::regist(sol::state& lua)
  192. {
  193. lua.new_usertype<module::update>("mysql_builder_update",
  194. "new", sol::constructors<module::update(ylib::mysql::conn*)>(),
  195. "exec", &module::update::exec,
  196. "limit", &module::update::limit,
  197. "orderby", &module::update::orderby,
  198. "page", &module::update::page,
  199. "table", &module::update::table,
  200. "where_dob", &module::update::where_dob,
  201. "where_expression", &module::update::where_expression,
  202. "where_i32", &module::update::where_i32,
  203. "where_i64", &module::update::where_i64,
  204. "where_str", &module::update::where_str,
  205. "set", &module::update::set,
  206. "set_dob", &module::update::set_dob,
  207. "set_i32", &module::update::set_i32,
  208. "set_i64", &module::update::set_i64,
  209. "set_str", &module::update::set_str,
  210. "clear", &module::update::clear
  211. );
  212. }
  213. module::insert::insert(ylib::mysql::conn* conn)
  214. {
  215. m_insert = std::make_shared<ylib::insert>(conn);
  216. }
  217. module::insert::~insert()
  218. {
  219. }
  220. module::insert& module::insert::table(const std::string& table_name)
  221. {
  222. m_insert->table(table_name);
  223. return *this;
  224. }
  225. module::insert& module::insert::set_i32(const std::string& name, int32 value)
  226. {
  227. m_insert->set(name, value);
  228. return *this;
  229. }
  230. module::insert& module::insert::set_i64(const std::string& name, int64 value)
  231. {
  232. m_insert->set(name, value);
  233. return *this;
  234. }
  235. module::insert& module::insert::set_dob(const std::string& name, double value)
  236. {
  237. m_insert->set(name, value);
  238. return *this;
  239. }
  240. module::insert& module::insert::set_str(const std::string& name, const std::string& value)
  241. {
  242. m_insert->set(name, value);
  243. return *this;
  244. }
  245. module::insert& module::insert::set_not_ppst(const std::string& name, const std::string& value)
  246. {
  247. m_insert->set_not_pret(name, value);
  248. return *this;
  249. }
  250. uint64 module::insert::exec()
  251. {
  252. return m_insert->exec();
  253. }
  254. void module::insert::clear()
  255. {
  256. m_insert->clear();
  257. }
  258. void module::insert::regist(sol::state& lua)
  259. {
  260. lua.new_usertype<module::insert>("mysql_builder_insert",
  261. "new", sol::constructors<module::insert(ylib::mysql::conn*)>(),
  262. "exec", &module::insert::exec,
  263. "table", &module::insert::table,
  264. "set_not_ppst", &module::insert::set_not_ppst,
  265. "set_dob", &module::insert::set_dob,
  266. "set_i32", &module::insert::set_i32,
  267. "set_i64", &module::insert::set_i64,
  268. "set_str", &module::insert::set_str,
  269. "clear", &module::insert::clear
  270. );
  271. }
  272. module::delete_::delete_(ylib::mysql::conn* conn)
  273. {
  274. m_delete = std::make_shared<ylib::delete_>(conn);
  275. }
  276. module::delete_::~delete_()
  277. {
  278. }
  279. module::delete_& module::delete_::table(const std::string& table_name)
  280. {
  281. m_delete->table(table_name);
  282. return *this;
  283. }
  284. module::delete_& module::delete_::where_i32(const std::string& name, const std::string& expression, int32 value)
  285. {
  286. m_delete->where(name, expression, value);
  287. return *this;
  288. }
  289. module::delete_& module::delete_::where_i64(const std::string& name, const std::string& expression, int64 value)
  290. {
  291. m_delete->where(name, expression, value);
  292. return *this;
  293. }
  294. module::delete_& module::delete_::where_dob(const std::string& name, const std::string& expression, double value)
  295. {
  296. m_delete->where(name, expression, value);
  297. return *this;
  298. }
  299. module::delete_& module::delete_::where_str(const std::string& name, const std::string& expression, const std::string& value)
  300. {
  301. m_delete->where(name, expression, value);
  302. return *this;
  303. }
  304. module::delete_& module::delete_::where_expression(const std::string& expression)
  305. {
  306. m_delete->where(expression);
  307. return *this;
  308. }
  309. module::delete_& module::delete_::page(uint32 page, uint32 count)
  310. {
  311. m_delete->page(page, count);
  312. return *this;
  313. }
  314. module::delete_& module::delete_::limit(uint32 start, uint32 count)
  315. {
  316. m_delete->limit(start, count);
  317. return *this;
  318. }
  319. module::delete_& module::delete_::orderby(const std::string& field, int sort)
  320. {
  321. m_delete->orderby(field, (ylib::sort)sort);
  322. return *this;
  323. }
  324. uint64 module::delete_::exec()
  325. {
  326. return m_delete->exec();
  327. }
  328. void module::delete_::clear()
  329. {
  330. m_delete->clear();
  331. }
  332. void module::delete_::regist(sol::state& lua)
  333. {
  334. lua.new_usertype<module::delete_>("mysql_builder_delete",
  335. "new", sol::constructors<module::delete_(ylib::mysql::conn*)>(),
  336. "exec", &module::delete_::exec,
  337. "limit", &module::delete_::limit,
  338. "orderby", &module::delete_::orderby,
  339. "page", &module::delete_::page,
  340. "table", &module::delete_::table,
  341. "where_dob", &module::delete_::where_dob,
  342. "where_expression", &module::delete_::where_expression,
  343. "where_i32", &module::delete_::where_i32,
  344. "where_i64", &module::delete_::where_i64,
  345. "where_str", &module::delete_::where_str,
  346. "clear", &module::delete_::clear
  347. );
  348. }
  349. void module::mysql_regist(sol::state& lua)
  350. {
  351. lua["DESC"] = ylib::sort::DESC;
  352. lua["ASC"] = ylib::sort::ASC;
  353. lua.new_usertype<ylib::mysql::conn>("mysql_conn",
  354. "clear", &ylib::mysql::conn::clear,
  355. "close", &ylib::mysql::conn::close,
  356. "commit", &ylib::mysql::conn::commit,
  357. "insert_id", &ylib::mysql::conn::insert_id,
  358. "last_error", &ylib::mysql::conn::last_error,
  359. "recover", &ylib::mysql::conn::recover,
  360. "rollback", &ylib::mysql::conn::rollback,
  361. "setsql", &ylib::mysql::conn::setsql
  362. );
  363. lua.new_usertype<module::mysql>("mysql_pool",
  364. "new", sol::constructors<module::mysql()>(),
  365. "start", &module::mysql::start,
  366. "close", &module::mysql::close,
  367. "self", &module::mysql::self,
  368. "select", &module::mysql::select,
  369. "update", &module::mysql::update,
  370. "insert", &module::mysql::insert,
  371. "delete", &module::mysql::delete_
  372. );
  373. module::mysql_result::regist(lua);
  374. module::select::regist(lua);
  375. module::update::regist(lua);
  376. module::delete_::regist(lua);
  377. module::insert::regist(lua);
  378. }
  379. module::mysql::mysql()
  380. {
  381. }
  382. module::mysql::~mysql()
  383. {
  384. close();
  385. }
  386. bool module::mysql::start(const std::string& ipaddress, const std::string& username, const std::string& password, const std::string& database, const std::string& charset, ushort port, int32 size)
  387. {
  388. close();
  389. m_pool = std::make_shared<ylib::mysql::pool>();
  390. ylib::mysql::mysql_conn_info info;
  391. info.ipaddress = ipaddress;
  392. info.username = username;
  393. info.password = password;
  394. info.charset = charset;
  395. info.port = port;
  396. info.database = database;
  397. return m_pool->start(info, size);
  398. }
  399. void module::mysql::close()
  400. {
  401. }
  402. std::shared_ptr<module::select> module::mysql::select()
  403. {
  404. return std::make_shared<module::select>(m_pool->get());
  405. }
  406. std::shared_ptr<module::insert> module::mysql::insert()
  407. {
  408. return std::make_shared<module::insert>(m_pool->get());
  409. }
  410. std::shared_ptr<module::update> module::mysql::update()
  411. {
  412. return std::make_shared<module::update>(m_pool->get());
  413. }
  414. std::shared_ptr<module::delete_> module::mysql::delete_()
  415. {
  416. return std::make_shared<module::delete_>(m_pool->get());
  417. }
  418. void module::mysql::regist_global(const std::string& name, sol::state* lua)
  419. {
  420. lua->registry()[name] = this;
  421. (*lua)[name] = this;
  422. }
  423. module::mysql_result::mysql_result(ylib::mysql::result* result):m_result(result)
  424. {
  425. }
  426. module::mysql_result::~mysql_result()
  427. {
  428. }
  429. uint32 module::mysql_result::field_count()
  430. {
  431. return m_result->field_count();
  432. }
  433. std::string module::mysql_result::field_type(sol::object obj)
  434. {
  435. if (obj.is<int>() || obj.is<double>()) {
  436. m_result->field_type((uint32)obj.as<int>());
  437. }
  438. else if (obj.is<std::string>()) {
  439. m_result->field_type((std::string)obj.as<std::string>());
  440. }
  441. return "";
  442. }
  443. std::string module::mysql_result::field_name(uint32 index)
  444. {
  445. return m_result->field_name(index);
  446. }
  447. size_t module::mysql_result::row_count()
  448. {
  449. return m_result->row_count();
  450. }
  451. bool module::mysql_result::next()
  452. {
  453. return m_result->next();
  454. }
  455. VarType module::mysql_result::get(sol::object obj, sol::this_state s)
  456. {
  457. std::string type;
  458. if (obj.get_type() == sol::type::string)
  459. type = m_result->field_type(obj.as<std::string>());
  460. else if (obj.get_type() == sol::type::number)
  461. type = m_result->field_type(obj.as<int>());
  462. #define GET_VALUE(FUNCTION) \
  463. if (obj.get_type() == sol::type::string) \
  464. return sol::make_object(s, m_result->FUNCTION(obj.as<std::string>())); \
  465. else if (obj.get_type() == sol::type::number) \
  466. return sol::make_object(s, m_result->FUNCTION(obj.as<uint32>()))
  467. if (type == "int" || type == "tinyint")
  468. {
  469. GET_VALUE(get_int32);
  470. }
  471. else if (type == "varchar" || type == "char" || type == "text" || type == "datetime")
  472. {
  473. GET_VALUE(get_string);
  474. }
  475. else if (type == "int unsigned")
  476. {
  477. GET_VALUE(get_uint32);
  478. }
  479. else if (type == "bigint")
  480. {
  481. GET_VALUE(get_int64);
  482. }
  483. else if (type == "decimal")
  484. {
  485. GET_VALUE(get_double);
  486. }
  487. return sol::make_object(s, sol::nil);
  488. }
  489. sol::table module::mysql_result::table(sol::this_state s)
  490. {
  491. sol::state_view lua(s);
  492. sol::table result_table = lua.create_table();
  493. int row_num = 1;
  494. while (next()) {
  495. sol::table row = lua.create_table();
  496. for (uint32_t i = 0; i < field_count(); ++i) {
  497. row[field_name(i+1)] = get(sol::make_object(s, i+1), s);
  498. }
  499. result_table[row_num++] = row;
  500. }
  501. return result_table;
  502. }
  503. void module::mysql_result::regist(sol::state& lua)
  504. {
  505. lua.new_usertype<module::mysql_result>("mysql_result",
  506. "field_name", &module::mysql_result::field_name,
  507. "get", &module::mysql_result::get,
  508. "next", &module::mysql_result::next,
  509. "row_count", &module::mysql_result::row_count,
  510. "table", &module::mysql_result::table,
  511. "field_count", &module::mysql_result::field_count,
  512. "field_type", &module::mysql_result::field_type
  513. );
  514. }