improve sql parser

This commit is contained in:
wenshao
2013-08-07 23:59:27 +08:00
parent 37ad0f15f5
commit 65dc02f6d0
14 changed files with 303 additions and 45 deletions
@@ -38,11 +38,14 @@ public class SQLGrantStatement extends SQLStatementImpl {
private boolean adminOption;
private SQLExpr identifiedBy;
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, on);
acceptChild(visitor, to);
acceptChild(visitor, identifiedBy);
}
visitor.endVisit(this);
}
@@ -115,6 +118,14 @@ public class SQLGrantStatement extends SQLStatementImpl {
this.adminOption = adminOption;
}
public SQLExpr getIdentifiedBy() {
return identifiedBy;
}
public void setIdentifiedBy(SQLExpr identifiedBy) {
this.identifiedBy = identifiedBy;
}
public static enum ObjectType {
TABLE, FUNCTION, PROCEDURE, USER
}
@@ -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 com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlSetPasswordStatement extends MySqlStatementImpl {
private SQLName user;
private SQLExpr password;
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, user);
acceptChild(visitor, password);
}
visitor.endVisit(this);
}
public SQLName getUser() {
return user;
}
public void setUser(SQLName user) {
if (user != null) {
user.setParent(this);
}
this.user = user;
}
public SQLExpr getPassword() {
return password;
}
public void setPassword(SQLExpr password) {
if (password != null) {
password.setParent(this);
}
this.password = password;
}
}
@@ -92,6 +92,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement;
@@ -1993,6 +1994,22 @@ public class MySqlStatementParser extends SQLStatementParser {
public SQLStatement parseSet() {
accept(Token.SET);
if (identifierEquals("PASSWORD")) {
lexer.nextToken();
MySqlSetPasswordStatement stmt = new MySqlSetPasswordStatement();
if (lexer.token() == Token.FOR) {
lexer.nextToken();
stmt.setUser(this.exprParser.name());
}
accept(Token.EQ);
stmt.setPassword(this.exprParser.expr());
return stmt;
}
Boolean global = null;
if (identifierEquals(GLOBAL)) {
global = Boolean.TRUE;
@@ -69,6 +69,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement;
@@ -557,4 +558,8 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlOptimizeStatement x);
void endVisit(MySqlOptimizeStatement x);
boolean visit(MySqlSetPasswordStatement x);
void endVisit(MySqlSetPasswordStatement x);
} //
@@ -74,6 +74,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement;
@@ -1236,4 +1237,14 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
public void endVisit(MySqlOptimizeStatement x) {
}
@Override
public boolean visit(MySqlSetPasswordStatement x) {
return true;
}
@Override
public void endVisit(MySqlSetPasswordStatement x) {
}
} //
@@ -97,6 +97,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement;
@@ -2999,4 +3000,27 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
@Override
public boolean visit(MySqlSetPasswordStatement x) {
print("SET PASSWORD ");
if (x.getUser() != null){
print("FOR ");
x.getUser().accept(this);
print(' ');
}
print("= ");
if (x.getPassword() != null) {
x.getPassword().accept(this);
}
return false;
}
@Override
public void endVisit(MySqlSetPasswordStatement x) {
}
} //
@@ -88,6 +88,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetCharSetStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetNamesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetPasswordStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionIsolationLevelStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowAuthorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowBinLogEventsStatement;
@@ -1322,5 +1323,15 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlOptimizeStatement x) {
}
@Override
public boolean visit(MySqlSetPasswordStatement x) {
return false;
}
@Override
public void endVisit(MySqlSetPasswordStatement x) {
}
}
@@ -616,6 +616,12 @@ public class SQLStatementParser extends SQLParser {
acceptIdentifier("OPTION");
stmt.setAdminOption(true);
}
if (lexer.token() == Token.IDENTIFIED) {
lexer.nextToken();
accept(Token.BY);
stmt.setIdentifiedBy(this.exprParser.expr());
}
return stmt;
}
@@ -1774,6 +1774,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
}
print(" ADMIN OPTION");
}
if (x.getIdentifiedBy() != null) {
print(" IDENTIFIED BY ");
x.getIdentifiedBy().accept(this);
}
return false;
}
@@ -55,7 +55,6 @@ public abstract class WallProvider {
1);
private LRUCache<String, WallSqlStat> whiteList;
private LRUCache<String, WallSqlStat> whiteMergedList;
private int MAX_SQL_LENGTH = 2048; // 1k
@@ -252,11 +251,11 @@ public abstract class WallProvider {
WallSqlStat mergedStat;
lock.readLock().lock();
try {
if (whiteMergedList == null) {
whiteMergedList = new LRUCache<String, WallSqlStat>(whiteSqlMaxSize);
if (whiteList == null) {
whiteList = new LRUCache<String, WallSqlStat>(whiteSqlMaxSize);
}
mergedStat = whiteMergedList.get(mergedSql);
mergedStat = whiteList.get(mergedSql);
} finally {
lock.readLock().unlock();
}
@@ -267,9 +266,9 @@ public abstract class WallProvider {
lock.writeLock().lock();
try {
mergedStat = whiteMergedList.get(mergedSql);
mergedStat = whiteList.get(mergedSql);
if (mergedStat == null) {
whiteMergedList.put(mergedSql, newStat);
whiteList.put(mergedSql, newStat);
mergedStat = newStat;
}
} finally {
@@ -288,21 +287,13 @@ public abstract class WallProvider {
whiteList = new LRUCache<String, WallSqlStat>(whiteSqlMaxSize);
}
if (whiteMergedList == null) {
whiteMergedList = new LRUCache<String, WallSqlStat>(whiteSqlMaxSize);
}
WallSqlStat wallStat = whiteList.get(sql);
if (wallStat == null) {
wallStat = whiteMergedList.get(mergedSql);
if (wallStat == null) {
wallStat = new WallSqlStat(tableStats, functionStats, syntaxError);
whiteMergedList.put(mergedSql, wallStat);
wallStat.setSample(sql);
}
wallStat = new WallSqlStat(tableStats, functionStats, syntaxError);
whiteList.put(sql, wallStat);
wallStat.setSample(sql);
wallStat.incrementAndGetExecuteCount();
whiteList.put(sql, wallStat);
}
return wallStat;
@@ -369,9 +360,10 @@ public abstract class WallProvider {
Set<String> hashSet = new HashSet<String>();
lock.readLock().lock();
try {
if (whiteMergedList != null) {
hashSet.addAll(whiteMergedList.keySet());
if (whiteList != null) {
hashSet.addAll(whiteList.keySet());
}
if (blackMergedList != null) {
hashSet.addAll(blackMergedList.keySet());
}
@@ -402,9 +394,6 @@ public abstract class WallProvider {
if (whiteList != null) {
whiteList = null;
}
if (whiteMergedList != null) {
whiteMergedList = null;
}
if (blackList != null) {
blackList = null;
@@ -443,32 +432,31 @@ public abstract class WallProvider {
WallSqlStat stat = null;
lock.readLock().lock();
try {
if (whiteList != null) {
stat = whiteList.get(sql);
if (whiteList == null) {
return null;
}
stat = whiteList.get(sql);
} finally {
lock.readLock().unlock();
}
if (stat == null) {
String mergedSql;
try {
mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
} catch (Exception ex) {
// skip
return null;
}
if (stat != null) {
return stat;
}
lock.readLock().lock();
try {
if (whiteMergedList == null) {
return null;
}
String mergedSql;
try {
mergedSql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
} catch (Exception ex) {
// skip
return null;
}
stat = whiteMergedList.get(mergedSql);
} finally {
lock.readLock().unlock();
}
lock.readLock().lock();
try {
stat = whiteList.get(mergedSql);
} finally {
lock.readLock().unlock();
}
return stat;
}
@@ -919,8 +907,8 @@ public abstract class WallProvider {
final Lock lock = reset ? this.lock.writeLock() : this.lock.readLock();
lock.lock();
try {
if (this.whiteMergedList != null) {
for (Map.Entry<String, WallSqlStat> entry : whiteMergedList.entrySet()) {
if (this.whiteList != null) {
for (Map.Entry<String, WallSqlStat> entry : whiteList.entrySet()) {
String sql = entry.getKey();
WallSqlStat sqlStat = entry.getValue();
WallSqlStatValue sqlStatValue = sqlStat.getStatValue(reset);