diff --git a/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/PreparedStatements.kt b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/PreparedStatements.kt index 5da19cb1adf..77fdc972fed 100644 --- a/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/PreparedStatements.kt +++ b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/PreparedStatements.kt @@ -13,6 +13,7 @@ fun PreparedStatement.update(): Int { close() } } + fun PreparedStatement.query(block: (ResultSet) -> T): T { try { val resultSet = this.executeQuery() diff --git a/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/ResultSets.kt b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/ResultSets.kt index 166085c0247..369eb959762 100644 --- a/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/ResultSets.kt +++ b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/ResultSets.kt @@ -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 ResultSet.map(fn: (ResultSet) -> T) : List { + val answer = arrayList() + mapTo(answer, fn) + return answer +} + +/** + * Maps the collection of rows to some value and adds it to the result collection + */ +fun ResultSet.mapTo(result: Collection, fn: (ResultSet) -> T) : Collection { + for (row in this) { + val element = fn(row) + result.add(element) + } + return result +} diff --git a/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/Statements.kt b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/Statements.kt index 9dbdfac5671..72030555dab 100644 --- a/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/Statements.kt +++ b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/Statements.kt @@ -8,7 +8,7 @@ import java.sql.Statement /** * Uses the statement with the given block then closes the statement */ -fun Statement.use(block : (Statement) -> T) : T { +fun S.use(block : (S) -> T) : T { try { return block(this) } finally { diff --git a/libraries/kotlin-jdbc/src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt b/libraries/kotlin-jdbc/src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt index 4d86b3795c0..bb3cf4f1826 100644 --- a/libraries/kotlin-jdbc/src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt +++ b/libraries/kotlin-jdbc/src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt @@ -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") diff --git a/libraries/kotlin-jdbc/src/test/kotlin/test/kotlin/jdbc/experiment1/JdbcTemplateTest.kt b/libraries/kotlin-jdbc/src/test/kotlin/test/kotlin/jdbc/experiment1/JdbcTemplateTest.kt index 31a5b4447f8..48eb57ed8fb 100644 --- a/libraries/kotlin-jdbc/src/test/kotlin/test/kotlin/jdbc/experiment1/JdbcTemplateTest.kt +++ b/libraries/kotlin-jdbc/src/test/kotlin/test/kotlin/jdbc/experiment1/JdbcTemplateTest.kt @@ -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) } }