From ec46b28f323fb41b70ad7867bfb86ec95343d192 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 8 Feb 2017 02:59:07 +0100 Subject: [PATCH] improved interface of SQLParserResult --- src/SQLParserResult.cpp | 44 ++++++++++++++++++++++++++++----------- src/SQLParserResult.h | 32 +++++++++++++++++++++------- src/sql/SQLStatement.h | 2 +- src/sql/statements.cpp | 2 +- test/lib/helper.h | 10 ++++----- test/sql_grammar_test.cpp | 6 +++--- test/sql_tests.cpp | 20 +++++++++--------- 7 files changed, 77 insertions(+), 39 deletions(-) diff --git a/src/SQLParserResult.cpp b/src/SQLParserResult.cpp index ed7e1b5..94cd5e0 100644 --- a/src/SQLParserResult.cpp +++ b/src/SQLParserResult.cpp @@ -4,33 +4,53 @@ namespace hsql { SQLParserResult::SQLParserResult() : - isValid(true), - errorMsg(NULL) {}; + isValid_(true), + errorMsg_(NULL) {}; SQLParserResult::SQLParserResult(SQLStatement* stmt) : - isValid(true), - errorMsg(NULL) { + isValid_(true), + errorMsg_(NULL) { addStatement(stmt); }; SQLParserResult::~SQLParserResult() { - for (std::vector::iterator it = statements.begin(); it != statements.end(); ++it) { - delete *it; + for (SQLStatement* statement : statements_) { + delete statement; } - delete errorMsg; + delete errorMsg_; } void SQLParserResult::addStatement(SQLStatement* stmt) { - statements.push_back(stmt); + statements_.push_back(stmt); } - SQLStatement* SQLParserResult::getStatement(int id) { - return statements[id]; + const SQLStatement* SQLParserResult::getStatement(int index) const { + return statements_[index]; } - size_t SQLParserResult::size() { - return statements.size(); + SQLStatement* SQLParserResult::getMutableStatement(int index) { + return statements_[index]; + } + + size_t SQLParserResult::size() const { + return statements_.size(); + } + + bool SQLParserResult::isValid() const { + return isValid_; + } + + const char* SQLParserResult::errorMsg() const { + return errorMsg_; + } + + int SQLParserResult::errorLine() const { + return errorLine_; + } + + int SQLParserResult::errorColumn() const { + return errorColumn_; } } // namespace hsql \ No newline at end of file diff --git a/src/SQLParserResult.h b/src/SQLParserResult.h index 5622dda..cbc815a 100644 --- a/src/SQLParserResult.h +++ b/src/SQLParserResult.h @@ -12,34 +12,52 @@ namespace hsql { SQLParserResult(); // Initialize with a single statement. + // Takes ownership of the statement. SQLParserResult(SQLStatement* stmt); // Deletes all statements in the resul. virtual ~SQLParserResult(); + // Returns true if parsing was successful. + bool isValid() const; + // Returns the number of statements in the result. - size_t size(); + size_t size() const; + + // Returns the error message, if an error occurred. + const char* errorMsg() const; + + // Returns the line number of the occurrance of the error in the query. + int errorLine() const; + + // Returns the column number of the occurrance of the error in the query. + int errorColumn() const; // Gets the SQL statement with the given index. - SQLStatement* getStatement(int id); + 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. + // Takes ownership of the statement. void addStatement(SQLStatement* stmt); + private: // List of statements within the result. - std::vector statements; + std::vector statements_; // Flag indicating the parsing was successful. - bool isValid; + bool isValid_; // Error message, if an error occurred. - const char* errorMsg; + const char* errorMsg_; // Line number of the occurrance of the error in the query. - int errorLine; + int errorLine_; // Column number of the occurrance of the error in the query. - int errorColumn; + int errorColumn_; }; } // namespace hsql diff --git a/src/sql/SQLStatement.h b/src/sql/SQLStatement.h index ffbe049..9aa9434 100644 --- a/src/sql/SQLStatement.h +++ b/src/sql/SQLStatement.h @@ -29,7 +29,7 @@ namespace hsql { virtual ~SQLStatement(); - virtual StatementType type(); + virtual StatementType type() const; private: StatementType _type; diff --git a/src/sql/statements.cpp b/src/sql/statements.cpp index 45da79d..36467b8 100644 --- a/src/sql/statements.cpp +++ b/src/sql/statements.cpp @@ -9,7 +9,7 @@ namespace hsql { SQLStatement::~SQLStatement() {} - StatementType SQLStatement::type() { + StatementType SQLStatement::type() const { return _type; } diff --git a/test/lib/helper.h b/test/lib/helper.h index 5e74da9..143e725 100644 --- a/test/lib/helper.h +++ b/test/lib/helper.h @@ -3,20 +3,20 @@ #define TEST_PARSE_SQL_QUERY(query, outputVar, numStatements) \ - SQLParserResult* outputVar = SQLParser::parseSQLString(query); \ - ASSERT(outputVar->isValid); \ + const SQLParserResult* outputVar = SQLParser::parseSQLString(query); \ + ASSERT(outputVar->isValid()); \ ASSERT_EQ(outputVar->size(), numStatements); #define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, outputVar) \ TEST_PARSE_SQL_QUERY(query, stmt_list, 1); \ ASSERT_EQ(stmt_list->getStatement(0)->type(), stmtType); \ - stmtClass* outputVar = (stmtClass*) stmt_list->getStatement(0); + const stmtClass* outputVar = (const stmtClass*) stmt_list->getStatement(0); #define TEST_CAST_STMT(stmt_list, stmt_index, stmtType, stmtClass, outputVar) \ ASSERT_EQ(stmt_list->getStatement(stmt_index)->type(), stmtType); \ - stmtClass* outputVar = (stmtClass*) stmt_list->getStatement(stmt_index); + const stmtClass* outputVar = (const stmtClass*) stmt_list->getStatement(stmt_index); -#endif \ No newline at end of file +#endif diff --git a/test/sql_grammar_test.cpp b/test/sql_grammar_test.cpp index e4ca602..177bfea 100644 --- a/test/sql_grammar_test.cpp +++ b/test/sql_grammar_test.cpp @@ -67,15 +67,15 @@ int main(int argc, char *argv[]) { start = std::chrono::system_clock::now(); // Parsing - SQLParserResult* stmt_list = SQLParser::parseSQLString(sql.c_str()); + SQLParserResult* result = SQLParser::parseSQLString(sql.c_str()); end = std::chrono::system_clock::now(); std::chrono::duration elapsed_seconds = end-start; double us = elapsed_seconds.count() * 1000 * 1000; - if (expectFalse == stmt_list->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", stmt_list->errorMsg, stmt_list->errorLine, stmt_list->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 { diff --git a/test/sql_tests.cpp b/test/sql_tests.cpp index 8550e00..c7248e3 100644 --- a/test/sql_tests.cpp +++ b/test/sql_tests.cpp @@ -11,12 +11,12 @@ using namespace hsql; TEST(DeleteStatementTest) { - SQLParserResult* result = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); - ASSERT(result->isValid); + const SQLParserResult* result = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); + ASSERT(result->isValid()); ASSERT_EQ(result->size(), 1); ASSERT(result->getStatement(0)->type() == kStmtDelete); - DeleteStatement* stmt = (DeleteStatement*) result->getStatement(0); + const DeleteStatement* stmt = (const DeleteStatement*) result->getStatement(0); ASSERT_STREQ(stmt->tableName, "students"); ASSERT_NOTNULL(stmt->expr); ASSERT(stmt->expr->isType(kExprOperator)); @@ -25,12 +25,12 @@ TEST(DeleteStatementTest) { } TEST(CreateStatementTest) { - SQLParserResult* result = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); - ASSERT(result->isValid); + const SQLParserResult* result = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); + ASSERT(result->isValid()); ASSERT_EQ(result->size(), 1); ASSERT_EQ(result->getStatement(0)->type(), kStmtCreate); - CreateStatement* stmt = (CreateStatement*) result->getStatement(0); + const CreateStatement* stmt = (const CreateStatement*) result->getStatement(0); ASSERT_EQ(stmt->type, CreateStatement::kTable); ASSERT_STREQ(stmt->tableName, "students"); ASSERT_NOTNULL(stmt->columns); @@ -47,12 +47,12 @@ TEST(CreateStatementTest) { TEST(UpdateStatementTest) { - SQLParserResult* result = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); - ASSERT(result->isValid); + const SQLParserResult* result = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); + ASSERT(result->isValid()); ASSERT_EQ(result->size(), 1); ASSERT_EQ(result->getStatement(0)->type(), kStmtUpdate); - UpdateStatement* stmt = (UpdateStatement*) result->getStatement(0); + const UpdateStatement* stmt = (const UpdateStatement*) result->getStatement(0); ASSERT_NOTNULL(stmt->table); ASSERT_STREQ(stmt->table->name, "students"); @@ -142,4 +142,4 @@ TEST(ExecuteStatementTest) { ASSERT_STREQ(stmt->name, "test"); ASSERT_EQ(stmt->parameters->size(), 2); -} \ No newline at end of file +}