All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdbcdslog.CallableStatementLoggingHandler Maven / Gradle / Ivy

package org.jdbcdslog;

import static org.jdbcdslog.Loggers.*;
import static org.jdbcdslog.ProxyUtils.*;

import java.lang.reflect.Method;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class CallableStatementLoggingHandler extends PreparedStatementLoggingHandler {

    protected TreeMap namedParameters = new TreeMap();
    protected List> batchNamedParameters = null;

    public CallableStatementLoggingHandler(LogMetaData logMetaData, CallableStatement ps, String sql) {
        super(logMetaData, ps, sql);
    }

    @Override
    protected boolean needsLogging(Object proxy,Method method, Object[] args) {
        return (statementLogger.isInfoEnabled() || slowQueryLogger.isInfoEnabled())
                && EXECUTE_METHODS.contains(method.getName());
    }

    @Override
    protected void appendStatement(StringBuilder sb, Object proxy, Method method, Object[] args) {
        LogUtils.appendSql(sb, sql, parameters, namedParameters);
    }

    @Override
    protected void doAddBatch(Object proxy, Method method, Object[] args) {
        if (namedParameters.isEmpty()) {
            super.doAddBatch(proxy,method,args);
        } else {
            if (this.batchNamedParameters == null) {
                this.batchNamedParameters = new ArrayList>();
            }
            this.batchNamedParameters.add(new TreeMap(this.namedParameters));
        }
    }

    @Override
    protected void appendBatchStatements(StringBuilder sb) {
        LogUtils.appendBatchSqls(sb, sql, batchParameters, batchNamedParameters);
    }

    @Override
    protected Object doAfterInvoke(Object proxy,Method method, Object[] args, Object result) {
        Object r = result;

        if (UNWRAP_METHOD_NAME.equals(method.getName())) {
            Class unwrapClass = (Class)args[0];
            if (r == target && unwrapClass.isInstance(proxy)) {
                r = proxy;      // returning original proxy if it is enough to represent the unwrapped obj
            } else if (unwrapClass.isInterface() && CallableStatement.class.isAssignableFrom(unwrapClass)) {
                r = wrapByCallableStatementProxy(logMetaData, (CallableStatement)r, sql);
            }
        }

        if (r instanceof ResultSet) {
            r = wrapByResultSetProxy(logMetaData, (ResultSet) r);
        }

        if (SET_METHODS.contains(method.getName())) {
             if (args[0] instanceof Integer) {
                 parameters.put((Integer)args[0], args[1]);
             } else if (args[0] instanceof String) {
                 namedParameters.put((String)args[0], args[1]);
             }
        }
        if ("clearParameters".equals(method.getName())) {
            parameters.clear();
            namedParameters.clear();
        }
        return r;
    }

    @Override
    protected void handleException(Throwable t, Object proxy, Method method, Object[] args) throws Throwable {
        LogUtils.handleException(t, statementLogger, LogUtils.createLogEntry(method, sql, parameters, namedParameters));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy