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 */
017package org.apache.commons.dbutils.handlers;
018
019import java.sql.ResultSet;
020import java.sql.SQLException;
021
022import org.apache.commons.dbutils.ResultSetHandler;
023
024/**
025 * {@code ResultSetHandler} implementation that converts one
026 * {@code ResultSet} column into an Object. This class is thread safe.
027 *
028 * @param <T> The type of the scalar
029 * @see org.apache.commons.dbutils.ResultSetHandler
030 */
031public class ScalarHandler<T> implements ResultSetHandler<T> {
032
033    /**
034     * The column number to retrieve.
035     */
036    private final int columnIndex;
037
038    /**
039     * The column name to retrieve.  Either columnName or columnIndex
040     * will be used but never both.
041     */
042    private final String columnName;
043
044    /**
045     * Creates a new instance of ScalarHandler.  The first column will
046     * be returned from {@code handle()}.
047     */
048    public ScalarHandler() {
049        this(1, null);
050    }
051
052    /**
053     * Creates a new instance of ScalarHandler.
054     *
055     * @param columnIndex The index of the column to retrieve from the
056     * {@code ResultSet}.
057     */
058    public ScalarHandler(final int columnIndex) {
059        this(columnIndex, null);
060    }
061
062    /** Helper constructor
063     * @param columnIndex The index of the column to retrieve from the
064     * {@code ResultSet}.
065     * @param columnName The name of the column to retrieve from the
066     * {@code ResultSet}.
067     */
068    private ScalarHandler(final int columnIndex, final String columnName) {
069        this.columnIndex = columnIndex;
070        this.columnName = columnName;
071    }
072
073    /**
074     * Creates a new instance of ScalarHandler.
075     *
076     * @param columnName The name of the column to retrieve from the
077     * {@code ResultSet}.
078     */
079    public ScalarHandler(final String columnName) {
080        this(1, columnName);
081    }
082
083    /**
084     * Returns one {@code ResultSet} column as an object via the
085     * {@code ResultSet.getObject()} method that performs type
086     * conversions.
087     * @param resultSet {@code ResultSet} to process.
088     * @return The column or {@code null} if there are no rows in
089     * the {@code ResultSet}.
090     *
091     * @throws SQLException if a database access error occurs
092     * @throws ClassCastException if the class datatype does not match the column type
093     *
094     * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
095     */
096    // We assume that the user has picked the correct type to match the column
097    // so getObject will return the appropriate type and the cast will succeed.
098    @SuppressWarnings("unchecked")
099    @Override
100    public T handle(final ResultSet resultSet) throws SQLException {
101
102        if (resultSet.next()) {
103            if (this.columnName == null) {
104                return (T) resultSet.getObject(this.columnIndex);
105            }
106            return (T) resultSet.getObject(this.columnName);
107        }
108        return null;
109    }
110}