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</code> implementation that converts one
026 * <code>ResultSet</code> 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()</code>.
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</code>.
057     */
058    public ScalarHandler(int columnIndex) {
059        this(columnIndex, null);
060    }
061
062    /**
063     * Creates a new instance of ScalarHandler.
064     *
065     * @param columnName The name of the column to retrieve from the
066     * <code>ResultSet</code>.
067     */
068    public ScalarHandler(String columnName) {
069        this(1, columnName);
070    }
071
072    /** Helper constructor
073     * @param columnIndex The index of the column to retrieve from the
074     * <code>ResultSet</code>.
075     * @param columnName The name of the column to retrieve from the
076     * <code>ResultSet</code>.
077     */
078    private ScalarHandler(int columnIndex, String columnName) {
079        this.columnIndex = columnIndex;
080        this.columnName = columnName;
081    }
082
083    /**
084     * Returns one <code>ResultSet</code> column as an object via the
085     * <code>ResultSet.getObject()</code> method that performs type
086     * conversions.
087     * @param rs <code>ResultSet</code> to process.
088     * @return The column or <code>null</code> if there are no rows in
089     * the <code>ResultSet</code>.
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(ResultSet rs) throws SQLException {
101
102        if (rs.next()) {
103            if (this.columnName == null) {
104                return (T) rs.getObject(this.columnIndex);
105            }
106            return (T) rs.getObject(this.columnName);
107        }
108        return null;
109    }
110}