From e94e80e674824ab59a2c79cf08bf4e4572a171a6 Mon Sep 17 00:00:00 2001 From: Pedro Flemming Date: Sat, 8 Apr 2017 02:37:30 +0200 Subject: [PATCH] add various utility methods to SQLParserResult like releaseStatements --- src/SQLParser.cpp | 4 +++- src/SQLParser.h | 5 +++++ src/SQLParserResult.cpp | 38 ++++++++++++++++++++++++++++++-------- src/SQLParserResult.h | 27 ++++++++++++++++++--------- test/sql_tests.cpp | 20 ++++++++++++++++++++ 5 files changed, 76 insertions(+), 18 deletions(-) diff --git a/src/SQLParser.cpp b/src/SQLParser.cpp index 9f6ee5b..a33f84d 100644 --- a/src/SQLParser.cpp +++ b/src/SQLParser.cpp @@ -27,7 +27,9 @@ namespace hsql { // Parse the tokens. // If parsing fails, the result will contain an error object. - hsql_parse(result, scanner); + int ret = hsql_parse(result, scanner); + bool success = (ret == 0); + result->setIsValid(success); hsql__delete_buffer(state, scanner); hsql_lex_destroy(scanner); diff --git a/src/SQLParser.h b/src/SQLParser.h index dffdcea..e494680 100644 --- a/src/SQLParser.h +++ b/src/SQLParser.h @@ -10,16 +10,21 @@ namespace hsql { class SQLParser { public: // Parses a given constant character SQL string into the result object. + // Returns true if the lexer and parser could run without internal errors. + // This does NOT mean that the SQL string was valid SQL. To check that + // you need to check result->isValid(); 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); + // Deprecated: // 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); + // Deprecated: // Parses an SQL std::string. // Note: This is kept for legacy reasons. It is recommended to use // the (const std::string&, SQLParserResult*) implementation. diff --git a/src/SQLParserResult.cpp b/src/SQLParserResult.cpp index ca106c9..b8df999 100644 --- a/src/SQLParserResult.cpp +++ b/src/SQLParserResult.cpp @@ -4,21 +4,17 @@ namespace hsql { SQLParserResult::SQLParserResult() : - isValid_(true), + isValid_(false), errorMsg_(NULL) {}; SQLParserResult::SQLParserResult(SQLStatement* stmt) : - isValid_(true), + isValid_(false), errorMsg_(NULL) { addStatement(stmt); }; SQLParserResult::~SQLParserResult() { - for (SQLStatement* statement : statements_) { - delete statement; - } - - free(errorMsg_); + reset(); } void SQLParserResult::addStatement(SQLStatement* stmt) { @@ -63,4 +59,30 @@ namespace hsql { errorColumn_ = errorColumn; } -} // namespace hsql \ No newline at end of file + const std::vector& SQLParserResult::getStatements() const { + return statements_; + } + + std::vector SQLParserResult::releaseStatements() { + std::vector copy = statements_; + + statements_.clear(); + + return copy; + } + + void SQLParserResult::reset() { + for (SQLStatement* statement : statements_) { + delete statement; + } + statements_.clear(); + + isValid_ = false; + + free(errorMsg_); + errorMsg_ = NULL; + errorLine_ = -1; + errorColumn_ = -1; + } + +} // namespace hsql diff --git a/src/SQLParserResult.h b/src/SQLParserResult.h index a39c1cb..570664c 100644 --- a/src/SQLParserResult.h +++ b/src/SQLParserResult.h @@ -18,12 +18,19 @@ namespace hsql { // Deletes all statements in the result. virtual ~SQLParserResult(); + // Set whether parsing was successful. + void setIsValid(bool isValid); + // Returns true if parsing was successful. bool isValid() const; // Returns the number of statements in the result. size_t size() const; + // Set the details of the error, if available. + // Takes ownership of errorMsg. + void setErrorDetails(char* errorMsg, int errorLine, int errorColumn); + // Returns the error message, if an error occurred. const char* errorMsg() const; @@ -33,23 +40,25 @@ namespace hsql { // Returns the column number of the occurrance of the error in the query. int errorColumn() const; + // Adds a statement to the result list of statements. + // SQLParserResult takes ownership of the statement. + void addStatement(SQLStatement* stmt); + // Gets the SQL statement with the given index. const SQLStatement* getStatement(int index) const; // Gets the non const SQL statement with the given index. SQLStatement* getMutableStatement(int index); - // Adds a statement to the result list of statements. - // SQLParserResult takes ownership of the statement. - void addStatement(SQLStatement* stmt); + // Get the list of all statements. + const std::vector& getStatements() const; - // Set whether parsing was successful. - void setIsValid(bool isValid); - - // Set the details of the error, if available. - // Takes ownership of errorMsg. - void setErrorDetails(char* errorMsg, int errorLine, int errorColumn); + // Returns a copy of the list of all statements in this result. + // Removes them from this result. + std::vector releaseStatements(); + // Deletes all statements and other data within the result. + void reset(); private: // List of statements within the result. diff --git a/test/sql_tests.cpp b/test/sql_tests.cpp index 56fb5a2..3e3d359 100644 --- a/test/sql_tests.cpp +++ b/test/sql_tests.cpp @@ -161,4 +161,24 @@ TEST(ExecuteStatementTest) { ASSERT_EQ(stmt->parameters->size(), 2); } +TEST(ReleaseStatementTest) { + TEST_PARSE_SINGLE_SQL( + "SELECT * FROM students;", + kStmtSelect, + SelectStatement, + result, + stmt); + + ASSERT_EQ(1, result.size()); + ASSERT_NULL(stmt->whereClause); + + std::vector statements = result.releaseStatements(); + + ASSERT_EQ(0, result.size()); + + for (SQLStatement* stmt : statements) { + delete stmt; + } +} + TEST_MAIN();