001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (c) 2004-2007 Paul Ferraro
004 * 
005 * This library is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published by the 
007 * Free Software Foundation; either version 2.1 of the License, or (at your 
008 * option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this library; if not, write to the Free Software Foundation, 
017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018 * 
019 * Contact: ferraro@users.sourceforge.net
020 */
021package net.sf.hajdbc.sql;
022
023import java.lang.reflect.Method;
024import java.sql.CallableStatement;
025import java.sql.Connection;
026import java.sql.PreparedStatement;
027import java.util.Map;
028import java.util.Set;
029
030import net.sf.hajdbc.Database;
031import net.sf.hajdbc.util.reflect.Methods;
032
033/**
034 * @author Paul Ferraro
035 * @param <D> 
036 */
037@SuppressWarnings("nls")
038public class CallableStatementInvocationHandler<D> extends AbstractPreparedStatementInvocationHandler<D, CallableStatement>
039{
040        private static final Set<Method> registerOutParameterMethodSet = Methods.findMethods(CallableStatement.class, "registerOutParameter");
041        private static final Set<Method> setMethodSet = Methods.findMethods(CallableStatement.class, "set\\w+");
042        private static final Set<Method> driverReadMethodSet = Methods.findMethods(CallableStatement.class, "get\\w+", "wasNull");
043        {
044                driverReadMethodSet.removeAll(Methods.findMethods(PreparedStatement.class, "get\\w+"));
045        }
046        
047        /**
048         * @param connection
049         * @param proxy
050         * @param invoker
051         * @param statementMap
052         * @param transactionContext 
053         * @param fileSupport 
054         * @throws Exception
055         */
056        public CallableStatementInvocationHandler(Connection connection, SQLProxy<D, Connection> proxy, Invoker<D, Connection, CallableStatement> invoker, Map<Database<D>, CallableStatement> statementMap, TransactionContext<D> transactionContext, FileSupport fileSupport) throws Exception
057        {
058                super(connection, proxy, invoker, CallableStatement.class, statementMap, transactionContext, fileSupport, setMethodSet);
059        }
060
061        /**
062         * @see net.sf.hajdbc.sql.AbstractStatementInvocationHandler#getInvocationStrategy(java.sql.Statement, java.lang.reflect.Method, java.lang.Object[])
063         */
064        @Override
065        protected InvocationStrategy<D, CallableStatement, ?> getInvocationStrategy(CallableStatement statement, Method method, Object[] parameters) throws Exception
066        {
067                if (registerOutParameterMethodSet.contains(method))
068                {
069                        return new DriverWriteInvocationStrategy<D, CallableStatement, Object>();
070                }
071                
072                if (driverReadMethodSet.contains(method))
073                {
074                        return new DriverReadInvocationStrategy<D, CallableStatement, Object>();
075                }
076                
077                return super.getInvocationStrategy(statement, method, parameters);
078        }
079
080        /**
081         * @see net.sf.hajdbc.sql.AbstractPreparedStatementInvocationHandler#isBatchMethod(java.lang.reflect.Method)
082         */
083        @Override
084        protected boolean isBatchMethod(Method method)
085        {
086                return registerOutParameterMethodSet.contains(method) || super.isBatchMethod(method);
087        }
088
089        /**
090         * @see net.sf.hajdbc.sql.AbstractPreparedStatementInvocationHandler#isIndexType(java.lang.Class)
091         */
092        @Override
093        protected boolean isIndexType(Class<?> type)
094        {
095                return type.equals(String.class) || super.isIndexType(type);
096        }
097}