1 16 package com.ibatis.common.jdbc.logging; 17 18 import com.ibatis.common.beans.ClassInfo; 19 import com.ibatis.common.logging.Log; 20 import com.ibatis.common.logging.LogFactory; 21 22 import java.lang.reflect.InvocationHandler ; 23 import java.lang.reflect.Method ; 24 import java.lang.reflect.Proxy ; 25 import java.sql.CallableStatement ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 29 32 public class PreparedStatementLogProxy extends BaseLogProxy implements InvocationHandler { 33 34 private static final Log log = LogFactory.getLog(PreparedStatement .class); 35 36 private PreparedStatement statement; 37 private String sql; 38 39 private PreparedStatementLogProxy(PreparedStatement stmt, String sql) { 40 this.statement = stmt; 41 this.sql = sql; 42 } 43 44 public Object invoke(Object proxy, Method method, Object [] params) throws Throwable { 45 try { 46 if (EXECUTE_METHODS.contains(method.getName())) { 47 if (log.isDebugEnabled()) { 48 log.debug("{pstm-" + id + "} PreparedStatement: " + removeBreakingWhitespace(sql)); 49 log.debug("{pstm-" + id + "} Parameters: " + getValueString()); 50 log.debug("{pstm-" + id + "} Types: " + getTypeString()); 51 } 52 clearColumnInfo(); 53 if ("executeQuery".equals(method.getName())) { 54 ResultSet rs = (ResultSet ) method.invoke(statement, params); 55 if ( rs != null ) { 56 return ResultSetLogProxy.newInstance(rs); 57 } 58 else { 59 return null; 60 } 61 } else { 62 return method.invoke(statement, params); 63 } 64 } else if (SET_METHODS.contains(method.getName())) { 65 if ("setNull".equals(method.getName())) { 66 setColumn(params[0], null); 67 } else { 68 setColumn(params[0], params[1]); 69 } 70 return method.invoke(statement, params); 71 } else if ("getResultSet".equals(method.getName())) { 72 ResultSet rs = (ResultSet ) method.invoke(statement, params); 73 if ( rs != null ) { 74 return ResultSetLogProxy.newInstance(rs); 75 } 76 else { 77 return null; 78 } 79 } else { 80 return method.invoke(statement, params); 81 } 82 } catch (Throwable t) { 83 throw ClassInfo.unwrapThrowable(t); 84 } 85 } 86 87 93 public static PreparedStatement newInstance(PreparedStatement stmt, String sql) { 94 InvocationHandler handler = new PreparedStatementLogProxy(stmt, sql); 95 ClassLoader cl = PreparedStatement .class.getClassLoader(); 96 return (PreparedStatement ) Proxy.newProxyInstance(cl, new Class []{PreparedStatement .class, CallableStatement .class}, handler); 97 } 98 99 } 100 | Popular Tags |