View Javadoc
1   /*
2    *    Copyright 2009-2023 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 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   * PreparedStatement proxy to add logging.
30   *
31   * @author Clinton Begin
32   * @author Eduardo Macarron
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     * Creates a logging version of a PreparedStatement.
87     *
88     * @param stmt
89     *          - the statement
90     * @param statementLog
91     *          - the statement log
92     * @param queryStack
93     *          - the query stack
94     *
95     * @return - the proxy
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    * Return the wrapped prepared statement.
106    *
107    * @return the PreparedStatement
108    */
109   public PreparedStatement getPreparedStatement() {
110     return statement;
111   }
112 
113 }