C++DB is the database layer that was designed to work with C++CMS. This customized version is used within Ye Ol' Pi Shack.
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.
 
 
 
 
 

126 lines
2.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // Distributed under:
  6. //
  7. // the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // or (at your opinion) under:
  12. //
  13. // The MIT License
  14. // (See accompanying file MIT.txt or a copy at
  15. // http://www.opensource.org/licenses/mit-license.php)
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include <cppdb/frontend.h>
  19. #include <iostream>
  20. #include <stdlib.h>
  21. #if defined WIN32 || defined _WIN32 || defined __WIN32 || defined(__CYGWIN__)
  22. #ifndef NOMINMAX
  23. #define NOMINMAX
  24. #endif
  25. #include <windows.h>
  26. class timer {
  27. public:
  28. timer()
  29. {
  30. QueryPerformanceFrequency(&freq_);
  31. }
  32. void start()
  33. {
  34. QueryPerformanceCounter(&start_);
  35. }
  36. void stop()
  37. {
  38. QueryPerformanceCounter(&stop_);
  39. }
  40. double diff() const
  41. {
  42. return double(stop_.QuadPart - start_.QuadPart) / freq_.QuadPart;
  43. }
  44. private:
  45. LARGE_INTEGER freq_;
  46. LARGE_INTEGER start_;
  47. LARGE_INTEGER stop_;
  48. };
  49. #else
  50. #include <sys/time.h>
  51. class timer {
  52. public:
  53. timer() {}
  54. void start()
  55. {
  56. gettimeofday(&start_,0);
  57. }
  58. void stop()
  59. {
  60. gettimeofday(&stop_,0);
  61. }
  62. double diff() const
  63. {
  64. double udiff = (stop_.tv_sec - start_.tv_sec) * 1000000LL + stop_.tv_usec - start_.tv_usec;
  65. return udiff * 1e-6;
  66. }
  67. private:
  68. struct timeval start_;
  69. struct timeval stop_;
  70. };
  71. #endif
  72. int main(int argc,char **argv)
  73. {
  74. if(argc!=2) {
  75. std::cerr << "conn string required" << std::endl;
  76. return 1;
  77. }
  78. try {
  79. static const int max_val = 10000;
  80. cppdb::session sql(argv[1]);
  81. try { sql << "DROP TABLE test" << cppdb::exec; } catch(...) {}
  82. if(sql.engine() == "mysql")
  83. sql << "create table test ( id integer primary key, val varchar(100)) Engine=innodb" << cppdb::exec;
  84. else
  85. sql << "create table test ( id integer primary key, val varchar(100))" << cppdb::exec;
  86. {
  87. cppdb::transaction tr(sql);
  88. for(int i=0;i<max_val;i++) {
  89. sql << "insert into test values(?,?)" << i << "Hello World" << cppdb::exec;
  90. }
  91. tr.commit();
  92. }
  93. timer tm;
  94. tm.start();
  95. for(int j=0;j<max_val * 10;j++) {
  96. std::string v;
  97. sql << "select val from test where id = ?" << (rand() % max_val)<< cppdb::row >> v;
  98. if(v!="Hello World")
  99. throw std::runtime_error("Wrong");
  100. }
  101. tm.stop();
  102. std::cout << "Passed " << tm.diff() << " seconds" << std::endl;
  103. }
  104. catch(std::exception const &e) {
  105. std::cerr << e.what() << std::endl;
  106. return 1;
  107. }
  108. std::cout << "Ok" << std::endl;
  109. return 0;
  110. }