Skip to content

Instantly share code, notes, and snippets.

@cameronism
Created October 12, 2016 01:30
Show Gist options
  • Save cameronism/5d245a852e7610af301764a3ebc3c418 to your computer and use it in GitHub Desktop.
Save cameronism/5d245a852e7610af301764a3ebc3c418 to your computer and use it in GitHub Desktop.
Extension functions on JDBC Connection and ResultSet
Copyright 2016 Cameron Jordan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
import java.math.BigDecimal
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Timestamp
import java.time.Instant
import java.util.*
class PreparedStatementParameters(private val stmt: PreparedStatement) {
private var index = 0
fun param(value: Boolean) {
index++
stmt.setBoolean(index, value)
}
fun param(value: Byte) {
index++
stmt.setByte(index, value)
}
fun param(value: Short) {
index++
stmt.setShort(index, value)
}
fun param(value: Int) {
index++
stmt.setInt(index, value)
}
fun param(value: Long) {
index++
stmt.setLong(index, value)
}
fun param(value: Float) {
index++
stmt.setFloat(index, value)
}
fun param(value: Double) {
index++
stmt.setDouble(index, value)
}
fun param(value: BigDecimal) {
index++
stmt.setBigDecimal(index, value)
}
fun param(value: Instant) {
index++
stmt.setTimestamp(index, Timestamp.from(value))
}
fun param(value: Instant, calendar: Calendar) {
index++
stmt.setTimestamp(index, Timestamp.from(value), calendar)
}
fun param(value: String) {
index++
stmt.setString(index, value)
}
fun param(value: ByteArray) {
index++
stmt.setBytes(index, value)
}
fun param(value: IntArray) {
index++
val tmp = kotlin.arrayOfNulls<Int?>(value.count())
// so, many, boxes, :(
for (i in value.indices)
tmp[i] = value[i]
val sqlArray = stmt.connection.createArrayOf(SQL_TYPE_INT, tmp)
stmt.setArray(index, sqlArray)
}
fun param(value: Array<String>) {
index++
val sqlArray = stmt.connection.createArrayOf(SQL_TYPE_STRING, value)
stmt.setArray(index, sqlArray)
}
companion object {
var SQL_TYPE_STRING = "text"
var SQL_TYPE_INT = "int4"
}
}
fun <T> Connection.execute(sql: String, params: PreparedStatementParameters.() -> Unit, block: (ResultSet) -> T): T {
val stmt = this.prepareStatement(sql)
try {
PreparedStatementParameters(stmt).params()
val rs = stmt.executeQuery()
try {
return block(rs)
}
finally {
rs.close()
}
} finally {
stmt.close()
}
}
fun Connection.execute(sql: String, params: PreparedStatementParameters.() -> Unit) {
val stmt = this.prepareStatement(sql)
try {
PreparedStatementParameters(stmt).params()
stmt.execute()
} finally {
stmt.close()
}
}
class ResultSetIterator<out T>(private val resultSet: ResultSet, private val selector: (ResultSet) -> T) : Iterator<T> {
override fun hasNext() = resultSet.next()
override fun next() = selector(resultSet)
}
fun <T> ResultSet.asSequence(selector: (ResultSet) -> T) = ResultSetIterator(this, selector).asSequence()
fun ResultSet.asSequence() = ResultSetIterator(this, { it }).asSequence()
fun demo() {
conn.execute("SELECT stuff, things FROM my_sproc(?, ?)", { param(someInt); param(someString) }) { resultSet ->
val rows = resultSet.asSequence { SomeDataClass(it.getBytes(1), it.getDouble(2)) }
for (row in rows) {
// do something
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment