All Downloads are FREE. Search and download functionalities are using the official Maven repository.

harness.sql.ConnectionFactory.scala Maven / Gradle / Ivy

There is a newer version: 5.1.3
Show newest version
package harness.sql

import harness.sql.error.ConnectionError
import harness.zio.*
import java.sql.{Connection, DriverManager}
import zio.*

final class ConnectionFactory private (
    private[sql] val getJDBCConnection: ZIO[Scope, ConnectionError, JDBCConnection],
)
object ConnectionFactory {

  private def wrapUnsafe(get: => Connection): ConnectionFactory =
    new ConnectionFactory(
      for {
        con <- ZIO.attempt(get).autoClose.mapError(ConnectionError(_))
        id <- Random.nextUUID
      } yield JDBCConnection(con, id),
    )

  def apply(url: String): ConnectionFactory = wrapUnsafe(DriverManager.getConnection(url))
  def apply(url: String, user: String, password: String): ConnectionFactory = wrapUnsafe(DriverManager.getConnection(url, user, password))
  def apply(dbConfig: DbConfig): ConnectionFactory = ConnectionFactory(dbConfig.psqlJdbcUrl, dbConfig.credentials.username, dbConfig.credentials.password)

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy