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

com.dbobjekts.api.ResultSetIterator.kt Maven / Gradle / Ivy

There is a newer version: 0.6.0-RC2
Show newest version
package com.dbobjekts.api

import com.dbobjekts.api.exception.StatementExecutionException
import com.dbobjekts.statement.ColumnInResultRow
import java.sql.ResultSet


class ResultSetIterator>(
    internal val selectResultSet: R,
    internal val resultSetColumns: List,
    internal val resultSet: ResultSet
) : Iterator {

    private var nextElement: T? = null

    override fun hasNext(): Boolean {
        if (resultSet.isClosed) {
            throw StatementExecutionException(
                "Cannot read from java.sql.ResultSet: it is already closed. " +
                        "This can happen if you try to access a com.dbobjekts.result.ResultSetBase outside the transaction block and the underlying java.sql.Connection is already closed."
            )
        }
        return if (nextElement == null) {
            resultSet.next().also { hasNext ->
                nextElement = if (hasNext) selectResultSet.extractRow(resultSetColumns, resultSet) else null
            }
        } else {
            true
        }
    }

    override fun next(): T {
        if (!hasNext())
            throw NoSuchElementException("No more rows in ResultSet. You should check hasNext() before calling next().")
        return (nextElement ?: throw NoSuchElementException()).also { nextElement = null }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy