1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
32 public class PreparedStatementLogProxy extends BaseLogProxy implements InvocationHandler {
33
34
35 private static final Log log = LogFactory.getLog(PreparedStatement.class);
36
37
38 private PreparedStatement statement;
39
40
41 private String sql;
42
43
44
45
46
47
48
49
50
51 private PreparedStatementLogProxy(PreparedStatement stmt, String sql) {
52 this.statement = stmt;
53 this.sql = sql;
54 }
55
56 public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
57 try {
58 if (EXECUTE_METHODS.contains(method.getName())) {
59 if (log.isDebugEnabled()) {
60 log.debug("{pstm-" + id + "} Executing Statement: " + removeBreakingWhitespace(sql));
61 log.debug("{pstm-" + id + "} Parameters: " + getValueString());
62 log.debug("{pstm-" + id + "} Types: " + getTypeString());
63 }
64 clearColumnInfo();
65 if ("executeQuery".equals(method.getName())) {
66 ResultSet rs = (ResultSet) method.invoke(statement, params);
67 if (rs != null) {
68 return ResultSetLogProxy.newInstance(rs);
69 } else {
70 return null;
71 }
72 } else {
73 return method.invoke(statement, params);
74 }
75 } else if (SET_METHODS.contains(method.getName())) {
76 if ("setNull".equals(method.getName())) {
77 setColumn(params[0], null);
78 } else {
79 setColumn(params[0], params[1]);
80 }
81 return method.invoke(statement, params);
82 } else if ("getResultSet".equals(method.getName())) {
83 ResultSet rs = (ResultSet) method.invoke(statement, params);
84 if (rs != null) {
85 return ResultSetLogProxy.newInstance(rs);
86 } else {
87 return null;
88 }
89 } else if ("equals".equals(method.getName())) {
90 Object ps = params[0];
91 if (ps instanceof Proxy) {
92 return new Boolean(proxy == ps);
93 }
94 return new Boolean(false);
95 } else if ("hashCode".equals(method.getName())) {
96 return Integer.valueOf(proxy.hashCode());
97 } else {
98 return method.invoke(statement, params);
99 }
100 } catch (Throwable t) {
101 throw ClassInfo.unwrapThrowable(t);
102 }
103 }
104
105
106
107
108
109
110
111
112
113
114
115 public static PreparedStatement newInstance(PreparedStatement stmt, String sql) {
116 InvocationHandler handler = new PreparedStatementLogProxy(stmt, sql);
117 ClassLoader cl = PreparedStatement.class.getClassLoader();
118 return (PreparedStatement) Proxy.newProxyInstance(cl,
119 new Class[] { PreparedStatement.class, CallableStatement.class }, handler);
120 }
121
122
123
124
125
126
127 public PreparedStatement getPreparedStatement() {
128 return statement;
129 }
130
131 }