added a basic set of extension functions for working with JDBC in kotlin
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<component name="libraryTable">
|
||||
<library name="Maven: com.h2database:h2:1.3.164">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/com/h2database/h2/1.3.164/h2-1.3.164.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/com/h2database/h2/1.3.164/h2-1.3.164-javadoc.jar!/" />
|
||||
</JAVADOC>
|
||||
<SOURCES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/com/h2database/h2/1.3.164/h2-1.3.164-sources.jar!/" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
||||
@@ -0,0 +1,13 @@
|
||||
<component name="libraryTable">
|
||||
<library name="Maven: junit:junit:4.9">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.9/junit-4.9.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.9/junit-4.9-javadoc.jar!/" />
|
||||
</JAVADOC>
|
||||
<SOURCES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.9/junit-4.9-sources.jar!/" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
||||
@@ -0,0 +1,13 @@
|
||||
<component name="libraryTable">
|
||||
<library name="Maven: org.hamcrest:hamcrest-core:1.1">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1-javadoc.jar!/" />
|
||||
</JAVADOC>
|
||||
<SOURCES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1-sources.jar!/" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
||||
Generated
+1
@@ -3,6 +3,7 @@
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/kdoc/kdoc.iml" filepath="$PROJECT_DIR$/kdoc/kdoc.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/kotlin-jdbc/kotlin-jdbc.iml" filepath="$PROJECT_DIR$/kotlin-jdbc/kotlin-jdbc.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/kunit/kunit.iml" filepath="$PROJECT_DIR$/kunit/kunit.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/runtests/runtests.iml" filepath="$PROJECT_DIR$/runtests/runtests.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/stdlib/stdlib.iml" filepath="$PROJECT_DIR$/stdlib/stdlib.iml" />
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="false">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: com.h2database:h2:1.3.164" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.9" level="project" />
|
||||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
|
||||
<orderEntry type="module" module-name="stdlib" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-project</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>kotlin-jdbc</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>stdlib</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.3.164</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.goldin.plugins</groupId>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,40 @@
|
||||
package kotlin.jdbc
|
||||
|
||||
import java.sql.*
|
||||
|
||||
/**
|
||||
* Helper method to process a statement on this collection
|
||||
*/
|
||||
fun <T> 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 <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")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <T> 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 <T> 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 <T> DataSource.query(sql: String, block: (ResultSet) -> T): T {
|
||||
return use{ it.query(sql, block) }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package kotlin.jdbc
|
||||
|
||||
import java.sql. *
|
||||
|
||||
/**
|
||||
* Creates an iterator through a [[ResultSet]]
|
||||
*/
|
||||
fun ResultSet.iterator() : Iterator<ResultSet> {
|
||||
val rs = this
|
||||
return object : Iterator<ResultSet>{
|
||||
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)
|
||||
|
||||
@@ -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"]}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@
|
||||
<module>stdlib</module>
|
||||
<module>kunit</module>
|
||||
<module>kdoc</module>
|
||||
<module>kotlin-jdbc</module>
|
||||
<module>website</module>
|
||||
</modules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user