add parseSQLString method with output parameter instead of return value

This commit is contained in:
Pedro Flemming 2017-04-07 16:16:25 +02:00
parent 074c564cc4
commit e16925e7a5
3 changed files with 40 additions and 19 deletions

View File

@ -12,37 +12,49 @@ namespace hsql {
fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n"); fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n");
} }
SQLParserResult* SQLParser::parseSQLString(const char* text) { // static
SQLParserResult* result = new SQLParserResult(); bool SQLParser::parseSQLString(const char* text, SQLParserResult* result) {
yyscan_t scanner; yyscan_t scanner;
YY_BUFFER_STATE state; YY_BUFFER_STATE state;
if (hsql_lex_init(&scanner)) { if (hsql_lex_init(&scanner)) {
// Couldn't initialize the lexer. // Couldn't initialize the lexer.
fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n"); fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n");
delete result; return false;
return NULL;
} }
state = hsql__scan_string(text, scanner); state = hsql__scan_string(text, scanner);
// Parser and return early if it failed. // Parse the tokens.
if (hsql_parse(result, scanner)) { // If parsing fails, the result will contain an error object.
// Returns an error stmt object. hsql_parse(result, scanner);
hsql__delete_buffer(state, scanner);
hsql_lex_destroy(scanner);
return result;
}
hsql__delete_buffer(state, scanner); hsql__delete_buffer(state, scanner);
hsql_lex_destroy(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; return result;
} }
// static
SQLParserResult* SQLParser::parseSQLString(const std::string& text) { SQLParserResult* SQLParser::parseSQLString(const std::string& text) {
return parseSQLString(text.c_str()); return parseSQLString(text.c_str());
} }
} // namespace hsql
} // namespace hsql

View File

@ -9,10 +9,20 @@ namespace hsql {
// Static methods used to parse SQL strings. // Static methods used to parse SQL strings.
class SQLParser { class SQLParser {
public: 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. // 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); static SQLParserResult* parseSQLString(const char* sql);
// Parses an SQL std::string. // 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); static SQLParserResult* parseSQLString(const std::string& sql);
private: private:

View File

@ -79,23 +79,22 @@ TEST(AutoGrammarTest) {
start = std::chrono::system_clock::now(); start = std::chrono::system_clock::now();
// Parsing // Parsing
SQLParserResult* result = SQLParser::parseSQLString(sql.c_str()); SQLParserResult result;
SQLParser::parseSQLString(sql.c_str(), &result);
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 == result->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", 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()); printf("\t%s\n", sql.c_str());
numFailed++; numFailed++;
} else { } else {
// TODO: indicate whether expectFalse was set // TODO: indicate whether expectFalse was set
printf("\033[0;32m{ ok} (%.1fus)\033[0m %s\n", us, sql.c_str()); printf("\033[0;32m{ ok} (%.1fus)\033[0m %s\n", us, sql.c_str());
} }
delete result;
} }
if (numFailed == 0) { if (numFailed == 0) {