mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sql parser
This commit is contained in:
+58
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.dialect.mysql.ast.statement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
public class MySqlOptimizeStatement extends MySqlStatementImpl {
|
||||
|
||||
private boolean noWriteToBinlog = false;
|
||||
private boolean local = false;
|
||||
|
||||
protected final List<SQLExprTableSource> tableSources = new ArrayList<SQLExprTableSource>();
|
||||
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, tableSources);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public boolean isNoWriteToBinlog() {
|
||||
return noWriteToBinlog;
|
||||
}
|
||||
|
||||
public void setNoWriteToBinlog(boolean noWriteToBinlog) {
|
||||
this.noWriteToBinlog = noWriteToBinlog;
|
||||
}
|
||||
|
||||
public boolean isLocal() {
|
||||
return local;
|
||||
}
|
||||
|
||||
public void setLocal(boolean local) {
|
||||
this.local = local;
|
||||
}
|
||||
|
||||
public List<SQLExprTableSource> getTableSources() {
|
||||
return tableSources;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -50,6 +50,7 @@ public class MySqlLexer extends Lexer {
|
||||
map.put("SHOW", Token.SHOW);
|
||||
map.put("CACHE", Token.CACHE);
|
||||
map.put("ANALYZE", Token.ANALYZE);
|
||||
map.put("OPTIMIZE", Token.OPTIMIZE);
|
||||
|
||||
DEFAULT_MYSQL_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
queryBlock.setLimit(parseLimit());
|
||||
}
|
||||
|
||||
if (identifierEquals("PROCEDURE")) {
|
||||
if (lexer.token() == Token.PROCEDURE) {
|
||||
lexer.nextToken();
|
||||
throw new ParserException("TODO");
|
||||
}
|
||||
|
||||
@@ -85,6 +85,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStat
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement.LockType;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlOptimizeStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement;
|
||||
@@ -506,6 +507,20 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public MySqlOptimizeStatement parseOptimize() {
|
||||
accept(Token.OPTIMIZE);
|
||||
accept(Token.TABLE);
|
||||
|
||||
MySqlOptimizeStatement stmt = new MySqlOptimizeStatement();
|
||||
List<SQLName> names = new ArrayList<SQLName>();
|
||||
this.exprParser.names(names, stmt);
|
||||
|
||||
for (SQLName name : names) {
|
||||
stmt.getTableSources().add(new SQLExprTableSource(name));
|
||||
}
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLStatement parseReset() {
|
||||
acceptIdentifier(RESET);
|
||||
@@ -594,6 +609,12 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.OPTIMIZE) {
|
||||
SQLStatement stmt = parseOptimize();
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (identifierEquals("HELP")) {
|
||||
lexer.nextToken();
|
||||
@@ -910,7 +931,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
if (identifierEquals("FUNCTION")) {
|
||||
if (lexer.token() == Token.FUNCTION) {
|
||||
lexer.nextToken();
|
||||
|
||||
MySqlShowCreateFunctionStatement stmt = new MySqlShowCreateFunctionStatement();
|
||||
@@ -918,7 +939,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
if (identifierEquals("PROCEDURE")) {
|
||||
if (lexer.token() == Token.PROCEDURE) {
|
||||
lexer.nextToken();
|
||||
|
||||
MySqlShowCreateProcedureStatement stmt = new MySqlShowCreateProcedureStatement();
|
||||
@@ -997,7 +1018,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
if (identifierEquals("FUNCTION")) {
|
||||
if (lexer.token() == Token.FUNCTION) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (identifierEquals("CODE")) {
|
||||
@@ -1131,7 +1152,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return stmt;
|
||||
}
|
||||
|
||||
if (identifierEquals("PROCEDURE")) {
|
||||
if (lexer.token() == Token.PROCEDURE) {
|
||||
lexer.nextToken();
|
||||
|
||||
if (identifierEquals("CODE")) {
|
||||
@@ -2342,7 +2363,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
} else if (identifierEquals("CHECK")) {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
} else if (identifierEquals("OPTIMIZE")) {
|
||||
} else if (lexer.token() == Token.OPTIMIZE) {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
} else if (identifierEquals("REBUILD")) {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
|
||||
@@ -56,6 +56,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlKillStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlOptimizeStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
@@ -562,4 +563,8 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
boolean visit(MySqlAlterUserStatement x);
|
||||
|
||||
void endVisit(MySqlAlterUserStatement x);
|
||||
|
||||
boolean visit(MySqlOptimizeStatement x);
|
||||
|
||||
void endVisit(MySqlOptimizeStatement x);
|
||||
} //
|
||||
|
||||
@@ -58,6 +58,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlKillStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlOptimizeStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
@@ -1247,4 +1248,14 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
|
||||
public void endVisit(MySqlAlterUserStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlOptimizeStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlOptimizeStatement x) {
|
||||
|
||||
}
|
||||
} //
|
||||
|
||||
@@ -80,6 +80,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlKillStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlOptimizeStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
@@ -3022,6 +3023,28 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlOptimizeStatement x) {
|
||||
print("OPTIMIZE ");
|
||||
if (x.isNoWriteToBinlog()) {
|
||||
print("NO_WRITE_TO_BINLOG ");
|
||||
}
|
||||
|
||||
if (x.isLocal()) {
|
||||
print("LOCAL ");
|
||||
}
|
||||
|
||||
print("TABLE ");
|
||||
|
||||
printAndAccept(x.getTableSources(), ", ");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlOptimizeStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlAlterUserStatement x) {
|
||||
print("ALTER USER");
|
||||
|
||||
@@ -71,6 +71,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlKillStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadDataInFileStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlOptimizeStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
|
||||
@@ -1330,5 +1331,15 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
public void endVisit(MySqlAlterUserStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlOptimizeStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlOptimizeStatement x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user