1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.logging.jdbc;
17
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.Method;
20 import java.lang.reflect.Proxy;
21 import java.sql.CallableStatement;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24
25 import org.apache.ibatis.logging.Log;
26 import org.apache.ibatis.reflection.ExceptionUtil;
27
28
29
30
31
32
33
34 public final class PreparedStatementLogger extends BaseJdbcLogger implements InvocationHandler {
35
36 private final PreparedStatement statement;
37
38 private PreparedStatementLogger(PreparedStatement stmt, Log statementLog, int queryStack) {
39 super(statementLog, queryStack);
40 this.statement = stmt;
41 }
42
43 @Override
44 public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
45 try {
46 if (Object.class.equals(method.getDeclaringClass())) {
47 return method.invoke(this, params);
48 }
49 if (EXECUTE_METHODS.contains(method.getName())) {
50 if (isDebugEnabled()) {
51 debug("Parameters: " + getParameterValueString(), true);
52 }
53 clearColumnInfo();
54 if ("executeQuery".equals(method.getName())) {
55 ResultSet rs = (ResultSet) method.invoke(statement, params);
56 return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
57 } else {
58 return method.invoke(statement, params);
59 }
60 }
61 if (SET_METHODS.contains(method.getName())) {
62 if ("setNull".equals(method.getName())) {
63 setColumn(params[0], null);
64 } else {
65 setColumn(params[0], params[1]);
66 }
67 return method.invoke(statement, params);
68 } else if ("getResultSet".equals(method.getName())) {
69 ResultSet rs = (ResultSet) method.invoke(statement, params);
70 return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
71 } else if ("getUpdateCount".equals(method.getName())) {
72 int updateCount = (Integer) method.invoke(statement, params);
73 if (updateCount != -1) {
74 debug(" Updates: " + updateCount, false);
75 }
76 return updateCount;
77 } else {
78 return method.invoke(statement, params);
79 }
80 } catch (Throwable t) {
81 throw ExceptionUtil.unwrapThrowable(t);
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public static PreparedStatement newInstance(PreparedStatement stmt, Log statementLog, int queryStack) {
98 InvocationHandler handler = new PreparedStatementLogger(stmt, statementLog, queryStack);
99 ClassLoader cl = PreparedStatement.class.getClassLoader();
100 return (PreparedStatement) Proxy.newProxyInstance(cl,
101 new Class[] { PreparedStatement.class, CallableStatement.class }, handler);
102 }
103
104
105
106
107
108
109 public PreparedStatement getPreparedStatement() {
110 return statement;
111 }
112
113 }