Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding the fetch size for a Query #327

Merged
merged 2 commits into from
Jul 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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