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

com.dbobjekts.jdbc.JDBCResultSetAdapter.kt Maven / Gradle / Ivy

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

import com.dbobjekts.api.Slice
import com.dbobjekts.statement.ColumnInResultRow
import com.dbobjekts.api.ResultRow
import com.dbobjekts.api.exception.StatementExecutionException
import java.sql.ResultSet
import java.util.LinkedList

class JDBCResultSetAdapter(
    val resultSetColumns: List,
    val resultSet: ResultSet
) {

    fun > retrieveWithIterator(selectResultSet: RS, mapper: (Int, T) -> Boolean) {
        var rowNumber = 0
        while (advanceResultSet()) {
            rowNumber += 1
            val proceed = selectResultSet.extractRow(resultSetColumns, resultSet).let { row ->
                try {
                    mapper.invoke(rowNumber, row)
                } catch (e: Exception) {
                    false
                }
            }
            if (!proceed)
                break
        }
        resultSet.close()
    }

    fun resultSetColumns(): List {
        return resultSetColumns
    }

    fun > retrieve(
        resultTemplate: RS,
        slice: Slice?
    ): List {
        val buffer = LinkedList()
        var rowNumber = 0
        while (advanceResultSet()) {
            if (slice == null || (slice.skip <= rowNumber && buffer.size < slice.limit)) {
                buffer.add(resultTemplate.extractRow(resultSetColumns, resultSet))
            }
            rowNumber += 1
        }
        resultSet.close()
        return buffer.toList()
    }

    private fun advanceResultSet(): 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 resultSet.next()
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy