From 88fe4059ff0fc4dce4dab333d9c8391af31b068a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Ga=C5=82uszka?= Date: Mon, 21 Aug 2017 06:30:55 +0200 Subject: [PATCH] Add TravisCI and update Kotlin to 1.1.4 (fix issue with kotlin package) --- .travis.yml | 2 + pom.xml | 4 +- src/main/kotlin/kotlin/jdbc/Connections.kt | 224 ------------------ src/main/kotlin/kotlin/template/Templates.kt | 182 -------------- src/main/kotlin/kotlinext/jdbc/Connections.kt | 63 +++++ .../{kotlin => kotlinext}/jdbc/DataSources.kt | 17 +- .../jdbc/PreparedStatements.kt | 2 +- .../{kotlin => kotlinext}/jdbc/ResultSets.kt | 2 +- .../{kotlin => kotlinext}/jdbc/Statements.kt | 2 +- .../test/kotlin/jdbc/JdbcTemplateTest.kt | 37 --- src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt | 6 +- 11 files changed, 74 insertions(+), 467 deletions(-) create mode 100644 .travis.yml delete mode 100644 src/main/kotlin/kotlin/jdbc/Connections.kt delete mode 100644 src/main/kotlin/kotlin/template/Templates.kt create mode 100644 src/main/kotlin/kotlinext/jdbc/Connections.kt rename src/main/kotlin/{kotlin => kotlinext}/jdbc/DataSources.kt (64%) rename src/main/kotlin/{kotlin => kotlinext}/jdbc/PreparedStatements.kt (95%) rename src/main/kotlin/{kotlin => kotlinext}/jdbc/ResultSets.kt (99%) rename src/main/kotlin/{kotlin => kotlinext}/jdbc/Statements.kt (92%) delete mode 100644 src/test/kotlin/test/kotlin/jdbc/JdbcTemplateTest.kt diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a98b760 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,2 @@ +language: java + diff --git a/pom.xml b/pom.xml index 95cc405..37a18c9 100644 --- a/pom.xml +++ b/pom.xml @@ -7,11 +7,11 @@ org.jetbrains.kotlin kotlin-jdbc - ${kotlin.version} + 1.1.4 jar - 1.0.0-beta-1103 + 1.1.4 4.12 diff --git a/src/main/kotlin/kotlin/jdbc/Connections.kt b/src/main/kotlin/kotlin/jdbc/Connections.kt deleted file mode 100644 index 5e784ee..0000000 --- a/src/main/kotlin/kotlin/jdbc/Connections.kt +++ /dev/null @@ -1,224 +0,0 @@ -package kotlin.jdbc - -import java.sql.* -import kotlin.template.StringTemplate -import java.math.BigDecimal -import java.util.Properties - -/** - * create connection for the specified jdbc url with no credentials - */ -fun getConnection(url : String) : Connection = DriverManager.getConnection(url) - -/** - * create connection for the specified jdbc url and properties - */ -fun getConnection(url : String, info : Map) : 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) - -/** - * Executes specified block with connection and close connection after this - */ -fun Connection.use(block : (Connection) -> T) : T { - try { - return block(this) - } finally { - this.close() - } -} - -/** - * Helper method to process a statement on this connection - */ -fun Connection.statement(block: (Statement) -> T): T { - val statement = createStatement() - if (statement != null) { - return statement.use(block) - } 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) } -} - -/** - * Performs the SQL update using the [[StringTemplate]] - */ -fun Connection.update(template : StringTemplate) : Int { - val preparedStatement = prepare(template) - return preparedStatement.update() -} - - -/** - * 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) - block(rs) - } -} - - - -/** - * Perform a query on the connection using the [[StringTemplate]] to generate the SQL text - * and processes the result set with a function - */ -fun Connection.query(template : StringTemplate, resultBlock : (ResultSet) -> T) : T { - val preparedStatement = prepare(template) - return preparedStatement.query(resultBlock) -} - -/** - * Creates a [[PreparedStatement]] from the [[StringTemplate]] - */ -fun Connection.prepare(template : StringTemplate) : PreparedStatement { - val builder = PreparedStatementBuilder(template, this) - builder.bind() - return builder.statement -} - -class PreparedStatementBuilder(val template : StringTemplate, val connection : Connection) { - private var parameterIndex = 0 - - public val sql : String = createSql() - - public val statement: PreparedStatement = lookupOrCreateStatement() - - /** - * Binds the values in the [[StringTemplate]] to the [[PreparedStatement]] - */ - fun bind() { - var constantText = true - template.forEach{ - if (!constantText) { - expression(it) - } - constantText = !constantText - } - } - - fun expression(value : Any?) : Unit { - // TODO causes compiler error - // if (value is Number) { - if (value is Int) { - expression(value) - } else if (value is Double) { - expression(value) - } else if (value is Float) { - expression(value) - } else if (value is BigDecimal) { - expression(value) - } else if (value is Byte) { - expression(value) - } else if (value is Long) { - expression(value) - } else if (value is Short) { - expression(value) - /* - } else { - expression(value.toDouble()) - } - */ - } - else if (value is String) { - expression(value) - } else if (value is ByteArray) { - expression(value) - } else if (value is Date) { - expression(value) - } else if (value is Time) { - expression(value) - } else if (value is Timestamp) { - expression(value) - } else { - statement.setObject(nextParameterIndex(), value) - } - } - - fun expression(value : String?) : Unit { - statement.setString(nextParameterIndex(), value) - } - - fun expression(value : Int) : Unit { - statement.setInt(nextParameterIndex(), value) - } - - fun expression(value : BigDecimal?) : Unit { - statement.setBigDecimal(nextParameterIndex(), value) - } - - fun expression(value : Byte) : Unit { - statement.setByte(nextParameterIndex(), value) - } - - fun expression(value : Double) : Unit { - statement.setDouble(nextParameterIndex(), value) - } - - fun expression(value : Float) : Unit { - statement.setFloat(nextParameterIndex(), value) - } - - fun expression(value : Long) : Unit { - statement.setLong(nextParameterIndex(), value) - } - - fun expression(value : Short) : Unit { - statement.setShort(nextParameterIndex(), value) - } - - fun expression(value : ByteArray) : Unit { - statement.setBytes(nextParameterIndex(), value) - } - - fun expression(value : Date) : Unit { - statement.setDate(nextParameterIndex(), value) - } - - fun expression(value : Time) : Unit { - statement.setTime(nextParameterIndex(), value) - } - - fun expression(value : Timestamp) : Unit { - statement.setTimestamp(nextParameterIndex(), value) - } - - // TODO bind other kinds! - - /** - * Looks up the [[PreparedStatement]] in a cache or creates a new one - */ - protected fun lookupOrCreateStatement(): PreparedStatement { - val answer = connection.prepareStatement(sql) - if (answer == null) { - throw IllegalStateException("No PreparedStatement returned from $connection") - } else { - return answer - } - } - - protected fun nextParameterIndex() : Int = ++parameterIndex - - protected fun createSql() : String { - val out = StringBuilder() - var constantText = true - template.forEach { - out.append(if (constantText) it else "?") - constantText = !constantText - } - return out.toString() - } -} - diff --git a/src/main/kotlin/kotlin/template/Templates.kt b/src/main/kotlin/kotlin/template/Templates.kt deleted file mode 100644 index bd72d30..0000000 --- a/src/main/kotlin/kotlin/template/Templates.kt +++ /dev/null @@ -1,182 +0,0 @@ -package kotlin.template - -import kotlin.dom. * -import org.w3c.dom.Node -import java.util.Locale -import java.text.NumberFormat -import java.text.DateFormat -import java.util.Date - -@Deprecated("This class is part of an experimental implementation of string templates and is going to be removed") -public class StringTemplate(private val values: Array) { - - /** - * Converts the template into a String - */ - override fun toString(): String { - val out = StringBuilder() - forEach { out.append(it) } - return out.toString() - } - - /** - * Performs the given function on each value in the collection - */ - public fun forEach(fn: (Any?) -> Unit): Unit { - for (v in values) { - fn(v) - } - } -} - -/** - * Converts the string template into a string using the given formatter - * to encode values as Strings performing any special encoding (such as for HTML) - * or internationalisation. - * - * See [[HtmlFormatter] and [[LocaleFormatter] respectively. - */ -@Deprecated("This function is part of an experimental implementation of string templates and is going to be removed") -public fun StringTemplate.toString(formatter: Formatter): String { - val buffer = StringBuilder() - append(buffer, formatter) - return buffer.toString() -} - -/** - * Appends the text representation of this string template to the given output - * using the supplied formatter - */ -@Deprecated("This function is part of an experimental implementation of string templates and is going to be removed") -public fun StringTemplate.append(out: Appendable, formatter: Formatter): Unit { - var constantText = true - this.forEach { - if (constantText) { - if (it == null) { - throw IllegalStateException("No constant checks should be null"); - } else { - val text = it.toString() - out.append(text) - } - } else { - formatter.format(out, it) - } - constantText = !constantText - } -} - -/** - * Converts this string template to internationalised text using the supplied - * [[LocaleFormatter]] - */ -@Deprecated("This function is part of an experimental implementation of string templates and is going to be removed") -public fun StringTemplate.toLocale(formatter: LocaleFormatter = LocaleFormatter()): String = toString(formatter) - -/** - * Converts this string template to HTML text - */ -@Deprecated("This function is part of an experimental implementation of string templates and is going to be removed") -public fun StringTemplate.toHtml(formatter: HtmlFormatter = HtmlFormatter()): String = toString(formatter) - -/** - * Represents a formatter and encoder of values in a [[StringTemplate]] which understands - * how to format values for a particular [[Locale]] such as with the [[LocaleFormatter]] or - * to escape particular characters in different output formats such as [[HtmlFormatter] - */ -@Deprecated("This interface is part of an experimental implementation of string templates and is going to be removed") -public interface Formatter { - public fun format(buffer: Appendable, value: Any?): Unit -} - -/** - * Formats strings with no special encoding other than allowing the null text to be - * configured - */ -@Deprecated("This class is part of an experimental implementation of string templates and is going to be removed") -public open class ToStringFormatter : Formatter { - - private val nullString: String = "null" - - override fun toString(): String = "ToStringFormatter" - - public override fun format(out: Appendable, value: Any?) { - if (value == null) { - format(out, nullString) - } else if (value is StringTemplate) { - value.append(out, this) - } else { - format(out, value.toString()) - } - } - - /** - * Formats the given string allowing derived classes to override this method - * to escape strings with special characters such as for HTML - */ - public open fun format(out: Appendable, text: String): Unit { - out.append(text) - } -} - -@Deprecated("This property is part of an experimental implementation of string templates and is going to be removed") -public val defaultLocale: Locale = Locale.getDefault() - -/** - * Formats values using a given [[Locale]] for internationalisation - */ -@Deprecated("This class is part of an experimental implementation of string templates and is going to be removed") -public open class LocaleFormatter(protected val locale: Locale = defaultLocale) : ToStringFormatter() { - - override fun toString(): String = "LocaleFormatter{$locale}" - - public var numberFormat: NumberFormat = NumberFormat.getInstance(locale)!! - - public var dateFormat: DateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, locale)!! - - public override fun format(out: Appendable, value: Any?) { - if (value is Number) { - format(out, format(value)) - } else if (value is Date) { - format(out, format(value)) - } else { - super.format(out, value) - } - } - - public fun format(number: Number): String { - return numberFormat.format(number) ?: "" - } - - public fun format(date: Date): String { - return dateFormat.format(date) ?: "" - } -} - -/** - * Formats values for HTML encoding, escaping special characters in HTML. - */ -@Deprecated("This class is part of an experimental implementation of string templates and is going to be removed") -public class HtmlFormatter(locale: Locale = defaultLocale) : LocaleFormatter(locale) { - - override fun toString(): String = "HtmlFormatter{$locale}" - - public override fun format(out: Appendable, value: Any?) { - if (value is Node) { - out.append(value.toXmlString()) - } else { - super.format(out, value) - } - } - - public override fun format(buffer: Appendable, text: String): Unit { - for (c in text) { - if (c == '<') buffer.append("<") - else if (c == '>') buffer.append(">") - else if (c == '&') buffer.append("&") - else if (c == '"') buffer.append(""") - else buffer.append(c) - } - } -} - - diff --git a/src/main/kotlin/kotlinext/jdbc/Connections.kt b/src/main/kotlin/kotlinext/jdbc/Connections.kt new file mode 100644 index 0000000..b29b53d --- /dev/null +++ b/src/main/kotlin/kotlinext/jdbc/Connections.kt @@ -0,0 +1,63 @@ +package kotlinext.jdbc + +import java.sql.* +import java.math.BigDecimal +import java.util.Properties + +/** + * create connection for the specified jdbc url with no credentials + */ +fun getConnection(url : String) : Connection = DriverManager.getConnection(url) + +/** + * create connection for the specified jdbc url and properties + */ +fun getConnection(url : String, info : Map) : 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) + +/** + * Executes specified block with connection and close connection after this + */ +fun Connection.use(block : (Connection) -> T) : T { + try { + return block(this) + } finally { + this.close() + } +} + +/** + * Helper method to process a statement on this connection + */ +fun Connection.statement(block: (Statement) -> T): T { + val statement = createStatement() + if (statement != null) { + return statement.use(block) + } 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) + block(rs) + } +} + + diff --git a/src/main/kotlin/kotlin/jdbc/DataSources.kt b/src/main/kotlin/kotlinext/jdbc/DataSources.kt similarity index 64% rename from src/main/kotlin/kotlin/jdbc/DataSources.kt rename to src/main/kotlin/kotlinext/jdbc/DataSources.kt index 291dafa..26c5006 100644 --- a/src/main/kotlin/kotlin/jdbc/DataSources.kt +++ b/src/main/kotlin/kotlinext/jdbc/DataSources.kt @@ -1,8 +1,7 @@ -package kotlin.jdbc +package kotlinext.jdbc import java.sql.* import javax.sql.* -import kotlin.template.StringTemplate /** * Processes a connection from the pool using the given function block @@ -40,17 +39,3 @@ fun DataSource.update(sql: String): Int { fun DataSource.query(sql: String, block: (ResultSet) -> T): T { return use { it.query(sql, block) } } - -/** - * Performs the update using the given SQL using a [[StringTemplate]] - */ -fun DataSource.update(template : StringTemplate) : Int { - return use { it.update(template) } -} - -/** - * Perform a query on the connection using the SQL from the [[StringTemplate]] and processes the result set with a function - */ -fun DataSource.query(template : StringTemplate, resultBlock : (ResultSet) -> T) : T { - return use { it.query(template, resultBlock) } -} diff --git a/src/main/kotlin/kotlin/jdbc/PreparedStatements.kt b/src/main/kotlin/kotlinext/jdbc/PreparedStatements.kt similarity index 95% rename from src/main/kotlin/kotlin/jdbc/PreparedStatements.kt rename to src/main/kotlin/kotlinext/jdbc/PreparedStatements.kt index 4e38948..cca96af 100644 --- a/src/main/kotlin/kotlin/jdbc/PreparedStatements.kt +++ b/src/main/kotlin/kotlinext/jdbc/PreparedStatements.kt @@ -1,4 +1,4 @@ -package kotlin.jdbc +package kotlinext.jdbc /** * Helper method to process a statement on this collection diff --git a/src/main/kotlin/kotlin/jdbc/ResultSets.kt b/src/main/kotlin/kotlinext/jdbc/ResultSets.kt similarity index 99% rename from src/main/kotlin/kotlin/jdbc/ResultSets.kt rename to src/main/kotlin/kotlinext/jdbc/ResultSets.kt index 258dca2..5ddeff6 100644 --- a/src/main/kotlin/kotlin/jdbc/ResultSets.kt +++ b/src/main/kotlin/kotlinext/jdbc/ResultSets.kt @@ -1,4 +1,4 @@ -package kotlin.jdbc +package kotlinext.jdbc import java.sql.* diff --git a/src/main/kotlin/kotlin/jdbc/Statements.kt b/src/main/kotlin/kotlinext/jdbc/Statements.kt similarity index 92% rename from src/main/kotlin/kotlin/jdbc/Statements.kt rename to src/main/kotlin/kotlinext/jdbc/Statements.kt index 7203055..2e36b10 100644 --- a/src/main/kotlin/kotlin/jdbc/Statements.kt +++ b/src/main/kotlin/kotlinext/jdbc/Statements.kt @@ -1,4 +1,4 @@ -package kotlin.jdbc +package kotlinext.jdbc /** * Helper method to process a statement on this collection diff --git a/src/test/kotlin/test/kotlin/jdbc/JdbcTemplateTest.kt b/src/test/kotlin/test/kotlin/jdbc/JdbcTemplateTest.kt deleted file mode 100644 index ce211ec..0000000 --- a/src/test/kotlin/test/kotlin/jdbc/JdbcTemplateTest.kt +++ /dev/null @@ -1,37 +0,0 @@ -package test.kotlin.jdbc.experiment1 - -import kotlin.jdbc.* -import kotlin.template.* -import kotlin.test.* -import org.junit.Test as test -import test.kotlin.jdbc.* - -class JdbcTemplateTest { - //val dataSource = createDataSource() - - @test fun templateInsert() { - val id = 3 - val name = "Stepan" - - // Mimicks the following code - // dataSource.update("insert into foo (id, name) values ($id, $name)") - - // TODO will use a tuple soon - dataSource.update( - StringTemplate(arrayOf("insert into foo (id, name) values (", id, ", ", name, ")")) - ) - - // Mimicks - // datasource.query("select * from foo where id = $id") { it.map{ it["name"] } } - - val names = dataSource.query( - StringTemplate(arrayOf("select * from foo where id = ", id)) - ) { - it.map{ it["name"] }.toList() - } - - println("Found names $names") - val actual = names.first() - assertEquals(name, actual) - } -} diff --git a/src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt b/src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt index 883260b..ea51657 100644 --- a/src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt +++ b/src/test/kotlin/test/kotlin/jdbc/JdbcTest.kt @@ -1,10 +1,10 @@ package test.kotlin.jdbc +import kotlinext.jdbc.* import java.sql.ResultSet import javax.sql.DataSource -import kotlin.jdbc.* -import kotlin.test.* import org.h2.jdbcx.JdbcConnectionPool +import org.junit.Assert.assertEquals import org.junit.Test as test public val dataSource : DataSource = createDataSource() @@ -49,7 +49,7 @@ class JdbcTest { } @test fun stringFormat() { - dataSource.query(kotlin.template.StringTemplate(arrayOf("select * from foo where id = ", 1))) { + dataSource.query("select * from foo where id = 1") { for (row in it) { println(row.getValuesAsMap()) }