|
@@ -0,0 +1,31 @@
|
|
|
|
+#include <string>
|
|
|
|
+#include <nlohmann/json.hpp>
|
|
|
|
+#include <fmt/core.h>
|
|
|
|
+#include <fstream>
|
|
|
|
+
|
|
|
|
+#include "utils.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+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);
|
|
|
|
+ res.exit_code = system(cmd.c_str());
|
|
|
|
+
|
|
|
|
+ std::ifstream stdout_f(stdout_name);
|
|
|
|
+ json stdout_j = json::parse(stdout_f);
|
|
|
|
+ res.out = stdout_j;
|
|
|
|
+
|
|
|
|
+ std::ifstream stderr_f(stderr_name);
|
|
|
|
+
|
|
|
|
+ res.err.assign((std::istreambuf_iterator<char>(stderr_f)),
|
|
|
|
+ (std::istreambuf_iterator<char>()));
|
|
|
|
+
|
|
|
|
+ return res;
|
|
|
|
+}
|