mirror of
https://github.com/OpenBankProject/OBP-API.git
synced 2026-02-06 13:26:51 +00:00
refactor/Remove redunant occurenies of function getDbConnectionParameters
This commit is contained in:
parent
4e6a260537
commit
ce1977b7c4
14
obp-api/src/main/scala/code/api/util/DBUtil.scala
Normal file
14
obp-api/src/main/scala/code/api/util/DBUtil.scala
Normal file
@ -0,0 +1,14 @@
|
||||
package code.api.util
|
||||
|
||||
import code.api.Constant
|
||||
|
||||
object DBUtil {
|
||||
def getDbConnectionParameters: (String, String, String) = {
|
||||
val dbUrl = APIUtil.getPropsValue("db.url") openOr Constant.h2DatabaseDefaultUrlValue
|
||||
val username = dbUrl.split(";").filter(_.contains("user")).toList.headOption.map(_.split("=")(1))
|
||||
val password = dbUrl.split(";").filter(_.contains("password")).toList.headOption.map(_.split("=")(1))
|
||||
val dbUser = APIUtil.getPropsValue("db.user").orElse(username)
|
||||
val dbPassword = APIUtil.getPropsValue("db.password").orElse(password)
|
||||
(dbUrl, dbUser.getOrElse(""), dbPassword.getOrElse(""))
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
package code.api.util.migration
|
||||
|
||||
import code.api.util.APIUtil
|
||||
import code.api.util.{APIUtil, DBUtil}
|
||||
import code.api.util.migration.Migration.{DbFunction, saveLog}
|
||||
import code.context.MappedConsentAuthContext
|
||||
import net.liftweb.common.Full
|
||||
@ -18,15 +18,6 @@ object MigrationOfConsentAuthContextDropIndex {
|
||||
val oneDayAgo = ZonedDateTime.now(ZoneId.of("UTC")).minusDays(1)
|
||||
val oneYearInFuture = ZonedDateTime.now(ZoneId.of("UTC")).plusYears(1)
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'")
|
||||
|
||||
private lazy val getDbConnectionParameters: (String, String, String) = {
|
||||
val dbUrl = APIUtil.getPropsValue("db.url") openOr Constant.h2DatabaseDefaultUrlValue
|
||||
val username = dbUrl.split(";").filter(_.contains("user")).toList.headOption.map(_.split("=")(1))
|
||||
val password = dbUrl.split(";").filter(_.contains("password")).toList.headOption.map(_.split("=")(1))
|
||||
val dbUser = APIUtil.getPropsValue("db.user").orElse(username)
|
||||
val dbPassword = APIUtil.getPropsValue("db.password").orElse(password)
|
||||
(dbUrl, dbUser.getOrElse(""), dbPassword.getOrElse(""))
|
||||
}
|
||||
|
||||
/**
|
||||
* this connection pool context corresponding db.url in default.props
|
||||
@ -39,7 +30,7 @@ object MigrationOfConsentAuthContextDropIndex {
|
||||
validationQuery = "select 1",
|
||||
connectionPoolFactoryName = "commons-dbcp2"
|
||||
)
|
||||
val (dbUrl, user, password) = getDbConnectionParameters
|
||||
val (dbUrl, user, password) = DBUtil.getDbConnectionParameters
|
||||
val dbName = "DB_NAME" // corresponding props db.url DB
|
||||
ConnectionPool.add(dbName, dbUrl, user, password, settings)
|
||||
val connectionPool = ConnectionPool.get(dbName)
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
package code.api.util.migration
|
||||
|
||||
import code.api.Constant
|
||||
import code.api.util.APIUtil
|
||||
import code.api.util.{APIUtil, DBUtil}
|
||||
import code.api.util.migration.Migration.{DbFunction, saveLog}
|
||||
import code.loginattempts.MappedBadLoginAttempt
|
||||
import net.liftweb.mapper.{DB, Schemifier}
|
||||
import net.liftweb.util.DefaultConnectionIdentifier
|
||||
import scalikejdbc.DB.CPContext
|
||||
import scalikejdbc._
|
||||
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.{ZoneId, ZonedDateTime}
|
||||
|
||||
@ -17,15 +16,6 @@ object MigrationOfMappedBadLoginAttemptDropIndex {
|
||||
val oneDayAgo = ZonedDateTime.now(ZoneId.of("UTC")).minusDays(1)
|
||||
val oneYearInFuture = ZonedDateTime.now(ZoneId.of("UTC")).plusYears(1)
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'")
|
||||
|
||||
private lazy val getDbConnectionParameters: (String, String, String) = {
|
||||
val dbUrl = APIUtil.getPropsValue("db.url") openOr Constant.h2DatabaseDefaultUrlValue
|
||||
val username = dbUrl.split(";").filter(_.contains("user")).toList.headOption.map(_.split("=")(1))
|
||||
val password = dbUrl.split(";").filter(_.contains("password")).toList.headOption.map(_.split("=")(1))
|
||||
val dbUser = APIUtil.getPropsValue("db.user").orElse(username)
|
||||
val dbPassword = APIUtil.getPropsValue("db.password").orElse(password)
|
||||
(dbUrl, dbUser.getOrElse(""), dbPassword.getOrElse(""))
|
||||
}
|
||||
|
||||
/**
|
||||
* this connection pool context corresponding db.url in default.props
|
||||
@ -38,7 +28,7 @@ object MigrationOfMappedBadLoginAttemptDropIndex {
|
||||
validationQuery = "select 1",
|
||||
connectionPoolFactoryName = "commons-dbcp2"
|
||||
)
|
||||
val (dbUrl, user, password) = getDbConnectionParameters
|
||||
val (dbUrl, user, password) = DBUtil.getDbConnectionParameters
|
||||
val dbName = "DB_NAME" // corresponding props db.url DB
|
||||
ConnectionPool.add(dbName, dbUrl, user, password, settings)
|
||||
val connectionPool = ConnectionPool.get(dbName)
|
||||
|
||||
@ -4,7 +4,7 @@ import java.time.format.DateTimeFormatter
|
||||
import java.time.{ZoneId, ZonedDateTime}
|
||||
|
||||
import code.api.Constant
|
||||
import code.api.util.APIUtil
|
||||
import code.api.util.{APIUtil, DBUtil}
|
||||
import code.api.util.migration.Migration.{DbFunction, saveLog}
|
||||
import code.context.MappedUserAuthContext
|
||||
import code.views.system.AccountAccess
|
||||
@ -19,15 +19,6 @@ object MigrationOfUserAuthContext {
|
||||
val oneDayAgo = ZonedDateTime.now(ZoneId.of("UTC")).minusDays(1)
|
||||
val oneYearInFuture = ZonedDateTime.now(ZoneId.of("UTC")).plusYears(1)
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'")
|
||||
|
||||
private lazy val getDbConnectionParameters: (String, String, String) = {
|
||||
val dbUrl = APIUtil.getPropsValue("db.url") openOr Constant.h2DatabaseDefaultUrlValue
|
||||
val username = dbUrl.split(";").filter(_.contains("user")).toList.headOption.map(_.split("=")(1))
|
||||
val password = dbUrl.split(";").filter(_.contains("password")).toList.headOption.map(_.split("=")(1))
|
||||
val dbUser = APIUtil.getPropsValue("db.user").orElse(username)
|
||||
val dbPassword = APIUtil.getPropsValue("db.password").orElse(password)
|
||||
(dbUrl, dbUser.getOrElse(""), dbPassword.getOrElse(""))
|
||||
}
|
||||
|
||||
/**
|
||||
* this connection pool context corresponding db.url in default.props
|
||||
@ -40,7 +31,7 @@ object MigrationOfUserAuthContext {
|
||||
validationQuery = "select 1",
|
||||
connectionPoolFactoryName = "commons-dbcp2"
|
||||
)
|
||||
val (dbUrl, user, password) = getDbConnectionParameters
|
||||
val (dbUrl, user, password) = DBUtil.getDbConnectionParameters
|
||||
val dbName = "DB_NAME" // corresponding props db.url DB
|
||||
ConnectionPool.add(dbName, dbUrl, user, password, settings)
|
||||
val connectionPool = ConnectionPool.get(dbName)
|
||||
|
||||
@ -867,15 +867,6 @@ object LocalMappedConnector extends Connector with MdcLoggable {
|
||||
getCoreBankAccountsLegacy(bankIdAccountIds: List[BankIdAccountId], callContext: Option[CallContext])
|
||||
}
|
||||
}
|
||||
|
||||
private lazy val getDbConnectionParameters: (String, String, String) = {
|
||||
val dbUrl = APIUtil.getPropsValue("db.url") openOr Constant.h2DatabaseDefaultUrlValue
|
||||
val username = dbUrl.split(";").filter(_.contains("user")).toList.headOption.map(_.split("=")(1))
|
||||
val password = dbUrl.split(";").filter(_.contains("password")).toList.headOption.map(_.split("=")(1))
|
||||
val dbUser = APIUtil.getPropsValue("db.user").orElse(username)
|
||||
val dbPassword = APIUtil.getPropsValue("db.password").orElse(password)
|
||||
(dbUrl, dbUser.getOrElse(""), dbPassword.getOrElse(""))
|
||||
}
|
||||
|
||||
/**
|
||||
* this connection pool context corresponding db.url in default.props
|
||||
@ -888,7 +879,7 @@ object LocalMappedConnector extends Connector with MdcLoggable {
|
||||
validationQuery = "select 1",
|
||||
connectionPoolFactoryName = "commons-dbcp2"
|
||||
)
|
||||
val (dbUrl, user, password) = getDbConnectionParameters
|
||||
val (dbUrl, user, password) = DBUtil.getDbConnectionParameters
|
||||
val dbName = "DB_NAME" // corresponding props db.url DB
|
||||
ConnectionPool.add(dbName, dbUrl, user, password, settings)
|
||||
val connectionPool = ConnectionPool.get(dbName)
|
||||
|
||||
@ -90,15 +90,6 @@ object MappedMetrics extends APIMetrics with MdcLoggable{
|
||||
metric.save
|
||||
}
|
||||
|
||||
private lazy val getDbConnectionParameters: (String, String, String) = {
|
||||
val dbUrl = APIUtil.getPropsValue("db.url") openOr Constant.h2DatabaseDefaultUrlValue
|
||||
val username = dbUrl.split(";").filter(_.contains("user")).toList.headOption.map(_.split("=")(1))
|
||||
val password = dbUrl.split(";").filter(_.contains("password")).toList.headOption.map(_.split("=")(1))
|
||||
val dbUser = APIUtil.getPropsValue("db.user").orElse(username)
|
||||
val dbPassword = APIUtil.getPropsValue("db.password").orElse(password)
|
||||
(dbUrl, dbUser.getOrElse(""), dbPassword.getOrElse(""))
|
||||
}
|
||||
|
||||
private def trueOrFalse(condition: Boolean) = if (condition) sqls"1=1" else sqls"0=1"
|
||||
private def falseOrTrue(condition: Boolean) = if (condition) sqls"0=1" else sqls"1=1"
|
||||
|
||||
@ -252,7 +243,7 @@ object MappedMetrics extends APIMetrics with MdcLoggable{
|
||||
validationQuery = "select 1",
|
||||
connectionPoolFactoryName = "commons-dbcp2"
|
||||
)
|
||||
val (dbUrl, user, password) = getDbConnectionParameters
|
||||
val (dbUrl, user, password) = DBUtil.getDbConnectionParameters
|
||||
val dbName = "DB_NAME" // corresponding props db.url DB
|
||||
ConnectionPool.add(dbName, dbUrl, user, password, settings)
|
||||
val connectionPool = ConnectionPool.get(dbName)
|
||||
@ -399,7 +390,7 @@ object MappedMetrics extends APIMetrics with MdcLoggable{
|
||||
|
||||
val excludeUrlPatternsQueries = extendLikeQuery(excludeUrlPatternsList, false)
|
||||
|
||||
val (dbUrl, _, _) = getDbConnectionParameters
|
||||
val (dbUrl, _, _) = DBUtil.getDbConnectionParameters
|
||||
|
||||
val result: List[TopApi] = scalikeDB readOnly { implicit session =>
|
||||
// MS SQL server has the specific syntax for limiting number of rows
|
||||
@ -475,7 +466,7 @@ object MappedMetrics extends APIMetrics with MdcLoggable{
|
||||
|
||||
val excludeUrlPatternsQueries = extendLikeQuery(excludeUrlPatternsList, false)
|
||||
|
||||
val (dbUrl, _, _) = getDbConnectionParameters
|
||||
val (dbUrl, _, _) = DBUtil.getDbConnectionParameters
|
||||
|
||||
// MS SQL server has the specific syntax for limiting number of rows
|
||||
val msSqlLimit = if (dbUrl.contains("sqlserver")) sqls"TOP ($limit)" else sqls""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user