improved sql parser, support apache phoenix.

This commit is contained in:
wenshao
2016-09-14 15:05:12 +08:00
parent 49d7884c96
commit 77ee426082
33 changed files with 717 additions and 359 deletions
@@ -1,129 +0,0 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.oracle.ast.stmt;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
public class OracleCreateSequenceStatement extends OracleStatementImpl {
private SQLName name;
private SQLExpr startWith;
private SQLExpr incrementBy;
private SQLExpr minValue;
private SQLExpr maxValue;
private boolean noMaxValue;
private boolean noMinValue;
private Boolean cycle;
private Boolean cache;
private Boolean order;
@Override
public void accept0(OracleASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
acceptChild(visitor, startWith);
acceptChild(visitor, incrementBy);
acceptChild(visitor, minValue);
acceptChild(visitor, maxValue);
}
visitor.endVisit(this);
}
public SQLName getName() {
return name;
}
public void setName(SQLName name) {
this.name = name;
}
public SQLExpr getStartWith() {
return startWith;
}
public void setStartWith(SQLExpr startWith) {
this.startWith = startWith;
}
public SQLExpr getIncrementBy() {
return incrementBy;
}
public void setIncrementBy(SQLExpr incrementBy) {
this.incrementBy = incrementBy;
}
public SQLExpr getMaxValue() {
return maxValue;
}
public void setMaxValue(SQLExpr maxValue) {
this.maxValue = maxValue;
}
public Boolean getCycle() {
return cycle;
}
public void setCycle(Boolean cycle) {
this.cycle = cycle;
}
public Boolean getCache() {
return cache;
}
public void setCache(Boolean cache) {
this.cache = cache;
}
public Boolean getOrder() {
return order;
}
public void setOrder(Boolean order) {
this.order = order;
}
public SQLExpr getMinValue() {
return minValue;
}
public void setMinValue(SQLExpr minValue) {
this.minValue = minValue;
}
public boolean isNoMaxValue() {
return noMaxValue;
}
public void setNoMaxValue(boolean noMaxValue) {
this.noMaxValue = noMaxValue;
}
public boolean isNoMinValue() {
return noMinValue;
}
public void setNoMinValue(boolean noMinValue) {
this.noMinValue = noMinValue;
}
}
@@ -51,7 +51,6 @@ import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCommitStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateDatabaseDbLinkStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateIndexStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateSequenceStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDropDbLinkStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleExceptionStatement;
@@ -1439,14 +1438,15 @@ public class OracleStatementParser extends SQLStatementParser {
return stmt;
}
public OracleCreateSequenceStatement parseCreateSequence(boolean acceptCreate) {
public SQLCreateSequenceStatement parseCreateSequence(boolean acceptCreate) {
if (acceptCreate) {
accept(Token.CREATE);
}
accept(Token.SEQUENCE);
OracleCreateSequenceStatement stmt = new OracleCreateSequenceStatement();
SQLCreateSequenceStatement stmt = new SQLCreateSequenceStatement();
stmt.setDbType(JdbcConstants.ORACLE);
stmt.setName(this.exprParser.name());
for (;;) {
@@ -63,7 +63,6 @@ import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCheck;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCommitStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateDatabaseDbLinkStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateIndexStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateSequenceStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateTableStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDropDbLinkStatement;
@@ -447,10 +446,6 @@ public interface OracleASTVisitor extends SQLASTVisitor {
void endVisit(OracleAlterTablespaceStatement x);
boolean visit(OracleCreateSequenceStatement x);
void endVisit(OracleCreateSequenceStatement x);
boolean visit(OracleExitStatement x);
void endVisit(OracleExitStatement x);
@@ -73,7 +73,6 @@ import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCheck;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCommitStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateDatabaseDbLinkStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateIndexStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateSequenceStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateTableStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDropDbLinkStatement;
@@ -1021,16 +1020,6 @@ public class OracleASTVisitorAdapter extends SQLASTVisitorAdapter implements Ora
}
@Override
public boolean visit(OracleCreateSequenceStatement x) {
return true;
}
@Override
public void endVisit(OracleCreateSequenceStatement x) {
}
@Override
public boolean visit(OracleExitStatement x) {
return true;
@@ -94,7 +94,6 @@ import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCommitStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateDatabaseDbLinkStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateIndexStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateSequenceStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateTableStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDropDbLinkStatement;
@@ -2567,63 +2566,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
return false;
}
@Override
public boolean visit(OracleCreateSequenceStatement x) {
print0(ucase ? "CREATE SEQUENCE " : "create sequence ");
x.getName().accept(this);
if (x.getStartWith() != null) {
print0(ucase ? " START WITH " : " start with ");
x.getStartWith().accept(this);
}
if (x.getIncrementBy() != null) {
print0(ucase ? " INCREMENT BY " : " increment by ");
x.getIncrementBy().accept(this);
}
if (x.getMaxValue() != null) {
print0(ucase ? " MAXVALUE " : " maxvalue ");
x.getMaxValue().accept(this);
}
if (x.isNoMaxValue()) {
print0(ucase ? " NOMAXVALUE" : " nomaxvalue");
}
if (x.getMinValue() != null) {
print0(ucase ? " MINVALUE " : " minvalue ");
x.getMinValue().accept(this);
}
if (x.isNoMinValue()) {
print0(ucase ? " NOMINVALUE" : " nominvalue");
}
if (x.getCycle() != null) {
if (x.getCycle().booleanValue()) {
print0(ucase ? " CYCLE" : " cycle");
} else {
print0(ucase ? " NOCYCLE" : " nocycle");
}
}
if (x.getCache() != null) {
if (x.getCache().booleanValue()) {
print0(ucase ? " CACHE" : " cache");
} else {
print0(ucase ? " NOCACHE" : " nocache");
}
}
return false;
}
@Override
public void endVisit(OracleCreateSequenceStatement x) {
}
@Override
public boolean visit(OracleExitStatement x) {
print0(ucase ? "EXIT" : "exit");
@@ -96,7 +96,6 @@ import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCheck;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCommitStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateDatabaseDbLinkStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateIndexStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateSequenceStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreateTableStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDropDbLinkStatement;
@@ -1354,16 +1353,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
}
@Override
public boolean visit(OracleCreateSequenceStatement x) {
return false;
}
@Override
public void endVisit(OracleCreateSequenceStatement x) {
}
@Override
public boolean visit(OracleExitStatement x) {
return true;
@@ -0,0 +1,27 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.ast;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitor;
import com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixASTVisitor;
/**
* Created by wenshao on 16/9/14.
*/
public interface PhoenixObject extends SQLObject {
void accept0(PhoenixASTVisitor visitor);
}
@@ -0,0 +1,46 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.ast;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.dialect.db2.ast.DB2Object;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitor;
import com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.JdbcConstants;
/**
* Created by wenshao on 16/9/14.
*/
public abstract class PhoenixStatementImpl extends SQLStatementImpl implements PhoenixObject {
public PhoenixStatementImpl() {
super(JdbcConstants.PHOENIX);
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor instanceof PhoenixASTVisitor) {
accept0((PhoenixASTVisitor) visitor);
return;
}
super.accept0(visitor);
}
@Override
public abstract void accept0(PhoenixASTVisitor visitor);
}
@@ -0,0 +1,29 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.ast;
import com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixASTVisitor;
/**
* Created by wenshao on 16/9/14.
*/
public class PhoenixUpsertStatement extends PhoenixStatementImpl {
@Override
public void accept0(PhoenixASTVisitor visitor) {
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.parser;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.SQLExprParser;
/**
* Created by wenshao on 16/9/13.
*/
public class PhoenixExprParser extends SQLExprParser {
public PhoenixExprParser(String sql){
this(new PhoenixLexer(sql));
this.lexer.nextToken();
}
public PhoenixExprParser(Lexer lexer){
super(lexer);
}
}
@@ -0,0 +1,56 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.parser;
import com.alibaba.druid.sql.parser.Keywords;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.Token;
import java.util.HashMap;
import java.util.Map;
/**
* Created by wenshao on 16/9/13.
*/
public class PhoenixLexer extends Lexer {
public final static Keywords DEFAULT_PHOENIX_KEYWORDS;
static {
Map<String, Token> map = new HashMap<String, Token>();
map.putAll(Keywords.DEFAULT_KEYWORDS.getKeywords());
map.put("FETCH", Token.FETCH);
map.put("FIRST", Token.FIRST);
map.put("ONLY", Token.ONLY);
map.put("OPTIMIZE", Token.OPTIMIZE);
map.put("OF", Token.OF);
map.put("CONCAT", Token.CONCAT);
map.put("CONTINUE", Token.CONTINUE);
map.put("IDENTITY", Token.IDENTITY);
map.put("MERGE", Token.MERGE);
map.put("USING", Token.USING);
map.put("MATCHED", Token.MATCHED);
map.put("UPSERT", Token.UPSERT);
DEFAULT_PHOENIX_KEYWORDS = new Keywords(map);
}
public PhoenixLexer(String input){
super(input);
super.keywods = DEFAULT_PHOENIX_KEYWORDS;
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.parser;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.parser.Token;
/**
* Created by wenshao on 16/9/13.
*/
public class PhoenixStatementParser extends SQLStatementParser {
public PhoenixStatementParser(String sql) {
super (new PhoenixExprParser(sql));
}
public PhoenixStatementParser(Lexer lexer){
super(new PhoenixExprParser(lexer));
}
}
@@ -0,0 +1,24 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.visitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
/**
* Created by wenshao on 16/9/13.
*/
public interface PhoenixASTVisitor extends SQLASTVisitor {
}
@@ -0,0 +1,27 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.visitor;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
/**
* Created by wenshao on 16/9/13.
*/
public class PhoenixOutputVisitor extends SQLASTOutputVisitor implements PhoenixASTVisitor {
public PhoenixOutputVisitor(Appendable appender){
super(appender);
}
}
@@ -0,0 +1,41 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.visitor;
import com.alibaba.druid.sql.visitor.ParameterizedVisitor;
/**
* Created by wenshao on 16/9/13.
*/
public class PhoenixParameterizedOutputVisitor extends PhoenixOutputVisitor implements ParameterizedVisitor {
private int replaceCount;
public PhoenixParameterizedOutputVisitor(){
this(new StringBuilder());
}
public PhoenixParameterizedOutputVisitor(Appendable appender){
super(appender);
}
public int getReplaceCount() {
return this.replaceCount;
}
public void incrementReplaceCunt() {
replaceCount++;
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.phoenix.visitor;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitor;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
/**
* Created by wenshao on 16/9/13.
*/
public class PhoenixSchemaStatVisitor extends SchemaStatVisitor implements PhoenixASTVisitor {
}