|
@@ -1,21 +1,22 @@
|
|
|
-#include <string>
|
|
|
-#include <nlohmann/json.hpp>
|
|
|
-#include <fmt/core.h>
|
|
|
-#include <fstream>
|
|
|
-
|
|
|
#include "utils.h"
|
|
|
|
|
|
+#include <fmt/core.h>
|
|
|
|
|
|
-using json = nlohmann::json;
|
|
|
+#include <fstream>
|
|
|
+#include <map>
|
|
|
+#include <nlohmann/json.hpp>
|
|
|
+#include <string>
|
|
|
+#include <vector>
|
|
|
|
|
|
+using json = nlohmann::json;
|
|
|
|
|
|
SQLParserRes parse_sql(const std::string& sql) {
|
|
|
SQLParserRes res;
|
|
|
auto stdout_name = std::tmpnam(nullptr);
|
|
|
auto stderr_name = std::tmpnam(nullptr);
|
|
|
-
|
|
|
- auto cmd =
|
|
|
- fmt::format("xmake run sql-parser \"{}\" > {} 2> {}", sql, stdout_name, stderr_name);
|
|
|
+
|
|
|
+ auto cmd = fmt::format("xmake run sql-parser \"{}\" > {} 2> {}", sql,
|
|
|
+ stdout_name, stderr_name);
|
|
|
res.exit_code = system(cmd.c_str());
|
|
|
|
|
|
std::ifstream stdout_f(stdout_name);
|
|
@@ -29,3 +30,47 @@ SQLParserRes parse_sql(const std::string& sql) {
|
|
|
|
|
|
return res;
|
|
|
}
|
|
|
+
|
|
|
+ExistTables::ExistTables(const char* file_name) {
|
|
|
+ table_file_name = file_name;
|
|
|
+
|
|
|
+ std::ifstream ifile(table_file_name);
|
|
|
+ fmt::print("{} {}\n", table_file_name, ifile.good());
|
|
|
+ if (!ifile.good()) {
|
|
|
+ std::ofstream ofile(table_file_name);
|
|
|
+ ofile << "{}";
|
|
|
+ ofile.close();
|
|
|
+ }
|
|
|
+ ifile.close();
|
|
|
+ read_from_file();
|
|
|
+}
|
|
|
+ExistTables* ExistTables::read_from_file() {
|
|
|
+ std::ifstream f(table_file_name);
|
|
|
+ json j = json::parse(f);
|
|
|
+ for (auto& item : j.items()) {
|
|
|
+ tables[item.key()] = item.value().get<std::vector<std::string>>();
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+};
|
|
|
+
|
|
|
+bool ExistTables::exists(const std::string& table_name) {
|
|
|
+ return tables.find(table_name) != tables.end();
|
|
|
+}
|
|
|
+void ExistTables::set(const std::string& table_name,
|
|
|
+ const std::vector<std::string>& cols) {
|
|
|
+ std::vector<std::string> to_vector;
|
|
|
+ std::copy(cols.begin(), cols.end(), std::back_inserter(to_vector));
|
|
|
+
|
|
|
+ tables[table_name] = to_vector;
|
|
|
+}
|
|
|
+void ExistTables::remove(const std::string& table_name) {
|
|
|
+ tables.erase(table_name);
|
|
|
+}
|
|
|
+void ExistTables::save() {
|
|
|
+ json j;
|
|
|
+ for (auto& item : tables) {
|
|
|
+ j[item.first] = item.second;
|
|
|
+ }
|
|
|
+ std::ofstream f(table_file_name);
|
|
|
+ f << j.dump(2);
|
|
|
+}
|