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} implementation that converts a 028 * {@code ResultSet} into an {@code Object[]}. 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 = {}; 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} 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} implementation 065 * to use when converting rows into arrays. 066 */ 067 public ArrayHandler(final RowProcessor convert) { 068 this.convert = convert; 069 } 070 071 /** 072 * Places the column values from the first row in an {@code Object[]}. 073 * @param resultSet {@code ResultSet} to process. 074 * @return An Object[]. If there are no rows in the {@code ResultSet} 075 * an empty array will be returned. 076 * 077 * @throws SQLException if a database access error occurs 078 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) 079 */ 080 @Override 081 public Object[] handle(final ResultSet resultSet) throws SQLException { 082 return resultSet.next() ? this.convert.toArray(resultSet) : EMPTY_ARRAY; 083 } 084 085}