Support escaped strings (e.g., 'Max O''Mustermann') (#104)

* Support escaped strings (e.g., 'Max O''Mustermann')

* review
This commit is contained in:
mrks 2018-11-12 15:35:31 +01:00 committed by GitHub
parent a59deb43c3
commit 62d162579a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1314 additions and 1283 deletions

File diff suppressed because it is too large Load Diff

View File

@ -457,7 +457,8 @@ void yyfree ( void * , yyscan_t yyscanner );
#ifdef YY_HEADER_EXPORT_START_CONDITIONS
#define INITIAL 0
#define COMMENT 1
#define singlequotedstring 1
#define COMMENT 2
#endif
@ -729,9 +730,9 @@ extern int yylex \
#undef yyTABLES_NAME
#endif
#line 231 "flex_lexer.l"
#line 236 "flex_lexer.l"
#line 735 "flex_lexer.h"
#line 736 "flex_lexer.h"
#undef hsql_IN_HEADER
#endif /* hsql_HEADER_H */

View File

@ -13,10 +13,15 @@
#include "../sql/Expr.h"
#include "bison_parser.h"
#include <stdio.h>
#include <sstream>
#define TOKEN(name) { return SQL_##name; }
static std::stringstream strbuf;
%}
%x singlequotedstring
/***************************
** Section 2: Rules
***************************/
@ -219,11 +224,11 @@ FALSE TOKEN(FALSE)
return SQL_IDENTIFIER;
}
'[^'\n]*' {
// Crop the leading and trailing quote char
yylval->sval = hsql::substr(yytext, 1, strlen(yytext)-1);
return SQL_STRING;
}
\' { BEGIN singlequotedstring; strbuf = std::stringstream{}; }
<singlequotedstring>\'\' { strbuf << '\''; }
<singlequotedstring>[^']* { strbuf << yytext; }
<singlequotedstring>\' { BEGIN 0; yylval->sval = strdup(strbuf.str().c_str()); return SQL_STRING; }
<singlequotedstring><<EOF>> { fprintf(stderr, "[SQL-Lexer-Error] Unterminated string\n"); return 0; }
. { fprintf(stderr, "[SQL-Lexer-Error] Unknown Character: %c\n", yytext[0]); return 0; }

View File

@ -61,7 +61,7 @@ TEST(CreateStatementTest) {
TEST(UpdateStatementTest) {
SQLParserResult result;
SQLParser::parse("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max Mustermann';", &result);
SQLParser::parse("UPDATE students SET grade = 5.0, name = 'test' WHERE name = 'Max O''Mustermann';", &result);
ASSERT(result.isValid());
ASSERT_EQ(result.size(), 1);
@ -84,7 +84,7 @@ TEST(UpdateStatementTest) {
ASSERT(stmt->where->isType(kExprOperator));
ASSERT_EQ(stmt->where->opType, kOpEquals);
ASSERT_STREQ(stmt->where->expr->name, "name");
ASSERT_STREQ(stmt->where->expr2->name, "Max Mustermann");
ASSERT_STREQ(stmt->where->expr2->name, "Max O'Mustermann");
}