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. // Parse the tokens.
// If parsing fails, the result will contain an error object. // 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__delete_buffer(state, scanner);
hsql_lex_destroy(scanner); hsql_lex_destroy(scanner);

View File

@ -10,16 +10,21 @@ namespace hsql {
class SQLParser { class SQLParser {
public: public:
// Parses a given constant character SQL string into the result object. // 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); static bool parseSQLString(const char* sql, SQLParserResult* result);
// Parses a given SQL string into the result object. // Parses a given SQL string into the result object.
static bool parseSQLString(const std::string& sql, SQLParserResult* result); static bool parseSQLString(const std::string& sql, SQLParserResult* result);
// Deprecated:
// Parses a given constant character SQL string. // Parses a given constant character SQL string.
// Note: This is kept for legacy reasons. It is recommended to use // Note: This is kept for legacy reasons. It is recommended to use
// the (const char*, SQLParserResult*) implementation. // the (const char*, SQLParserResult*) implementation.
static SQLParserResult* parseSQLString(const char* sql); static SQLParserResult* parseSQLString(const char* sql);
// Deprecated:
// Parses an SQL std::string. // Parses an SQL std::string.
// Note: This is kept for legacy reasons. It is recommended to use // Note: This is kept for legacy reasons. It is recommended to use
// the (const std::string&, SQLParserResult*) implementation. // the (const std::string&, SQLParserResult*) implementation.

View File

@ -4,21 +4,17 @@
namespace hsql { namespace hsql {
SQLParserResult::SQLParserResult() : SQLParserResult::SQLParserResult() :
isValid_(true), isValid_(false),
errorMsg_(NULL) {}; errorMsg_(NULL) {};
SQLParserResult::SQLParserResult(SQLStatement* stmt) : SQLParserResult::SQLParserResult(SQLStatement* stmt) :
isValid_(true), isValid_(false),
errorMsg_(NULL) { errorMsg_(NULL) {
addStatement(stmt); addStatement(stmt);
}; };
SQLParserResult::~SQLParserResult() { SQLParserResult::~SQLParserResult() {
for (SQLStatement* statement : statements_) { reset();
delete statement;
}
free(errorMsg_);
} }
void SQLParserResult::addStatement(SQLStatement* stmt) { void SQLParserResult::addStatement(SQLStatement* stmt) {
@ -63,4 +59,30 @@ namespace hsql {
errorColumn_ = errorColumn; 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. // Deletes all statements in the result.
virtual ~SQLParserResult(); virtual ~SQLParserResult();
// Set whether parsing was successful.
void setIsValid(bool isValid);
// Returns true if parsing was successful. // Returns true if parsing was successful.
bool isValid() const; bool isValid() const;
// Returns the number of statements in the result. // Returns the number of statements in the result.
size_t size() const; 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. // Returns the error message, if an error occurred.
const char* errorMsg() const; const char* errorMsg() const;
@ -33,23 +40,25 @@ namespace hsql {
// Returns the column number of the occurrance of the error in the query. // Returns the column number of the occurrance of the error in the query.
int errorColumn() const; 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. // Gets the SQL statement with the given index.
const SQLStatement* getStatement(int index) const; const SQLStatement* getStatement(int index) const;
// Gets the non const SQL statement with the given index. // Gets the non const SQL statement with the given index.
SQLStatement* getMutableStatement(int index); SQLStatement* getMutableStatement(int index);
// Adds a statement to the result list of statements. // Get the list of all statements.
// SQLParserResult takes ownership of the statement. const std::vector<SQLStatement*>& getStatements() const;
void addStatement(SQLStatement* stmt);
// Set whether parsing was successful. // Returns a copy of the list of all statements in this result.
void setIsValid(bool isValid); // Removes them from this result.
std::vector<SQLStatement*> releaseStatements();
// Set the details of the error, if available.
// Takes ownership of errorMsg.
void setErrorDetails(char* errorMsg, int errorLine, int errorColumn);
// Deletes all statements and other data within the result.
void reset();
private: private:
// List of statements within the result. // List of statements within the result.

View File

@ -161,4 +161,24 @@ TEST(ExecuteStatementTest) {
ASSERT_EQ(stmt->parameters->size(), 2); 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(); TEST_MAIN();