001/*
002 * Copyright 2002-2018 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.core;
018
019import java.sql.PreparedStatement;
020import java.sql.SQLException;
021
022import org.springframework.jdbc.support.JdbcUtils;
023import org.springframework.lang.Nullable;
024
025/**
026 * Interface to be implemented for setting values for more complex database-specific
027 * types not supported by the standard {@code setObject} method. This is
028 * effectively an extended variant of {@link org.springframework.jdbc.support.SqlValue}.
029 *
030 * <p>Implementations perform the actual work of setting the actual values. They must
031 * implement the callback method {@code setTypeValue} which can throw SQLExceptions
032 * that will be caught and translated by the calling code. This callback method has
033 * access to the underlying Connection via the given PreparedStatement object, if that
034 * should be needed to create any database-specific objects.
035 *
036 * @author Thomas Risberg
037 * @author Juergen Hoeller
038 * @since 1.1
039 * @see java.sql.Types
040 * @see java.sql.PreparedStatement#setObject
041 * @see JdbcOperations#update(String, Object[], int[])
042 * @see org.springframework.jdbc.support.SqlValue
043 */
044public interface SqlTypeValue {
045
046        /**
047         * Constant that indicates an unknown (or unspecified) SQL type.
048         * Passed into {@code setTypeValue} if the original operation method
049         * does not specify an SQL type.
050         * @see java.sql.Types
051         * @see JdbcOperations#update(String, Object[])
052         */
053        int TYPE_UNKNOWN = JdbcUtils.TYPE_UNKNOWN;
054
055
056        /**
057         * Set the type value on the given PreparedStatement.
058         * @param ps the PreparedStatement to work on
059         * @param paramIndex the index of the parameter for which we need to set the value
060         * @param sqlType the SQL type of the parameter we are setting
061         * @param typeName the type name of the parameter (optional)
062         * @throws SQLException if an SQLException is encountered while setting parameter values
063         * @see java.sql.Types
064         * @see java.sql.PreparedStatement#setObject
065         */
066        void setTypeValue(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName)
067                        throws SQLException;
068
069}