add parseSQLString method with output parameter instead of return value
This commit is contained in:
parent
074c564cc4
commit
e16925e7a5
|
@ -12,37 +12,49 @@ namespace hsql {
|
|||
fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n");
|
||||
}
|
||||
|
||||
SQLParserResult* SQLParser::parseSQLString(const char* text) {
|
||||
SQLParserResult* result = new SQLParserResult();
|
||||
// static
|
||||
bool SQLParser::parseSQLString(const char* text, SQLParserResult* result) {
|
||||
yyscan_t scanner;
|
||||
YY_BUFFER_STATE state;
|
||||
|
||||
if (hsql_lex_init(&scanner)) {
|
||||
// Couldn't initialize the lexer.
|
||||
fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n");
|
||||
delete result;
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
state = hsql__scan_string(text, scanner);
|
||||
|
||||
// Parser and return early if it failed.
|
||||
if (hsql_parse(result, scanner)) {
|
||||
// Returns an error stmt object.
|
||||
hsql__delete_buffer(state, scanner);
|
||||
hsql_lex_destroy(scanner);
|
||||
return result;
|
||||
}
|
||||
// Parse the tokens.
|
||||
// If parsing fails, the result will contain an error object.
|
||||
hsql_parse(result, scanner);
|
||||
|
||||
hsql__delete_buffer(state, scanner);
|
||||
hsql_lex_destroy(scanner);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// static
|
||||
bool SQLParser::parseSQLString(const std::string& text, SQLParserResult* result) {
|
||||
return parseSQLString(text.c_str(), result);
|
||||
}
|
||||
|
||||
// static
|
||||
SQLParserResult* SQLParser::parseSQLString(const char* text) {
|
||||
SQLParserResult* result = new SQLParserResult();
|
||||
|
||||
if (!SQLParser::parseSQLString(text, result)) {
|
||||
delete result;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// static
|
||||
SQLParserResult* SQLParser::parseSQLString(const std::string& text) {
|
||||
return parseSQLString(text.c_str());
|
||||
}
|
||||
|
||||
|
||||
} // namespace hsql
|
|
@ -9,10 +9,20 @@ namespace hsql {
|
|||
// Static methods used to parse SQL strings.
|
||||
class SQLParser {
|
||||
public:
|
||||
// Parses a given constant character SQL string into the result object.
|
||||
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);
|
||||
|
||||
// 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);
|
||||
|
||||
// Parses an SQL std::string.
|
||||
// Note: This is kept for legacy reasons. It is recommended to use
|
||||
// the (const std::string&, SQLParserResult*) implementation.
|
||||
static SQLParserResult* parseSQLString(const std::string& sql);
|
||||
|
||||
private:
|
||||
|
|
|
@ -79,23 +79,22 @@ TEST(AutoGrammarTest) {
|
|||
start = std::chrono::system_clock::now();
|
||||
|
||||
// Parsing
|
||||
SQLParserResult* result = SQLParser::parseSQLString(sql.c_str());
|
||||
SQLParserResult result;
|
||||
SQLParser::parseSQLString(sql.c_str(), &result);
|
||||
|
||||
end = std::chrono::system_clock::now();
|
||||
std::chrono::duration<double> elapsed_seconds = end - start;
|
||||
double us = elapsed_seconds.count() * 1000 * 1000;
|
||||
|
||||
if (expectFalse == result->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", result->errorMsg(), result->errorLine(), result->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 {
|
||||
// TODO: indicate whether expectFalse was set
|
||||
printf("\033[0;32m{ ok} (%.1fus)\033[0m %s\n", us, sql.c_str());
|
||||
}
|
||||
|
||||
delete result;
|
||||
}
|
||||
|
||||
if (numFailed == 0) {
|
||||
|
|
Loading…
Reference in New Issue