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
022/**
023 * <code>ResultSetHandler</code> implementation that converts one
024 * <code>ResultSet</code> column into a <code>List</code> of
025 * <code>Object</code>s. This class is thread safe.
026 *
027 * @param <T> The type of the column.
028 * @see org.apache.commons.dbutils.ResultSetHandler
029 * @since DbUtils 1.1
030 */
031public class ColumnListHandler<T> extends AbstractListHandler<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 ColumnListHandler.  The first column of each
046     * row will be returned from <code>handle()</code>.
047     */
048    public ColumnListHandler() {
049        this(1, null);
050    }
051
052    /**
053     * Creates a new instance of ColumnListHandler.
054     *
055     * @param columnIndex The index of the column to retrieve from the
056     * <code>ResultSet</code>.
057     */
058    public ColumnListHandler(int columnIndex) {
059        this(columnIndex, null);
060    }
061
062    /**
063     * Creates a new instance of ColumnListHandler.
064     *
065     * @param columnName The name of the column to retrieve from the
066     * <code>ResultSet</code>.
067     */
068    public ColumnListHandler(String columnName) {
069        this(1, columnName);
070    }
071
072    /** Private Helper
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 ColumnListHandler(int columnIndex, String columnName) {
079        super();
080        this.columnIndex = columnIndex;
081        this.columnName = columnName;
082    }
083
084    /**
085     * Returns one <code>ResultSet</code> column value as <code>Object</code>.
086     * @param rs <code>ResultSet</code> to process.
087     * @return <code>Object</code>, never <code>null</code>.
088     *
089     * @throws SQLException if a database access error occurs
090     * @throws ClassCastException if the class datatype does not match the column type
091     *
092     * @see org.apache.commons.dbutils.handlers.AbstractListHandler#handle(ResultSet)
093     */
094    // We assume that the user has picked the correct type to match the column
095    // so getObject will return the appropriate type and the cast will succeed.
096    @SuppressWarnings("unchecked")
097    @Override
098    protected T handleRow(ResultSet rs) throws SQLException {
099        if (this.columnName == null) {
100            return (T) rs.getObject(this.columnIndex);
101        }
102        return (T) rs.getObject(this.columnName);
103   }
104
105}