Merge pull request #77 from lawben/master

Add stringLength information to Statement
This commit is contained in:
mrks 2018-01-18 10:04:10 +01:00 committed by GitHub
commit fe51c9e4f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 589 additions and 545 deletions

View File

@ -60,7 +60,7 @@ $(LIB_BUILD): $(LIB_OBJ)
$(LIBLINKER) $(LIB_LFLAGS) $(LIB_BUILD) $(LIB_OBJ)
$(SRCPARSER)/flex_lexer.o: $(SRCPARSER)/flex_lexer.cpp $(SRCPARSER)/bison_parser.cpp
$(CXX) $(LIB_CFLAGS) -c -o $@ $< -Wno-sign-compare -Wno-unneeded-internal-declaration
$(CXX) $(LIB_CFLAGS) -c -o $@ $< -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-register
%.o: %.cpp $(PARSER_CPP) $(LIB_H)
$(CXX) $(LIB_CFLAGS) -c -o $@ $<

View File

@ -1,9 +1,11 @@
# bison's version is too old on OSX, allow user to pass in custom path
BISON?=bison
all: bison_parser.cpp flex_lexer.cpp
bison_parser.cpp: bison_parser.y
@bison --version | head -n 1
bison bison_parser.y --output=bison_parser.cpp --defines=bison_parser.h --verbose
@$(BISON) --version | head -n 1
$(BISON) bison_parser.y --output=bison_parser.cpp --defines=bison_parser.h --verbose
flex_lexer.cpp: flex_lexer.l
@flex --version
@ -14,4 +16,4 @@ clean:
# Tests if the parser builds correctly and doesn't contain conflicts.
test:
! bison bison_parser.y -v --output=conflict_test.cpp 2>&1 | grep "conflict" >&2
! $(BISON) bison_parser.y -v --output=conflict_test.cpp 2>&1 | grep "conflict" >&2

File diff suppressed because it is too large Load Diff

View File

@ -48,7 +48,7 @@
extern int hsql_debug;
#endif
/* "%code requires" blocks. */
#line 35 "bison_parser.y" /* yacc.c:1909 */
#line 35 "bison_parser.y" /* yacc.c:1915 */
// %code requires block
@ -62,6 +62,7 @@ extern int hsql_debug;
yylloc->first_column = yylloc->last_column; \
for(int i = 0; yytext[i] != '\0'; i++) { \
yylloc->total_column++; \
yylloc->string_length++; \
if(yytext[i] == '\n') { \
yylloc->last_line++; \
yylloc->last_column = 0; \
@ -71,7 +72,7 @@ extern int hsql_debug;
} \
}
#line 75 "bison_parser.h" /* yacc.c:1909 */
#line 76 "bison_parser.h" /* yacc.c:1915 */
/* Token type. */
#ifndef HSQL_TOKENTYPE
@ -217,7 +218,7 @@ extern int hsql_debug;
union HSQL_STYPE
{
#line 93 "bison_parser.y" /* yacc.c:1909 */
#line 95 "bison_parser.y" /* yacc.c:1915 */
double fval;
int64_t ival;
@ -236,7 +237,7 @@ union HSQL_STYPE
hsql::PrepareStatement* prep_stmt;
hsql::ExecuteStatement* exec_stmt;
hsql::ShowStatement* show_stmt;
hsql::TableName table_name;
hsql::TableRef* table;
hsql::Expr* expr;
@ -256,7 +257,7 @@ union HSQL_STYPE
std::vector<hsql::Expr*>* expr_vec;
std::vector<hsql::OrderDescription*>* order_vec;
#line 260 "bison_parser.h" /* yacc.c:1909 */
#line 261 "bison_parser.h" /* yacc.c:1915 */
};
typedef union HSQL_STYPE HSQL_STYPE;

View File

@ -45,6 +45,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
yylloc->first_column = yylloc->last_column; \
for(int i = 0; yytext[i] != '\0'; i++) { \
yylloc->total_column++; \
yylloc->string_length++; \
if(yytext[i] == '\n') { \
yylloc->last_line++; \
yylloc->last_column = 0; \
@ -76,6 +77,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
@$.first_line = 0;
@$.last_line = 0;
@$.total_column = 0;
@$.string_length = 0;
};
@ -108,7 +110,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
hsql::PrepareStatement* prep_stmt;
hsql::ExecuteStatement* exec_stmt;
hsql::ShowStatement* show_stmt;
hsql::TableName table_name;
hsql::TableRef* table;
hsql::Expr* expr;
@ -133,7 +135,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
/*********************************
** Descrutor symbols
*********************************/
%destructor { } <fval> <ival> <uval> <bval> <order_type>
%destructor { } <fval> <ival> <uval> <bval> <order_type>
%destructor { free( ($$.name) ); free( ($$.schema) ); } <table_name>
%destructor { free( ($$) ); } <sval>
%destructor {
@ -185,7 +187,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
%type <delete_stmt> delete_statement truncate_statement
%type <update_stmt> update_statement
%type <drop_stmt> drop_statement
%type <show_stmt> show_statement
%type <show_stmt> show_statement
%type <table_name> table_name
%type <sval> opt_alias alias file_path prepare_target_query
%type <bval> opt_not_exists opt_exists opt_distinct
@ -263,8 +265,18 @@ input:
statement_list:
statement { $$ = new std::vector<SQLStatement*>(); $$->push_back($1); }
| statement_list ';' statement { $1->push_back($3); $$ = $1; }
statement {
$1->stringLength = yylloc.string_length;
yylloc.string_length = 0;
$$ = new std::vector<SQLStatement*>();
$$->push_back($1);
}
| statement_list ';' statement {
$3->stringLength = yylloc.string_length;
yylloc.string_length = 0;
$1->push_back($3);
$$ = $1;
}
;
statement:
@ -471,7 +483,7 @@ opt_exists:
IF EXISTS { $$ = true; }
| /* empty */ { $$ = false; }
;
/******************************
* Delete Statement / Truncate statement
* DELETE FROM students WHERE grade > 3.0

View File

@ -22,6 +22,9 @@ struct HSQL_CUST_LTYPE {
int total_column;
// Length of the string in the SQL query string
int string_length;
// Parameters.
// int param_id;
std::vector<void*> param_list;

View File

@ -37,6 +37,9 @@ namespace hsql {
// Shorthand for isType(type).
bool is(StatementType type) const;
// Length of the string in the SQL query string
size_t stringLength;
std::vector<Expr*>* hints;
private:

View File

@ -213,4 +213,15 @@ TEST(HintTest) {
ASSERT_EQ(10, stmt->hints->at(1)->exprList->at(0)->ival);
}
TEST(StringLengthTest) {
TEST_PARSE_SQL_QUERY(
"SELECT * FROM bar; INSERT INTO foo VALUES (4);\t\n SELECT * FROM foo;",
result,
3);
ASSERT_EQ(result.getStatement(0)->stringLength, 18);
ASSERT_EQ(result.getStatement(1)->stringLength, 28);
ASSERT_EQ(result.getStatement(2)->stringLength, 21);
}
TEST_MAIN();