mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser support drop database
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLDropDatabaseStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
|
||||
private SQLExpr database;
|
||||
private boolean ifExists;
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, database);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public void setDatabase(SQLExpr database) {
|
||||
if (database != null) {
|
||||
database.setParent(this);
|
||||
}
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public boolean isIfExists() {
|
||||
return ifExists;
|
||||
}
|
||||
|
||||
public void setIfExists(boolean ifExists) {
|
||||
this.ifExists = ifExists;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,6 +43,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropSequenceStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
@@ -180,6 +181,10 @@ public class SQLStatementParser extends SQLParser {
|
||||
SQLStatement stmt = parseDropTrigger(false);
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.DATABASE) {
|
||||
SQLStatement stmt = parseDropDatabase(false);
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
@@ -767,6 +772,28 @@ public class SQLStatementParser extends SQLParser {
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
protected SQLDropDatabaseStatement parseDropDatabase(boolean acceptDrop) {
|
||||
if (acceptDrop) {
|
||||
accept(Token.DROP);
|
||||
}
|
||||
|
||||
SQLDropDatabaseStatement stmt = new SQLDropDatabaseStatement();
|
||||
|
||||
accept(Token.DATABASE);
|
||||
|
||||
if (lexer.token() == Token.IF) {
|
||||
lexer.nextToken();
|
||||
accept(Token.EXISTS);
|
||||
stmt.setIfExists(true);
|
||||
}
|
||||
|
||||
SQLName name = this.exprParser.name();
|
||||
stmt.setDatabase(name);
|
||||
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLStatement parseTruncate() {
|
||||
accept(Token.TRUNCATE);
|
||||
|
||||
@@ -95,6 +95,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropSequenceStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
@@ -1723,12 +1724,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
|
||||
if (x.getOn() != null) {
|
||||
print(" ON ");
|
||||
|
||||
|
||||
if (x.getObjectType() != null) {
|
||||
print(x.getObjectType().name());
|
||||
print(' ');
|
||||
}
|
||||
|
||||
|
||||
x.getOn().accept(this);
|
||||
}
|
||||
|
||||
@@ -1773,7 +1774,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
print(" MAX_USER_CONNECTIONS ");
|
||||
x.getMaxUserConnections().accept(this);
|
||||
}
|
||||
|
||||
|
||||
if (x.isAdminOption()) {
|
||||
if (!with) {
|
||||
print(" WITH");
|
||||
@@ -1784,4 +1785,17 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropDatabaseStatement x) {
|
||||
print("DROP DATABASE ");
|
||||
|
||||
if (x.isIfExists()) {
|
||||
print("IF EXISTS ");
|
||||
}
|
||||
|
||||
x.getDatabase().accept(this);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropSequenceStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
@@ -509,4 +510,8 @@ public interface SQLASTVisitor {
|
||||
void endVisit(SQLGrantStatement x);
|
||||
|
||||
boolean visit(SQLGrantStatement x);
|
||||
|
||||
void endVisit(SQLDropDatabaseStatement x);
|
||||
|
||||
boolean visit(SQLDropDatabaseStatement x);
|
||||
}
|
||||
|
||||
@@ -80,6 +80,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropSequenceStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
@@ -986,4 +987,14 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
public boolean visit(SQLGrantStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLDropDatabaseStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropDatabaseStatement x) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropSequenceStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
@@ -1266,4 +1267,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
public boolean visit(SQLGrantStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropDatabaseStatement x) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user