Browse Source

创建表相关的utils和JSON构造

myuan 2 years ago
parent
commit
0faa15ac4d
2 changed files with 17 additions and 10 deletions
  1. 3 7
      src/utils.cpp
  2. 14 3
      src/utils.h

+ 3 - 7
src/utils.cpp

@@ -47,7 +47,7 @@ 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>>();
+        tables[item.key()] = item.value();
     }
     return this;
 };
@@ -55,12 +55,8 @@ ExistTables* ExistTables::read_from_file() {
 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::set(const std::string& table_name, const TableCols& cols) {
+    tables[table_name] = cols;
 }
 void ExistTables::remove(const std::string& table_name) {
     tables.erase(table_name);

+ 14 - 3
src/utils.h

@@ -10,18 +10,29 @@ struct SQLParserRes {
     std::string err;
 };
 
+struct TableCol {
+    std::string column_name;
+    std::string type;
+    std::string data_type;
+    bool primary_key;
+    NLOHMANN_DEFINE_TYPE_INTRUSIVE(TableCol, column_name, type, data_type,
+                                   primary_key);
+};
+
+using TableCols = std::vector<TableCol>;
+
+
 SQLParserRes parse_sql(const std::string& sql);
 
 class ExistTables {
    public:
     std::string table_file_name;
-    std::map<std::string, std::vector<std::string>> tables;
+    std::map<std::string, TableCols> 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 set(const std::string& table_name, const TableCols& cols);
     void remove(const std::string& table_name);
     void save();
 };