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.BasicRowProcessor;
023import org.apache.commons.dbutils.ResultSetHandler;
024import org.apache.commons.dbutils.RowProcessor;
025
026/**
027 * <code>ResultSetHandler</code> implementation that converts a
028 * <code>ResultSet</code> into an <code>Object[]</code>. This class is
029 * thread safe.
030 *
031 * @see org.apache.commons.dbutils.ResultSetHandler
032 */
033public class ArrayHandler implements ResultSetHandler<Object[]> {
034
035    /**
036     * Singleton processor instance that handlers share to save memory.  Notice
037     * the default scoping to allow only classes in this package to use this
038     * instance.
039     */
040    static final RowProcessor ROW_PROCESSOR = new BasicRowProcessor();
041
042    /**
043     * An empty array to return when no more rows are available in the ResultSet.
044     */
045    private static final Object[] EMPTY_ARRAY = new Object[0];
046
047    /**
048     * The RowProcessor implementation to use when converting rows
049     * into arrays.
050     */
051    private final RowProcessor convert;
052
053    /**
054     * Creates a new instance of ArrayHandler using a
055     * <code>BasicRowProcessor</code> for conversion.
056     */
057    public ArrayHandler() {
058        this(ROW_PROCESSOR);
059    }
060
061    /**
062     * Creates a new instance of ArrayHandler.
063     *
064     * @param convert The <code>RowProcessor</code> implementation
065     * to use when converting rows into arrays.
066     */
067    public ArrayHandler(RowProcessor convert) {
068        super();
069        this.convert = convert;
070    }
071
072    /**
073     * Places the column values from the first row in an <code>Object[]</code>.
074     * @param rs <code>ResultSet</code> to process.
075     * @return An Object[]. If there are no rows in the <code>ResultSet</code>
076     * an empty array will be returned.
077     *
078     * @throws SQLException if a database access error occurs
079     * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
080     */
081    @Override
082    public Object[] handle(ResultSet rs) throws SQLException {
083        return rs.next() ? this.convert.toArray(rs) : EMPTY_ARRAY;
084    }
085
086}