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 Java class for this OUT parameter.
074     * @return the Java class for this OUT parameter.
075     */
076    public Class<T> getJavaType() {
077        return javaType;
078    }
079
080    /**
081     * Get the JDBC SQL type for this OUT parameter.
082     * @return the JDBC SQL type for this OUT parameter.
083     */
084    public int getSqlType() {
085        return sqlType;
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 up the given statement by registering an OUT parameter at the given
099     * index using the {@code sqlType} and {@code value} of this
100     * {@code OutParameter}.  If the value is not null, the parameter is
101     * treated like an INOUT parameter and the value is set on the statement.
102     * @param stmt the statement the parameter should register on.
103     * @param index the (1-based) index of the parameter.
104     * @throws SQLException if the parameter could not be registered, or if the
105     * value of the parameter could not be set.
106     */
107    void register(final CallableStatement stmt, final int index) throws SQLException {
108        stmt.registerOutParameter(index, sqlType);
109        if (value != null) {
110            stmt.setObject(index, value);
111        }
112    }
113
114    /**
115     * Set the value using the return value of the parameter an the given index
116     * from the given {@code CallableStatement}.
117     * @param stmt the already executed statement
118     * @param index the (1-based) index of the parameter
119     * @throws SQLException when the value could not be retrieved from the
120     * statement.
121     */
122    void setValue(final CallableStatement stmt, final int index) throws SQLException {
123        value = javaType.cast(stmt.getObject(index));
124    }
125
126    /**
127     * Set the value of the OUT parameter.  If the value is not null when the
128     * stored procedure is executed, then the parameter will be treated like an
129     * INOUT parameter.
130     * @param value the new value for the parameter.
131     */
132    public void setValue(final T value) {
133        this.value = value;
134    }
135
136    @Override
137    public String toString() {
138        return "OutParameter{" + "sqlType=" + sqlType + ", javaType="
139            + javaType + ", value=" + value + '}';
140    }
141}