improved sql parser.

This commit is contained in:
wenshao
2017-10-26 11:18:54 +08:00
parent 9256b904db
commit e89063a971
21 changed files with 992 additions and 22 deletions
@@ -100,8 +100,11 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
return from;
}
public void setFrom(SQLExpr from) {
this.from = from;
public void setFrom(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this.from = x;
}
public List<SQLExpr> getParameters() {
@@ -138,6 +141,9 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
if (visitor.visit(this)) {
acceptChild(visitor, this.owner);
acceptChild(visitor, this.parameters);
acceptChild(visitor, this.from);
acceptChild(visitor, this.using);
acceptChild(visitor, this._for);
}
visitor.endVisit(this);
@@ -158,6 +164,9 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
if (visitor.visit(this)) {
acceptChild(visitor, this.owner);
acceptChild(visitor, this.parameters);
acceptChild(visitor, this.from);
acceptChild(visitor, this.using);
acceptChild(visitor, this._for);
}
visitor.endVisit(this);
@@ -215,6 +224,7 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
if (target == null) {
return false;
}
for (int i = 0; i < parameters.size(); ++i) {
if (parameters.get(i) == expr) {
parameters.set(i, target);
@@ -222,6 +232,22 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
return true;
}
}
if (from == expr) {
setFrom(target);
return true;
}
if (using == expr) {
setUsing(target);
return true;
}
if (_for == expr) {
setFor(target);
return true;
}
return false;
}
@@ -278,11 +304,11 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
return using;
}
public void setUsing(SQLExpr using) {
if (using != null) {
using.setParent(this);
public void setUsing(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this.using = using;
this.using = x;
}
public SQLExpr getFor() {
@@ -55,6 +55,9 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
protected SQLPartitionBy partitioning;
protected SQLName storedAs;
protected boolean onCommitPreserveRows;
protected boolean onCommitDeleteRows;
public SQLCreateTableStatement(){
}
@@ -786,6 +789,22 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
return true;
}
public boolean renameColumn(String colummName, String newColumnName) {
if (colummName == null || newColumnName == null || newColumnName.length() == 0) {
return false;
}
int columnIndex = columnIndexOf(new SQLIdentifierExpr(colummName));
if (columnIndex == -1) {
return false;
}
SQLColumnDefinition column = (SQLColumnDefinition) tableElementList.get(columnIndex);
column.setName(new SQLIdentifierExpr(newColumnName));
return true;
}
private boolean apply(SQLAlterTableRename item) {
SQLName name = item.getToName();
if (name == null) {
@@ -978,6 +997,9 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
if (comment != null) {
x.setComment(comment.clone());
}
x.onCommitPreserveRows = onCommitPreserveRows;
x.onCommitDeleteRows = onCommitDeleteRows;
}
public SQLName getStoredAs() {
@@ -1001,4 +1023,12 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
return SQLUtils.toSQLString(this, dbType);
}
public boolean isOnCommitPreserveRows() {
return onCommitPreserveRows;
}
public void setOnCommitPreserveRows(boolean onCommitPreserveRows) {
this.onCommitPreserveRows = onCommitPreserveRows;
}
}
@@ -56,9 +56,6 @@ public class OracleCreateTableStatement extends SQLCreateTableStatement implemen
private Integer compressLevel;
private boolean compressForOltp;
private boolean onCommitPreserveRows;
private boolean onCommitDeleteRows;
private Boolean cache;
private DeferredSegmentCreation deferredSegmentCreation;
@@ -125,14 +122,6 @@ public class OracleCreateTableStatement extends SQLCreateTableStatement implemen
this.cache = cache;
}
public boolean isOnCommitPreserveRows() {
return onCommitPreserveRows;
}
public void setOnCommitPreserveRows(boolean onCommitPreserveRows) {
this.onCommitPreserveRows = onCommitPreserveRows;
}
public boolean isOnCommitDeleteRows() {
return onCommitDeleteRows;
}
@@ -801,18 +801,18 @@ public class OracleExprParser extends SQLExprParser {
over.setOrderBy(parseOrderBy());
if (over.getOrderBy() != null) {
OracleAnalyticWindowing windowing = null;
if (lexer.stringVal().equalsIgnoreCase("ROWS")) {
if (lexer.identifierEquals(FnvHash.Constants.ROWS)) {
lexer.nextToken();
windowing = new OracleAnalyticWindowing();
windowing.setType(OracleAnalyticWindowing.Type.ROWS);
} else if (lexer.stringVal().equalsIgnoreCase("RANGE")) {
} else if (lexer.identifierEquals(FnvHash.Constants.RANGE)) {
lexer.nextToken();
windowing = new OracleAnalyticWindowing();
windowing.setType(OracleAnalyticWindowing.Type.RANGE);
}
if (windowing != null) {
if (lexer.stringVal().equalsIgnoreCase("CURRENT")) {
if (lexer.identifierEquals(FnvHash.Constants.CURRENT)) {
lexer.nextToken();
if (lexer.stringVal().equalsIgnoreCase("ROW")) {
lexer.nextToken();
@@ -821,7 +821,7 @@ public class OracleExprParser extends SQLExprParser {
}
throw new ParserException("syntax error. " + lexer.info());
}
if (lexer.stringVal().equalsIgnoreCase("UNBOUNDED")) {
if (lexer.identifierEquals(FnvHash.Constants.UNBOUNDED)) {
lexer.nextToken();
if (lexer.stringVal().equalsIgnoreCase("PRECEDING")) {
lexer.nextToken();
@@ -829,6 +829,12 @@ public class OracleExprParser extends SQLExprParser {
} else {
throw new ParserException("syntax error. " + lexer.info());
}
} else {
SQLExpr expr = this.expr();
windowing.setExpr(expr);
acceptIdentifier("PRECEDING");
over.setWindowingPreceding(true);
}
over.setWindowing(windowing);
@@ -116,6 +116,10 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
visit(windowing);
}
if (x.isWindowingPreceding()) {
print0(ucase ? " PRECEDING" : " preceding");
}
print(')');
return false;
@@ -1310,6 +1310,11 @@ public class SQLExprParser extends SQLParser {
case DO:
case JOIN:
case TYPE:
case FUNCTION:
case KEY:
case SCHEMA:
case INTERVAL:
case EXPLAIN:
identName = lexer.stringVal();
lexer.nextToken();
break;
@@ -353,7 +353,7 @@ public class SQLStatementParser extends SQLParser {
continue;
}
if (lexer.identifierEquals("COMMIT")) {
if (lexer.identifierEquals(FnvHash.Constants.COMMIT)) {
SQLStatement stmt = parseCommit();
statementList.add(stmt);
@@ -570,6 +570,7 @@ public final class FnvHash {
long ISOPEN = fnv1a_64_lower("ISOPEN");
long CONFLICT = fnv1a_64_lower("CONFLICT");
long NOTHING = fnv1a_64_lower("NOTHING");
long COMMIT = fnv1a_64_lower("COMMIT");
long RS = fnv1a_64_lower("RS");
long RR = fnv1a_64_lower("RR");