org.sfm.jdbc.impl.ResultSetIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simpleFlatMapper Show documentation
Show all versions of simpleFlatMapper Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
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();
}
}