PageRenderTime 86ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/protobuf_test/rethink-db-cpp-driver/example.cpp

https://gitlab.com/asimpletune/rethinkdb
C++ | 265 lines | 239 code | 25 blank | 1 comment | 109 complexity | 3da0ea7a6d78ce88647d8bd8efe3a756 MD5 | raw file
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <boost/algorithm/string.hpp>
  5. #include "rethink_db.hpp"
  6. #define default_host "10.211.55.2"
  7. #define default_port "28015"
  8. #define default_db "test"
  9. #define default_auth_key ""
  10. using namespace std;
  11. using namespace com::rethinkdb;
  12. using namespace com::rethinkdb::driver;
  13. string ask(const string& description) {
  14. std::cout << "> " << description << ": ";
  15. string input = "";
  16. getline(cin, input);
  17. std::cout << endl;
  18. boost::algorithm::trim(input);
  19. return input;
  20. }
  21. int main(int argc, char* argv) {
  22. string host = ask("host (leave empty for '" default_host "')");
  23. if (host.empty()) host = default_host;
  24. string port = ask("port (leave empty for '" default_port "')");
  25. if (port.empty()) port = default_port;
  26. string db = ask("default database (leave empty for '" default_db "')");
  27. if (db.empty()) db = default_db;
  28. string auth_key = ask("authorization key (leave empty for '" default_auth_key "')");
  29. if (auth_key.empty()) auth_key = default_auth_key;
  30. shared_ptr <connection> conn(new connection(host, port, db, auth_key));
  31. vector<shared_ptr<Response>> responses = vector<shared_ptr<Response>>();
  32. string action;
  33. while (!cin.eof()) {
  34. try {
  35. responses = vector<shared_ptr<Response>>();
  36. std::cout << endl;
  37. std::cout << "========================================================================" << endl;
  38. std::cout << " ACCESSING REQL: use" << endl;
  39. std::cout << " MANIPULATING DATABASES: db_create db_drop db_list use" << endl;
  40. std::cout << " MANIPULATING TABLES: table_create table_drop table_list" << endl;
  41. std::cout << " WRITING DATA: insert remove update" << endl;
  42. std::cout << " SELECTING DATA: between filter get table" << endl;
  43. std::cout << " TRANSFORMATIONS: limit order_by skip" << endl;
  44. std::cout << " exit" << endl;
  45. std::cout << "========================================================================" << endl << endl;
  46. std::cout << endl;
  47. action = ask("action");
  48. if (action.length() == 0) {
  49. std::cout << "Invalid action." << endl;
  50. }
  51. else if (action == "exit") {
  52. std::cout << "Exiting..." << endl << endl;
  53. break;
  54. }
  55. else if (action == "use") {
  56. conn->use(ask("db_name"));
  57. std::cout << endl << "Default database has been changed." << endl;
  58. }
  59. else if (action == "db_create") {
  60. responses = conn->r()->db_create(ask("db_name"))->run();
  61. }
  62. else if (action == "db_drop") {
  63. responses = conn->r()->db_drop(ask("db_name"))->run();
  64. }
  65. else if (action == "db_list") {
  66. responses = conn->r()->db_list()->run();
  67. }
  68. else if (action == "table") {
  69. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  70. if (db_name == "") {
  71. responses = conn->r()->table(ask("table_name"))->run();
  72. }
  73. else {
  74. responses = conn->r()->db(db_name)->table(ask("table_name"))->run();
  75. }
  76. }
  77. else if (action == "table_create") {
  78. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  79. if (db_name == "") {
  80. responses = conn->r()->table_create(ask("table_name"))->run();
  81. }
  82. else {
  83. responses = conn->r()->db(db_name)->table_create(ask("table_name"))->run();
  84. }
  85. }
  86. else if (action == "table_drop") {
  87. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  88. if (db_name == "") {
  89. responses = conn->r()->table_drop(ask("table_name"))->run();
  90. }
  91. else {
  92. responses = conn->r()->db(db_name)->table_drop(ask("table_name"))->run();
  93. }
  94. }
  95. else if (action == "table_list") {
  96. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  97. if (db_name == "") {
  98. responses = conn->r()->table_list()->run();
  99. }
  100. else {
  101. responses = conn->r()->db(db_name)->table_list()->run();
  102. }
  103. }
  104. else if (action == "insert") {
  105. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  106. shared_ptr<RQL_Table> table;
  107. if (db_name == "") {
  108. table = conn->r()->table(ask("table_name"));
  109. }
  110. else {
  111. table = conn->r()->db(db_name)->table(ask("table_name"));
  112. }
  113. int count = atoi(ask("number of objects you want to insert").c_str());
  114. if (count == 1) {
  115. // insert one object
  116. responses = table->insert(RQL_Object(ask("key (string)"), RQL_String(ask("value (string)"))))->run();
  117. }
  118. else if (count > 1) {
  119. vector<shared_ptr<RQL_Datum>> objects = vector<shared_ptr<RQL_Datum>>();
  120. for (int i = 1; i <= count; ++i) {
  121. objects.push_back(make_shared<RQL_Datum>(RQL_Object(ask("key " + to_string(i) + " (string)"), RQL_String(ask("value " + to_string(i) + " (string)")))));
  122. }
  123. responses = table->insert(RQL_Array(objects))->run();
  124. }
  125. }
  126. else if (action == "between") {
  127. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  128. shared_ptr<RQL_Table> table;
  129. if (db_name == "") {
  130. table = conn->r()->table(ask("table_name"));
  131. }
  132. else {
  133. table = conn->r()->db(db_name)->table(ask("table_name"));
  134. }
  135. responses = table->between(RQL_String(ask("id from (string)")), RQL_String(ask("id until (string)")))->run();
  136. }
  137. else if (action == "filter") {
  138. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  139. shared_ptr<RQL_Table> table;
  140. if (db_name == "") {
  141. table = conn->r()->table(ask("table_name"));
  142. }
  143. else {
  144. table = conn->r()->db(db_name)->table(ask("table_name"));
  145. }
  146. responses = table->filter(RQL_Object(ask("key (string)"), RQL_String(ask("value (string)"))))->run();
  147. }
  148. else if (action == "get") {
  149. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  150. shared_ptr<RQL_Table> table;
  151. if (db_name == "") {
  152. table = conn->r()->table(ask("table_name"));
  153. }
  154. else {
  155. table = conn->r()->db(db_name)->table(ask("table_name"));
  156. }
  157. responses = table->get(ask("id (string)"))->run();
  158. }
  159. else if (action == "remove") {
  160. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  161. shared_ptr<RQL_Table> table;
  162. if (db_name == "") {
  163. table = conn->r()->table(ask("table_name"));
  164. }
  165. else {
  166. table = conn->r()->db(db_name)->table(ask("table_name"));
  167. }
  168. string id = ask("id (leave empty for entire table)");
  169. if (id == "") {
  170. responses = table->remove()->run();
  171. }
  172. else {
  173. responses = table->get(id)->remove()->run();
  174. }
  175. }
  176. else if (action == "update") {
  177. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  178. shared_ptr<RQL_Table> table;
  179. if (db_name == "") {
  180. table = conn->r()->table(ask("table_name"));
  181. }
  182. else {
  183. table = conn->r()->db(db_name)->table(ask("table_name"));
  184. }
  185. responses = table->get(ask("id (string)"))->update(RQL_Object(ask("key (string)"), RQL_String(ask("value (string)"))))->run();
  186. }
  187. else if (action == "limit") {
  188. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  189. shared_ptr<RQL_Table> table;
  190. if (db_name == "") {
  191. table = conn->r()->table(ask("table_name"));
  192. }
  193. else {
  194. table = conn->r()->db(db_name)->table(ask("table_name"));
  195. }
  196. vector<shared_ptr<RQL_Ordering>> orderings = vector<shared_ptr<RQL_Ordering>>();
  197. orderings.push_back(RQL_Ordering::asc(ask("order_by key (string)")));
  198. responses = table->order_by(orderings)->limit(atoi(ask("limit").c_str()))->run();
  199. }
  200. else if (action == "order_by") {
  201. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  202. shared_ptr<RQL_Table> table;
  203. if (db_name == "") {
  204. table = conn->r()->table(ask("table_name"));
  205. }
  206. else {
  207. table = conn->r()->db(db_name)->table(ask("table_name"));
  208. }
  209. vector<shared_ptr<RQL_Ordering>> orderings = vector<shared_ptr<RQL_Ordering>>();
  210. orderings.push_back(RQL_Ordering::asc(ask("order_by key (string)")));
  211. responses = table->order_by(orderings)->run();
  212. }
  213. else if (action == "skip") {
  214. string db_name = ask("db_name (leave empty for '" + conn->database + "')");
  215. shared_ptr<RQL_Table> table;
  216. if (db_name == "") {
  217. table = conn->r()->table(ask("table_name"));
  218. }
  219. else {
  220. table = conn->r()->db(db_name)->table(ask("table_name"));
  221. }
  222. vector<shared_ptr<RQL_Ordering>> orderings = vector<shared_ptr<RQL_Ordering>>();
  223. orderings.push_back(RQL_Ordering::asc(ask("order_by key (string)")));
  224. responses = table->order_by(orderings)->skip(atoi(ask("skip").c_str()))->run();
  225. }
  226. else {
  227. std::cout << "Invalid action." << endl;
  228. }
  229. for_each(responses.begin(), responses.end(), [] (shared_ptr<Response> response) {
  230. response->PrintDebugString();
  231. });
  232. }
  233. catch (runtime_error& e) {
  234. cerr << e.what() << endl;
  235. }
  236. action = "";
  237. }
  238. conn->close();
  239. return 0;
  240. }