Browse Source

解析命令行和新建表

myuan 2 năm trước cách đây
mục cha
commit
ab790f9351
1 tập tin đã thay đổi với 69 bổ sung0 xóa
  1. 69 0
      src/checker.cpp

+ 69 - 0
src/checker.cpp

@@ -0,0 +1,69 @@
+#include <fmt/core.h>
+#include <stdio.h>
+
+#include <CLI/CLI.hpp>
+
+#include "utils.h"
+
+using json = nlohmann::json;
+using string = std::string;
+auto tables = ExistTables("./tables.json");
+
+void create_table(const std::string& table_name, const TableCols& cols) {
+    tables.set(table_name, cols);
+    tables.save();
+}
+
+void process_sql(json& j) {
+    auto type = j.value("type", "none");
+
+    if (type == "create_stmt") {
+        auto table_name = j["table_name"];
+        if (tables.exists(table_name)) {
+            throw std::runtime_error(
+                fmt::format("table: `{}` exists\n", table_name));
+        }
+        create_table(j["table_name"], j["cols"]);
+    } else {
+        throw std::runtime_error(
+            fmt::format("Unknown expression type `{}` total: \n{}", type, j.dump(2)));
+    }
+
+}
+
+int main(int argc, char** argv) {
+    CLI::App app{"sql 检查器, 附带创建表相关功能"};
+    string sql = "", filename = "";
+
+    app.add_option("-f,--file", filename, "输入的 sql 解析后的 json 文件")
+        ->check(CLI::ExistingFile);
+    app.add_option("-s,--sql", sql, "输入的 sql");
+
+    CLI11_PARSE(app, argc, argv);
+
+    json stmts;
+    if (sql.length() > 0) {
+        stmts = parse_sql(sql).out;
+    } else if (filename.length() > 0) {
+        stmts = json::parse(std::ifstream(filename));
+    } else {
+        std::cout << "请输入 sql 或者 sql 文件" << std::endl;
+        return 1;
+    }
+
+    for (auto& stmt : stmts) {
+        // for(auto& item : stmt.items()) {
+        //     fmt::print("{}: {}\n", item.key(), item.value().dump());
+        // }
+        process_sql(stmt);
+    }
+
+    // auto t = res.out[0].value("type", "default");
+    // fmt::print("{}\n", t);
+
+    // tables.set("t1", {"a", "b", "c"});
+    // tables.set("t3", {"asdge", "safaw", "qwer"});
+    // fmt::print("tables.exists {}\n", tables.exists("t1"));
+    // tables.save();
+    return 0;
+}