improved sql parser.

This commit is contained in:
wenshao
2017-08-24 10:40:09 +08:00
parent 0c46c2a165
commit 026295ddbd
41 changed files with 565 additions and 186 deletions
@@ -177,8 +177,9 @@ public class MySqlExprParser extends SQLExprParser {
}
case CACHE:
case GROUP:
String strVal = lexer.stringVal();
lexer.nextToken();
return primaryRest(new SQLIdentifierExpr(lexer.stringVal()));
return primaryRest(new SQLIdentifierExpr(strVal));
default:
return super.primary();
}
@@ -269,52 +269,47 @@ public class MySqlLexer extends Lexer {
token = Token.IDENTIFIER;
}
} else {
final boolean firstFlag = isFirstIdentifierChar(first);
if (!firstFlag) {
throw new ParserException("illegal identifier. " + info());
}
long hashCode = 0xcbf29ce484222325L;
hashCode ^= ((ch >= 'A' && ch <= 'Z') ? (ch + 32) : ch);
hashCode *= 0x100000001b3L;
mark = pos;
bufPos = 1;
char ch = '\0', last_ch;
char ch = '\0';
for (;;) {
last_ch = ch;
ch = charAt(++pos);
if (!isIdentifierChar(ch)) {
// if (ch == '-' && pos < text.length() - 1) {
// if (mark > 0 && text.charAt(mark - 1) == '.') {
// break;
// }
//
// char next_char = text.charAt(pos + 1);
// if (isIdentifierChar(next_char)) {
// bufPos++;
// continue;
// }
// }
// if (last_ch == '-' && charAt(pos-2) != '-') {
// ch = last_ch;
// bufPos--;
// pos--;
// }
break;
}
bufPos++;
hashCode ^= ((ch >= 'A' && ch <= 'Z') ? (ch + 32) : ch);
hashCode *= 0x100000001b3L;
continue;
}
this.ch = charAt(pos);
stringVal = addSymbol();
Token tok = keywods.getKeyword(stringVal);
Token tok = keywods.getKeyword(hashCode);
if (tok != null) {
token = tok;
} else {
token = Token.IDENTIFIER;
}
if (token == Token.IDENTIFIER) {
stringVal = addSymbol();
} else {
stringVal = null;
}
}
}
@@ -99,10 +99,6 @@ public class MySqlSelectParser extends SQLSelectParser {
this.exprParser.parseHints(queryBlock.getHints());
}
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
}
if (lexer.token() == (Token.DISTINCT)) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
@@ -165,7 +165,7 @@ public class MySqlEvalVisitorImpl extends MySqlASTVisitorAdapter implements SQLE
@Override
public boolean visit(SQLBooleanExpr x) {
x.getAttributes().put(EVAL_VALUE, x.getValue());
x.getAttributes().put(EVAL_VALUE, x.getBooleanValue());
return false;
}
}
@@ -889,7 +889,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
println();
incrementIndent();
print0(ucase ? "WHERE " : "where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
@@ -1252,7 +1251,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print0(ucase ? " WHERE " : " where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -1279,7 +1277,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print0(ucase ? " WHERE " : " where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -1302,7 +1299,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print0(ucase ? " WHERE " : " where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -1355,7 +1351,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print0(ucase ? " WHERE " : " where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -1688,7 +1683,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
println();
incrementIndent();
print0(ucase ? "WHERE " : "where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
@@ -2377,7 +2371,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print0(ucase ? " WHERE " : " where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -2410,7 +2403,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getWhere() != null) {
print0(ucase ? " WHERE " : " where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -3124,7 +3116,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
println();
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
SQLStatement item = x.getStatements().get(i);
item.setParent(x);
item.accept(this);
if (i != size - 1) {
println();
@@ -3151,7 +3142,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
println();
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
SQLStatement item = x.getStatements().get(i);
item.setParent(x);
item.accept(this);
if (i != size - 1) {
println();
@@ -3169,7 +3159,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
println();
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
SQLStatement item = x.getStatements().get(i);
item.setParent(x);
item.accept(this);
if (i != size - 1) {
println();
@@ -3259,7 +3248,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
println();
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
SQLStatement item = x.getStatements().get(i);
item.setParent(x);
item.accept(this);
if (i != size - 1) {
println();
@@ -3583,7 +3571,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
println();
x.getBlock().setParent(x);
x.getBlock().accept(this);
return false;
}
@@ -1179,8 +1179,6 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
@Override
public boolean visit(MySqlDeclareStatement x) {
for (SQLDeclareItem item : x.getVarList()) {
item.setParent(x);
SQLName var = (SQLName) item.getName();
this.variants.put(var.toString(), var);
}
@@ -30,7 +30,8 @@ public class NameResolveVisitor extends OracleASTVisitorAdapter {
public boolean visit(SQLIdentifierExpr x) {
SQLObject parent = x.getParent();
if (parent instanceof SQLBinaryOpExpr) {
if (parent instanceof SQLBinaryOpExpr
&& x.getResolvedColumn() == null) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) parent;
boolean isJoinCondition = binaryOpExpr.getLeft() instanceof SQLName
&& binaryOpExpr.getRight() instanceof SQLName;