001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.dbutils;
019
020import java.sql.CallableStatement;
021import java.sql.SQLException;
022
023/**
024 * Represents an OUT parameter for a stored procedure.  When running a stored
025 * procedure with {@link QueryRunner}, pass an instance of
026 * {@code OutParameter} to indicate that the parameter at that index is an
027 * OUT parameter.  The value of the parameter may be obtained from the
028 * {@code OutParameter} instance via {@link #getValue() }.
029 * <p>
030 * INOUT parameters are also supported by setting the {@code value} of
031 * the {@code OutParameter} instance before invoking the stored procedure.
032 *
033 * @param <T> the class of the parameter; should be compatible via cast with the
034 * class returned by the {@code CallableStatement.getObject(int)} method.
035 */
036public class OutParameter<T> {
037    private final int sqlType;
038    private final Class<T> javaType;
039    private T value = null;
040
041    /**
042     * Construct an {@code OutParameter} for the given JDBC SQL type and
043     * Java type.
044     * @param sqlType the JDBC SQL type of the parameter as in
045     * {@code java.sql.Types}.
046     * @param javaType the Java class of the parameter value, cast compatible
047     * with the type returned by {@code CallableStatement.getObject(int)}
048     * for the JDBC type given by {@code sqlType}.
049     */
050    public OutParameter(final int sqlType, final Class<T> javaType) {
051        this.sqlType = sqlType;
052        this.javaType = javaType;
053    }
054
055    /**
056     * Construct an {@code OutParameter} for the given JDBC SQL type and
057     * Java type and with the given value.  The parameter will be treated as an
058     * INOUT parameter if the value is null.
059     * @param sqlType the JDBC SQL type of the parameter as in
060     * {@code java.sql.Types}.
061     * @param javaType the Java class of the parameter value, cast compatible
062     * with the type returned by {@code CallableStatement.getObject(int)}
063     * for the JDBC type given by {@code sqlType}.
064     * @param value the IN value of the parameter
065     */
066    public OutParameter(final int sqlType, final Class<T> javaType, final T value) {
067        this.sqlType = sqlType;
068        this.javaType = javaType;
069        this.value = value;
070    }
071
072    /**
073     * Get the JDBC SQL type for this OUT parameter.
074     * @return the JDBC SQL type for this OUT parameter.
075     */
076    public int getSqlType() {
077        return sqlType;
078    }
079
080    /**
081     * Get the Java class for this OUT parameter.
082     * @return the Java class for this OUT parameter.
083     */
084    public Class<T> getJavaType() {
085        return javaType;
086    }
087
088    /**
089     * Get the value of the OUT parameter.  After the stored procedure has
090     * been executed, the value is the value returned via this parameter.
091     * @return the value of the OUT parameter.
092     */
093    public T getValue() {
094        return value;
095    }
096
097    /**
098     * Set the value of the OUT parameter.  If the value is not null when the
099     * stored procedure is executed, then the parameter will be treated like an
100     * INOUT parameter.
101     * @param value the new value for the parameter.
102     */
103    public void setValue(final T value) {
104        this.value = value;
105    }
106
107    /**
108     * Set the value using the return value of the parameter an the given index
109     * from the given {@code CallableStatement}.
110     * @param stmt the already executed statement
111     * @param index the (1-based) index of the parameter
112     * @throws SQLException when the value could not be retrieved from the
113     * statement.
114     */
115    void setValue(final CallableStatement stmt, final int index) throws SQLException {
116        final Object object = stmt.getObject(index);
117        value = javaType.cast(object);
118    }
119
120    /**
121     * Set up the given statement by registering an OUT parameter at the given
122     * index using the {@code sqlType} and {@code value} of this
123     * {@code OutParameter}.  If the value is not null, the parameter is
124     * treated like an INOUT parameter and the value is set on the statement.
125     * @param stmt the statement the parameter should register on.
126     * @param index the (1-based) index of the parameter.
127     * @throws SQLException if the parameter could not be registered, or if the
128     * value of the parameter could not be set.
129     */
130    void register(final CallableStatement stmt, final int index) throws SQLException {
131        stmt.registerOutParameter(index, sqlType);
132        if (value != null) {
133            stmt.setObject(index, value);
134        }
135    }
136
137    @Override
138    public String toString() {
139        return "OutParameter{" + "sqlType=" + sqlType + ", javaType="
140            + javaType + ", value=" + value + '}';
141    }
142}