001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.sql;
019
020import java.lang.reflect.Method;
021import java.sql.Connection;
022import java.sql.PreparedStatement;
023import java.sql.ResultSet;
024import java.sql.SQLException;
025import java.util.List;
026import java.util.Set;
027import java.util.concurrent.locks.Lock;
028
029import net.sf.hajdbc.Database;
030import net.sf.hajdbc.invocation.InvocationStrategies;
031import net.sf.hajdbc.invocation.InvocationStrategy;
032import net.sf.hajdbc.invocation.Invoker;
033import net.sf.hajdbc.invocation.LockingInvocationStrategy;
034import net.sf.hajdbc.util.reflect.Methods;
035
036/**
037 * @author Paul Ferraro
038 * @param <D> 
039 * @param <S> 
040 */
041public abstract class AbstractPreparedStatementInvocationHandler<Z, D extends Database<Z>, S extends PreparedStatement, F extends AbstractPreparedStatementProxyFactory<Z, D, S>> extends AbstractStatementInvocationHandler<Z, D, S, F>
042{
043        private static final Set<Method> databaseReadMethodSet = Methods.findMethods(PreparedStatement.class, "getMetaData", "getParameterMetaData");
044        private static final Method executeMethod = Methods.getMethod(PreparedStatement.class, "execute");
045        private static final Method executeUpdateMethod = Methods.getMethod(PreparedStatement.class, "executeUpdate");
046        private static final Method executeQueryMethod = Methods.getMethod(PreparedStatement.class, "executeQuery");
047        private static final Method clearParametersMethod = Methods.getMethod(PreparedStatement.class, "clearParameters");
048        private static final Method addBatchMethod = Methods.getMethod(PreparedStatement.class, "addBatch");
049        
050        private final Set<Method> setMethods;
051        
052        public AbstractPreparedStatementInvocationHandler(Class<S> statementClass, F proxyFactory, Set<Method> setMethods)
053        {
054                super(statementClass, proxyFactory);
055                this.setMethods = setMethods;
056        }
057        
058        @Override
059        protected ProxyFactoryFactory<Z, D, S, SQLException, ?, ? extends Exception> getProxyFactoryFactory(S object, Method method, Object... parameters) throws SQLException
060        {
061                if (method.equals(executeQueryMethod))
062                {
063                        return new ResultSetProxyFactoryFactory<Z, D, S>(this.getProxyFactory().getTransactionContext(), this.getProxyFactory().getInputSinkRegistry());
064                }
065                
066                return super.getProxyFactoryFactory(object, method, parameters);
067        }
068
069        /**
070         * @see net.sf.hajdbc.sql.AbstractStatementInvocationHandler#getInvocationStrategy(java.sql.Statement, java.lang.reflect.Method, java.lang.Object[])
071         */
072        @Override
073        protected InvocationStrategy getInvocationStrategy(S statement, Method method, Object... parameters) throws SQLException
074        {
075                if (databaseReadMethodSet.contains(method))
076                {
077                        return InvocationStrategies.INVOKE_ON_NEXT;
078                }
079                
080                if (this.setMethods.contains(method) || method.equals(clearParametersMethod) || method.equals(addBatchMethod))
081                {
082                        return InvocationStrategies.INVOKE_ON_EXISTING;
083                }
084                
085                if (method.equals(executeMethod) || method.equals(executeUpdateMethod))
086                {
087                        return this.getProxyFactory().getTransactionContext().start(new LockingInvocationStrategy(InvocationStrategies.TRANSACTION_INVOKE_ON_ALL, this.getProxyFactory().getLocks()), this.getProxyFactory().getParentProxy());
088                }
089                
090                if (method.equals(executeQueryMethod))
091                {
092                        List<Lock> locks = this.getProxyFactory().getLocks();
093                        int concurrency = statement.getResultSetConcurrency();
094                        boolean selectForUpdate = this.getProxyFactory().isSelectForUpdate();
095                        
096                        if (locks.isEmpty() && (concurrency == ResultSet.CONCUR_READ_ONLY) && !selectForUpdate)
097                        {
098                                boolean repeatableReadSelect = (statement.getConnection().getTransactionIsolation() >= Connection.TRANSACTION_REPEATABLE_READ);
099                                
100                                return repeatableReadSelect ? InvocationStrategies.INVOKE_ON_PRIMARY : InvocationStrategies.INVOKE_ON_NEXT;
101                        }
102                        
103                        InvocationStrategy strategy = InvocationStrategies.TRANSACTION_INVOKE_ON_ALL;
104                        if (!locks.isEmpty())
105                        {
106                                strategy = new LockingInvocationStrategy(strategy, locks);
107                        }
108                        
109                        return selectForUpdate ? this.getProxyFactory().getTransactionContext().start(strategy, this.getProxyFactory().getParentProxy()) : strategy;
110                }
111                
112                return super.getInvocationStrategy(statement, method, parameters);
113        }
114
115        @Override
116        protected <R> Invoker<Z, D, S, R, SQLException> getInvoker(S statement, final Method method, final Object... parameters) throws SQLException
117        {
118                if (this.isSetParameterMethod(method) && (parameters.length > 1))
119                {
120                        return this.getInvoker(method.getParameterTypes()[1], 1, statement, method, parameters);
121                }
122                
123                return super.getInvoker(statement, method, parameters);
124        }
125        
126        @Override
127        protected boolean isBatchMethod(Method method)
128        {
129                return method.equals(addBatchMethod) || method.equals(clearParametersMethod) || this.isSetParameterMethod(method) || super.isBatchMethod(method);
130        }
131
132        private boolean isSetParameterMethod(Method method)
133        {
134                Class<?>[] types = method.getParameterTypes();
135                
136                return this.setMethods.contains(method) && (types.length > 0) && this.isIndexType(types[0]);
137        }
138        
139        protected boolean isIndexType(Class<?> type)
140        {
141                return type.equals(Integer.TYPE);
142        }
143}