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.ResultSet;
26
27
28
29
30 public class ResultSetLogProxy extends BaseLogProxy implements InvocationHandler {
31
32
33 private static final Log log = LogFactory.getLog(ResultSet.class);
34
35
36 boolean first = true;
37
38
39 private ResultSet rs;
40
41
42
43
44
45
46
47 private ResultSetLogProxy(ResultSet rs) {
48 super();
49 this.rs = rs;
50 if (log.isDebugEnabled()) {
51 log.debug("{rset-" + id + "} ResultSet");
52 }
53 }
54
55 @Override
56 public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
57 try {
58 Object o = method.invoke(rs, params);
59 if (GET_METHODS.contains(method.getName())) {
60 if (params[0] instanceof String) {
61 if (rs.wasNull()) {
62 setColumn(params[0], null);
63 } else {
64 setColumn(params[0], o);
65 }
66 }
67 } else if ("next".equals(method.getName()) || "close".equals(method.getName())) {
68 String s = getValueString();
69 if (!"[]".equals(s)) {
70 if (first) {
71 first = false;
72 if (log.isDebugEnabled()) {
73 log.debug("{rset-" + id + "} Header: " + getColumnString());
74 }
75 }
76 if (log.isDebugEnabled()) {
77 log.debug("{rset-" + id + "} Result: " + s);
78 }
79 }
80 clearColumnInfo();
81 }
82 return o;
83 } catch (Throwable t) {
84 throw ClassInfo.unwrapThrowable(t);
85 }
86 }
87
88
89
90
91
92
93
94
95
96 public static ResultSet newInstance(ResultSet rs) {
97 InvocationHandler handler = new ResultSetLogProxy(rs);
98 ClassLoader cl = ResultSet.class.getClassLoader();
99 return (ResultSet) Proxy.newProxyInstance(cl, new Class[] { ResultSet.class }, handler);
100 }
101
102
103
104
105
106
107 public ResultSet getRs() {
108 return rs;
109 }
110
111 }