ResultSetLogger.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.ResultSetMetaData;
  22. import java.sql.SQLException;
  23. import java.sql.Types;
  24. import java.util.HashSet;
  25. import java.util.Set;
  26. import java.util.StringJoiner;

  27. import org.apache.ibatis.logging.Log;
  28. import org.apache.ibatis.reflection.ExceptionUtil;

  29. /**
  30.  * ResultSet proxy to add logging.
  31.  *
  32.  * @author Clinton Begin
  33.  * @author Eduardo Macarron
  34.  */
  35. public final class ResultSetLogger extends BaseJdbcLogger implements InvocationHandler {

  36.   private static final Set<Integer> BLOB_TYPES = new HashSet<>();
  37.   private boolean first = true;
  38.   private int rows;
  39.   private final ResultSet rs;
  40.   private final Set<Integer> blobColumns = new HashSet<>();

  41.   static {
  42.     BLOB_TYPES.add(Types.BINARY);
  43.     BLOB_TYPES.add(Types.BLOB);
  44.     BLOB_TYPES.add(Types.CLOB);
  45.     BLOB_TYPES.add(Types.LONGNVARCHAR);
  46.     BLOB_TYPES.add(Types.LONGVARBINARY);
  47.     BLOB_TYPES.add(Types.LONGVARCHAR);
  48.     BLOB_TYPES.add(Types.NCLOB);
  49.     BLOB_TYPES.add(Types.VARBINARY);
  50.   }

  51.   private ResultSetLogger(ResultSet rs, Log statementLog, int queryStack) {
  52.     super(statementLog, queryStack);
  53.     this.rs = rs;
  54.   }

  55.   @Override
  56.   public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
  57.     try {
  58.       if (Object.class.equals(method.getDeclaringClass())) {
  59.         return method.invoke(this, params);
  60.       }
  61.       Object o = method.invoke(rs, params);
  62.       if ("next".equals(method.getName())) {
  63.         if ((Boolean) o) {
  64.           rows++;
  65.           if (isTraceEnabled()) {
  66.             ResultSetMetaData rsmd = rs.getMetaData();
  67.             final int columnCount = rsmd.getColumnCount();
  68.             if (first) {
  69.               first = false;
  70.               printColumnHeaders(rsmd, columnCount);
  71.             }
  72.             printColumnValues(columnCount);
  73.           }
  74.         } else {
  75.           debug("     Total: " + rows, false);
  76.         }
  77.       }
  78.       clearColumnInfo();
  79.       return o;
  80.     } catch (Throwable t) {
  81.       throw ExceptionUtil.unwrapThrowable(t);
  82.     }
  83.   }

  84.   private void printColumnHeaders(ResultSetMetaData rsmd, int columnCount) throws SQLException {
  85.     StringJoiner row = new StringJoiner(", ", "   Columns: ", "");
  86.     for (int i = 1; i <= columnCount; i++) {
  87.       if (BLOB_TYPES.contains(rsmd.getColumnType(i))) {
  88.         blobColumns.add(i);
  89.       }
  90.       row.add(rsmd.getColumnLabel(i));
  91.     }
  92.     trace(row.toString(), false);
  93.   }

  94.   private void printColumnValues(int columnCount) {
  95.     StringJoiner row = new StringJoiner(", ", "       Row: ", "");
  96.     for (int i = 1; i <= columnCount; i++) {
  97.       try {
  98.         if (blobColumns.contains(i)) {
  99.           row.add("<<BLOB>>");
  100.         } else {
  101.           row.add(rs.getString(i));
  102.         }
  103.       } catch (SQLException e) {
  104.         // generally can't call getString() on a BLOB column
  105.         row.add("<<Cannot Display>>");
  106.       }
  107.     }
  108.     trace(row.toString(), false);
  109.   }

  110.   /**
  111.    * Creates a logging version of a ResultSet.
  112.    *
  113.    * @param rs
  114.    *          the ResultSet to proxy
  115.    * @param statementLog
  116.    *          the statement log
  117.    * @param queryStack
  118.    *          the query stack
  119.    *
  120.    * @return the ResultSet with logging
  121.    */
  122.   public static ResultSet newInstance(ResultSet rs, Log statementLog, int queryStack) {
  123.     InvocationHandler handler = new ResultSetLogger(rs, statementLog, queryStack);
  124.     ClassLoader cl = ResultSet.class.getClassLoader();
  125.     return (ResultSet) Proxy.newProxyInstance(cl, new Class[] { ResultSet.class }, handler);
  126.   }

  127.   /**
  128.    * Get the wrapped result set.
  129.    *
  130.    * @return the resultSet
  131.    */
  132.   public ResultSet getRs() {
  133.     return rs;
  134.   }

  135. }