StatementLogger.java

  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. import java.lang.reflect.InvocationHandler;
  18. import java.lang.reflect.Method;
  19. import java.lang.reflect.Proxy;
  20. import java.sql.ResultSet;
  21. import java.sql.Statement;

  22. import org.apache.ibatis.logging.Log;
  23. import org.apache.ibatis.reflection.ExceptionUtil;

  24. /**
  25.  * Statement proxy to add logging.
  26.  *
  27.  * @author Clinton Begin
  28.  * @author Eduardo Macarron
  29.  */
  30. public final class StatementLogger extends BaseJdbcLogger implements InvocationHandler {

  31.   private final Statement statement;

  32.   private StatementLogger(Statement stmt, Log statementLog, int queryStack) {
  33.     super(statementLog, queryStack);
  34.     this.statement = stmt;
  35.   }

  36.   @Override
  37.   public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
  38.     try {
  39.       if (Object.class.equals(method.getDeclaringClass())) {
  40.         return method.invoke(this, params);
  41.       }
  42.       if (EXECUTE_METHODS.contains(method.getName())) {
  43.         if (isDebugEnabled()) {
  44.           debug(" Executing: " + removeExtraWhitespace((String) params[0]), true);
  45.         }
  46.         if ("executeQuery".equals(method.getName())) {
  47.           ResultSet rs = (ResultSet) method.invoke(statement, params);
  48.           return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
  49.         } else {
  50.           return method.invoke(statement, params);
  51.         }
  52.       }
  53.       if ("getResultSet".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.     } catch (Throwable t) {
  60.       throw ExceptionUtil.unwrapThrowable(t);
  61.     }
  62.   }

  63.   /**
  64.    * Creates a logging version of a Statement.
  65.    *
  66.    * @param stmt
  67.    *          the statement
  68.    * @param statementLog
  69.    *          the statement log
  70.    * @param queryStack
  71.    *          the query stack
  72.    *
  73.    * @return the proxy
  74.    */
  75.   public static Statement newInstance(Statement stmt, Log statementLog, int queryStack) {
  76.     InvocationHandler handler = new StatementLogger(stmt, statementLog, queryStack);
  77.     ClassLoader cl = Statement.class.getClassLoader();
  78.     return (Statement) Proxy.newProxyInstance(cl, new Class[] { Statement.class }, handler);
  79.   }

  80.   /**
  81.    * return the wrapped statement.
  82.    *
  83.    * @return the statement
  84.    */
  85.   public Statement getStatement() {
  86.     return statement;
  87.   }

  88. }