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

net.sf.hajdbc.sql.ResultSetInvocationHandler Maven / Gradle / Ivy

There is a newer version: 3.6.61
Show newest version
/*
 * HA-JDBC: High-Availability JDBC
 * Copyright (C) 2012  Paul Ferraro
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package net.sf.hajdbc.sql;

import java.lang.reflect.Method;
import java.sql.Clob;
import java.sql.NClob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;

import net.sf.hajdbc.Database;
import net.sf.hajdbc.invocation.InvocationStrategies;
import net.sf.hajdbc.invocation.InvocationStrategy;
import net.sf.hajdbc.invocation.Invoker;
import net.sf.hajdbc.util.reflect.Methods;

/**
 * @author Paul Ferraro
 * @param  
 * @param  
 */
@SuppressWarnings("nls")
public class ResultSetInvocationHandler, S extends Statement> extends InputSinkRegistryInvocationHandler>
{
	private static final Set driverReadMethodSet = Methods.findMethods(ResultSet.class, "findColumn", "getConcurrency", "getCursorName", "getFetchDirection", "getFetchSize", "getHoldability", "getMetaData", "getRow", "getType", "getWarnings", "isAfterLast", "isBeforeFirst", "isClosed", "isFirst", "isLast", "row(Deleted|Inserted|Updated)", "wasNull");
	private static final Set driverWriteMethodSet = Methods.findMethods(ResultSet.class, "absolute", "afterLast", "beforeFirst", "cancelRowUpdates", "clearWarnings", "first", "last", "moveTo(Current|Insert)Row", "next", "previous", "relative", "setFetchDirection", "setFetchSize");
	private static final Set transactionalWriteMethodSet = Methods.findMethods(ResultSet.class, "(delete|insert|update)Row");
	private static final Set getArrayMethodSet = Methods.findMethods(ResultSet.class, "getArray");
	private static final Set getBlobMethodSet = Methods.findMethods(ResultSet.class, "getBlob");
	private static final Set getClobMethodSet = Methods.findMethods(ResultSet.class, "getClob");
	private static final Set getNClobMethodSet = Methods.findMethods(ResultSet.class, "getNClob");
	private static final Set getRefMethodSet = Methods.findMethods(ResultSet.class, "getRef");
	private static final Set getSQLXMLMethodSet = Methods.findMethods(ResultSet.class, "getSQLXML");

	private static final Method closeMethod = Methods.getMethod(ResultSet.class, "close");
	private static final Method getStatementMethod = Methods.getMethod(ResultSet.class, "getStatement");
	
	/**
	 * @param factory a factory for creating result set proxies
	 */
	public ResultSetInvocationHandler(ResultSetProxyFactory factory)
	{
		super(ResultSet.class, factory, getStatementMethod);
	}

	@Override
	protected ProxyFactoryFactory getProxyFactoryFactory(ResultSet object, Method method, Object... parameters) throws SQLException
	{
		if (getArrayMethodSet.contains(method))
		{
			return new ArrayProxyFactoryFactory(this.getProxyFactory().locatorsUpdateCopy());
		}
		if (getBlobMethodSet.contains(method))
		{
			return new BlobProxyFactoryFactory(this.getProxyFactory().locatorsUpdateCopy());
		}
		if (getClobMethodSet.contains(method))
		{
			return new ClobProxyFactoryFactory(Clob.class, this.getProxyFactory().locatorsUpdateCopy());
		}
		if (getNClobMethodSet.contains(method))
		{
			return new ClobProxyFactoryFactory(NClob.class, this.getProxyFactory().locatorsUpdateCopy());
		}
		if (getRefMethodSet.contains(method))
		{
			return new RefProxyFactoryFactory(this.getProxyFactory().locatorsUpdateCopy());
		}
		if (getSQLXMLMethodSet.contains(method))
		{
			return new SQLXMLProxyFactoryFactory(this.getProxyFactory().locatorsUpdateCopy());
		}
		
		return super.getProxyFactoryFactory(object, method, parameters);
	}

	@Override
	protected InvocationStrategy getInvocationStrategy(ResultSet resultSet, Method method, Object... parameters) throws SQLException
	{
		if (driverReadMethodSet.contains(method))
		{
			return InvocationStrategies.INVOKE_ON_ANY;
		}
		
		if (driverWriteMethodSet.contains(method) || method.equals(closeMethod))
		{
			return InvocationStrategies.INVOKE_ON_EXISTING;
		}
		
		if (transactionalWriteMethodSet.contains(method))
		{
			return this.getProxyFactory().getTransactionContext().start(InvocationStrategies.TRANSACTION_INVOKE_ON_ALL, this.getProxyFactory().getParentProxy().getConnection());
		}
		
		if (isGetMethod(method))
		{
			return InvocationStrategies.INVOKE_ON_ANY;
		}
		
		if (isUpdateMethod(method))
		{
			return InvocationStrategies.INVOKE_ON_EXISTING;
		}
		
		return super.getInvocationStrategy(resultSet, method, parameters);
	}

	@Override
	protected  Invoker getInvoker(ResultSet results, final Method method, final Object... parameters) throws SQLException
	{
		if (isUpdateMethod(method) && (parameters.length > 1))
		{
			return this.getInvoker(method.getParameterTypes()[1], 1, results, method, parameters);
		}
		
		return super.getInvoker(results, method, parameters);
	}

	@Override
	protected  void postInvoke(Invoker invoker, ResultSet results, Method method, Object... parameters)
	{
		if (method.equals(closeMethod))
		{
			this.getProxyFactory().remove();
		}
		
		if (driverWriteMethodSet.contains(method) || isUpdateMethod(method))
		{
			this.getProxyFactory().addInvoker(invoker);
		}
	}
	
	private static boolean isGetMethod(Method method)
	{
		Class[] types = method.getParameterTypes();
		
		return method.getName().startsWith("get") && (types != null) && (types.length > 0) && (types[0].equals(String.class) || types[0].equals(Integer.TYPE));
	}
	
	private static boolean isUpdateMethod(Method method)
	{
		Class[] types = method.getParameterTypes();
		
		return method.getName().startsWith("update") && (types != null) && (types.length > 0) && (types[0].equals(String.class) || types[0].equals(Integer.TYPE));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy