001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.support.nativejdbc;
018
019import java.sql.CallableStatement;
020import java.sql.Connection;
021import java.sql.PreparedStatement;
022import java.sql.ResultSet;
023import java.sql.SQLException;
024import java.sql.Statement;
025
026/**
027 * {@link NativeJdbcExtractor} implementation that delegates to JDBC 4.0's
028 * {@code unwrap} method, as defined by {@link java.sql.Wrapper}.
029 * You will typically need to specify a vendor {@link #setConnectionType Connection type}
030 * / {@link #setStatementType Statement type} / {@link #setResultSetType ResultSet type}
031 * to extract, since JDBC 4.0 only actually unwraps to a given target type.
032 *
033 * <p>Note: Only use this when actually running against a JDBC 4.0 driver, with a
034 * connection pool that supports the JDBC 4.0 API (i.e. at least accepts JDBC 4.0
035 * API calls and passes them through to the underlying driver)! Other than that,
036 * there is no need for connection pool specific setup. As of JDBC 4.0,
037 * NativeJdbcExtractors will typically be implemented for specific drivers
038 * instead of for specific pools (e.g. {@link OracleJdbc4NativeJdbcExtractor}).
039 *
040 * @author Juergen Hoeller
041 * @since 2.5
042 * @see java.sql.Wrapper#unwrap
043 * @see SimpleNativeJdbcExtractor
044 * @see org.springframework.jdbc.core.JdbcTemplate#setNativeJdbcExtractor
045 * @see org.springframework.jdbc.support.lob.OracleLobHandler#setNativeJdbcExtractor
046 */
047public class Jdbc4NativeJdbcExtractor extends NativeJdbcExtractorAdapter {
048
049        private Class<? extends Connection> connectionType = Connection.class;
050
051        private Class<? extends Statement> statementType = Statement.class;
052
053        private Class<? extends PreparedStatement> preparedStatementType = PreparedStatement.class;
054
055        private Class<? extends CallableStatement> callableStatementType = CallableStatement.class;
056
057        private Class<? extends ResultSet> resultSetType = ResultSet.class;
058
059
060        /**
061         * Set the vendor's Connection type, e.g. {@code oracle.jdbc.OracleConnection}.
062         */
063        public void setConnectionType(Class<? extends Connection> connectionType) {
064                this.connectionType = connectionType;
065        }
066
067        /**
068         * Set the vendor's Statement type, e.g. {@code oracle.jdbc.OracleStatement}.
069         */
070        public void setStatementType(Class<? extends Statement> statementType) {
071                this.statementType = statementType;
072        }
073
074        /**
075         * Set the vendor's PreparedStatement type, e.g. {@code oracle.jdbc.OraclePreparedStatement}.
076         */
077        public void setPreparedStatementType(Class<? extends PreparedStatement> preparedStatementType) {
078                this.preparedStatementType = preparedStatementType;
079        }
080
081        /**
082         * Set the vendor's CallableStatement type, e.g. {@code oracle.jdbc.OracleCallableStatement}.
083         */
084        public void setCallableStatementType(Class<? extends CallableStatement> callableStatementType) {
085                this.callableStatementType = callableStatementType;
086        }
087
088        /**
089         * Set the vendor's ResultSet type, e.g. {@code oracle.jdbc.OracleResultSet}.
090         */
091        public void setResultSetType(Class<? extends ResultSet> resultSetType) {
092                this.resultSetType = resultSetType;
093        }
094
095
096        @Override
097        protected Connection doGetNativeConnection(Connection con) throws SQLException {
098                return con.unwrap(this.connectionType);
099        }
100
101        @Override
102        public Statement getNativeStatement(Statement stmt) throws SQLException {
103                return stmt.unwrap(this.statementType);
104        }
105
106        @Override
107        public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException {
108                return ps.unwrap(this.preparedStatementType);
109        }
110
111        @Override
112        public CallableStatement getNativeCallableStatement(CallableStatement cs) throws SQLException {
113                return cs.unwrap(this.callableStatementType);
114        }
115
116        @Override
117        public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
118                return rs.unwrap(this.resultSetType);
119        }
120
121}