working version of templates (experiment1) and jdbc

This commit is contained in:
James Strachan
2012-03-13 06:52:51 +00:00
parent f490242c00
commit 1700777f9b
5 changed files with 58 additions and 7 deletions
@@ -13,6 +13,7 @@ fun PreparedStatement.update(): Int {
close()
}
}
fun <T> PreparedStatement.query(block: (ResultSet) -> T): T {
try {
val resultSet = this.executeQuery()
@@ -1,6 +1,9 @@
package kotlin.jdbc
import java.sql. *
import java.util.List
import kotlin.util.arrayList
import java.util.Collection
/**
* Creates an iterator through a [[ResultSet]]
@@ -25,3 +28,22 @@ fun ResultSet.get(columnId: Int): Any? = this.getObject(columnId)
*/
fun ResultSet.get(columnName: String): Any? = this.getObject(columnName)
/**
* Maps the collection of rows to some value
*/
fun <T> ResultSet.map(fn: (ResultSet) -> T) : List<T> {
val answer = arrayList<T>()
mapTo(answer, fn)
return answer
}
/**
* Maps the collection of rows to some value and adds it to the result collection
*/
fun <T> ResultSet.mapTo(result: Collection<T>, fn: (ResultSet) -> T) : Collection<T> {
for (row in this) {
val element = fn(row)
result.add(element)
}
return result
}
@@ -8,7 +8,7 @@ import java.sql.Statement
/**
* Uses the statement with the given block then closes the statement
*/
fun <T> Statement.use(block : (Statement) -> T) : T {
fun <T, S : Statement> S.use(block : (S) -> T) : T {
try {
return block(this)
} finally {
@@ -8,7 +8,7 @@ import org.h2.jdbcx.JdbcDataSource
import javax.sql.DataSource
import org.h2.jdbcx.JdbcConnectionPool
val dataSource = createDataSource()
public val dataSource : DataSource = createDataSource()
fun createDataSource() : DataSource {
val dataSource = JdbcConnectionPool.create("jdbc:h2:mem:KotlinJdbcTest;DB_CLOSE_DELAY=-1", "user", "password")
@@ -3,12 +3,13 @@ package test.kotlin.jdbc.experiment1
import kotlin.template.experiment1.*
import kotlin.jdbc.*
import kotlin.test.*
import kotlin.util.arrayList
import kotlin.util.*
import test.kotlin.jdbc.*
import junit.framework.TestCase
import javax.sql.DataSource
import test.kotlin.jdbc.createDataSource
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
@@ -60,6 +61,15 @@ class PreparedStatementBuilder(val connection: Connection) {
})
}
fun expression(value: Int): Unit {
sql.append("?")
tasks.add(object: Binding {
override fun bind(statement : PreparedStatement) {
statement.setInt(nextParameterIndex(), value)
}
})
}
// TODO bind other kinds!
fun statement(): PreparedStatement {
@@ -82,14 +92,32 @@ class JdbcTemplateTest : TestCase() {
//val dataSource = createDataSource()
fun testTemplateInsert() {
val id = 1
val id = 3
val name = "Stepan"
// Mimicks the following code
// dataSource.update("insert into foo (id, name) values ($id, $name)")
/*
dataSource.update {
it.text("insert into foo (id, name) values (")
it.expression(id)
it.text(", ")
it.expression(name)
it.text(")")
}
// Mimicks
// datasource.query("select * from foo where id = $id") { it["name"] }
val names = dataSource.query({
it.text("select * from foo where id = ")
it.expression(id)
}) {
it.map{ it["name"] }
}
*/
println("Found names $names")
val actual = names.first()
assertEquals(name, actual)
}
}