View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * ResultSet proxy to add logging.
29   */
30  public class ResultSetLogProxy extends BaseLogProxy implements InvocationHandler {
31  
32    /** The Constant log. */
33    private static final Log log = LogFactory.getLog(ResultSet.class);
34  
35    /** The first. */
36    boolean first = true;
37  
38    /** The rs. */
39    private ResultSet rs;
40  
41    /**
42     * Instantiates a new result set log proxy.
43     *
44     * @param rs
45     *          the rs
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     * Creates a logging version of a ResultSet.
90     *
91     * @param rs
92     *          - the ResultSet to proxy
93     *
94     * @return - the ResultSet with logging
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    * Get the wrapped result set.
104    *
105    * @return the resultSet
106    */
107   public ResultSet getRs() {
108     return rs;
109   }
110 
111 }