10000 r2dbc: parse duration when string option · jasync-sql/jasync-sql@109fb79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 109fb79

Browse files
committed
r2dbc: parse duration when string option
Fix issue #412
1 parent 8c25f9e commit 109fb79

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

r2dbc-mysql/src/main/java/MysqlConnectionFactoryProvider.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class MysqlConnectionFactoryProvider : ConnectionFactoryProvider {
7777
password = connectionFactoryOptions.getValue(PASSWORD)?.toString(),
7878
database = connectionFactoryOptions.getValue(DATABASE) as String?,
7979
applicationName = connectionFactoryOptions.getValue(APPLICATION_NAME) as String?,
80-
connectionTimeout = (connectionFactoryOptions.getValue(CONNECT_TIMEOUT) as Duration?)?.toMillis()?.toInt() ?: 5000,
81-
queryTimeout = connectionFactoryOptions.getValue(STATEMENT_TIMEOUT) as Duration?,
80+
connectionTimeout = connectionFactoryOptions.getValue(CONNECT_TIMEOUT)?.parseDuration()?.toMillis()?.toInt() ?: 5000,
81+
queryTimeout = connectionFactoryOptions.getValue(STATEMENT_TIMEOUT)?.parseDuration(),
8282
ssl = MysqlSSLConfigurationFactory.create(connectionFactoryOptions),
8383
rsaPublicKey = (connectionFactoryOptions.getValue(SERVER_RSA_PUBLIC_KEY_FILE) as String?)?.let { Paths.get(it) }
8484
)
@@ -97,3 +97,15 @@ class MysqlConnectionFactoryProvider : ConnectionFactoryProvider {
9797

9898
override fun getDriver(): String = MYSQL_DRIVER
9999
}
100+
101+
private fun Any.parseDuration(): Duration {
102+
return when (this) {
103+
is Duration -> {
104+
this
105+
}
106+
is String -> {
107+
Duration.parse(this)
108+
}
109+
else -> throw Exception("cant parse $this to Duration")
110+
}
111+
}

r2dbc-mysql/src/test/java/com/github/jasync/r2dbc/mysql/MysqlConnectionFactoryProviderTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.github.jasync.sql.db.SSLConfiguration
44
import io.r2dbc.spi.ConnectionFactoryOptions
55
import org.junit.Assert.assertEquals
66
import org.junit.Test
7+
import java.time.Duration
78

89
class MysqlConnectionFactoryProviderTest {
910

@@ -63,4 +64,15 @@ class MysqlConnectionFactoryProviderTest {
6364
// then
6465
assertEquals("rsa.pem", result.mySQLConnectionFactory.configuration.rsaPublicKey.toString())
6566
}
67+
68+
@Test
69+
fun shouldUseTimeoutAsString() {
70+
val options = ConnectionFactoryOptions.parse("r2dbc:mysql://user@host/db?connectTimeout=PT3S")
71+
72+
// when
73+
val result = provider.create(options)
74+
75+
// then
76+
assertEquals(Duration.parse("PT3S").toMillis().toInt(), result.mySQLConnectionFactory.configuration.connectionTimeout)
77+
}
6678
}

0 commit comments

Comments
 (0)
0