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.ResultSet;
22  import java.sql.Statement;
23  
24  import org.apache.ibatis.logging.Log;
25  import org.apache.ibatis.reflection.ExceptionUtil;
26  
27  /**
28   * Statement proxy to add logging.
29   *
30   * @author Clinton Begin
31   * @author Eduardo Macarron
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     * Creates a logging version of a Statement.
72     *
73     * @param stmt
74     *          the statement
75     * @param statementLog
76     *          the statement log
77     * @param queryStack
78     *          the query stack
79     *
80     * @return the proxy
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     * return the wrapped statement.
90     *
91     * @return the statement
92     */
93    public Statement getStatement() {
94      return statement;
95    }
96  
97  }