This commit is contained in:
温少
2012-03-19 09:30:00 +08:00
parent 6624603b20
commit 0414d72750
5 changed files with 114 additions and 48 deletions
@@ -29,6 +29,8 @@ public class WallConfig {
private boolean truncateAllow = false;
private boolean commentAllow = false;
private boolean schemaCheck = true;
private boolean tableCheck = true;
private boolean functionCheck = true;
@@ -238,6 +240,14 @@ public class WallConfig {
// ///////////////////
public boolean isCommentAllow() {
return commentAllow;
}
public void setCommentAllow(boolean commentAllow) {
this.commentAllow = commentAllow;
}
public Set<String> getPermitFunctions() {
return permitFunctions;
}
@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.ParserException;
@@ -19,10 +20,12 @@ public abstract class WallProvider {
private int whileListMaxSize = 1024;
private int whiteSqlMaxLength = 1024; // 1k
private int whiteSqlMaxLength = 1024; // 1k
protected final WallConfig config;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public WallProvider(WallConfig config){
this.config = config;
}
@@ -31,26 +34,47 @@ public abstract class WallProvider {
return config;
}
public synchronized void addWhiteSql(String sql) {
if (whiteList == null) {
whiteList = new LinkedHashMap<String, Object>(whileListMaxSize, 0.75f, true);
}
public void addWhiteSql(String sql) {
lock.writeLock().lock();
try {
if (whiteList == null) {
whiteList = new LinkedHashMap<String, Object>(whileListMaxSize, 0.75f, true);
}
whiteList.put(sql, PRESENT);
whiteList.put(sql, PRESENT);
} finally {
lock.writeLock().unlock();
}
}
public void clearCache() {
lock.writeLock().lock();
try {
if (whiteList != null) {
whiteList = null;
}
} finally {
lock.writeLock().unlock();
}
}
public synchronized boolean whiteContains(String sql) {
if (whiteList == null) {
return false;
}
public boolean whiteContains(String sql) {
lock.readLock().lock();
try {
if (whiteList == null) {
return false;
}
return whiteList.get(sql) != null;
return whiteList.get(sql) != null;
} finally {
lock.readLock().unlock();
}
}
public abstract SQLStatementParser createParser(String sql);
public abstract WallVisitor createWallVisitor();
public boolean checkValid(String sql) {
return check(sql).size() == 0;
}
@@ -63,7 +87,10 @@ public abstract class WallProvider {
}
SQLStatementParser parser = createParser(sql);
parser.getLexer().setAllowComment(false); // permit comment
if (!config.isCommentAllow()) {
parser.getLexer().setAllowComment(false); // permit comment
}
List<SQLStatement> statementList = new ArrayList<SQLStatement>();