package play.api.db
import javax.sql.DataSource
import com.typesafe.config.Config
import play.api.{ Environment, Mode }
import play.api.inject.Injector
import play.utils.Reflect
trait ConnectionPool {
def create(name: String, dbConfig: DatabaseConfig, configuration: Config): DataSource
def close(dataSource: DataSource): Unit
}
object ConnectionPool {
def fromConfig(config: String, injector: Injector, environment: Environment, default: ConnectionPool): ConnectionPool = {
config match {
case "default" => default
case "bonecp" => new BoneConnectionPool(environment)
case "hikaricp" => new HikariCPConnectionPool(environment)
case fqcn => injector.instanceOf(Reflect.getClass[ConnectionPool](fqcn, environment.classLoader))
}
}
private val PostgresFullUrl = "^postgres://([a-zA-Z0-9_]+):([^@]+)@([^/]+)/([^\\s]+)$".r
private val MysqlFullUrl = "^mysql://([a-zA-Z0-9_]+):([^@]+)@([^/]+)/([^\\s]+)$".r
private val MysqlCustomProperties = ".*\\?(.*)".r
private val H2DefaultUrl = "^jdbc:h2:mem:.+".r
def (: Option[String], : Mode.Mode): (Option[String], Option[(String, String)]) = {
maybeUrl match {
case Some(PostgresFullUrl(username, password, host, dbname)) =>
Some(s"jdbc:postgresql://$host/$dbname") -> Some(username -> password)
case Some(url @ MysqlFullUrl(username, password, host, dbname)) =>
val = "?useUnicode=yes&characterEncoding=UTF-8&connectionCollation=utf8_general_ci"
val = MysqlCustomProperties.findFirstMatchIn(url).map( => "").getOrElse(defaultProperties)
Some(s"jdbc:mysql://$host/${dbname + addDefaultPropertiesIfNeeded}") -> Some(username -> password)
case Some(url @ H2DefaultUrl()) if !url.contains("DB_CLOSE_DELAY") && mode == Mode.Dev =>
Some(s"$url;DB_CLOSE_DELAY=-1") -> None
case Some(url) =>
Some(url) -> None
case None =>
None -> None
}
}
}