package com.ibatis.common.jdbc.logging;

import com.ailk.ecs.esf.conf.EsfProperties;
import com.github.bingoohuang.ibatis.BlackcatUtils;
import com.ibatis.common.beans.ClassInfo;
import com.ibatis.common.logging.Log;
import com.ibatis.common.logging.LogFactory;

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

/**
 * ResultSet proxy to add logging
 */
public class ResultSetLogProxy extends BaseLogProxy implements InvocationHandler {

    private static final Log log = LogFactory.getLog(ResultSet.class);

    int rows = 0;
    boolean first = true;
    private ResultSet rs;

    private ResultSetLogProxy(ResultSet rs) {
        super();
        this.rs = rs;
        if (log.isDebugEnabled()) {
            log.debug("{rset-" + id + "} ResultSet");
        }
    }

    static int maxRows = EsfProperties.getInt("Blackcat.SQLResult.MaxRows", 5);

    public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
        try {
            Object o = method.invoke(rs, params);
            if (GET_METHODS.contains(method.getName())) {
                if (params[0] instanceof String) {
                    setColumn(params[0], rs.wasNull() ? null : o);
                }
            } else if ("next".equals(method.getName())
                    || "close".equals(method.getName())) {
                if ("next".equals(method.getName())) ++rows;

                String s = getValueString();
                if (!"[]".equals(s)) {
                    if (first) {
                        first = false;
                        String columnString = getColumnString();
                        if (BlackcatUtils.HasBlackcat)
                            BlackcatUtils.log("SQL.Header", columnString);

                        if (log.isDebugEnabled()) {
                            log.debug("{rset-" + id + "} Header: " + columnString);
                        }
                    }

                    if (maxRows <= 0 || rows <= maxRows)
                        BlackcatUtils.log("SQL.Result", s);

                    if (log.isDebugEnabled()) {
                        log.debug("{rset-" + id + "} Result: " + s);
                    }
                }
                if (maxRows > 0 && rows - maxRows > 0 && "close".equals(method.getName()))
                    BlackcatUtils.log("SQL.Result", "[And more " + (rows - maxRows) + " rows ignored]");

                clearColumnInfo();
            }
            return o;
        } catch (Throwable t) {
            throw ClassInfo.unwrapThrowable(t);
        }
    }

    /**
     * Creates a logging version of a ResultSet
     *
     * @param rs - the ResultSet to proxy
     * @return - the ResultSet with logging
     */
    public static ResultSet newInstance(ResultSet rs) {
        InvocationHandler handler = new ResultSetLogProxy(rs);
        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;
    }

}

@@ -1316,32 +1316,34 @@
              %7D%0D%0A
+%0D%0A
             %7D el
@@ -1493,16 +1493,31 @@
 tName())
+ && (Boolean) o
 ) ++rows
@@ -1751,68 +1751,8 @@
 );%0D%0A
-                        if (BlackcatUtils.HasBlackcat)%0D%0A    
     
@@ -1862,34 +1862,32 @@
 sDebugEnabled())
- %7B
 %0D%0A              
@@ -1961,35 +1961,8 @@
 );%0D%0A
-                        %7D%0D%0A
     
@@ -2037,16 +2037,20 @@
  maxRows
+ + 1
 )%0D%0A     
@@ -2144,34 +2144,32 @@
 sDebugEnabled())
- %7B
 %0D%0A              
@@ -2224,39 +2224,16 @@
  + s);%0D%0A
-                    %7D%0D%0A
         

fix max result rows problem