ModuleSQL.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "ModuleSQL.h"
  2. #include <boost/algorithm/string.hpp>
  3. namespace mdd {
  4. void ModuleSQL::erase_keyword(std::string& str, std::string key) {
  5. while (true) {
  6. int start = (int)str.find(key);
  7. if (start != -1) {
  8. str.erase(start, key.length());
  9. }
  10. else {
  11. return;
  12. }
  13. }
  14. }
  15. ModuleSQL::ModuleSQL(std::string dbname) {
  16. int rc = sqlite3_open(dbname.c_str(), &_db);
  17. if (rc) {
  18. fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(_db));
  19. return;
  20. }
  21. else {
  22. fprintf(stderr, "Opened database successfully\n");
  23. }
  24. std::string sql;
  25. sqlite3_stmt* res;
  26. int step;
  27. // Create SQL statement
  28. sql = "SELECT tbl_name "\
  29. "FROM sqlite_master "\
  30. "WHERE type = 'table' ";
  31. // Execute SQL statement
  32. rc = sqlite3_prepare_v2(_db, sql.c_str(), -1, &res, 0);
  33. if (rc != SQLITE_OK) {
  34. fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(_db));
  35. sqlite3_close(_db);
  36. }
  37. else {
  38. fprintf(stdout, "Operation done successfully\n");
  39. }
  40. step = sqlite3_step(res);
  41. if (step == SQLITE_ROW) {
  42. int nCol = sqlite3_column_count(res);
  43. for (size_t i = 0; i < nCol; i++)
  44. {
  45. _tbname = (char*)sqlite3_column_text(res, i);
  46. }
  47. }
  48. sqlite3_finalize(res);
  49. // Create SQL statement
  50. sql = "SELECT sql "\
  51. "FROM sqlite_master "\
  52. "WHERE type = 'table' AND tbl_name = '" + _tbname + "'";
  53. //std::cout << sql << std::endl;
  54. // Execute SQL statement
  55. rc = sqlite3_prepare_v2(_db, sql.c_str(), -1, &res, 0);
  56. if (rc != SQLITE_OK) {
  57. fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(_db));
  58. sqlite3_close(_db);
  59. }
  60. else {
  61. fprintf(stdout, "Operation done successfully\n");
  62. }
  63. step = sqlite3_step(res);
  64. if (step == SQLITE_ROW) {
  65. std::string sql_res = (char*)sqlite3_column_text(res, 0);
  66. int start = sql_res.find("(");
  67. int end = sql_res.find(")");
  68. sql_res = sql_res.substr(++start, (size_t)(--end) - start);
  69. erase_keyword(sql_res, "KEY");
  70. erase_keyword(sql_res, "PRIMARY");
  71. erase_keyword(sql_res, "AUTOINCREMENT");
  72. erase_keyword(sql_res, "NOT NULL");
  73. std::string::iterator new_end = std::unique(sql_res.begin(), sql_res.end(), [](char lhs, char rhs)->bool { return (lhs == rhs) && (lhs == ' '); });
  74. sql_res.erase(new_end, sql_res.end());
  75. _content.clear();
  76. while(true){
  77. end = sql_res.find(" ,");
  78. if (end == -1)
  79. {
  80. end = sql_res.length();
  81. }
  82. std::string sub_str = sql_res.substr(0, end);
  83. int mid = sub_str.find(" ");
  84. _entity enti;
  85. enti.key = sub_str.substr(0, mid);
  86. enti.type = sub_str.substr(mid + 1, sub_str.length() - (mid + 1));
  87. _content.push_back(enti);
  88. //std::cout << "'" << enti.key << "': '" << enti.type << "'\n";
  89. if (end != sql_res.length())
  90. {
  91. sql_res.erase(0, end + 2);
  92. }
  93. else {
  94. break;
  95. }
  96. }
  97. }
  98. sqlite3_finalize(res);
  99. addInput(_content[0].key, {0});
  100. for (auto& cont :_content)
  101. {
  102. addOutput(cont.key, {1});
  103. }
  104. setType("SQL");
  105. }
  106. state ModuleSQL::update() {
  107. int rc;
  108. std::string sql;
  109. sqlite3_stmt* res;
  110. int step;
  111. // Create SQL statement
  112. sql = "SELECT * "\
  113. "FROM " + _tbname + " "\
  114. "WHERE " + _content[0].key + " = " + std::to_string(getInput(0)->getValue()[0]);
  115. std::cout << sql << std::endl;
  116. // Execute SQL statement
  117. rc = sqlite3_prepare_v2(_db, sql.c_str(), -1, &res, 0);
  118. if (rc != SQLITE_OK) {
  119. fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(_db));
  120. sqlite3_close(_db);
  121. }
  122. else {
  123. fprintf(stdout, "Operation done successfully\n");
  124. }
  125. step = sqlite3_step(res);
  126. state state = state::UNCHANGED;
  127. if (step == SQLITE_ROW) {
  128. int nCol = sqlite3_column_count(res);
  129. std::string parse_str = "{\"" + _tbname + "\" : {";
  130. for (size_t i = 0; i < nCol; i++)
  131. {
  132. //json val = (char*)sqlite3_column_text(res, i);
  133. getOutput(i)->setValue({ std::atof((char*)sqlite3_column_text(res, i)) });
  134. }
  135. }
  136. sqlite3_finalize(res);
  137. return state;
  138. }
  139. ModuleSQL::~ModuleSQL() {
  140. sqlite3_close(_db);
  141. }
  142. }