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

org.hibernate.cfg.reveng.dialect.ResultSetIterator Maven / Gradle / Ivy

There is a newer version: 5.6.15.Final
Show newest version
package org.hibernate.cfg.reveng.dialect;

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

import org.hibernate.exception.spi.SQLExceptionConverter;


/**
 * Iterator over a resultset; intended usage only for metadata reading.  
 */
public abstract class ResultSetIterator implements Iterator> {

	private ResultSet rs;

	protected boolean current = false;

	protected boolean endOfRows = false;

	private SQLExceptionConverter sec;

	private Statement statement = null;

	protected ResultSetIterator(ResultSet resultset, SQLExceptionConverter sec) {
		this(null, resultset, sec);
	}

	public ResultSetIterator(Statement stmt, ResultSet resultset, SQLExceptionConverter exceptionConverter) {
		this.rs = resultset;
		this.sec = exceptionConverter;
		this.statement  = stmt;		
	}

	protected SQLExceptionConverter getSQLExceptionConverter() {
		return sec;
	}
	
	public boolean hasNext() {
		try {
			advance();
			return !endOfRows;
		}
		catch (SQLException e) {
			handleSQLException( e );
			return false;
		}
	}

	
	public Map next() {
		try {
			advance();
			if ( endOfRows ) {
				throw new NoSuchElementException();
			}
			current = false;
			return convertRow( rs );
		}
		catch (SQLException e) {
			handleSQLException(e);
			throw new NoSuchElementException("excpetion occurred " + e);
		}

	}

	abstract protected Throwable handleSQLException(SQLException e);
	abstract protected Map convertRow(ResultSet rs) throws SQLException;

	public void remove() {
		throw new UnsupportedOperationException(
				"remove() not possible on ResultSet" );
	}

	protected void advance() throws SQLException {

		if ( !current && !endOfRows ) {
			if ( rs.next() ) {
				current = true;
				endOfRows = false;
			}
			else {
				current = false;
				endOfRows = true;
			}
		}
	}

	public void close() {
		try {
			rs.close();
			if(statement!=null) {
				statement.close();
			}			
		}
		catch (SQLException e) {
			handleSQLException(e);			
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy