summaryrefslogtreecommitdiff
path: root/common/src/main/kotlin/db/utils.kt
diff options
context:
space:
mode:
Diffstat (limited to 'common/src/main/kotlin/db/utils.kt')
-rw-r--r--common/src/main/kotlin/db/utils.kt98
1 files changed, 0 insertions, 98 deletions
diff --git a/common/src/main/kotlin/db/utils.kt b/common/src/main/kotlin/db/utils.kt
index 33d50531..d1197894 100644
--- a/common/src/main/kotlin/db/utils.kt
+++ b/common/src/main/kotlin/db/utils.kt
@@ -34,104 +34,6 @@ import kotlin.io.path.Path
internal val logger: Logger = LoggerFactory.getLogger("libeufin-db")
-/**
- * This function converts postgresql:// URIs to JDBC URIs.
- *
- * URIs that are already jdbc: URIs are passed through.
- *
- * This avoids the user having to create complex JDBC URIs for postgres connections.
- * They are especially complex when using unix domain sockets, as they're not really
- * supported natively by JDBC.
- */
-fun getJdbcConnectionFromPg(pgConn: String): String {
- // Pass through jdbc URIs.
- if (pgConn.startsWith("jdbc:")) {
- return pgConn
- }
- if (!pgConn.startsWith("postgresql://") && !pgConn.startsWith("postgres://")) {
- throw Exception("Not a Postgres connection string: $pgConn")
- }
- var maybeUnixSocket = false
- val parsed = URI(pgConn)
- var hostAsParam: String? = if (parsed.query != null) {
- getQueryParam(parsed.query, "host")
- } else {
- null
- }
- var pgHost = System.getenv("PGHOST")
- if (null == pgHost)
- pgHost = parsed.host
- var pgPort = System.getenv("PGPORT")
- if (null == pgPort) {
- if (-1 == parsed.port)
- pgPort = "5432"
- else
- pgPort = parsed.port.toString()
- }
-
- /**
- * In some cases, it is possible to leave the hostname empty
- * and specify it via a query param, therefore a "postgresql:///"-starting
- * connection string does NOT always mean Unix domain socket.
- * https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
- */
- if (pgHost == null &&
- (hostAsParam == null || hostAsParam.startsWith('/'))
- ) {
- maybeUnixSocket = true
- }
- if (pgHost != null &&
- (pgHost.startsWith('/'))
- ) {
- maybeUnixSocket = true
- }
- if (maybeUnixSocket) {
- // Check whether the database user should differ from the process user.
- var pgUser = getCurrentUser()
- if (parsed.query != null) {
- val maybeUserParam = getQueryParam(parsed.query, "user")
- if (maybeUserParam != null) pgUser = maybeUserParam
- }
- // Check whether the Unix domain socket location was given non-standard.
- if ( (null == hostAsParam) && (null != pgHost) )
- hostAsParam = pgHost + "/.s.PGSQL." + pgPort
- val socketLocation = hostAsParam ?: "/var/run/postgresql/.s.PGSQL." + pgPort
- if (!socketLocation.startsWith('/')) {
- throw Exception("PG connection wants Unix domain socket, but non-null host doesn't start with slash")
- }
- return "jdbc:postgresql://localhost${parsed.path}?user=$pgUser&socketFactory=org.newsclub.net.unix." +
- "AFUNIXSocketFactory\$FactoryArg&socketFactoryArg=$socketLocation"
- }
- if (pgConn.startsWith("postgres://")) {
- // The JDBC driver doesn't like postgres://, only postgresql://.
- // For consistency with other components, we normalize the postgres:// URI
- // into one that the JDBC driver likes.
- return "jdbc:postgresql://" + pgConn.removePrefix("postgres://")
- }
- logger.info("connecting to database via JDBC string '$pgConn'")
- return "jdbc:$pgConn"
-}
-
-data class DatabaseConfig(
- val dbConnStr: String,
- val sqlDir: Path
-)
-
-fun pgDataSource(dbConfig: String): PGSimpleDataSource {
- val jdbcConnStr = getJdbcConnectionFromPg(dbConfig)
- logger.debug("connecting to database via JDBC string '$jdbcConnStr'")
- val pgSource = PGSimpleDataSource()
- pgSource.setUrl(jdbcConnStr)
- pgSource.prepareThreshold = 1
- return pgSource
-}
-
-fun PGSimpleDataSource.pgConnection(schema: String? = null): PgConnection {
- val conn = connection.unwrap(PgConnection::class.java)
- if (schema != null) conn.execSQLUpdate("SET search_path TO $schema")
- return conn
-}
-
fun <R> PgConnection.transaction(lambda: (PgConnection) -> R): R {
try {
autoCommit = false