mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser support drop table
This commit is contained in:
@@ -111,12 +111,24 @@ public class SQLStatementParser extends SQLParser {
|
||||
|
||||
if (lexer.token() == Token.TABLE) {
|
||||
lexer.nextToken();
|
||||
SQLName name = this.exprParser.name();
|
||||
statementList.add(new SQLDropTableStatement(name));
|
||||
|
||||
SQLDropTableStatement stmt = new SQLDropTableStatement();
|
||||
|
||||
for (;;) {
|
||||
SQLName name = this.exprParser.name();
|
||||
stmt.getTableNames().add(name);
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.TRUNCATE) {
|
||||
SQLStatement stmt = parseTruncate();
|
||||
statementList.add(stmt);
|
||||
@@ -136,9 +148,9 @@ public class SQLStatementParser extends SQLParser {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public SQLStatement parseTruncate() {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
|
||||
public SQLStatement parseInsert() {
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
@@ -65,6 +66,36 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropTableStatement x) {
|
||||
x.putAttribute("_original_use_mode", modeLocal.get());
|
||||
modeLocal.set(Mode.Insert);
|
||||
|
||||
aliasLocal.set(new HashMap<String, String>());
|
||||
|
||||
String originalTable = currentTableLocal.get();
|
||||
|
||||
for (SQLName name : x.getTableNames()) {
|
||||
String ident = name.toString();
|
||||
currentTableLocal.set(ident);
|
||||
x.putAttribute("_old_local_", originalTable);
|
||||
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(new TableStat.Name(ident), stat);
|
||||
}
|
||||
stat.incrementDropCount();
|
||||
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
if (aliasMap != null) {
|
||||
aliasMap.put(ident, ident);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLInsertStatement x) {
|
||||
|
||||
@@ -21,6 +21,19 @@ public class TableStat {
|
||||
int updateCount = 0;
|
||||
int deleteCount = 0;
|
||||
int insertCount = 0;
|
||||
int dropCount = 0;
|
||||
|
||||
public int getDropCount() {
|
||||
return dropCount;
|
||||
}
|
||||
|
||||
public void incrementDropCount() {
|
||||
dropCount++;
|
||||
}
|
||||
|
||||
public void setDropCount(int dropCount) {
|
||||
this.dropCount = dropCount;
|
||||
}
|
||||
|
||||
public int getSelectCount() {
|
||||
return selectCount;
|
||||
@@ -73,16 +86,19 @@ public class TableStat {
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder(4);
|
||||
if (insertCount > 0) {
|
||||
buf.append("C");
|
||||
buf.append("Insert");
|
||||
}
|
||||
if (updateCount > 0) {
|
||||
buf.append("U");
|
||||
buf.append("Update");
|
||||
}
|
||||
if (selectCount > 0) {
|
||||
buf.append("R");
|
||||
buf.append("Selelct");
|
||||
}
|
||||
if (deleteCount > 0) {
|
||||
buf.append("D");
|
||||
buf.append("Delete");
|
||||
}
|
||||
if (dropCount > 0) {
|
||||
buf.append("Drop");
|
||||
}
|
||||
|
||||
return buf.toString();
|
||||
|
||||
Reference in New Issue
Block a user