001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (c) 2004-2008 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.Connection;
025import java.sql.DatabaseMetaData;
026import java.sql.SQLException;
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 *
036 */
037public class DatabaseMetaDataInvocationHandler<D> extends AbstractChildInvocationHandler<D, Connection, DatabaseMetaData>
038{
039        private static final Set<Method> databaseReadMethodSet = Methods.findMethods(DatabaseMetaData.class, "getAttributes", "getBestRowIdentifier", "getCatalogs", "getColumnPrivileges", "getColumns", "getCrossReference", "getExportedKeys", "getFunctionColumns", "getFunctions", "getImportedKeys", "getIndexInfo", "getPrimaryKeys", "getProcedureColumns", "getProcedures", "getSchemas", "getSuperTables", "getSuperTypes", "getTablePrivileges", "getTables", "getUDTs", "getVersionColumns");
040        private static final Method getConnectionMethod = Methods.getMethod(DatabaseMetaData.class, "getConnection");
041        
042        /**
043         * @param parent
044         * @param proxy
045         * @param invoker
046         * @param objectMap
047         * @throws Exception
048         */
049        public DatabaseMetaDataInvocationHandler(Connection parent, SQLProxy<D, Connection> proxy, Invoker<D, Connection, DatabaseMetaData> invoker, Map<Database<D>, DatabaseMetaData> objectMap) throws Exception
050        {
051                super(parent, proxy, invoker, DatabaseMetaData.class, objectMap);
052        }
053
054        @Override
055        protected InvocationStrategy<D, DatabaseMetaData, ?> getInvocationStrategy(DatabaseMetaData object, Method method, Object[] parameters) throws Exception
056        {
057                if (databaseReadMethodSet.contains(method))
058                {
059                        return new DatabaseReadInvocationStrategy<D, DatabaseMetaData, Object>();
060                }
061                
062                if (method.equals(getConnectionMethod))
063                {
064                        return new InvocationStrategy<D, DatabaseMetaData, Connection>()
065                        {
066                                public Connection invoke(SQLProxy<D, DatabaseMetaData> proxy, Invoker<D, DatabaseMetaData, Connection> invoker) throws Exception
067                                {
068                                        return DatabaseMetaDataInvocationHandler.this.getParent();
069                                }
070                        };
071                }
072                
073                return new DriverReadInvocationStrategy<D, DatabaseMetaData, Object>();
074        }
075
076        @Override
077        protected void close(Connection parent, DatabaseMetaData object) throws SQLException
078        {
079                // Nothing to close
080        }
081}