sql-parser
sql-parser copied to clipboard
whereClause bug?
Hello,All When I run this code:
hsql::SelectStatement* stmt;
hsql::SQLParserResult result;
// The sql to parse
std::string sql("SELECT name FROM foo WHERE name = \"test\";");
SQLParser::parse(sql, &result);
stmt = (hsql::SelectStatement*)result.getStatement(0);
std::cout << stmt->whereClause->expr2->name << std::endl;
// I hope expr2 type is kExprLiteralString
std::cout << "kExprColumnRef " << stmt->whereClause->expr2->isType(kExprColumnRef) << std::endl;
return 0;
But result is:
test
kExprColumnRef 1
I can't understand this output,Is this bug? I hope someone can tell me why this code get so strange output.
flex_lexer.l define the identifier is:
\"[^\"\n]+\" {
// Crop the leading and trailing quote char
yylval->sval = hsql::substr(yytext, 1, strlen(yytext)-1);
return SQL_IDENTIFIER;
}
change to
\"[^\"\n]+\" {
// Crop the leading and trailing quote char
yylval->sval = hsql::substr(yytext, 0, strlen(yytext));
return SQL_IDENTIFIER;
}
will solve my problem
I don't know whether this change is right,need your comment!
Have a look at the example binary and/or printStatementInfo
:
./a.out "SELECT name FROM foo WHERE name = \"test\""
Parsed successfully!
Number of statements: 1
SelectStatement
Fields:
name
Sources:
foo
Search Conditions:
=
name
test
stmt->whereClause points to a kExprOperator. Only that iterator has the column and the search string.
@mrks Hi,In your case,When you try to get "test"
’s type, you will get kExprColumnRef type;This is my problem.I think "test"
type should be kExprLiteralString.
Ah, sorry. I misunderstood your issue. This is expected and, as far as I understand it, correct. If you look at the SQL documentation, you will find that <double quote>
is used for <delimited identifier>
s while a <character string literal>
uses single quotes.
Some DBMSs (MySQL, sqlite) are more lenient when it comes to the quote type, as you can check on SQLFiddle.