All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sfm.jdbc.impl.ResultSetIterator Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.jdbc.impl;

import org.sfm.map.Mapper;
import org.sfm.map.MappingContext;
import org.sfm.utils.ErrorHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ResultSetIterator implements Iterator {

	private final ResultSet rs;
	private final Mapper mapper;
    private final MappingContext mappingContext;
	
	private boolean isFetched;
	private boolean hasValue;
	
	public ResultSetIterator(ResultSet rs, Mapper mapper, MappingContext mappingContext) {
		this.rs = rs;
		this.mapper = mapper;
        this.mappingContext = mappingContext;
    }

	@Override
	public boolean hasNext() {
		fetch();
		return hasValue;
	}

	private void fetch() {
		if (!isFetched) {
			try {
				hasValue = rs.next();
				isFetched = true;
			} catch(SQLException e) {
                ErrorHelper.rethrow(e);
			}
		}
	}

	@Override
	public T next() {
		fetch();
		if (hasValue) {
			T t = mapper.map(rs, mappingContext);
			isFetched = false;
			return t;
		} else {
			throw new NoSuchElementException("No more rows");
		}
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy