Add stringLength information to hsql::Statement

This commit is contained in:
Lawrence 2018-01-17 15:43:38 +01:00
parent 59140d573e
commit 77e396703f
8 changed files with 579 additions and 535 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 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;
@ -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;
};
@ -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:

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();