scalikejdbc.JDBCUrl.scala Maven / Gradle / Ivy
package scalikejdbc
/**
* Companion object of JDBC URL
*/
object JDBCUrl {
// Heroku support
val HerokuPostgresRegexp = "^postgres://([a-zA-Z0-9_]+):([^@]+)@([^/]+)/([^\\s]+)$".r
val HerokuMySQLRegexp = "^mysql://([a-zA-Z0-9_]+):([^@]+)@([^/]+)/([^\\s]+)$".r
val MysqlCustomProperties = ".*\\?(.*)".r
def apply(url: String): JDBCUrl = try {
val urlParts = url.split("/")
val hostAndPort = urlParts(2).split(":")
val (host, port) = (hostAndPort.head, hostAndPort.tail.headOption.map(_.toInt).getOrElse(defaultPort(url)))
val database = urlParts(3)
JDBCUrl(
host = host,
port = port,
database = database)
} catch {
case e: Exception =>
throw new IllegalArgumentException("Failed to parse JDBC URL (" + url + ")")
}
private[this] def defaultPort(url: String): Int = if (url.startsWith("jdbc:mysql://")) 3306 else 5432
}
/**
* JDBC URL which contains host, port and database name
*
* @param host
* @param port
* @param database
*/
case class JDBCUrl(host: String, port: Int, database: String)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy