Skip to content

Commit

Permalink
Lower footprint on creating ResultRow from ResultSet
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Apr 18, 2021
1 parent f7b926b commit 75b6ad5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ abstract class AbstractQuery<T : AbstractQuery<T>>(targets: List<Table>) : Sized
protected inner class ResultIterator(val rs: ResultSet) : Iterator<ResultRow> {
private var hasNext: Boolean? = null

private val fields = set.realFields
private val fieldsIndex = set.realFields.toSet().mapIndexed { index, expression -> expression to index }.toMap()

override operator fun next(): ResultRow {
if (hasNext == null) hasNext()
if (hasNext == false) throw NoSuchElementException()
hasNext = null
return ResultRow.create(rs, fields)
return ResultRow.create(rs, fieldsIndex)
}

override fun hasNext(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@ class ResultRow(val fieldIndex: Map<Expression<*>, Int>) {
internal object NotInitializedValue

companion object {

@Deprecated(level = DeprecationLevel.ERROR, message = "Consider to use [create] with map param instead")
fun create(rs: ResultSet, fields: List<Expression<*>>): ResultRow {
val fieldsIndex = fields.distinct().mapIndexed { i, field ->
val value = (field as? Column<*>)?.columnType?.readObject(rs, i + 1) ?: rs.getObject(i + 1)
(field to i) to value
}.toMap()
return ResultRow(fieldsIndex.keys.toMap()).apply {
fieldsIndex.forEach{ (i, f) ->
data[i.second] = f
return create(rs, fields.distinct().mapIndexed { index, field -> field to index }.toMap())
}

fun create(rs: ResultSet, fieldsIndex: Map<Expression<*>, Int>): ResultRow {
return ResultRow(fieldsIndex).apply {
fieldsIndex.forEach{ (field, index) ->
val value = (field as? Column<*>)?.columnType?.readObject(rs, index + 1) ?: rs.getObject(index + 1)
data[index] = value
}
}
}
Expand Down

0 comments on commit 75b6ad5

Please sign in to comment.