diff --git a/libraries/.idea/libraries/Maven__com_h2database_h2_1_3_164.xml b/libraries/.idea/libraries/Maven__com_h2database_h2_1_3_164.xml new file mode 100644 index 00000000000..e997d9eb7c0 --- /dev/null +++ b/libraries/.idea/libraries/Maven__com_h2database_h2_1_3_164.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/libraries/.idea/libraries/Maven__junit_junit_4_9.xml b/libraries/.idea/libraries/Maven__junit_junit_4_9.xml new file mode 100644 index 00000000000..c42d5abd613 --- /dev/null +++ b/libraries/.idea/libraries/Maven__junit_junit_4_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/libraries/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml b/libraries/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml new file mode 100644 index 00000000000..acdf44300b4 --- /dev/null +++ b/libraries/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/libraries/.idea/modules.xml b/libraries/.idea/modules.xml index cbd2d27be07..c9dcf59b2db 100644 --- a/libraries/.idea/modules.xml +++ b/libraries/.idea/modules.xml @@ -3,6 +3,7 @@ + diff --git a/libraries/kotlin-jdbc/kotlin-jdbc.iml b/libraries/kotlin-jdbc/kotlin-jdbc.iml new file mode 100644 index 00000000000..e8fb5f6f438 --- /dev/null +++ b/libraries/kotlin-jdbc/kotlin-jdbc.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/libraries/kotlin-jdbc/pom.xml b/libraries/kotlin-jdbc/pom.xml new file mode 100644 index 00000000000..d88e0ddd553 --- /dev/null +++ b/libraries/kotlin-jdbc/pom.xml @@ -0,0 +1,46 @@ + + + + 4.0.0 + + + org.jetbrains.kotlin + kotlin-project + 1.0-SNAPSHOT + + + kotlin-jdbc + + + + org.jetbrains.kotlin + stdlib + ${project.version} + + + + com.h2database + h2 + 1.3.164 + test + + + junit + junit + ${junit-version} + test + + + + + + + com.goldin.plugins + kotlin-maven-plugin + + + + + diff --git a/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/Connections.kt b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/Connections.kt new file mode 100644 index 00000000000..d68e57355ac --- /dev/null +++ b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/Connections.kt @@ -0,0 +1,40 @@ +package kotlin.jdbc + +import java.sql.* + +/** + * Helper method to process a statement on this collection + */ +fun Connection.statement(block: (Statement) -> T): T { + val statement = createStatement() + if (statement != null) { + try { + return block(statement) + } finally { + statement.close() + } + } else { + throw IllegalStateException("No Statement returned from $this") + } +} + +/** + * Perform an SQL update on the connection + */ +fun Connection.update(sql: String): Int { + return statement{ it.executeUpdate(sql) } +} + +/** + * Perform a query on the connection and processes the result set with a function + */ +fun 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") + } + } +} \ No newline at end of file diff --git a/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/DataSources.kt b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/DataSources.kt new file mode 100644 index 00000000000..fbbe43c2591 --- /dev/null +++ b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/DataSources.kt @@ -0,0 +1,41 @@ +package kotlin.jdbc + +import java.sql.* +import javax.sql.* + +/** + * Processes a connection from the pool using the given function block + */ +fun DataSource.use(block : (Connection) -> T): T { + val connection = getConnection() + if (connection != null) { + try { + return block(connection) + } finally { + connection.close() + } + } else { + throw IllegalStateException("No Connection returned from $this") + } +} + +/** + * Helper method to process a statement on this collection + */ +fun DataSource.statement(block: (Statement) -> T): T { + return use{ it.statement(block) } +} + +/** + * Perform an SQL update on the connection + */ +fun DataSource.update(sql: String): Int { + return use{ it.update(sql) } +} + +/** + * Perform a query on the connection and processes the result set with a function + */ +fun DataSource.query(sql: String, block: (ResultSet) -> T): T { + return use{ it.query(sql, block) } +} \ No newline at end of file diff --git a/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/ResultSets.kt b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/ResultSets.kt new file mode 100644 index 00000000000..166085c0247 --- /dev/null +++ b/libraries/kotlin-jdbc/src/main/kotlin/kotlin/jdbc/ResultSets.kt @@ -0,0 +1,27 @@ +package kotlin.jdbc + +import java.sql. * + +/** + * Creates an iterator through a [[ResultSet]] + */ +fun ResultSet.iterator() : Iterator { + val rs = this + return object : Iterator{ + override val hasNext : Boolean + get() = rs.next() + + override fun next() : ResultSet = rs + } +} + +/** + * Returns the value at the given column index (starting at 1) + */ +fun ResultSet.get(columnId: Int): Any? = this.getObject(columnId) + +/** + * Returns the value at the given column name + */ +fun ResultSet.get(columnName: String): Any? = this.getObject(columnName) + 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 new file mode 100644 index 00000000000..e27ab608958 --- /dev/null +++ b/libraries/kotlin-jdbc/src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt @@ -0,0 +1,42 @@ +package test.kotlin.jdbc + +import kotlin.jdbc.* +import kotlin.test.* + +import junit.framework.TestCase +import org.h2.jdbcx.JdbcDataSource +import javax.sql.DataSource +import org.h2.jdbcx.JdbcConnectionPool + +fun createDataSource(): DataSource { + val pool = JdbcConnectionPool.create("jdbc:h2:mem:KotlinJdbcTest;DB_CLOSE_DELAY=-1", "user", "password") + if (pool == null) { + throw IllegalStateException("No DataSource created") + } else { + return pool + } +} + +class JdbcTest : TestCase() { + val dataSource = createDataSource() + + fun testDatabase() { + dataSource.update("create table foo (id int primary key, name varchar(100))") + assertEquals(1, dataSource.update("insert into foo (id, name) values (1, 'James')")) + assertEquals(1, dataSource.update("insert into foo (id, name) values (2, 'Andrey')")) + + // lets query using integer lookups + dataSource.query("select * from foo") { + for (row in it) { + println("id ${row[1]} and name: ${row[2]}") + } + } + + // query using names + dataSource.query("select * from foo") { + for (row in it) { + println("name: ${row["name"]} has id ${row["id"]}") + } + } + } +} \ No newline at end of file diff --git a/libraries/pom.xml b/libraries/pom.xml index f391fe75cd2..3c6e3927022 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -38,6 +38,7 @@ stdlib kunit kdoc + kotlin-jdbc website