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
34 public final class StatementLogger extends BaseJdbcLogger implements InvocationHandler {
35
36 private final Statement statement;
37
38 private StatementLogger(Statement 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(" Executing: " + removeExtraWhitespace((String) params[0]), true);
52 }
53 if ("executeQuery".equals(method.getName())) {
54 ResultSet rs = (ResultSet) method.invoke(statement, params);
55 return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
56 } else {
57 return method.invoke(statement, params);
58 }
59 } else 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 public static Statement newInstance(Statement stmt, Log statementLog, int queryStack) {
82 InvocationHandler handler = new StatementLogger(stmt, statementLog, queryStack);
83 ClassLoader cl = Statement.class.getClassLoader();
84 return (Statement) Proxy.newProxyInstance(cl, new Class[]{Statement.class}, handler);
85 }
86
87
88
89
90
91
92 public Statement getStatement() {
93 return statement;
94 }
95
96 }