소스 검색

优化错误输出

myuan 2 년 전
부모
커밋
e319e4ed95
3개의 변경된 파일21개의 추가작업 그리고 3개의 파일을 삭제
  1. 3 3
      src/checker.cpp
  2. 16 0
      src/utils.cpp
  3. 2 0
      src/utils.h

+ 3 - 3
src/checker.cpp

@@ -60,8 +60,8 @@ void check_where_clause(const vector<string> table_names, const json& j) {
             auto col_def = get_select_column_define(table_names, curr_branch);
             if (!col_def.has_value()) {
                 throw std::runtime_error(
-                    fmt::format("column `{}` not exists in `{}`\n",
-                                curr_branch["value"].dump(2),
+                    fmt::format("column `{}` not exists in `{}`\n", 
+                                select_column_get_name(curr_branch),
                                 fmt::join(table_names, ", ")));
             }
         } else {
@@ -82,7 +82,7 @@ void check_select_table(const vector<string> table_names,
         if (!col_def.has_value()) {
             throw std::runtime_error(
                 fmt::format("column `{}` not exists in `{}`\n",
-                            select_col["target"]["value"].dump(2),
+                            select_column_get_name(select_col["target"]),
                             fmt::join(table_names, ", ")));
         }
     }

+ 16 - 0
src/utils.cpp

@@ -10,6 +10,7 @@
 #include <vector>
 
 using json = nlohmann::json;
+using std::string;
 
 SQLParserRes parse_sql(const std::string& sql) {
     SQLParserRes res;
@@ -104,3 +105,18 @@ void ExistTables::save() {
     std::ofstream f(table_file_name);
     f << j.dump(2);
 }
+
+string select_column_get_name(const json &j) {
+    let type = j["type"];
+    if (type == "identifier") {
+        return j["value"];
+    } else if (type == "select_all_column") {
+        return "select_all_column";
+    } else if (type == "int" || type == "string" || type == "float") {
+        return fmt::format("{}:{}", type, j["value"]);
+    } else if (type == "table_field") {
+        return fmt::format("{}.{}", j["table"], j["field"]);
+    }
+    throw std::runtime_error(
+        fmt::format("无法将 json 视为 select_column: {}", j.dump(2)));
+}

+ 2 - 0
src/utils.h

@@ -57,3 +57,5 @@ inline auto table_names_of(const nlohmann::json& j) {
     return j["table_names"];
 }
 inline auto type_of(const nlohmann::json& j) { return j.value("type", "none"); }
+
+std::string select_column_get_name(const nlohmann::json &j);