add various utility methods to SQLParserResult like releaseStatements

This commit is contained in:
Pedro Flemming 2017-04-08 02:37:30 +02:00
parent d318ef0de4
commit e94e80e674
5 changed files with 76 additions and 18 deletions

View File

@ -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);

View File

@ -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.

View File

@ -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
const std::vector<SQLStatement*>& SQLParserResult::getStatements() const {
return statements_;
}
std::vector<SQLStatement*> SQLParserResult::releaseStatements() {
std::vector<SQLStatement*> 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

View File

@ -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<SQLStatement*>& 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<SQLStatement*> releaseStatements();
// Deletes all statements and other data within the result.
void reset();
private:
// List of statements within the result.

View File

@ -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<SQLStatement*> statements = result.releaseStatements();
ASSERT_EQ(0, result.size());
for (SQLStatement* stmt : statements) {
delete stmt;
}
}
TEST_MAIN();