improved interface of SQLParserResult

This commit is contained in:
Pedro 2017-02-08 02:59:07 +01:00
parent 5cf62f6b1d
commit ec46b28f32
7 changed files with 77 additions and 39 deletions

View File

@ -4,33 +4,53 @@
namespace hsql { namespace hsql {
SQLParserResult::SQLParserResult() : SQLParserResult::SQLParserResult() :
isValid(true), isValid_(true),
errorMsg(NULL) {}; errorMsg_(NULL) {};
SQLParserResult::SQLParserResult(SQLStatement* stmt) : SQLParserResult::SQLParserResult(SQLStatement* stmt) :
isValid(true), isValid_(true),
errorMsg(NULL) { errorMsg_(NULL) {
addStatement(stmt); addStatement(stmt);
}; };
SQLParserResult::~SQLParserResult() { SQLParserResult::~SQLParserResult() {
for (std::vector<SQLStatement*>::iterator it = statements.begin(); it != statements.end(); ++it) { for (SQLStatement* statement : statements_) {
delete *it; delete statement;
} }
delete errorMsg; delete errorMsg_;
} }
void SQLParserResult::addStatement(SQLStatement* stmt) { void SQLParserResult::addStatement(SQLStatement* stmt) {
statements.push_back(stmt); statements_.push_back(stmt);
} }
SQLStatement* SQLParserResult::getStatement(int id) { const SQLStatement* SQLParserResult::getStatement(int index) const {
return statements[id]; return statements_[index];
} }
size_t SQLParserResult::size() { SQLStatement* SQLParserResult::getMutableStatement(int index) {
return statements.size(); 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 } // namespace hsql

View File

@ -12,34 +12,52 @@ namespace hsql {
SQLParserResult(); SQLParserResult();
// Initialize with a single statement. // Initialize with a single statement.
// Takes ownership of the statement.
SQLParserResult(SQLStatement* stmt); SQLParserResult(SQLStatement* stmt);
// Deletes all statements in the resul. // Deletes all statements in the resul.
virtual ~SQLParserResult(); virtual ~SQLParserResult();
// Returns true if parsing was successful.
bool isValid() const;
// Returns the number of statements in the result. // 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. // 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. // Adds a statement to the result list of statements.
// Takes ownership of the statement.
void addStatement(SQLStatement* stmt); void addStatement(SQLStatement* stmt);
private:
// List of statements within the result. // List of statements within the result.
std::vector<SQLStatement*> statements; std::vector<SQLStatement*> statements_;
// Flag indicating the parsing was successful. // Flag indicating the parsing was successful.
bool isValid; bool isValid_;
// Error message, if an error occurred. // Error message, if an error occurred.
const char* errorMsg; const char* errorMsg_;
// Line number of the occurrance of the error in the query. // 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. // Column number of the occurrance of the error in the query.
int errorColumn; int errorColumn_;
}; };
} // namespace hsql } // namespace hsql

View File

@ -29,7 +29,7 @@ namespace hsql {
virtual ~SQLStatement(); virtual ~SQLStatement();
virtual StatementType type(); virtual StatementType type() const;
private: private:
StatementType _type; StatementType _type;

View File

@ -9,7 +9,7 @@ namespace hsql {
SQLStatement::~SQLStatement() {} SQLStatement::~SQLStatement() {}
StatementType SQLStatement::type() { StatementType SQLStatement::type() const {
return _type; return _type;
} }

View File

@ -3,20 +3,20 @@
#define TEST_PARSE_SQL_QUERY(query, outputVar, numStatements) \ #define TEST_PARSE_SQL_QUERY(query, outputVar, numStatements) \
SQLParserResult* outputVar = SQLParser::parseSQLString(query); \ const SQLParserResult* outputVar = SQLParser::parseSQLString(query); \
ASSERT(outputVar->isValid); \ ASSERT(outputVar->isValid()); \
ASSERT_EQ(outputVar->size(), numStatements); ASSERT_EQ(outputVar->size(), numStatements);
#define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, outputVar) \ #define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, outputVar) \
TEST_PARSE_SQL_QUERY(query, stmt_list, 1); \ TEST_PARSE_SQL_QUERY(query, stmt_list, 1); \
ASSERT_EQ(stmt_list->getStatement(0)->type(), stmtType); \ 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) \ #define TEST_CAST_STMT(stmt_list, stmt_index, stmtType, stmtClass, outputVar) \
ASSERT_EQ(stmt_list->getStatement(stmt_index)->type(), stmtType); \ 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 #endif

View File

@ -67,15 +67,15 @@ int main(int argc, char *argv[]) {
start = std::chrono::system_clock::now(); start = std::chrono::system_clock::now();
// Parsing // Parsing
SQLParserResult* stmt_list = SQLParser::parseSQLString(sql.c_str()); SQLParserResult* result = SQLParser::parseSQLString(sql.c_str());
end = std::chrono::system_clock::now(); end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start; std::chrono::duration<double> elapsed_seconds = end-start;
double us = elapsed_seconds.count() * 1000 * 1000; 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("\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()); printf("\t%s\n", sql.c_str());
numFailed++; numFailed++;
} else { } else {

View File

@ -11,12 +11,12 @@ using namespace hsql;
TEST(DeleteStatementTest) { TEST(DeleteStatementTest) {
SQLParserResult* result = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;"); const SQLParserResult* result = SQLParser::parseSQLString("DELETE FROM students WHERE grade > 2.0;");
ASSERT(result->isValid); ASSERT(result->isValid());
ASSERT_EQ(result->size(), 1); ASSERT_EQ(result->size(), 1);
ASSERT(result->getStatement(0)->type() == kStmtDelete); 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_STREQ(stmt->tableName, "students");
ASSERT_NOTNULL(stmt->expr); ASSERT_NOTNULL(stmt->expr);
ASSERT(stmt->expr->isType(kExprOperator)); ASSERT(stmt->expr->isType(kExprOperator));
@ -25,12 +25,12 @@ TEST(DeleteStatementTest) {
} }
TEST(CreateStatementTest) { TEST(CreateStatementTest) {
SQLParserResult* result = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)"); const SQLParserResult* result = SQLParser::parseSQLString("CREATE TABLE students (name TEXT, student_number INT, city INTEGER, grade DOUBLE)");
ASSERT(result->isValid); ASSERT(result->isValid());
ASSERT_EQ(result->size(), 1); ASSERT_EQ(result->size(), 1);
ASSERT_EQ(result->getStatement(0)->type(), kStmtCreate); 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_EQ(stmt->type, CreateStatement::kTable);
ASSERT_STREQ(stmt->tableName, "students"); ASSERT_STREQ(stmt->tableName, "students");
ASSERT_NOTNULL(stmt->columns); ASSERT_NOTNULL(stmt->columns);
@ -47,12 +47,12 @@ TEST(CreateStatementTest) {
TEST(UpdateStatementTest) { TEST(UpdateStatementTest) {
SQLParserResult* result = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';"); const SQLParserResult* result = SQLParser::parseSQLString("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';");
ASSERT(result->isValid); ASSERT(result->isValid());
ASSERT_EQ(result->size(), 1); ASSERT_EQ(result->size(), 1);
ASSERT_EQ(result->getStatement(0)->type(), kStmtUpdate); 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_NOTNULL(stmt->table);
ASSERT_STREQ(stmt->table->name, "students"); ASSERT_STREQ(stmt->table->name, "students");
@ -142,4 +142,4 @@ TEST(ExecuteStatementTest) {
ASSERT_STREQ(stmt->name, "test"); ASSERT_STREQ(stmt->name, "test");
ASSERT_EQ(stmt->parameters->size(), 2); ASSERT_EQ(stmt->parameters->size(), 2);
} }