ResultSetIterator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbutils;

  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.Iterator;

  21. /**
  22.  * <p>
  23.  * Wraps a {@code ResultSet} in an {@code Iterator&lt;Object[]&gt;}.  This is useful
  24.  * when you want to present a non-database application layer with domain
  25.  * neutral data.
  26.  * </p>
  27.  *
  28.  * <p>
  29.  * This implementation requires the {@code ResultSet.isLast()} method
  30.  * to be implemented.
  31.  * </p>
  32.  */
  33. public class ResultSetIterator implements Iterator<Object[]> {

  34.     /**
  35.      * Generates an {@code Iterable}, suitable for use in for-each loops.
  36.      *
  37.      * @param resultSet Wrap this {@code ResultSet} in an {@code Iterator}.
  38.      * @return an {@code Iterable}, suitable for use in for-each loops.
  39.      */
  40.     public static Iterable<Object[]> iterable(final ResultSet resultSet) {
  41.         return () -> new ResultSetIterator(resultSet);
  42.     }

  43.     /**
  44.      * The wrapped {@code ResultSet}.
  45.      */
  46.     private final ResultSet resultSet;

  47.     /**
  48.      * The processor to use when converting a row into an Object[].
  49.      */
  50.     private final RowProcessor convert;

  51.     /**
  52.      * Constructor for ResultSetIterator.
  53.      * @param resultSet Wrap this {@code ResultSet} in an {@code Iterator}.
  54.      */
  55.     public ResultSetIterator(final ResultSet resultSet) {
  56.         this(resultSet, new BasicRowProcessor());
  57.     }

  58.     /**
  59.      * Constructor for ResultSetIterator.
  60.      * @param resultSet Wrap this {@code ResultSet} in an {@code Iterator}.
  61.      * @param convert The processor to use when converting a row into an
  62.      * {@code Object[]}.  Defaults to a
  63.      * {@code BasicRowProcessor}.
  64.      */
  65.     public ResultSetIterator(final ResultSet resultSet, final RowProcessor convert) {
  66.         this.resultSet = resultSet;
  67.         this.convert = convert;
  68.     }

  69.     /**
  70.      * Returns true if there are more rows in the ResultSet.
  71.      * @return boolean {@code true} if there are more rows
  72.      * @throws RuntimeException if an SQLException occurs.
  73.      */
  74.     @Override
  75.     public boolean hasNext() {
  76.         try {
  77.             return !resultSet.isLast();
  78.         } catch (final SQLException e) {
  79.             rethrow(e);
  80.             return false;
  81.         }
  82.     }

  83.     /**
  84.      * Returns the next row as an {@code Object[]}.
  85.      * @return An {@code Object[]} with the same number of elements as
  86.      * columns in the {@code ResultSet}.
  87.      * @see java.util.Iterator#next()
  88.      * @throws RuntimeException if an SQLException occurs.
  89.      */
  90.     @Override
  91.     public Object[] next() {
  92.         try {
  93.             resultSet.next();
  94.             return this.convert.toArray(resultSet);
  95.         } catch (final SQLException e) {
  96.             rethrow(e);
  97.             return null;
  98.         }
  99.     }

  100.     /**
  101.      * Deletes the current row from the {@code ResultSet}.
  102.      * @see java.util.Iterator#remove()
  103.      * @throws RuntimeException if an SQLException occurs.
  104.      */
  105.     @Override
  106.     public void remove() {
  107.         try {
  108.             this.resultSet.deleteRow();
  109.         } catch (final SQLException e) {
  110.             rethrow(e);
  111.         }
  112.     }

  113.     /**
  114.      * Rethrow the SQLException as a RuntimeException.  This implementation
  115.      * creates a new RuntimeException with the SQLException's error message.
  116.      * @param e SQLException to rethrow
  117.      * @since 1.1
  118.      */
  119.     protected void rethrow(final SQLException e) {
  120.         throw new RuntimeException(e.getMessage());
  121.     }

  122. }