test_db.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "test_db.h"
  2. #include <cppcms/http_response.h>
  3. #include <cppcms/url_dispatcher.h>
  4. #include <cppcms/json.h>
  5. #include <cppcms/cache_interface.h>
  6. #include <cppcms/serialization.h>
  7. #include <ctime>
  8. class world_object : public cppcms::serializable {
  9. public:
  10. int id;
  11. int randomNumber;
  12. void serialize(cppcms::archive &a) {
  13. a & id & randomNumber;
  14. }
  15. };
  16. test_db::test_db(cppcms::service &s) : test_db_base(s) {
  17. srand(std::time(nullptr));
  18. dispatcher().assign("/db", &test_db::servre_db, this);
  19. dispatcher().assign("/queries/(.{0,3}).*", &test_db::servre_queries, this, 1);
  20. dispatcher().assign("/updates/(.{0,3}).*", &test_db::servre_updates, this, 1);
  21. dispatcher().assign("/cached-worlds/(.{0,3}).*", &test_db::servre_cached_worlds, this, 1);
  22. }
  23. void test_db::servre_db() {
  24. int r = get_random_id();
  25. world_object wo;
  26. if (get_world_object_db(wo, r)) {
  27. cppcms::json::value my_object;
  28. my_object["id"] = wo.id;
  29. my_object["randomNumber"] = wo.randomNumber;
  30. send_json(my_object);
  31. }
  32. }
  33. void test_db::servre_queries(const std::string val) {
  34. int count = cinvert_constraint_count(val);
  35. cppcms::json::array my_object;
  36. for (int i = 0; i < count; ++i) {
  37. cppcms::json::value my_val;
  38. int r = get_random_id();
  39. world_object wo;
  40. if (get_world_object_db(wo, r)) {
  41. my_val["id"] = wo.id;
  42. my_val["randomNumber"] = wo.randomNumber;
  43. my_object.push_back(my_val);
  44. } else {
  45. return;
  46. }
  47. }
  48. send_json(my_object);
  49. }
  50. void test_db::servre_updates(const std::string val) {
  51. int count = cinvert_constraint_count(val);
  52. cppcms::json::array my_object;
  53. for (int i = 0; i < count; ++i) {
  54. cppcms::json::value my_val;
  55. int r = get_random_id();
  56. world_object wo;
  57. if (get_world_object_db(wo, r)) {
  58. my_val["id"] = wo.id;
  59. my_val["randomNumber"] = wo.randomNumber;
  60. int rv = get_random_value();
  61. wo.randomNumber = rv;
  62. if (!update_world_object_db(wo)) {
  63. return;
  64. }
  65. my_object.push_back(my_val);
  66. } else {
  67. return;
  68. }
  69. }
  70. send_json(my_object);
  71. }
  72. void test_db::servre_cached_worlds(const std::string val) {
  73. int count = cinvert_constraint_count(val);
  74. cppcms::json::array my_object;
  75. for (int i = 0; i < count; ++i) {
  76. cppcms::json::value my_val;
  77. int r = get_random_id();
  78. world_object wo;
  79. if (!cache().fetch_data(std::to_string(r), wo)) {
  80. if (get_world_object_db_ch(wo, r)) {
  81. cache().store_data(std::to_string(r), wo);
  82. } else {
  83. return;
  84. }
  85. }
  86. my_val["id"] = wo.id;
  87. my_val["randomNumber"] = wo.randomNumber;
  88. my_object.push_back(my_val);
  89. }
  90. send_json(my_object);
  91. }
  92. int test_db::get_random_id() {
  93. return rand() % (MAX_WORLD_ID - 1) + 1;
  94. }
  95. int test_db::get_random_value() {
  96. return rand() % (MAX_RANDOM_VALUE - 1) + 1;
  97. }
  98. int test_db::cinvert_constraint_count(const std::string & val) {
  99. int count = 0;
  100. count = atoi(val.c_str());
  101. if (count < 1)count = 1;
  102. if (count > MAX_QUERY_COUNT)count = MAX_QUERY_COUNT;
  103. return count;
  104. }
  105. //For CachedWorld
  106. bool test_db::get_world_object_db_ch(world_object &c, int id) {
  107. cppdb::result res;
  108. res = sql << "SELECT id, randomNumber FROM world WHERE id = ?" << id << cppdb::row;
  109. if (res.empty()) {
  110. response().make_error_response(response().internal_server_error, "SQL result empty");
  111. return false;
  112. }
  113. res >> c.id >> c.randomNumber;
  114. return true;
  115. }
  116. bool test_db::get_world_object_db(world_object &c, int id) {
  117. cppdb::result res;
  118. res = sql << "SELECT id, randomNumber FROM world WHERE id = ?" << id << cppdb::row;
  119. if (res.empty()) {
  120. response().make_error_response(response().internal_server_error, "SQL result empty");
  121. return false;
  122. }
  123. res >> c.id >> c.randomNumber;
  124. return true;
  125. }
  126. bool test_db::update_world_object_db(const world_object &c) {
  127. cppdb::statement st = sql
  128. << "UPDATE world SET randomNumber = ? WHERE id = ?"
  129. << c.randomNumber << c.id;
  130. st.exec();
  131. return true;
  132. }
  133. void test_db::send_json(const cppcms::json::value & json) {
  134. response().content_type("application/json");
  135. response().date(std::time(nullptr));
  136. response().out() << json;
  137. }