StatementLogger.java

  1. /*
  2.  *    Copyright 2009-2021 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.  *       http://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.  */
  31. public final class StatementLogger extends BaseJdbcLogger implements InvocationHandler {

  32.   private final Statement statement;

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

  37.   @Override
  38.   public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
  39.     try {
  40.       if (Object.class.equals(method.getDeclaringClass())) {
  41.         return method.invoke(this, params);
  42.       }
  43.       if (EXECUTE_METHODS.contains(method.getName())) {
  44.         if (isDebugEnabled()) {
  45.           debug(" Executing: " + removeExtraWhitespace((String) params[0]), true);
  46.         }
  47.         if ("executeQuery".equals(method.getName())) {
  48.           ResultSet rs = (ResultSet) method.invoke(statement, params);
  49.           return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
  50.         } else {
  51.           return method.invoke(statement, params);
  52.         }
  53.       } else 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.    * @return the proxy
  73.    */
  74.   public static Statement newInstance(Statement stmt, Log statementLog, int queryStack) {
  75.     InvocationHandler handler = new StatementLogger(stmt, statementLog, queryStack);
  76.     ClassLoader cl = Statement.class.getClassLoader();
  77.     return (Statement) Proxy.newProxyInstance(cl, new Class[]{Statement.class}, handler);
  78.   }

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

  87. }