From e16925e7a5f9d2e05592da374bc18c1d65ae04c9 Mon Sep 17 00:00:00 2001 From: Pedro Flemming Date: Fri, 7 Apr 2017 16:16:25 +0200 Subject: [PATCH] add parseSQLString method with output parameter instead of return value --- src/SQLParser.cpp | 40 +++++++++++++++++++++++++-------------- src/SQLParser.h | 10 ++++++++++ test/sql_grammar_test.cpp | 9 ++++----- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/SQLParser.cpp b/src/SQLParser.cpp index 5f89911..9f6ee5b 100644 --- a/src/SQLParser.cpp +++ b/src/SQLParser.cpp @@ -12,37 +12,49 @@ namespace hsql { fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n"); } - SQLParserResult* SQLParser::parseSQLString(const char* text) { - SQLParserResult* result = new SQLParserResult(); + // static + bool SQLParser::parseSQLString(const char* text, SQLParserResult* result) { yyscan_t scanner; YY_BUFFER_STATE state; if (hsql_lex_init(&scanner)) { // Couldn't initialize the lexer. fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n"); - delete result; - return NULL; + return false; } state = hsql__scan_string(text, scanner); - // Parser and return early if it failed. - if (hsql_parse(result, scanner)) { - // Returns an error stmt object. - hsql__delete_buffer(state, scanner); - hsql_lex_destroy(scanner); - return result; - } + // Parse the tokens. + // If parsing fails, the result will contain an error object. + hsql_parse(result, scanner); hsql__delete_buffer(state, scanner); hsql_lex_destroy(scanner); + + return true; + } + + // static + bool SQLParser::parseSQLString(const std::string& text, SQLParserResult* result) { + return parseSQLString(text.c_str(), result); + } + + // static + SQLParserResult* SQLParser::parseSQLString(const char* text) { + SQLParserResult* result = new SQLParserResult(); + + if (!SQLParser::parseSQLString(text, result)) { + delete result; + return NULL; + } + return result; } - + // static SQLParserResult* SQLParser::parseSQLString(const std::string& text) { return parseSQLString(text.c_str()); } - -} // namespace hsql \ No newline at end of file +} // namespace hsql diff --git a/src/SQLParser.h b/src/SQLParser.h index 7ee774e..dffdcea 100644 --- a/src/SQLParser.h +++ b/src/SQLParser.h @@ -9,10 +9,20 @@ namespace hsql { // Static methods used to parse SQL strings. class SQLParser { public: + // Parses a given constant character SQL string into the result object. + static bool parseSQLString(const char* sql, SQLParserResult* result); + + // Parses a given SQL string into the result object. + static bool parseSQLString(const std::string& sql, SQLParserResult* result); + // Parses a given constant character SQL string. + // Note: This is kept for legacy reasons. It is recommended to use + // the (const char*, SQLParserResult*) implementation. static SQLParserResult* parseSQLString(const char* sql); // Parses an SQL std::string. + // Note: This is kept for legacy reasons. It is recommended to use + // the (const std::string&, SQLParserResult*) implementation. static SQLParserResult* parseSQLString(const std::string& sql); private: diff --git a/test/sql_grammar_test.cpp b/test/sql_grammar_test.cpp index 3bfecc8..0daf0c0 100644 --- a/test/sql_grammar_test.cpp +++ b/test/sql_grammar_test.cpp @@ -79,23 +79,22 @@ TEST(AutoGrammarTest) { start = std::chrono::system_clock::now(); // Parsing - SQLParserResult* result = SQLParser::parseSQLString(sql.c_str()); + SQLParserResult result; + SQLParser::parseSQLString(sql.c_str(), &result); end = std::chrono::system_clock::now(); std::chrono::duration elapsed_seconds = end - start; double us = elapsed_seconds.count() * 1000 * 1000; - if (expectFalse == result->isValid()) { + if (expectFalse == result.isValid()) { printf("\033[0;31m{ failed}\033[0m\n"); - printf("\t\033[0;31m%s (L%d:%d)\n\033[0m", result->errorMsg(), result->errorLine(), result->errorColumn()); + printf("\t\033[0;31m%s (L%d:%d)\n\033[0m", result.errorMsg(), result.errorLine(), result.errorColumn()); printf("\t%s\n", sql.c_str()); numFailed++; } else { // TODO: indicate whether expectFalse was set printf("\033[0;32m{ ok} (%.1fus)\033[0m %s\n", us, sql.c_str()); } - - delete result; } if (numFailed == 0) {