You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.6KB

  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <iostream>
  11. #include "Util/logger.h"
  12. #if defined(ENABLE_MYSQL)
  13. #include "Util/SqlPool.h"
  14. #endif
  15. using namespace std;
  16. using namespace toolkit;
  17. int main() {
  18. //初始化日志
  19. Logger::Instance().add(std::make_shared<ConsoleChannel> ());
  20. #if defined(ENABLE_MYSQL)
  21. /*
  22. * 测试方法:
  23. * 请按照实际数据库情况修改源码然后编译执行测试
  24. */
  25. string sql_ip = "127.0.0.1";
  26. unsigned short sql_port = 3306;
  27. string user = "root";
  28. string password = "111111";
  29. string character = "utf8mb4";
  30. #if defined(SUPPORT_DYNAMIC_TEMPLATE)
  31. //初始化数据
  32. SqlPool::Instance().Init(sql_ip,sql_port,"",user,password/*,character*/);
  33. #else
  34. //由于需要编译器对可变参数模板的支持,所以gcc5.0以下一般都不支持,否则编译报错
  35. ErrorL << "your compiler does not support variable parameter templates!" << endl;
  36. return -1;
  37. #endif //defined(SUPPORT_DYNAMIC_TEMPLATE)
  38. //建议数据库连接池大小设置跟CPU个数一致(大一点为佳)
  39. SqlPool::Instance().setSize(3 + thread::hardware_concurrency());
  40. vector<vector<string> > sqlRet;
  41. SqlWriter("create database test_db;", false) << sqlRet;
  42. SqlWriter("create table test_db.test_table(user_name varchar(128),user_id int auto_increment primary key,user_pwd varchar(128));", false) << sqlRet;
  43. //同步插入
  44. SqlWriter insertSql("insert into test_db.test_table(user_name,user_pwd) values('?','?');");
  45. insertSql<< "zltoolkit" << "123456" << sqlRet;
  46. //我们可以知道插入了几条数据,并且可以获取新插入(第一条)数据的rowID
  47. DebugL << "AffectedRows:" << insertSql.getAffectedRows() << ",RowID:" << insertSql.getRowID();
  48. //同步查询
  49. SqlWriter sqlSelect("select user_id , user_pwd from test_db.test_table where user_name='?' limit 1;") ;
  50. sqlSelect << "zltoolkit" ;
  51. vector<vector<string> > sqlRet0;
  52. vector<list<string> > sqlRet1;
  53. vector<deque<string> > sqlRet2;
  54. vector<map<string,string> > sqlRet3;
  55. vector<unordered_map<string,string> > sqlRet4;
  56. sqlSelect << sqlRet0;
  57. sqlSelect << sqlRet1;
  58. sqlSelect << sqlRet2;
  59. sqlSelect << sqlRet3;
  60. sqlSelect << sqlRet4;
  61. for(auto &line : sqlRet0){
  62. DebugL << "vector<string> user_id:" << line[0] << ",user_pwd:"<< line[1];
  63. }
  64. for(auto &line : sqlRet1){
  65. DebugL << "list<string> user_id:" << line.front() << ",user_pwd:"<< line.back();
  66. }
  67. for(auto &line : sqlRet2){
  68. DebugL << "deque<string> user_id:" << line[0] << ",user_pwd:"<< line[1];
  69. }
  70. for(auto &line : sqlRet3){
  71. DebugL << "map<string,string> user_id:" << line["user_id"] << ",user_pwd:"<< line["user_pwd"];
  72. }
  73. for(auto &line : sqlRet4){
  74. DebugL << "unordered_map<string,string> user_id:" << line["user_id"] << ",user_pwd:"<< line["user_pwd"];
  75. }
  76. //异步删除
  77. SqlWriter insertDel("delete from test_db.test_table where user_name='?';");
  78. insertDel << "zltoolkit" << endl;
  79. //注意!
  80. //如果SqlWriter 的 "<<" 操作符后面紧跟SqlPool::SqlRetType类型,则说明是同步操作并等待结果
  81. //如果紧跟std::endl ,则是异步操作,在后台线程完成sql操作。
  82. #else
  83. ErrorL << "ENABLE_MYSQL not defined!" << endl;
  84. return -1;
  85. #endif//ENABLE_MYSQL
  86. return 0;
  87. }