|
| 1 | +#include "mysql_parser/mysql_parser.h" // Ensure this path is correct |
| 2 | +#include "mysql_parser/mysql_ast.h" // Ensure this path is correct |
| 3 | +#include <iostream> |
| 4 | +#include <vector> |
| 5 | +#include <string> |
| 6 | +#include <memory> // Required for std::unique_ptr |
| 7 | +#include <chrono> // Required for timing |
| 8 | +#include <iomanip> // Required for std::fixed and std::setprecision |
| 9 | +#include <sstream> // Required for std::stringstream |
| 10 | +#include <algorithm> // Required for std::all_of if used for whitespace check |
| 11 | + |
| 12 | +// Function to parse command line arguments (same as before) |
| 13 | +void parse_arguments(int argc, char* argv[], int& iterations, bool& print_ast_first_iteration) { |
| 14 | + iterations = 1; // Default value |
| 15 | + print_ast_first_iteration = false; // Default value |
| 16 | + |
| 17 | + for (int i = 1; i < argc; ++i) { |
| 18 | + std::string arg = argv[i]; |
| 19 | + if (arg == "-i") { |
| 20 | + if (i + 1 < argc) { |
| 21 | + try { |
| 22 | + iterations = std::stoi(argv[++i]); |
| 23 | + if (iterations <= 0) { |
| 24 | + std::cerr << "Warning: Number of iterations must be positive. Using default (1)." << std::endl; |
| 25 | + iterations = 1; |
| 26 | + } |
| 27 | + } catch (const std::invalid_argument& ia) { |
| 28 | + std::cerr << "Warning: Invalid number for iterations. Using default (1)." << std::endl; |
| 29 | + iterations = 1; |
| 30 | + } catch (const std::out_of_range& oor) { |
| 31 | + std::cerr << "Warning: Iterations number out of range. Using default (1)." << std::endl; |
| 32 | + iterations = 1; |
| 33 | + } |
| 34 | + } else { |
| 35 | + std::cerr << "Warning: -i option requires one argument." << std::endl; |
| 36 | + } |
| 37 | + } else if (arg == "-v") { |
| 38 | + print_ast_first_iteration = true; |
| 39 | + } else { |
| 40 | + std::cerr << "Warning: Unknown argument: " << arg << std::endl; |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Helper to trim whitespace from both ends of a string |
| 46 | +std::string trim_string(const std::string &s) { |
| 47 | + auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c){return std::isspace(c);}); |
| 48 | + auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c){return std::isspace(c);}).base(); |
| 49 | + return (wsback <= wsfront ? std::string() : std::string(wsfront, wsback)); |
| 50 | +} |
| 51 | + |
| 52 | +// Helper to check if a line is effectively empty (contains only whitespace) |
| 53 | +bool is_line_empty(const std::string& s) { |
| 54 | + return std::all_of(s.begin(), s.end(), isspace); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +int main(int argc, char* argv[]) { |
| 59 | + int iterations_count; |
| 60 | + bool verbose_ast_first_iteration; |
| 61 | + |
| 62 | + parse_arguments(argc, argv, iterations_count, verbose_ast_first_iteration); |
| 63 | + |
| 64 | + std::vector<std::string> all_queries; |
| 65 | + std::cout << "Reading SQL queries from standard input. Press Ctrl+D (Linux/macOS) or Ctrl+Z then Enter (Windows) to end input." << std::endl; |
| 66 | + std::cout << "Queries can be optionally terminated by ';'. An empty line also acts as a delimiter for multi-line queries." << std::endl; |
| 67 | + |
| 68 | + std::string line; |
| 69 | + std::stringstream current_query_buffer; |
| 70 | + |
| 71 | + while (std::getline(std::cin, line)) { |
| 72 | + bool line_is_effectively_empty = is_line_empty(line); |
| 73 | + |
| 74 | + if (line_is_effectively_empty) { |
| 75 | + if (current_query_buffer.tellp() > 0) { // Check if buffer has content (tellp gives current put position) |
| 76 | + std::string query_candidate = current_query_buffer.str(); |
| 77 | + current_query_buffer.str(""); // Clear buffer |
| 78 | + current_query_buffer.clear(); // Clear error flags |
| 79 | + |
| 80 | + std::string final_query = trim_string(query_candidate); |
| 81 | + if (!final_query.empty()) { |
| 82 | + all_queries.push_back(final_query); |
| 83 | + } |
| 84 | + } |
| 85 | + } else { |
| 86 | + current_query_buffer << line << "\n"; // Append line and a newline |
| 87 | + |
| 88 | + // Check if the non-empty line ends with a semicolon |
| 89 | + std::string trimmed_current_line = trim_string(line); // Trim the current line for semicolon check |
| 90 | + if (!trimmed_current_line.empty() && trimmed_current_line.back() == ';') { |
| 91 | + if (current_query_buffer.tellp() > 0) { |
| 92 | + std::string query_candidate = current_query_buffer.str(); |
| 93 | + current_query_buffer.str(""); |
| 94 | + current_query_buffer.clear(); |
| 95 | + |
| 96 | + std::string final_query = trim_string(query_candidate); |
| 97 | + if (!final_query.empty()) { |
| 98 | + all_queries.push_back(final_query); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + // Add any remaining content in the buffer as the last query (EOF) |
| 105 | + if (current_query_buffer.tellp() > 0) { |
| 106 | + std::string query_candidate = current_query_buffer.str(); |
| 107 | + current_query_buffer.str(""); |
| 108 | + current_query_buffer.clear(); |
| 109 | + std::string final_query = trim_string(query_candidate); |
| 110 | + if (!final_query.empty()) { |
| 111 | + all_queries.push_back(final_query); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (all_queries.empty()) { |
| 116 | + std::cout << "No queries read from standard input. Exiting." << std::endl; |
| 117 | + return 0; |
| 118 | + } |
| 119 | + std::cout << all_queries.size() << " query/queries read from input. Starting parsing iterations." << std::endl; |
| 120 | + |
| 121 | + MysqlParser::Parser parser; |
| 122 | + long long successful_parses = 0; |
| 123 | + long long failed_parses = 0; |
| 124 | + |
| 125 | + // Start timer *after* reading input and *before* parsing loop |
| 126 | + auto total_start_time = std::chrono::high_resolution_clock::now(); |
| 127 | + |
| 128 | + for (int iter = 0; iter < iterations_count; ++iter) { |
| 129 | + if (iterations_count > 1 && all_queries.size() > 0) { |
| 130 | + std::cout << "Iteration " << (iter + 1) << "/" << iterations_count << std::endl; |
| 131 | + } |
| 132 | + for (const std::string& query_to_parse : all_queries) { |
| 133 | + // Output the query being parsed if verbose on first iteration, or for debugging |
| 134 | + // if (verbose_ast_first_iteration && iter == 0) { |
| 135 | + // std::cout << "Parsing query: [" << query_to_parse << "]" << std::endl; |
| 136 | + // } |
| 137 | + |
| 138 | + parser.clearErrors(); |
| 139 | + std::unique_ptr<MysqlParser::AstNode> ast = parser.parse(query_to_parse); |
| 140 | + |
| 141 | + if (ast) { |
| 142 | + successful_parses++; |
| 143 | + if (verbose_ast_first_iteration && iter == 0) { |
| 144 | + std::cout << "------------------------------------------\n"; |
| 145 | + std::cout << "Query: " << query_to_parse << std::endl; |
| 146 | + std::cout << "Parsing successful! AST:" << std::endl; |
| 147 | + MysqlParser::print_ast(ast.get()); |
| 148 | + std::cout << "------------------------------------------\n\n"; |
| 149 | + } |
| 150 | + } else { |
| 151 | + failed_parses++; |
| 152 | + if (verbose_ast_first_iteration && iter == 0) { |
| 153 | + std::cout << "------------------------------------------\n"; |
| 154 | + std::cout << "Query: " << query_to_parse << std::endl; |
| 155 | + std::cout << "Parsing failed." << std::endl; |
| 156 | + const auto& errors = parser.getErrors(); |
| 157 | + if (errors.empty()) { |
| 158 | + std::cout << " (No specific error messages)" << std::endl; |
| 159 | + } else { |
| 160 | + for (const auto& error : errors) { |
| 161 | + std::cout << " Error: " << error << std::endl; |
| 162 | + } |
| 163 | + } |
| 164 | + std::cout << "------------------------------------------\n\n"; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + auto total_end_time = std::chrono::high_resolution_clock::now(); |
| 171 | + std::chrono::duration<double> total_duration = total_end_time - total_start_time; |
| 172 | + double total_seconds = total_duration.count(); |
| 173 | + |
| 174 | + long long total_parsing_attempts = static_cast<long long>(iterations_count) * all_queries.size(); |
| 175 | + |
| 176 | + double parsing_per_second = (total_seconds > 0 && total_parsing_attempts > 0) ? (total_parsing_attempts / total_seconds) : 0; |
| 177 | + |
| 178 | + std::cout << "\n======= SUMMARY =======\n"; |
| 179 | + std::cout << "Unique queries read from input: " << all_queries.size() << std::endl; |
| 180 | + std::cout << "Iterations performed over these queries: " << iterations_count << std::endl; |
| 181 | + std::cout << "Total parsing attempts: " << total_parsing_attempts << std::endl; |
| 182 | + std::cout << "Successful parses: " << successful_parses << std::endl; |
| 183 | + std::cout << "Failed parses: " << failed_parses << std::endl; |
| 184 | + std::cout << "Total parsing time: " << std::fixed << std::setprecision(3) << total_seconds << " seconds" << std::endl; |
| 185 | + if (total_parsing_attempts > 0 && total_seconds > 0) { |
| 186 | + std::cout << "Average parsing speed: " << std::fixed << std::setprecision(2) << parsing_per_second << " queries/second" << std::endl; |
| 187 | + } else { |
| 188 | + std::cout << "Average parsing speed: N/A (no queries parsed or zero execution time)" << std::endl; |
| 189 | + } |
| 190 | + std::cout << "=======================\n"; |
| 191 | + |
| 192 | + return 0; |
| 193 | +} |
0 commit comments