Skip to content

Commit

Permalink
improve sql-wall
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 25, 2013
1 parent e10c681 commit b056714
Show file tree
Hide file tree
Showing 25 changed files with 497 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ParameterizedOutputVisitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlParameterizedOutputVisitor;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleParameterizedOutputVisitor;
Expand Down Expand Up @@ -158,6 +159,7 @@ public static boolean checkParameterize(SQLObject x) {
|| parent instanceof SQLColumnDefinition //
|| parent instanceof SQLServerTop //
|| parent instanceof SQLAssignItem //
|| parent instanceof SQLSelectOrderByItem //
) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public static boolean visit(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
char ch = (char) intValue;
x.putAttribute(EVAL_VALUE, Character.toString(ch));
}
} else if ("CURRENT_USER".equals(methodName)) {
} else if ("current_user".equals(methodName)) {
x.putAttribute(EVAL_VALUE, "CURRENT_USER");
}
return false;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/alibaba/druid/support/json/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ public void writeString(String text) {
write("\\\\");
} else if (c == '\t') {
write("\\t");
} else if (c < 16) {
write("\\u000");
write(Integer.toHexString(c));
} else if (c < 32) {
write("\\u00");
write(Integer.toHexString(c));
} else if (c >= 0x7f && c <= 0xA0) {
write("\\u00");
write(Integer.toHexString(c));
} else {
write(c);
}
Expand Down
96 changes: 81 additions & 15 deletions src/main/java/com/alibaba/druid/wall/WallFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public synchronized void init(DataSourceProxy dataSource) {
} else {
throw new IllegalStateException("dbType not support : " + dbType + ", url " + dataSource.getUrl());
}

provider.setName(dataSource.getName());

this.inited = true;
Expand Down Expand Up @@ -364,6 +364,9 @@ public boolean statement_execute(FilterChain chain, StatementProxy statement, St
setSqlStatAttribute(statement);
}
return firstResult;
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
if (originalContext != null) {
WallContext.setContext(originalContext);
Expand All @@ -385,6 +388,9 @@ public boolean statement_execute(FilterChain chain, StatementProxy statement, St
setSqlStatAttribute(statement);
}
return firstResult;
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
WallContext.clearContext();
}
Expand All @@ -404,6 +410,9 @@ public boolean statement_execute(FilterChain chain, StatementProxy statement, St
setSqlStatAttribute(statement);
}
return firstResult;
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
WallContext.clearContext();
}
Expand All @@ -423,6 +432,9 @@ public boolean statement_execute(FilterChain chain, StatementProxy statement, St
setSqlStatAttribute(statement);
}
return firstResult;
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
WallContext.clearContext();
}
Expand All @@ -443,6 +455,9 @@ public int[] statement_executeBatch(FilterChain chain, StatementProxy statement)
}

return updateCounts;
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
WallContext.clearContext();
}
Expand All @@ -455,6 +470,9 @@ public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy s
try {
sql = check(sql);
return chain.statement_executeQuery(statement, sql);
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
WallContext.clearContext();
}
Expand All @@ -468,6 +486,9 @@ public int statement_executeUpdate(FilterChain chain, StatementProxy statement,
int updateCount = chain.statement_executeUpdate(statement, sql);
statExecuteUpdate(updateCount);
return updateCount;
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
WallContext.clearContext();
}
Expand All @@ -482,6 +503,9 @@ public int statement_executeUpdate(FilterChain chain, StatementProxy statement,
int updateCount = chain.statement_executeUpdate(statement, sql, autoGeneratedKeys);
statExecuteUpdate(updateCount);
return updateCount;
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
WallContext.clearContext();
}
Expand All @@ -496,6 +520,9 @@ public int statement_executeUpdate(FilterChain chain, StatementProxy statement,
int updateCount = chain.statement_executeUpdate(statement, sql, columnIndexes);
statExecuteUpdate(updateCount);
return updateCount;
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
WallContext.clearContext();
}
Expand All @@ -520,40 +547,58 @@ public int statement_executeUpdate(FilterChain chain, StatementProxy statement,
int updateCount = chain.statement_executeUpdate(statement, sql, columnNames);
statExecuteUpdate(updateCount);
return updateCount;
} catch (SQLException ex) {
incrementExecuteErrorCount();
throw ex;
} finally {
WallContext.clearContext();
}
}

@Override
public boolean preparedStatement_execute(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
boolean firstResult = chain.preparedStatement_execute(statement);
try {
boolean firstResult = chain.preparedStatement_execute(statement);

if (!firstResult) {
WallSqlStat sqlStat = (WallSqlStat) statement.getAttribute(ATTR_SQL_STAT);
int updateCount = statement.getUpdateCount();
if (sqlStat != null) {
provider.addUpdateCount(sqlStat, updateCount);
if (!firstResult) {
WallSqlStat sqlStat = (WallSqlStat) statement.getAttribute(ATTR_SQL_STAT);
int updateCount = statement.getUpdateCount();
if (sqlStat != null) {
provider.addUpdateCount(sqlStat, updateCount);
}
}
}

return firstResult;
return firstResult;
} catch (SQLException ex) {
incrementExecuteErrorCount(statement);
throw ex;
}
}

@Override
public ResultSetProxy preparedStatement_executeQuery(FilterChain chain, PreparedStatementProxy statement)
throws SQLException {
return chain.preparedStatement_executeQuery(statement);
try {
return chain.preparedStatement_executeQuery(statement);
} catch (SQLException ex) {
incrementExecuteErrorCount(statement);
throw ex;
}
}

@Override
public int preparedStatement_executeUpdate(FilterChain chain, PreparedStatementProxy statement) throws SQLException {
int updateCount = chain.preparedStatement_executeUpdate(statement);
WallSqlStat sqlStat = (WallSqlStat) statement.getAttribute(ATTR_SQL_STAT);
if (sqlStat != null) {
provider.addUpdateCount(sqlStat, updateCount);
try {
int updateCount = chain.preparedStatement_executeUpdate(statement);
WallSqlStat sqlStat = (WallSqlStat) statement.getAttribute(ATTR_SQL_STAT);
if (sqlStat != null) {
provider.addUpdateCount(sqlStat, updateCount);
}
return updateCount;
} catch (SQLException ex) {
incrementExecuteErrorCount(statement);
throw ex;
}
return updateCount;
}

public void setSqlStatAttribute(StatementProxy stmt) {
Expand Down Expand Up @@ -585,6 +630,27 @@ public void statExecuteUpdate(int updateCount) {
provider.addUpdateCount(sqlStat, updateCount);
}
}

public void incrementExecuteErrorCount(PreparedStatementProxy statement) {
WallSqlStat sqlStat = (WallSqlStat) statement.getAttribute(ATTR_SQL_STAT);
if (sqlStat != null) {
sqlStat.incrementAndGetExecuteErrorCount();
}
}

public void incrementExecuteErrorCount() {
WallContext context = WallContext.current();
if (context == null) {
return;
}

WallSqlStat sqlStat = context.getSqlStat();
if (sqlStat == null) {
return;
}

sqlStat.incrementAndGetExecuteErrorCount();
}

public String check(String sql) throws SQLException {
WallCheckResult checkResult = provider.check(sql);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/druid/wall/WallProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public abstract class WallProvider {
private LRUCache<String, WallSqlStat> blackList;
private LRUCache<String, WallSqlStat> blackMergedList;

private int blackSqlMaxSize = 100; // 1k
private int blackSqlMaxSize = 200; // 1k

protected final WallConfig config;

Expand Down
25 changes: 17 additions & 8 deletions src/main/java/com/alibaba/druid/wall/WallSqlStat.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
public class WallSqlStat {

private volatile long executeCount;
private volatile long executeErrorCount;
private volatile long fetchRowCount;
private volatile long updateCount;

final static AtomicLongFieldUpdater<WallSqlStat> executeCountUpdater = AtomicLongFieldUpdater.newUpdater(WallSqlStat.class,
"executeCount");
final static AtomicLongFieldUpdater<WallSqlStat> fetchRowCountUpdater = AtomicLongFieldUpdater.newUpdater(WallSqlStat.class,
"fetchRowCount");
final static AtomicLongFieldUpdater<WallSqlStat> updateCountUpdater = AtomicLongFieldUpdater.newUpdater(WallSqlStat.class,
"updateCount");
final static AtomicLongFieldUpdater<WallSqlStat> executeCountUpdater = AtomicLongFieldUpdater.newUpdater(WallSqlStat.class,
"executeCount");
final static AtomicLongFieldUpdater<WallSqlStat> executeErrorCountUpdater = AtomicLongFieldUpdater.newUpdater(WallSqlStat.class,
"executeErrorCount");

final static AtomicLongFieldUpdater<WallSqlStat> fetchRowCountUpdater = AtomicLongFieldUpdater.newUpdater(WallSqlStat.class,
"fetchRowCount");
final static AtomicLongFieldUpdater<WallSqlStat> updateCountUpdater = AtomicLongFieldUpdater.newUpdater(WallSqlStat.class,
"updateCount");
private final Map<String, WallSqlTableStat> tableStats;

private final List<Violation> violations;
Expand Down Expand Up @@ -79,12 +83,16 @@ public long incrementAndGetExecuteCount() {
return executeCountUpdater.incrementAndGet(this);
}

public long incrementAndGetExecuteErrorCount() {
return executeErrorCountUpdater.incrementAndGet(this);
}

public long getExecuteCount() {
return executeCount;
}

public long incrementAndGetFetchRowCount() {
return fetchRowCountUpdater.incrementAndGet(this);
public long getExecuteErrorCount() {
return executeErrorCount;
}

public long addAndFetchRowCount(long delta) {
Expand Down Expand Up @@ -123,6 +131,7 @@ public WallSqlStatValue getStatValue(boolean reset) {
final WallSqlStatValue statValue = new WallSqlStatValue();

statValue.setExecuteCount(get(this, executeCountUpdater, reset));
statValue.setExecuteErrorCount(get(this, executeErrorCountUpdater, reset));
statValue.setFetchRowCount(get(this, fetchRowCountUpdater, reset));
statValue.setUpdateCount(get(this, updateCountUpdater, reset));
statValue.setSyntaxError(this.syntaxError);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/alibaba/druid/wall/WallSqlStatValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class WallSqlStatValue {
@MField(aggregate = AggregateType.Sum)
private long executeCount;

@MField(aggregate = AggregateType.Sum)
private long executeErrorCount;

@MField(aggregate = AggregateType.Sum)
private long fetchRowCount;

Expand Down Expand Up @@ -118,13 +121,25 @@ public void setViolationMessage(String violationMessage) {
this.violationMessage = violationMessage;
}

public long getExecuteErrorCount() {
return executeErrorCount;
}

public void setExecuteErrorCount(long executeErrorCount) {
this.executeErrorCount = executeErrorCount;
}

public Map<String, Object> toMap() {
Map<String, Object> sqlStatMap = new LinkedHashMap<String, Object>();
sqlStatMap.put("sql", sql);
if (sql != sqlSample) {
sqlStatMap.put("sample", sqlSample);
}
sqlStatMap.put("executeCount", getExecuteCount());

if (executeErrorCount > 0) {
sqlStatMap.put("executeErrorCount", executeErrorCount);
}

if (fetchRowCount > 0) {
sqlStatMap.put("fetchRowCount", fetchRowCount);
Expand Down
Loading

0 comments on commit b056714

Please sign in to comment.