oshai
11/20/2018, 5:22 PMval connectionUri = "jdbc:postgresql://$host:$port/$database?user=$username&password=$password"
val connection = PostgreSQLConnectionBuilder.createConnectionPool(connectionUri) { it.copy(
connectionCreateTimeout = 1
)}
I am writing a lib and I want to configure a connection. Configuration is a data class and all are vals.
Can I avoid the it.copy
in the code above (call the copy somehow in the lib), or is there an apply method?data class ConnectionPoolConfiguration @JvmOverloads constructor(
val host: String = "localhost",
val port: Int = 5432,
val database: String? = null,
val username: String = "dbuser",
val password: String? = null,
val connectionCreateTimeout: Long? = null
)
it.copy
diesieben07
11/20/2018, 5:30 PMcopy
is the right tool here. Why don't you want to use it?oshai
11/20/2018, 5:32 PMdiesieben07
11/20/2018, 5:32 PMoshai
11/20/2018, 5:34 PMdiesieben07
11/20/2018, 5:36 PMoshai
11/20/2018, 5:36 PMPostgreSQLConnectionBuilder.createConnectionPool(connectionUri) {
connectionCreateTimeout = 1
}
diesieben07
11/20/2018, 5:46 PMvar
somewhere to achieve that syntax. It doesn't have to be in the actual connection properties, you can have a temporary mutable objectPostgreSQLConnectionBuilder.createConnectionPool(connectionUri, ConnectionProperties(createTimeout = 1))
?oshai
11/20/2018, 5:53 PMPostgreSQLConnectionBuilder.createConnectionPool(connectionUri)
is building a default configuration and I would like to be able to override some of the properties later, so I need to use that objectNikky
11/20/2018, 7:15 PMoshai
11/20/2018, 7:26 PMdiegog
11/20/2018, 7:47 PMcreateConnectionPool
?oshai
11/20/2018, 7:57 PM@JvmStatic
fun createConnectionPool(url: String,
configurator: (ConnectionPoolConfiguration) -> ConnectionPoolConfiguration = { it }): ConnectionPool<MySQLConnection> {
val configuration = URLParser.parseOrDie(url)
val connectionPoolConfiguration = configurator(with(configuration) {
ConnectionPoolConfiguration(
username = username,
host = host,
port = port,
password = password,
database = database,
ssl = ssl,
charset = charset,
maximumMessageSize = maximumMessageSize,
allocator = allocator,
queryTimeout = queryTimeout?.toMillis()
)})
return ConnectionPool(
MySQLConnectionFactory(connectionPoolConfiguration.connectionConfiguration),
connectionPoolConfiguration.poolConfiguration,
connectionPoolConfiguration.executionContext
)
}
diegog
11/20/2018, 8:40 PMdave08
11/26/2018, 8:51 AMoshai
11/26/2018, 6:25 PMdave08
11/26/2018, 8:42 PMoshai
11/26/2018, 9:39 PM