8000 r2dbc: parse duration when string option by oshai · Pull Request #415 · jasync-sql/jasync-sql · GitHub
[go: up one dir, main page]

Skip to content

r2dbc: parse duration when string option #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions r2dbc-mysql/src/main/java/MysqlConnectionFactoryProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class MysqlConnectionFactoryProvider : ConnectionFactoryProvider {
password = connectionFactoryOptions.getValue(PASSWORD)?.toString(),
database = connectionFactoryOptions.getValue(DATABASE) as String?,
applicationName = connectionFactoryOptions.getValue(APPLICATION_NAME) as String?,
connectionTimeout = (connectionFactoryOptions.getValue(CONNECT_TIMEOUT) as Duration?)?.toMillis()?.toInt() ?: 5000,
queryTimeout = connectionFactoryOptions.getValue(STATEMENT_TIMEOUT) as Duration?,
connectionTimeout = connectionFactoryOptions.getValue(CONNECT_TIMEOUT)?.parseDuration()?.toMillis()?.toInt() ?: 5000,
queryTimeout = connectionFactoryOptions.getValue(STATEMENT_TIMEOUT)?.parseDuration(),
ssl = MysqlSSLConfigurationFactory.create(connectionFactoryOptions),
rsaPublicKey = (connectionFactoryOptions.getValue(SERVER_RSA_PUBLIC_KEY_FILE) as String?)?.let { Paths.get(it) }
)
Expand All @@ -97,3 +97,15 @@ class MysqlConnectionFactoryProvider : ConnectionFactoryProvider {

override fun getDriver(): String = MYSQL_DRIVER
}

private fun Any.parseDuration(): Duration {
return when (this) {
is Duration -> {
this
}
is String -> {
Duration.parse(this)
}
else -> throw Exception("cant parse $this to Duration")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.jasync.sql.db.SSLConfiguration
import io.r2dbc.spi.ConnectionFactoryOptions
import org.junit.Assert.assertEquals
import org.junit.Test
import java.time.Duration

class MysqlConnectionFactoryProviderTest {

Expand Down Expand Up @@ -63,4 +64,15 @@ class MysqlConnectionFactoryProviderTest {
// then
assertEquals("rsa.pem", result.mySQLConnectionFactory.configuration.rsaPublicKey.toString())
}

@Test
fun shouldUseTimeoutAsString() {
val options = ConnectionFactoryOptions.parse("r2dbc:mysql://user@host/db?connectTimeout=PT3S")

// when
val result = provider.create(options)

// then
assertEquals(Duration.parse("PT3S").toMillis().toInt(), result.mySQLConnectionFactory.configuration.connectionTimeout)
}
}
0