/*
 *    Copyright 2009-2012 The MyBatis Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.logging.jdbc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

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

/*
 * ResultSet proxy to add logging
 */
public final class ResultSetLogger extends BaseJdbcLogger implements InvocationHandler {

  private boolean first = true;
  private ResultSet rs;

  private ResultSetLogger(ResultSet rs, Log statementLog) {
    super(statementLog);
    this.rs = rs;
  }

  public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
    try {
      Object o = method.invoke(rs, params);
      if ("next".equals(method.getName())) {
        if (((Boolean) o)) {
          ResultSetMetaData rsmd = rs.getMetaData();
          final int columnCount = rsmd.getColumnCount();
          if (isTraceEnabled()) {
            if (first) {
              first = false;
              printColumnHeaders(rsmd, columnCount);
            }
            printColumnValues(columnCount);
          }
        }
      }
      clearColumnInfo();
      return o;
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
  }

  private void printColumnHeaders(ResultSetMetaData rsmd, int columnCount) throws SQLException {
    StringBuilder row = new StringBuilder();
    row.append("<==    Columns: ");
    for (int i = 1; i <= columnCount; i++) {
      String colname = rsmd.getColumnName(i);
      row.append(colname);
      if (i != columnCount) row.append(", ");
    }
    trace(row.toString());
  }

  private void printColumnValues(int columnCount) throws SQLException {
    StringBuilder row = new StringBuilder();
    row.append("<==        Row: ");
    for (int i = 1; i <= columnCount; i++) {
      String colname;
      try {
        colname = rs.getString(i);
      } catch (SQLException e) {
        // generally can't call getString() on a BLOB column
        colname = "<<Cannot Display>>";
      }
      row.append(colname);
      if (i != columnCount) row.append(", ");
    }
    trace(row.toString());
  }

  /*
   * Creates a logging version of a ResultSet
   *
   * @param rs - the ResultSet to proxy
   * @return - the ResultSet with logging
   */
  public static ResultSet newInstance(ResultSet rs, Log statementLog) {
    InvocationHandler handler = new ResultSetLogger(rs, statementLog);
    ClassLoader cl = ResultSet.class.getClassLoader();
    return (ResultSet) Proxy.newProxyInstance(cl, new Class[]{ResultSet.class}, handler);
  }

  /*
   * Get the wrapped result set
   *
   * @return the resultSet
   */
  public ResultSet getRs() {
    return rs;
  }

}

@@ -891,16 +891,90 @@
 eption;%0D
+%0Aimport java.sql.Types;%0D%0Aimport java.util.HashSet;%0D%0Aimport java.util.Set;%0D
 %0A%0D%0Aimpor
@@ -1254,16 +1254,517 @@
 Set rs;%0D
+%0A  %0D%0A  private static Set%3CInteger%3E BLOB_TYPES;%0D%0A  private static Set%3CInteger%3E BLOB_COLUMNS;%0D%0A  %0D%0A  static %7B%0D%0A    BLOB_TYPES = new HashSet%3CInteger%3E();%0D%0A    BLOB_TYPES.add(Types.BINARY);%0D%0A    BLOB_TYPES.add(Types.BLOB);%0D%0A    BLOB_TYPES.add(Types.CLOB);%0D%0A    BLOB_TYPES.add(Types.LONGNVARCHAR);%0D%0A    BLOB_TYPES.add(Types.LONGVARBINARY);%0D%0A    BLOB_TYPES.add(Types.LONGVARCHAR);%0D%0A    BLOB_TYPES.add(Types.NCLOB);%0D%0A    BLOB_TYPES.add(Types.VARBINARY);%0D%0A%0D%0A    BLOB_COLUMNS = new HashSet%3CInteger%3E();%0D%0A%7D%0D%0A    %0D
 %0A%0D%0A  pri
@@ -2794,32 +2794,128 @@
 nCount; i++) %7B%0D%0A
+      if (BLOB_TYPES.contains(rsmd.getColumnType(i))) %7B%0D%0A        BLOB_COLUMNS.add(i);%0D%0A      %7D%0D%0A
       String col
@@ -3296,24 +3296,118 @@
      try %7B%0D%0A
+        if (BLOB_COLUMNS.contains(i)) %7B%0D%0A          colname = %22%3C%3CBLOB%3E%3E%22;%0D%0A        %7D else %7B%0D%0A  
         coln
@@ -3426,24 +3426,35 @@
 String(i);%0D%0A
+        %7D%0D%0A
       %7D catc

Patch for http://code.google.com/p/mybatis/issues/detail?id=751 - don't print BLOB columns values with the logger - messes up further type handlers.