utils.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "utils.h"
  2. #include <fmt/core.h>
  3. #include <algorithm>
  4. #include <fstream>
  5. #include <map>
  6. #include <nlohmann/json.hpp>
  7. #include <string>
  8. #include <vector>
  9. using json = nlohmann::json;
  10. SQLParserRes parse_sql(const std::string& sql) {
  11. SQLParserRes res;
  12. // auto stdout_name = std::tmpnam(nullptr);
  13. // auto stderr_name = std::tmpnam(nullptr);
  14. auto stdout_name = "/tmp/sql-parser-stdout";
  15. auto stderr_name = "/tmp/sql-parser-stderr";
  16. auto cmd = fmt::format("xmake run sql-parser \"{}\" > {} 2> {}", sql,
  17. stdout_name, stderr_name);
  18. res.exit_code = system(cmd.c_str());
  19. std::ifstream stdout_f(stdout_name);
  20. json stdout_j = json::parse(stdout_f);
  21. res.out = stdout_j;
  22. std::ifstream stderr_f(stderr_name);
  23. res.err.assign((std::istreambuf_iterator<char>(stderr_f)),
  24. (std::istreambuf_iterator<char>()));
  25. return res;
  26. }
  27. ExistTables::ExistTables(const char* file_name) {
  28. table_file_name = file_name;
  29. std::ifstream ifile(table_file_name);
  30. if (!ifile.good()) {
  31. std::ofstream ofile(table_file_name);
  32. ofile << "{}";
  33. ofile.close();
  34. }
  35. ifile.close();
  36. read_from_file();
  37. }
  38. ExistTables* ExistTables::read_from_file() {
  39. std::ifstream f(table_file_name);
  40. json j = json::parse(f);
  41. for (auto& item : j.items()) {
  42. tables[item.key()] = item.value();
  43. }
  44. return this;
  45. };
  46. bool ExistTables::exists(const std::string& table_name) {
  47. return tables.find(table_name) != tables.end();
  48. }
  49. void ExistTables::set(const std::string& table_name, const TableCols& cols) {
  50. tables[table_name] = cols;
  51. }
  52. TableCols ExistTables::operator[](const std::string& table_name) {
  53. return this->get(table_name);
  54. }
  55. TableCols ExistTables::operator[](const std::vector<std::string>& table_names) {
  56. return this->get(table_names);
  57. }
  58. TableCols ExistTables::get(const std::string& table_name) {
  59. return tables[table_name];
  60. }
  61. TableCols ExistTables::get(const std::vector<std::string>& table_names) {
  62. TableCols res = {};
  63. for (let table_name : table_names) {
  64. let cols = this->get(table_name);
  65. res.assign(cols.begin(), cols.end());
  66. }
  67. return res;
  68. }
  69. optional<TableCol> ExistTables::find_col_in_tables(
  70. const std::vector<std::string>& table_names,
  71. const std::string col_name) {
  72. for (let table_name : table_names) {
  73. let cols = this->get(table_name);
  74. for(let col : cols) {
  75. if (col.column_name == col_name) {
  76. return col;
  77. }
  78. }
  79. }
  80. return nullopt;
  81. }
  82. void ExistTables::remove(const std::string& table_name) {
  83. tables.erase(table_name);
  84. }
  85. void ExistTables::save() {
  86. json j;
  87. for (auto& item : tables) {
  88. j[item.first] = item.second;
  89. }
  90. std::ofstream f(table_file_name);
  91. f << j.dump(2);
  92. }