2015-12-23 16:01:08 +01:00
|
|
|
|
2014-10-09 01:30:22 +02:00
|
|
|
#include "SQLParser.h"
|
2015-12-23 16:01:08 +01:00
|
|
|
#include "parser/bison_parser.h"
|
|
|
|
#include "parser/flex_lexer.h"
|
2014-10-09 01:30:22 +02:00
|
|
|
#include <stdio.h>
|
2015-05-23 18:39:19 +02:00
|
|
|
#include <string>
|
2014-10-09 01:30:22 +02:00
|
|
|
|
2014-10-31 18:36:02 +01:00
|
|
|
namespace hsql {
|
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
SQLParser::SQLParser() {
|
|
|
|
fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n");
|
|
|
|
}
|
2014-10-09 01:30:22 +02:00
|
|
|
|
2017-04-07 16:16:25 +02:00
|
|
|
// static
|
2017-07-21 02:47:45 +02:00
|
|
|
bool SQLParser::parse(const std::string& sql, SQLParserResult* result) {
|
2017-02-08 02:06:15 +01:00
|
|
|
yyscan_t scanner;
|
|
|
|
YY_BUFFER_STATE state;
|
2014-10-09 01:30:22 +02:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
if (hsql_lex_init(&scanner)) {
|
2017-02-08 02:33:42 +01:00
|
|
|
// Couldn't initialize the lexer.
|
2017-07-21 02:47:45 +02:00
|
|
|
fprintf(stderr, "SQLParser: Error when initializing lexer!\n");
|
2017-04-07 16:16:25 +02:00
|
|
|
return false;
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
2017-07-21 02:47:45 +02:00
|
|
|
const char* text = sql.c_str();
|
2017-02-08 02:06:15 +01:00
|
|
|
state = hsql__scan_string(text, scanner);
|
2014-10-09 01:30:22 +02:00
|
|
|
|
2017-04-07 16:16:25 +02:00
|
|
|
// Parse the tokens.
|
|
|
|
// If parsing fails, the result will contain an error object.
|
2017-04-08 02:37:30 +02:00
|
|
|
int ret = hsql_parse(result, scanner);
|
|
|
|
bool success = (ret == 0);
|
|
|
|
result->setIsValid(success);
|
2014-10-09 01:30:22 +02:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
hsql__delete_buffer(state, scanner);
|
|
|
|
hsql_lex_destroy(scanner);
|
2017-04-07 16:16:25 +02:00
|
|
|
|
|
|
|
return true;
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
2015-01-07 13:24:39 +01:00
|
|
|
|
2017-04-07 16:16:25 +02:00
|
|
|
// static
|
2017-07-21 02:47:45 +02:00
|
|
|
bool SQLParser::parseSQLString(const char* sql, SQLParserResult* result) {
|
|
|
|
return parse(sql, result);
|
2017-04-07 16:16:25 +02:00
|
|
|
}
|
2016-02-27 14:45:59 +01:00
|
|
|
|
2017-07-21 02:47:45 +02:00
|
|
|
bool SQLParser::parseSQLString(const std::string& sql, SQLParserResult* result) {
|
|
|
|
return parse(sql, result);
|
|
|
|
}
|
2017-04-07 16:16:25 +02:00
|
|
|
|
2017-07-21 02:47:45 +02:00
|
|
|
// static
|
|
|
|
bool SQLParser::tokenize(const std::string& sql, std::vector<int16_t>* tokens) {
|
|
|
|
// Initialize the scanner.
|
|
|
|
yyscan_t scanner;
|
|
|
|
if (hsql_lex_init(&scanner)) {
|
|
|
|
fprintf(stderr, "SQLParser: Error when initializing lexer!\n");
|
|
|
|
return false;
|
2017-04-07 16:16:25 +02:00
|
|
|
}
|
|
|
|
|
2017-07-21 02:47:45 +02:00
|
|
|
YY_BUFFER_STATE state;
|
|
|
|
state = hsql__scan_string(sql.c_str(), scanner);
|
2017-04-07 16:16:25 +02:00
|
|
|
|
2017-07-21 02:47:45 +02:00
|
|
|
YYSTYPE yylval;
|
|
|
|
YYLTYPE yylloc;
|
|
|
|
|
|
|
|
// Step through the string until EOF is read.
|
|
|
|
// Note: hsql_lex returns int, but we know that its range is within 16 bit.
|
|
|
|
int16_t token = hsql_lex(&yylval, &yylloc, scanner);
|
|
|
|
while (token != 0) {
|
|
|
|
tokens->push_back(token);
|
|
|
|
token = hsql_lex(&yylval, &yylloc, scanner);
|
|
|
|
|
|
|
|
if (token == SQL_IDENTIFIER || token == SQL_STRING) {
|
|
|
|
free(yylval.sval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hsql__delete_buffer(state, scanner);
|
|
|
|
hsql_lex_destroy(scanner);
|
|
|
|
return true;
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-04-07 16:16:25 +02:00
|
|
|
} // namespace hsql
|