From b647f59e05acf90c925379bc0dafa7e5a298881e Mon Sep 17 00:00:00 2001 From: Laurent Van der Linden Date: Fri, 27 Nov 2015 13:07:43 +0100 Subject: [PATCH] Mapper fixes SingleConnection wrapper publish non osgi jar --- build.gradle | 4 +- src/main/kotlin/sqlbuilder/impl/SelectImpl.kt | 4 +- .../sqlbuilder/impl/mappers/BooleanMapper.kt | 2 +- .../sqlbuilder/impl/mappers/CharMapper.kt | 2 +- .../sqlbuilder/impl/mappers/DateMapper.kt | 2 +- .../sqlbuilder/impl/mappers/IntegerMapper.kt | 2 +- .../pool/SingleConnectionDataSource.kt | 52 +++++++++++++++++++ .../pool/TransactionalConnection.kt | 2 +- 8 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/sqlbuilder/pool/SingleConnectionDataSource.kt diff --git a/build.gradle b/build.gradle index 7ba338f..ba9c811 100755 --- a/build.gradle +++ b/build.gradle @@ -71,7 +71,7 @@ publishing { } } -jar { +/*jar { from(configurations.embedded) { into('lib') } @@ -81,7 +81,7 @@ jar { instruction 'Import-Package', '!jet.*,!kotlin.*,*' instruction 'Bundle-Classpath', ".,${configurations.embedded.files.collect({'lib/' + it.name}).join(',')}" } -} +}*/ defaultTasks 'build' sourceSets { diff --git a/src/main/kotlin/sqlbuilder/impl/SelectImpl.kt b/src/main/kotlin/sqlbuilder/impl/SelectImpl.kt index 327500b..60ba547 100755 --- a/src/main/kotlin/sqlbuilder/impl/SelectImpl.kt +++ b/src/main/kotlin/sqlbuilder/impl/SelectImpl.kt @@ -255,7 +255,7 @@ class SelectImpl(val backend: Backend) : Select { } val con = backend.getSqlConnection() try { - logger.info(sql) + logger.info("{} ({})", sql, whereParameters) val sqlConverter = SqlConverter(backend.configuration) @@ -265,7 +265,7 @@ class SelectImpl(val backend: Backend) : Select { val parameterCount = ps.parameterMetaData.parameterCount whereParameters.withIndex().forEach { pair -> if (pair.index < parameterCount) { - sqlConverter.setParameter(ps, pair.value, pair.index + 1) + sqlConverter.setParameter(ps, pair.value, pair.index + 1, pair.value.javaClass) } } diff --git a/src/main/kotlin/sqlbuilder/impl/mappers/BooleanMapper.kt b/src/main/kotlin/sqlbuilder/impl/mappers/BooleanMapper.kt index efe086a..1d6c7d8 100644 --- a/src/main/kotlin/sqlbuilder/impl/mappers/BooleanMapper.kt +++ b/src/main/kotlin/sqlbuilder/impl/mappers/BooleanMapper.kt @@ -28,6 +28,6 @@ public class BooleanMapper : BiMapper { } override fun handles(targetType: Class<*>): Boolean { - return Boolean::class.java == targetType + return Boolean::class.java == targetType || java.lang.Boolean::class.java == targetType } } \ No newline at end of file diff --git a/src/main/kotlin/sqlbuilder/impl/mappers/CharMapper.kt b/src/main/kotlin/sqlbuilder/impl/mappers/CharMapper.kt index 4e9c69f..89c83f9 100644 --- a/src/main/kotlin/sqlbuilder/impl/mappers/CharMapper.kt +++ b/src/main/kotlin/sqlbuilder/impl/mappers/CharMapper.kt @@ -25,6 +25,6 @@ class CharMapper : BiMapper { } override fun handles(targetType: Class<*>): Boolean { - return targetType == Char::class.java + return targetType == Char::class.java || java.lang.Character::class.java == targetType } } \ No newline at end of file diff --git a/src/main/kotlin/sqlbuilder/impl/mappers/DateMapper.kt b/src/main/kotlin/sqlbuilder/impl/mappers/DateMapper.kt index a6201cc..cfd2eba 100644 --- a/src/main/kotlin/sqlbuilder/impl/mappers/DateMapper.kt +++ b/src/main/kotlin/sqlbuilder/impl/mappers/DateMapper.kt @@ -32,6 +32,6 @@ public class DateMapper : BiMapper { } override fun handles(targetType: Class<*>): Boolean { - return Boolean::class.java == targetType + return Date::class.java == targetType } } \ No newline at end of file diff --git a/src/main/kotlin/sqlbuilder/impl/mappers/IntegerMapper.kt b/src/main/kotlin/sqlbuilder/impl/mappers/IntegerMapper.kt index 46a31b0..1528012 100644 --- a/src/main/kotlin/sqlbuilder/impl/mappers/IntegerMapper.kt +++ b/src/main/kotlin/sqlbuilder/impl/mappers/IntegerMapper.kt @@ -27,6 +27,6 @@ public class IntegerMapper : BiMapper { } override fun handles(targetType: Class<*>): Boolean { - return Int::class.java == targetType + return Int::class.java == targetType || java.lang.Integer::class.java == targetType } } \ No newline at end of file diff --git a/src/main/kotlin/sqlbuilder/pool/SingleConnectionDataSource.kt b/src/main/kotlin/sqlbuilder/pool/SingleConnectionDataSource.kt new file mode 100644 index 0000000..acf8a9f --- /dev/null +++ b/src/main/kotlin/sqlbuilder/pool/SingleConnectionDataSource.kt @@ -0,0 +1,52 @@ +package sqlbuilder.pool + +import java.io.PrintWriter +import java.sql.Connection +import java.util.logging.Logger +import javax.sql.DataSource + +/** + * Adapter to use SqlBuilder with an externally provided sql Connection. + * Will not close the target connection. + */ +public class SingleConnectionDataSource(val target: Connection) : DataSource { + override fun getConnection(): Connection? { + return NonClosingConnection(target) + } + + override fun getConnection(username: String?, password: String?): Connection? { + return connection + } + + override fun setLogWriter(out: PrintWriter?) { + throw UnsupportedOperationException() + } + + override fun getLogWriter(): PrintWriter? { + throw UnsupportedOperationException() + } + + override fun setLoginTimeout(seconds: Int) { + throw UnsupportedOperationException() + } + + override fun getParentLogger(): Logger? { + throw UnsupportedOperationException() + } + + override fun getLoginTimeout(): Int { + throw UnsupportedOperationException() + } + + override fun isWrapperFor(iface: Class<*>?): Boolean { + throw UnsupportedOperationException() + } + + override fun unwrap(iface: Class?): T { + throw UnsupportedOperationException() + } +} + +private class NonClosingConnection(private val connection: Connection) : Connection by connection { + override fun close() {} +} \ No newline at end of file diff --git a/src/main/kotlin/sqlbuilder/pool/TransactionalConnection.kt b/src/main/kotlin/sqlbuilder/pool/TransactionalConnection.kt index a92c2b5..7669805 100755 --- a/src/main/kotlin/sqlbuilder/pool/TransactionalConnection.kt +++ b/src/main/kotlin/sqlbuilder/pool/TransactionalConnection.kt @@ -20,7 +20,7 @@ class TransactionalConnection(val target: Connection, val datasource: DataSource override fun invoke(proxy: Any?, method: Method, args: Array?): Any? { return synchronized(datasource) { lastModified = System.currentTimeMillis() - val methodName = method.getName() + val methodName = method.name if ("close" == methodName) { datasource.freeConnection(this) } else {