Skip to content

Commit

Permalink
Allow overriding the fetch size for a Query (#327)
Browse files Browse the repository at this point in the history
* Allow overriding the fetch size for a Query
* Allow setting default fetch size in Database
  • Loading branch information
Pwootage authored and Tapac committed Jul 22, 2018
1 parent 0dc80ae commit d903802
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/main/kotlin/org/jetbrains/exposed/sql/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class Database private constructor(val connector: () -> Connection) {
}
}

var defaultFetchSize: Int? = null
private set

fun defaultFetchSize(size: Int): Database {
defaultFetchSize = size
return this
}

private fun String.isIdentifier() = !isEmpty() && first().isIdentifierStart() && all { it.isIdentifierStart() || it in '0'..'9' }
private fun Char.isIdentifierStart(): Boolean = this in 'a'..'z' || this in 'A'..'Z' || this == '_' || this in extraNameCharacters

Expand Down
15 changes: 14 additions & 1 deletion src/main/kotlin/org/jetbrains/exposed/sql/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ open class Query(set: FieldSet, where: Op<Boolean>?): SizedIterable<ResultRow>,
private set
var offset: Int = 0
private set
var fetchSize: Int? = null
private set

/**
* Changes [set.fields] field of a Query, [set.source] will be preserved
Expand Down Expand Up @@ -121,7 +123,13 @@ open class Query(set: FieldSet, where: Op<Boolean>?): SizedIterable<ResultRow>,
fun hasCustomForUpdateState() = forUpdate != null
fun isForUpdate() = (forUpdate ?: false) && currentDialect.supportsSelectForUpdate()

override fun PreparedStatement.executeInternal(transaction: Transaction): ResultSet? = executeQuery()
override fun PreparedStatement.executeInternal(transaction: Transaction): ResultSet? {
val fetchSize = this@Query.fetchSize ?: transaction.db.defaultFetchSize
if (fetchSize != null) {
this.fetchSize = fetchSize
}
return executeQuery()
}

override fun arguments() = QueryBuilder(true).let {
prepareSQL(it)
Expand Down Expand Up @@ -232,6 +240,11 @@ open class Query(set: FieldSet, where: Op<Boolean>?): SizedIterable<ResultRow>,
return this
}

fun fetchSize(n: Int): Query {
this.fetchSize = n
return this
}

private inner class ResultIterator(val rs: ResultSet): Iterator<ResultRow> {
private var hasNext: Boolean? = null
private val fieldsIndex = HashMap<Expression<*>, Int>()
Expand Down

0 comments on commit d903802

Please sign in to comment.