ResultSetLogger.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.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.  */
  36. public final class ResultSetLogger extends BaseJdbcLogger implements InvocationHandler {

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

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

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

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

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

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

  111.   /**
  112.    * Creates a logging version of a ResultSet.
  113.    *
  114.    * @param rs
  115.    *          the ResultSet to proxy
  116.    * @param statementLog
  117.    *          the statement log
  118.    * @param queryStack
  119.    *          the query stack
  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. }