mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
mysql parser support alter table drop key
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLAlterTableDropKey extends SQLObjectImpl implements SQLAlterTableItem {
|
||||
|
||||
private SQLName keyName;
|
||||
|
||||
public SQLName getKeyName() {
|
||||
return keyName;
|
||||
}
|
||||
|
||||
public void setKeyName(SQLName keyName) {
|
||||
this.keyName = keyName;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if(visitor.visit(this)) {
|
||||
acceptChild(visitor, keyName);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -44,6 +44,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropForeignKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableKeys;
|
||||
@@ -2602,6 +2603,12 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
SQLAlterTableDropForeignKey item = new SQLAlterTableDropForeignKey();
|
||||
item.setIndexName(indexName);
|
||||
stmt.getItems().add(item);
|
||||
} else if (lexer.token() == Token.KEY) {
|
||||
lexer.nextToken();
|
||||
SQLName keyName = this.exprParser.name();
|
||||
SQLAlterTableDropKey item = new SQLAlterTableDropKey();
|
||||
item.setKeyName(keyName);
|
||||
stmt.getItems().add(item);
|
||||
} else if (lexer.token() == Token.PRIMARY) {
|
||||
lexer.nextToken();
|
||||
accept(Token.KEY);
|
||||
|
||||
@@ -82,6 +82,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropForeignKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPartition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint;
|
||||
@@ -1810,6 +1811,13 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
print0(ucase ? "DROP PRIMARY KEY" : "drop primary key");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableDropKey x) {
|
||||
print0(ucase ? "DROP KEY " : "drop key ");
|
||||
x.getKeyName().accept(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableEnableKeys x) {
|
||||
|
||||
@@ -67,6 +67,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropForeignKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPartition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint;
|
||||
@@ -676,4 +677,8 @@ public interface SQLASTVisitor {
|
||||
boolean visit(SQLBlockStatement x);
|
||||
|
||||
void endVisit(SQLBlockStatement x);
|
||||
|
||||
boolean visit(SQLAlterTableDropKey x);
|
||||
|
||||
void endVisit(SQLAlterTableDropKey x);
|
||||
}
|
||||
|
||||
@@ -68,6 +68,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropForeignKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPartition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint;
|
||||
@@ -1354,5 +1355,15 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
public void endVisit(SQLBlockStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableDropKey x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLAlterTableDropKey x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.bvt.sql.mysql;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public class MySqlAlterTableDropKey extends TestCase {
|
||||
|
||||
public void test_alter_first() throws Exception {
|
||||
String sql = "alter table t6 drop key v";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("ALTER TABLE t6" + //
|
||||
"\n\tDROP KEY v", output);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user