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.ResultSet;
22 import java.sql.Statement;
23
24 import org.apache.ibatis.logging.Log;
25 import org.apache.ibatis.reflection.ExceptionUtil;
26
27
28
29
30
31
32
33 public final class StatementLogger extends BaseJdbcLogger implements InvocationHandler {
34
35 private final Statement statement;
36
37 private StatementLogger(Statement stmt, Log statementLog, int queryStack) {
38 super(statementLog, queryStack);
39 this.statement = stmt;
40 }
41
42 @Override
43 public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
44 try {
45 if (Object.class.equals(method.getDeclaringClass())) {
46 return method.invoke(this, params);
47 }
48 if (EXECUTE_METHODS.contains(method.getName())) {
49 if (isDebugEnabled()) {
50 debug(" Executing: " + removeExtraWhitespace((String) params[0]), true);
51 }
52 if ("executeQuery".equals(method.getName())) {
53 ResultSet rs = (ResultSet) method.invoke(statement, params);
54 return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
55 } else {
56 return method.invoke(statement, params);
57 }
58 }
59 if ("getResultSet".equals(method.getName())) {
60 ResultSet rs = (ResultSet) method.invoke(statement, params);
61 return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
62 } else {
63 return method.invoke(statement, params);
64 }
65 } catch (Throwable t) {
66 throw ExceptionUtil.unwrapThrowable(t);
67 }
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public static Statement newInstance(Statement stmt, Log statementLog, int queryStack) {
83 InvocationHandler handler = new StatementLogger(stmt, statementLog, queryStack);
84 ClassLoader cl = Statement.class.getClassLoader();
85 return (Statement) Proxy.newProxyInstance(cl, new Class[] { Statement.class }, handler);
86 }
87
88
89
90
91
92
93 public Statement getStatement() {
94 return statement;
95 }
96
97 }