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;
@@ -163,7 +163,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
println();
incrementIndent();
print0(ucase ? "WHERE " : "where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
@@ -401,14 +400,12 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
if (x.getFrom() == null) {
print0(ucase ? "DUAL" : "dual");
} else {
x.getFrom().setParent(x);
x.getFrom().accept(this);
}
if (x.getWhere() != null) {
println();
print0(ucase ? "WHERE " : "where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -611,7 +608,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
println();
print0(ucase ? "WHERE " : "where ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
@@ -1155,7 +1151,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
} else {
if (x.getQuery() != null) {
println();
x.getQuery().setParent(x);
x.getQuery().accept(this);
}
}
@@ -1208,7 +1203,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
} else {
if (x.getQuery() != null) {
println();
x.getQuery().setParent(x);
x.getQuery().accept(this);
}
}
@@ -1943,7 +1937,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
println();
}
SQLStatement item = x.getStatements().get(i);
item.setParent(x);
item.accept(this);
}
@@ -3214,7 +3207,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
println();
SQLStatement item = x.getStatements().get(i);
item.setParent(x);
item.accept(this);
}
@@ -213,10 +213,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
}
public boolean visit(OracleSelectQueryBlock x) {
if (x.getWhere() != null) {
x.getWhere().setParent(x);
}
if (x.getInto() instanceof SQLName) {
String tableName = x.getInto().toString();
TableStat stat = getTableStat(tableName);
@@ -742,11 +738,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
setAliasMap();
accept(x.getSubQuery());
for (OracleMultiInsertStatement.Entry entry : x.getEntries()) {
entry.setParent(x);
}
accept(x.getEntries());
return false;
@@ -759,12 +750,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
@Override
public boolean visit(ConditionalInsertClause x) {
for (ConditionalInsertClauseItem item : x.getItems()) {
item.setParent(x);
}
if (x.getElseItem() != null) {
x.getElseItem().setParent(x);
}
return true;
}
@@ -1031,8 +1016,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
@Override
public boolean visit(OracleForStatement x) {
x.getRange().setParent(x);
SQLName index = x.getIndex();
this.getVariants().put(index.toString(), x);
@@ -208,7 +208,6 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.getWhere() != null) {
println();
print0(ucase ? "WHERE " : "where ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
}
@@ -307,7 +306,6 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
println();
print0(ucase ? "WHERE " : "where ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
@@ -412,7 +410,6 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
println();
print0(ucase ? "WHERE " : "where ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}