Pārlūkot izejas kodu

表相关操作

myuan 2 gadi atpakaļ
vecāks
revīzija
09c88d99cb
2 mainītis faili ar 71 papildinājumiem un 10 dzēšanām
  1. 54 9
      src/utils.cpp
  2. 17 1
      src/utils.h

+ 54 - 9
src/utils.cpp

@@ -1,21 +1,22 @@
-#include <string>
-#include <nlohmann/json.hpp>
-#include <fmt/core.h>
-#include <fstream>
-
 #include "utils.h"
 #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 parse_sql(const std::string& sql) {
     SQLParserRes res;
     SQLParserRes res;
     auto stdout_name = std::tmpnam(nullptr);
     auto stdout_name = std::tmpnam(nullptr);
     auto stderr_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());
     res.exit_code = system(cmd.c_str());
 
 
     std::ifstream stdout_f(stdout_name);
     std::ifstream stdout_f(stdout_name);
@@ -29,3 +30,47 @@ SQLParserRes parse_sql(const std::string& sql) {
 
 
     return res;
     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);
+}

+ 17 - 1
src/utils.h

@@ -1,6 +1,8 @@
 #pragma once
 #pragma once
 
 
+#include <map>
 #include <nlohmann/json.hpp>
 #include <nlohmann/json.hpp>
+#include <vector>
 
 
 struct SQLParserRes {
 struct SQLParserRes {
     int exit_code;
     int exit_code;
@@ -8,4 +10,18 @@ struct SQLParserRes {
     std::string err;
     std::string err;
 };
 };
 
 
-SQLParserRes parse_sql(const std::string& sql);
+SQLParserRes parse_sql(const std::string& sql);
+
+class ExistTables {
+   public:
+    std::string table_file_name;
+    std::map<std::string, std::vector<std::string>> tables;
+
+    ExistTables(const char* file_name = "./tables.json");
+    ExistTables* read_from_file();
+    bool exists(const std::string& table_name);
+    void set(const std::string& table_name,
+             const std::vector<std::string>& cols);
+    void remove(const std::string& table_name);
+    void save();
+};