jdk annotations for some java.sql methods, related cleanup in stdlib & tests

This commit is contained in:
Alex Tkachman
2012-09-27 12:00:57 +02:00
parent c8e711f84b
commit 57c27bdd37
8 changed files with 147 additions and 20 deletions
@@ -8,17 +8,17 @@ import java.util.Properties
/**
* create connection for the specified jdbc url with no credentials
*/
fun getConnection(url : String) : Connection = DriverManager.getConnection(url)!!
fun getConnection(url : String) : Connection = DriverManager.getConnection(url)
/**
* create connection for the specified jdbc url and properties
*/
fun getConnection(url : String, info : Map<String, String>) : Connection = DriverManager.getConnection(url, info.toProperties())!!
fun getConnection(url : String, info : Map<String, String>) : Connection = DriverManager.getConnection(url, info.toProperties())
/**
* create connection for the specified jdbc url and credentials
*/
fun getConnection(url : String, user : String, password : String) : Connection = DriverManager.getConnection(url, user, password)!!
fun getConnection(url : String, user : String, password : String) : Connection = DriverManager.getConnection(url, user, password)
/**
* Executes specified block with connection and close connection after this
@@ -65,11 +65,7 @@ fun Connection.update(template : StringTemplate) : Int {
fun <T> Connection.query(sql: String, block: (ResultSet) -> T): T {
return statement{
val rs = it.executeQuery(sql)
if (rs != null) {
block(rs)
} else {
throw IllegalStateException("No ResultSet returned executeQuery($sql) on $this")
}
block(rs)
}
}
@@ -222,7 +218,7 @@ class PreparedStatementBuilder(val template : StringTemplate, val connection : C
out.append(if (constantText) it else "?")
constantText = !constantText
}
return out.toString() ?: ""
return out.toString()
}
}
@@ -17,11 +17,7 @@ fun PreparedStatement.update(): Int {
fun <T> PreparedStatement.query(block: (ResultSet) -> T): T {
try {
val resultSet = this.executeQuery()
if (resultSet == null) {
throw IllegalStateException("No ResultSet returned from $this")
} else {
return block(resultSet)
}
return block(resultSet)
} finally {
close()
}
@@ -47,7 +47,7 @@ fun <T> ResultSet.map(fn : (ResultSet) -> T) : jet.Iterable<T> {
* Returns array with column names
*/
fun ResultSet.getColumnNames() : jet.Array<String> {
val meta = getMetaData()!!
val meta = getMetaData()
return jet.Array<String>(meta.getColumnCount(), {meta.getColumnName(it + 1) ?: it.toString()})
}