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

org.cattleframework.db.datasource.reflect.invocation.StatementInvocationHandler Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * 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.cattleframework.db.datasource.reflect.invocation;

import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.cattleframework.db.datasource.DataSourceConstants;
import org.cattleframework.utils.reflect.ReflectUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.reflect.AbstractInvocationHandler;

/**
 * 数据库语句调用处理
 * 
 * @author orange
 *
 */
public class StatementInvocationHandler extends AbstractInvocationHandler {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    protected static final String METHOD_NAME_EXECUTE_QUERY = "executeQuery";

    protected static final String METHOD_NAME_EXECUTE_UPDATE = "executeUpdate";

    protected static final String METHOD_NAME_EXECUTE = "execute";

    private static final String SET_OBJECT_METHOD_NAME = "setObject";

    private static final String[] SET_METHOD_NAMES = { "setString", "setNString", "setInt", "setFloat", "setShort",
	    "setDouble", "setBigDecimal", "setLong", "setBoolean" };

    private static final String[] DATETIME_SET_METHOD_NAMES = { "setTimestamp", "setDate", "setTime" };

    private static final String SET_BINARY_STREAM_METHOD_NAME = "setBinaryStream";

    private static final String SET_BLOB_METHOD_NAME = "setBlob";

    private static final String[] SET_CLOB_METHOD_NAMES = { "setClob", "setNClob" };

    private static final String SET_NULL_METHOD_NAME = "setNull";

    private static final String[] SET_CHARACTER_STREAM_METHOD_NAMES = { "setCharacterStream", "setNCharacterStream" };

    private static final int DATETIME_SET_METHOD_CALENDAR_ARGS = 3;

    protected static final String METHOD_NAME_ADD_BATCH = "addBatch";

    private static final String METHOD_NAME_EXECUTE_BATCH = "executeBatch";

    private static final String METHOD_NAME_CLEAR_BATCH = "clearBatch";

    private static final String METHOD_NAME_CLEAR_PARAMETERS = "clearParameters";

    private final Statement target;

    private final SortedMap parameters;

    private final List batchStatementSqlInfos;

    private boolean logDebug = false;

    private boolean logInfo = false;

    public StatementInvocationHandler(Statement target) {
	this.target = target;
	parameters = new TreeMap();
	batchStatementSqlInfos = new ArrayList();
	if (logger.isDebugEnabled()) {
	    logDebug = true;
	} else if (logger.isInfoEnabled()) {
	    logInfo = true;
	}
    }

    protected void log(String format, Object... arguments) {
	if (logDebug) {
	    logger.debug(format, arguments);
	} else if (logInfo) {
	    logger.info(format, arguments);
	}
    }

    @Override
    protected Object handleInvocation(Object proxy, Method method, @Nullable Object[] args) throws Throwable {
	String methodName = method.getName();
	if (DataSourceConstants.METHOD_NAME_UNWRAP.equals(methodName)) {
	    if (((Class) args[0]).isInstance(proxy)) {
		return proxy;
	    }
	} else if (DataSourceConstants.METHOD_NAME_IS_WRAPPER_FOR.equals(methodName)) {
	    if (((Class) args[0]).isInstance(proxy)) {
		return true;
	    }
	} else if (METHOD_NAME_EXECUTE_QUERY.equals(methodName)) {
	    return executeQuery(method, (String) args[0], args);
	} else if (METHOD_NAME_EXECUTE_UPDATE.equals(methodName)) {
	    return executeUpdate(method, (String) args[0], args);
	} else if (METHOD_NAME_EXECUTE.equals(methodName)) {
	    return execute(method, (String) args[0], args);
	}
	if (SET_NULL_METHOD_NAME.equals(methodName)) {
	    parameters.put(args[0], "{空值}");
	} else if (SET_OBJECT_METHOD_NAME.equals(methodName)) {
	    if (args[1] == null) {
		parameters.put(args[0], "{空值}");
	    } else if (args[1] instanceof byte[] || args[1] instanceof Byte[]) {
		parameters.put(args[0], "{二进制流}");
	    } else {
		parameters.put(args[0], String.valueOf(args[1]));
	    }
	} else if (ArrayUtils.contains(SET_METHOD_NAMES, methodName)) {
	    parameters.put(args[0], args[1] == null ? "{空值}" : String.valueOf(args[1]));
	} else if (ArrayUtils.contains(DATETIME_SET_METHOD_NAMES, methodName)) {
	    if (args.length == DATETIME_SET_METHOD_CALENDAR_ARGS) {
		parameters.put(args[0], args[1] == null ? "{空值}"
			: DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format((Date) args[1])
				+ (args[2] != null ? "(" + ((Calendar) args[2]).getTimeZone().getDisplayName() + ")"
					: ""));
	    } else {
		parameters.put(args[0], args[1] == null ? "{空值}"
			: DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT.format((Date) args[1]));
	    }
	} else if (SET_BINARY_STREAM_METHOD_NAME.equals(methodName)) {
	    parameters.put(args[0], args[1] == null ? "{空值}" : "{二进制流}");
	} else if (ArrayUtils.contains(SET_CHARACTER_STREAM_METHOD_NAMES, methodName)) {
	    parameters.put(args[0], args[1] == null ? "{空值}" : "{字符流}");
	} else if (SET_BLOB_METHOD_NAME.equals(methodName)) {
	    parameters.put(args[0], args[1] == null ? "{空值}" : "{Blob对象}");
	} else if (ArrayUtils.contains(SET_CLOB_METHOD_NAMES, methodName)) {
	    parameters.put(args[0], args[1] == null ? "{空值}" : "{Clob对象}");
	} else if (METHOD_NAME_ADD_BATCH.equals(methodName)) {
	    return addBatch(method, (String) args[0], args);
	} else if (METHOD_NAME_EXECUTE_BATCH.equals(methodName)) {
	    return executeBatch(method, args);
	} else if (METHOD_NAME_CLEAR_BATCH.equals(methodName)) {
	    batchStatementSqlInfos.clear();
	} else if (METHOD_NAME_CLEAR_PARAMETERS.equals(methodName)) {
	    parameters.clear();
	}
	return ReflectUtils.invokeObjectMethod(method, target, args);
    }

    private Object executeBatch(Method method, Object... args) throws Throwable {
	long startTime = System.currentTimeMillis();
	int[] result = (int[]) ReflectUtils.invokeObjectMethod(method, target);
	long endTime = System.currentTimeMillis();
	int size = batchStatementSqlInfos.size();
	if (size > 0) {
	    if (size == 1) {
		BatchStatementSqlInfo batchStatementSqlInfo = batchStatementSqlInfos.get(0);
		if (StringUtils.isNotBlank(batchStatementSqlInfo.getParametersInfo())) {
		    log("执行SQL语句:{},参数:[{}],成功处理:{}条数据,执行时间:{}毫秒", batchStatementSqlInfo.getSql(),
			    batchStatementSqlInfo.getParametersInfo(), result[0], (endTime - startTime));
		} else {
		    log("执行SQL语句:{},成功处理:{}条数据,执行时间:{}毫秒", batchStatementSqlInfo.getSql(), result[0],
			    (endTime - startTime));
		}
	    } else {
		log("开始执行批处理SQL语句");
		for (int i = 0; i < size; i++) {
		    BatchStatementSqlInfo batchStatementSqlInfo = batchStatementSqlInfos.get(i);
		    if (StringUtils.isNotBlank(batchStatementSqlInfo.getParametersInfo())) {
			log("执行第{}条SQL语句:{},参数:[{}],成功处理:{}条数据", i + 1, batchStatementSqlInfo.getSql(),
				batchStatementSqlInfo.getParametersInfo(), result[i]);
		    } else {
			log("执行第{}条SQL语句:{},成功处理:{}条数据", i + 1, batchStatementSqlInfo.getSql(), result[i]);
		    }
		}
		log("结束执行批处理SQL语句,总共执行{}条SQL语句,总共执行时间:{}毫秒", size, endTime - startTime);
	    }
	    batchStatementSqlInfos.clear();
	}
	return result;
    }

    protected Object addBatch(Method method, String sql, Object... args) throws Throwable {
	Object result = ReflectUtils.invokeObjectMethod(method, target, args);
	batchStatementSqlInfos.add(new BatchStatementSqlInfo(sql, getParametersInfo()));
	return result;
    }

    protected boolean execute(Method method, String sql, Object... args) throws Throwable {
	long startTime = System.currentTimeMillis();
	boolean result = (boolean) ReflectUtils.invokeObjectMethod(method, target, args);
	long endTime = System.currentTimeMillis();
	log("执行SQL语句:{},执行时间:{}毫秒", sql, (endTime - startTime));
	return result;
    }

    protected ResultSet executeQuery(Method method, String sql, Object... args) throws Throwable {
	long startTime = System.currentTimeMillis();
	ResultSet result = (ResultSet) ReflectUtils.invokeObjectMethod(method, target, args);
	long endTime = System.currentTimeMillis();
	if (parameters.size() > 0) {
	    String parametersInfo = getParametersInfo();
	    log("执行SQL语句:{},参数:[{}],执行时间:{}毫秒", sql, parametersInfo, (endTime - startTime));
	} else {
	    log("执行SQL语句:{},执行时间:{}毫秒", sql, (endTime - startTime));
	}
	return result;
    }

    protected int executeUpdate(Method method, String sql, Object... args) throws Throwable {
	long startTime = System.currentTimeMillis();
	int records = (int) ReflectUtils.invokeObjectMethod(method, target, args);
	long endTime = System.currentTimeMillis();
	if (parameters.size() > 0) {
	    String parametersInfo = getParametersInfo();
	    log("执行SQL语句:{},参数:[{}],成功处理:{}条数据,执行时间:{}毫秒", sql, parametersInfo, records, (endTime - startTime));
	} else {
	    log("执行SQL语句:{},成功处理:{}条数据,执行时间:{}毫秒", sql, records, (endTime - startTime));
	}
	return records;
    }

    private String getParametersInfo() {
	StringBuffer stringBuffer = new StringBuffer();
	Iterator iterator = parameters.keySet().iterator();
	while (iterator.hasNext()) {
	    if (!stringBuffer.isEmpty()) {
		stringBuffer.append(",");
	    }
	    Object key = iterator.next();
	    String object = parameters.get(key);
	    if (key instanceof String) {
		stringBuffer.append((String) key + ":" + object);
	    } else {
		stringBuffer.append("#" + String.valueOf(key) + ":" + object);
	    }
	}
	parameters.clear();
	return stringBuffer.toString();
    }
}