Coverage Report - org.apache.commons.dbutils.ResultSetIterator
 
Classes in this File Line Coverage Branch Coverage Complexity
ResultSetIterator
0%
0/21
0%
0/2
2
 
 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  
 
 19  
 import java.sql.ResultSet;
 20  
 import java.sql.SQLException;
 21  
 import java.util.Iterator;
 22  
 
 23  
 /**
 24  
  * <p>
 25  
  * Wraps a <code>ResultSet</code> in an <code>Iterator</code>.  This is useful
 26  
  * when you want to present a non-database application layer with domain
 27  
  * neutral data.
 28  
  * </p>
 29  
  * 
 30  
  * <p>
 31  
  * This implementation requires the <code>ResultSet.isLast()</code> method
 32  
  * to be implemented.
 33  
  * </p>
 34  
  */
 35  
 public class ResultSetIterator implements Iterator {
 36  
 
 37  
     /**
 38  
      * The wrapped <code>ResultSet</code>.
 39  
      */
 40  
     private final ResultSet rs;
 41  
     
 42  
     /**
 43  
      * The processor to use when converting a row into an Object[].
 44  
      */
 45  
     private final RowProcessor convert;
 46  
 
 47  
     /**
 48  
      * Constructor for ResultSetIterator.
 49  
      * @param rs Wrap this <code>ResultSet</code> in an <code>Iterator</code>.
 50  
      */
 51  
     public ResultSetIterator(ResultSet rs) {
 52  0
         this(rs , new BasicRowProcessor());
 53  0
     }
 54  
     
 55  
     /**
 56  
      * Constructor for ResultSetIterator.
 57  
      * @param rs Wrap this <code>ResultSet</code> in an <code>Iterator</code>.
 58  
      * @param convert The processor to use when converting a row into an 
 59  
      * <code>Object[]</code>.  Defaults to a 
 60  
      * <code>BasicRowProcessor</code>.
 61  
      */
 62  0
     public ResultSetIterator(ResultSet rs, RowProcessor convert) {
 63  0
         this.rs = rs;
 64  0
         this.convert = convert;
 65  0
     }
 66  
 
 67  
     /**
 68  
      * Returns true if there are more rows in the ResultSet.
 69  
      * @return boolean <code>true</code> if there are more rows
 70  
      * @throws RuntimeException if an SQLException occurs.
 71  
      */
 72  
     public boolean hasNext() {
 73  
         try {
 74  0
             return !rs.isLast();
 75  0
         } catch (SQLException e) {
 76  0
             rethrow(e);
 77  0
             return false;
 78  
         }
 79  
     }
 80  
 
 81  
     /**
 82  
      * Returns the next row as an <code>Object[]</code>.
 83  
      * @return An <code>Object[]</code> with the same number of elements as
 84  
      * columns in the <code>ResultSet</code>. 
 85  
      * @see java.util.Iterator#next()
 86  
      * @throws RuntimeException if an SQLException occurs.
 87  
      */
 88  
     public Object next() {
 89  
         try {
 90  0
             rs.next();
 91  0
             return this.convert.toArray(rs);
 92  0
         } catch (SQLException e) {
 93  0
             rethrow(e);
 94  0
             return null;
 95  
         }
 96  
     }
 97  
 
 98  
     /**
 99  
      * Deletes the current row from the <code>ResultSet</code>.
 100  
      * @see java.util.Iterator#remove()
 101  
      * @throws RuntimeException if an SQLException occurs.
 102  
      */
 103  
     public void remove() {
 104  
         try {
 105  0
             this.rs.deleteRow();
 106  0
         } catch (SQLException e) {
 107  0
             rethrow(e);
 108  0
         }
 109  0
     }
 110  
 
 111  
     /**
 112  
      * Rethrow the SQLException as a RuntimeException.  This implementation
 113  
      * creates a new RuntimeException with the SQLException's error message.
 114  
      * @param e SQLException to rethrow
 115  
      * @since DbUtils 1.1
 116  
      */
 117  
     protected void rethrow(SQLException e) {
 118  0
         throw new RuntimeException(e.getMessage());
 119  
     }
 120  
 
 121  
 }