Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2009-2014 Pavel Ponec
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 package org.ujorm.core;
 
 import org.ujorm.Ujo;
ResultSet iterator. It is not a thread safe implementation.

Author(s):
Pavel Ponec
 
 final class ResultSetIterator<T extends OrmUjoextends UjoIterator<T> implements Closeable {

    
Base query
 
     private final Query query;
    
Query columns
 
     private final ColumnWrapper[] queryColumns;
    
Result set
 
     private final ResultSet rs;
    
If the statemtnt is null then is a sign that it is closed.
 
     private PreparedStatement statement;
    
Is the query a view?
 
     private final boolean view;
    
A count of the item count, the negative value means the undefined value.
 
     private long count = -1L;
    
A state before the first reading a BO. An auxiliary variable
 
     private boolean initState = true;

    
It the cursor ready for reading? After a row reading the value will be set to false.
 
     private boolean cursorReady = false;
    
Has a resultset a next row?
 
     private boolean hasNext = true;
 
     public ResultSetIterator(Query querythrows IllegalStateException {
         try {
             this. = query;
             this. = query.getColumnArray();
             this. = query.getStatement();
             this. = .executeQuery();
             this. = query.getTableModel().isSelectModel();
         } catch (SQLException e) {
             close();
             throw new IllegalStateException(. + querye);
         }
     }

    
Returns true if the recored has next record

 
     @Override
     public boolean hasNext() throws IllegalStateException {
 
         if (!try {
              = true;
              = .next();
             if (!) {
                 close();
             }
         } catch (SQLException e) {
             throw new IllegalStateException("A hasNext() reading exception"e);
         }
         return ;
     }

    
Close all resources. If the current iterator moves after the last entry then this method is called automatically.
 
     @Override
     public void close() {
         if (!=nulltry {
             .close();
              = null;
         } catch (SQLException e) {
            throw new IllegalStateException("Can't close statement: " + e);
        }
    }

    
Returns a next table row.
    @Override
    @SuppressWarnings("fallthrough")
    public T next() throws NoSuchElementException {
        if (!hasNext()) {
            throw new NoSuchElementException("Query: " + .toString());
        }
        try {
             = false// switch off the cursor flag.
            @SuppressWarnings("unchecked")
            final T row = (T) .getTableModel().createBO();
            for (int i=0, max=.i<maxi++) {
                final ColumnWrapper colWrap = [i];
                final MetaColumn column = colWrap.getModel();
                final int iCol =  ? .findColumn(..of(column)) : (i+1);
                final Object value = column.getConverter().getValue(columniCol);
                if (colWrap.isCompositeKey()) {
                    final Ujo semiRow = ((CompositeKey)colWrap.getKey()).getSemiValue(rowtrue);
                    column.setValue(semiRowvalue);
                    // A session of the related object will be assigned using the OrmProperty later.
                } else {
                    column.setValue(rowvalue);
                }
            }
            row.writeSession(.getSession());
            if () {
                 = false;
            }
            return row;
        } catch (Throwable e) {
            throw new UnsupportedOperationException("Query: " + e);
        }
    }

    
Returns the count if items. The fist call can perform a new SQL statement. This additional SQL calling is skipped if the result is zero.
    @Override
    public long count() {
        if (<0L) {
             = !hasNext() && 
            ? 0L
            : .getCount()
            ;
        }
        return ;
    }

    
Skip some rows by the parameter without reading date from a ResultSet.

Parameters:
count A count of item to skip.
Returns:
Returns a true value if the skip count was no limited.
    @Override
    public boolean skip(int count) {
        for (; count>0 && hasNext(); --count) {
            =false;
        }
        return count==0;
    }
//    /** This solution is unsupported for ResultSet FORWARD_ONLY by the JDBC specification! <br>
//     * Skip some rows by the parameter without reading date from a ResultSet.
//     * @param count A count of item to skip.
//     * @return Returns a true value if the skip count was no limited.
//     */
//    @Override
//    public boolean skip_UNSUPPORTED(int count) {
//        if (!hasNext()) {
//            return count==0;
//        }
//        try {
//            cursorReady = true;
//            hasNext = rs.relative(--count);
//            return hasNext;
//
//        } catch (Throwable e) {
//            throw new UnsupportedOperationException("Skip Query: " + query, e);
//        }
//    }
New to GrepCode? Check out our FAQ X