mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
oracle sql parser support alter sequence.
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright 1999-2017 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.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLAlterSequenceStatement extends SQLStatementImpl implements SQLAlterStatement {
|
||||
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 SQLExpr cacheValue;
|
||||
|
||||
private Boolean order;
|
||||
|
||||
@Override
|
||||
public void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, name);
|
||||
acceptChild(visitor, startWith);
|
||||
acceptChild(visitor, incrementBy);
|
||||
acceptChild(visitor, minValue);
|
||||
acceptChild(visitor, maxValue);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SQLObject> getChildren() {
|
||||
List<SQLObject> children = new ArrayList<SQLObject>();
|
||||
if (name != null) {
|
||||
children.add(name);
|
||||
}
|
||||
if (startWith != null) {
|
||||
children.add(startWith);
|
||||
}
|
||||
if (incrementBy != null) {
|
||||
children.add(incrementBy);
|
||||
}
|
||||
if (minValue != null) {
|
||||
children.add(minValue);
|
||||
}
|
||||
if (maxValue != null) {
|
||||
children.add(maxValue);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getSchema() {
|
||||
SQLName name = getName();
|
||||
if (name == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (name instanceof SQLPropertyExpr) {
|
||||
return ((SQLPropertyExpr) name).getOwnernName();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public SQLExpr getCacheValue() {
|
||||
return cacheValue;
|
||||
}
|
||||
|
||||
public void setCacheValue(SQLExpr cacheValue) {
|
||||
if (cacheValue != null) {
|
||||
cacheValue.setParent(this);
|
||||
}
|
||||
this.cacheValue = cacheValue;
|
||||
}
|
||||
}
|
||||
@@ -1430,6 +1430,9 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
} else if (lexer.token() == Token.FUNCTION) {
|
||||
lexer.reset(savePoint);
|
||||
return parseAlterFunction();
|
||||
} else if (lexer.token() == Token.SEQUENCE) {
|
||||
lexer.reset(savePoint);
|
||||
return parseAlterSequence();
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.TYPE)) {
|
||||
lexer.reset(savePoint);
|
||||
return parseAlterType();
|
||||
@@ -2518,6 +2521,78 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLAlterSequenceStatement parseAlterSequence() {
|
||||
accept(Token.ALTER);
|
||||
|
||||
accept(Token.SEQUENCE);
|
||||
|
||||
SQLAlterSequenceStatement stmt = new SQLAlterSequenceStatement();
|
||||
stmt.setDbType(dbType);
|
||||
stmt.setName(this.exprParser.name());
|
||||
|
||||
for (;;) {
|
||||
if (lexer.token() == Token.START) {
|
||||
lexer.nextToken();
|
||||
accept(Token.WITH);
|
||||
stmt.setStartWith(this.exprParser.expr());
|
||||
continue;
|
||||
} else if (lexer.identifierEquals("INCREMENT")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
stmt.setIncrementBy(this.exprParser.expr());
|
||||
continue;
|
||||
} else if (lexer.token() == Token.CACHE) {
|
||||
lexer.nextToken();
|
||||
stmt.setCache(Boolean.TRUE);
|
||||
|
||||
if (lexer.token() == Token.LITERAL_INT || lexer.token() == Token.QUES) {
|
||||
stmt.setCacheValue(this.exprParser.primary());
|
||||
}
|
||||
|
||||
continue;
|
||||
} else if (lexer.token() == Token.NOCACHE) {
|
||||
lexer.nextToken();
|
||||
stmt.setCache(Boolean.FALSE);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.ORDER) {
|
||||
lexer.nextToken();
|
||||
stmt.setOrder(Boolean.TRUE);
|
||||
continue;
|
||||
} else if (lexer.identifierEquals("NOORDER")) {
|
||||
lexer.nextToken();
|
||||
stmt.setOrder(Boolean.FALSE);
|
||||
continue;
|
||||
} else if (lexer.identifierEquals("CYCLE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setCycle(Boolean.TRUE);
|
||||
continue;
|
||||
} else if (lexer.identifierEquals("NOCYCLE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setCycle(Boolean.FALSE);
|
||||
continue;
|
||||
} else if (lexer.identifierEquals("MINVALUE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMinValue(this.exprParser.expr());
|
||||
continue;
|
||||
} else if (lexer.identifierEquals("MAXVALUE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setMaxValue(this.exprParser.expr());
|
||||
continue;
|
||||
} else if (lexer.identifierEquals("NOMAXVALUE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setNoMaxValue(true);
|
||||
continue;
|
||||
} else if (lexer.identifierEquals("NOMINVALUE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setNoMinValue(true);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLCreateSequenceStatement parseCreateSequence(boolean acceptCreate) {
|
||||
if (acceptCreate) {
|
||||
accept(Token.CREATE);
|
||||
|
||||
@@ -5438,6 +5438,86 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterSequenceStatement x) {
|
||||
print0(ucase ? "ALTER SEQUENCE " : "alter 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()) {
|
||||
if (JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
print0(ucase ? " NO MAXVALUE" : " no maxvalue");
|
||||
} else {
|
||||
print0(ucase ? " NOMAXVALUE" : " nomaxvalue");
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getMinValue() != null) {
|
||||
print0(ucase ? " MINVALUE " : " minvalue ");
|
||||
x.getMinValue().accept(this);
|
||||
}
|
||||
|
||||
if (x.isNoMinValue()) {
|
||||
if (JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
print0(ucase ? " NO MINVALUE" : " no minvalue");
|
||||
} else {
|
||||
print0(ucase ? " NOMINVALUE" : " nominvalue");
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getCycle() != null) {
|
||||
if (x.getCycle().booleanValue()) {
|
||||
print0(ucase ? " CYCLE" : " cycle");
|
||||
} else {
|
||||
if (JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
print0(ucase ? " NO CYCLE" : " no cycle");
|
||||
} else {
|
||||
print0(ucase ? " NOCYCLE" : " nocycle");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Boolean cache = x.getCache();
|
||||
if (cache != null) {
|
||||
if (cache.booleanValue()) {
|
||||
print0(ucase ? " CACHE" : " cache");
|
||||
|
||||
SQLExpr cacheValue = x.getCacheValue();
|
||||
if (cacheValue != null) {
|
||||
print(' ');
|
||||
cacheValue.accept(this);
|
||||
}
|
||||
} else {
|
||||
print0(ucase ? " NOCACHE" : " nocache");
|
||||
}
|
||||
}
|
||||
|
||||
Boolean order = x.getOrder();
|
||||
if (order != null) {
|
||||
if (order.booleanValue()) {
|
||||
print0(ucase ? " ORDER" : " order");
|
||||
} else {
|
||||
print0(ucase ? " NOORDER" : " noorder");
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLDateExpr x) {
|
||||
SQLExpr literal = x.getLiteral();
|
||||
print0(ucase ? "DATE " : "date ");
|
||||
|
||||
@@ -820,4 +820,7 @@ public interface SQLASTVisitor {
|
||||
|
||||
boolean visit(SQLAlterTableRenameIndex x);
|
||||
void endVisit(SQLAlterTableRenameIndex x);
|
||||
|
||||
boolean visit(SQLAlterSequenceStatement x);
|
||||
void endVisit(SQLAlterSequenceStatement x);
|
||||
}
|
||||
|
||||
@@ -1934,6 +1934,16 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterSequenceStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLAlterSequenceStatement x) {
|
||||
|
||||
}
|
||||
|
||||
public final boolean isEnabled(VisitorFeature feature) {
|
||||
return VisitorFeature.isEnabled(this.features, feature);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 1999-2017 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.bvt.sql.oracle.alter;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class OracleAlterSequence_0 extends TestCase {
|
||||
|
||||
public void test_alter_seq_0() throws Exception {
|
||||
String sql = "ALTER SEQUENCE SEQ_XXX INCREMENT BY 2";
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toOracleString(stmt);
|
||||
Assert.assertEquals("ALTER SEQUENCE SEQ_XXX INCREMENT BY 2", output);
|
||||
}
|
||||
|
||||
public void test_alter_seq_1() throws Exception {
|
||||
String sql = "ALTER SEQUENCE customers_seq \n" +
|
||||
" MAXVALUE 1500;";
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toOracleString(stmt);
|
||||
Assert.assertEquals("ALTER SEQUENCE customers_seq MAXVALUE 1500;", output);
|
||||
}
|
||||
|
||||
public void test_alter_seq_2() throws Exception {
|
||||
String sql = "ALTER SEQUENCE customers_seq \n" +
|
||||
" CYCLE\n" +
|
||||
" CACHE 5;";
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toOracleString(stmt);
|
||||
Assert.assertEquals("ALTER SEQUENCE customers_seq CYCLE CACHE 5;", output);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user