Skip to content

Improve JDBC Url parsing #366

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 5 commits into from
Jan 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,49 @@
package scalikejdbc.async.internal

import com.github.jasync.sql.db.Configuration
import scalikejdbc.JDBCUrl
import scalikejdbc.async.AsyncConnectionSettings

/**
* Configuration attribute
*/
private[scalikejdbc] trait JasyncConfiguration {

@deprecated("Will be removed in the future", "0.16.0")
val defaultConfiguration = new Configuration("")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this field is no longer used, but I didnt remove it due to backwards compatibility. If that is not an issue I can remove it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, can you add @deprecated annotation to the public field? The since version will be 0.16.0


protected def parseUrl(url: String): Configuration

private[scalikejdbc] def configuration(
url: String,
user: String,
password: String,
connectionSettings: AsyncConnectionSettings
) = {
val jdbcUrl = JDBCUrl(url)
new Configuration(
val baseConf = parseUrl(url)
baseConf.copy(
user,
jdbcUrl.host,
jdbcUrl.port,
baseConf.getHost,
baseConf.getPort,
password,
jdbcUrl.database,
connectionSettings.ssl.getOrElse(defaultConfiguration.getSsl),
connectionSettings.charset.getOrElse(defaultConfiguration.getCharset),
baseConf.getDatabase,
connectionSettings.ssl.getOrElse(baseConf.getSsl),
connectionSettings.charset.getOrElse(baseConf.getCharset),
connectionSettings.maximumMessageSize.getOrElse(
defaultConfiguration.getMaximumMessageSize
baseConf.getMaximumMessageSize
),
connectionSettings.allocator.getOrElse(defaultConfiguration.getAllocator),
connectionSettings.allocator.getOrElse(baseConf.getAllocator),
connectionSettings.connectTimeout
.map(_.toMillis.toInt)
.getOrElse(defaultConfiguration.getConnectionTimeout),
.getOrElse(baseConf.getConnectionTimeout),
connectionSettings.queryTimeout
.map(x => java.time.Duration.ofMillis(x.toMillis))
.getOrElse(defaultConfiguration.getQueryTimeout),
defaultConfiguration.getApplicationName,
defaultConfiguration.getInterceptors,
defaultConfiguration.getEventLoopGroup,
defaultConfiguration.getExecutionContext
.getOrElse(baseConf.getQueryTimeout),
baseConf.getApplicationName,
baseConf.getInterceptors,
baseConf.getEventLoopGroup,
baseConf.getExecutionContext,
baseConf.getCurrentSchema,
baseConf.getSocketPath,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import scalikejdbc._, async._, internal._
import com.github.jasync.sql.db.Configuration
import com.github.jasync.sql.db.mysql.MySQLConnection
import com.github.jasync.sql.db.mysql.pool.MySQLConnectionFactory
import com.github.jasync.sql.db.mysql.util.URLParser

import java.nio.charset.StandardCharsets

/**
* MySQL Connection Pool
Expand All @@ -42,6 +45,9 @@ private[scalikejdbc] class MySQLConnectionPoolImpl(
settings
) {

override protected def parseUrl(url: String): Configuration =
URLParser.INSTANCE.parse(url, StandardCharsets.UTF_8)

override def borrow(): AsyncConnection = new PoolableAsyncConnection(pool)
with MySQLConnectionImpl

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package scalikejdbc.async.internal.mysql

import com.github.jasync.sql.db.Configuration
import com.github.jasync.sql.db.mysql.util.URLParser
import scalikejdbc.async._, internal._

import java.nio.charset.StandardCharsets

/**
* MySQL Single Connection
*/
Expand All @@ -29,6 +33,9 @@ private[scalikejdbc] case class SingleAsyncMySQLConnection(
with MySQLConnectionImpl
with JasyncConfiguration {

override protected def parseUrl(url: String): Configuration =
URLParser.INSTANCE.parse(url, StandardCharsets.UTF_8)

private[scalikejdbc] val underlying = {
new com.github.jasync.sql.db.mysql.MySQLConnection(
configuration(url, user, password, connectionSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import scalikejdbc._, async._, internal._
import com.github.jasync.sql.db.Configuration
import com.github.jasync.sql.db.postgresql.PostgreSQLConnection
import com.github.jasync.sql.db.postgresql.pool.PostgreSQLConnectionFactory
import com.github.jasync.sql.db.postgresql.util.URLParser

import java.nio.charset.StandardCharsets

/**
* PostgreSQL Connection Pool
Expand All @@ -42,6 +45,9 @@ private[scalikejdbc] class PostgreSQLConnectionPoolImpl(
settings
) {

override protected def parseUrl(url: String): Configuration =
URLParser.INSTANCE.parse(url, StandardCharsets.UTF_8)

override def borrow(): AsyncConnection = new PoolableAsyncConnection(pool)
with PostgreSQLConnectionImpl

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package scalikejdbc.async.internal.postgresql

import com.github.jasync.sql.db.Configuration
import com.github.jasync.sql.db.postgresql.util.URLParser
import scalikejdbc.async._, internal._

import java.nio.charset.StandardCharsets

/**
* PostgreSQL Single Connection
*/
Expand All @@ -29,6 +33,9 @@ private[scalikejdbc] case class SingleAsyncPostgreSQLConnection(
with PostgreSQLConnectionImpl
with JasyncConfiguration {

override protected def parseUrl(url: String): Configuration =
URLParser.INSTANCE.parse(url, StandardCharsets.UTF_8)

private[scalikejdbc] val underlying = {
new com.github.jasync.sql.db.postgresql.PostgreSQLConnection(
configuration(url, user, password, connectionSettings)
Expand Down
5 changes: 2 additions & 3 deletions core/src/test/scala/unit/DBSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ trait DBSettings extends ForAllTestContainer { self: Suite =>
)
override val container = MultipleContainers(mysql, postgres)

// https://github.com/testcontainers/testcontainers-java/issues/2544
protected[this] final def mysqlJdbcUrl = mysql.jdbcUrl.split('?').head
protected[this] final def postgresJdbcUrl = postgres.jdbcUrl.split('?').head
protected[this] final def mysqlJdbcUrl = mysql.jdbcUrl
protected[this] final def postgresJdbcUrl = postgres.jdbcUrl

protected[this] final val asyncConnectionPoolSettings
: AsyncConnectionPoolSettings = AsyncConnectionPoolSettings()
Expand Down