Search in sources :

Example 1 with SQLServerDbReadAccess

use of com.axway.ats.log.autodb.io.SQLServerDbReadAccess in project ats-framework by Axway.

the class ActiveDbAppender method obtainDbReadAccessObject.

/**
 * Get {@link IDbReadAccess} using the appender's db configuration
 * @throws DatabaseAccessException In case of DB error
 */
public IDbReadAccess obtainDbReadAccessObject() throws DatabaseAccessException {
    DbConnection dbConnection = null;
    if (dbReadAccess == null) {
        Exception mssqlException = DbUtils.isMSSQLDatabaseAvailable(appenderConfig.getHost(), Integer.parseInt(appenderConfig.getPort()), appenderConfig.getDatabase(), appenderConfig.getUser(), appenderConfig.getPassword());
        if (mssqlException == null) {
            Map<String, Object> props = new HashMap<>();
            props.put(DbKeys.DRIVER, appenderConfig.getDriver().toUpperCase());
            dbConnection = new DbConnSQLServer(appenderConfig.getHost(), Integer.parseInt(appenderConfig.getPort()), appenderConfig.getDatabase(), appenderConfig.getUser(), appenderConfig.getPassword(), props);
            // create the db access layer
            dbReadAccess = new SQLServerDbReadAccess((DbConnSQLServer) dbConnection);
        } else {
            Exception pgsqlException = DbUtils.isPostgreSQLDatabaseAvailable(appenderConfig.getHost(), Integer.parseInt(appenderConfig.getPort()), appenderConfig.getDatabase(), appenderConfig.getUser(), appenderConfig.getPassword());
            if (pgsqlException == null) {
                dbConnection = new DbConnPostgreSQL(appenderConfig.getHost(), Integer.parseInt(appenderConfig.getPort()), appenderConfig.getDatabase(), appenderConfig.getUser(), appenderConfig.getPassword(), null);
                // create the db access layer
                dbReadAccess = new PGDbReadAccess((DbConnPostgreSQL) dbConnection);
            } else {
                String errMsg = "Neither MSSQL, nor PostgreSQL server at '" + appenderConfig.getHost() + ":" + appenderConfig.getPort() + "' has database with name '" + appenderConfig.getDatabase() + "'. Exception for MSSQL is : \n\t" + mssqlException + "\n\nException for PostgreSQL is: \n\t" + pgsqlException;
                throw new DatabaseAccessException(errMsg);
            }
        }
    }
    return dbReadAccess;
}
Also used : DbConnSQLServer(com.axway.ats.core.dbaccess.mssql.DbConnSQLServer) HashMap(java.util.HashMap) SQLServerDbReadAccess(com.axway.ats.log.autodb.io.SQLServerDbReadAccess) PGDbReadAccess(com.axway.ats.log.autodb.io.PGDbReadAccess) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL) DbConnection(com.axway.ats.core.dbaccess.DbConnection) DbAppenederException(com.axway.ats.log.autodb.exceptions.DbAppenederException) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException)

Aggregations

DbConnection (com.axway.ats.core.dbaccess.DbConnection)1 DbConnSQLServer (com.axway.ats.core.dbaccess.mssql.DbConnSQLServer)1 DbConnPostgreSQL (com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL)1 DatabaseAccessException (com.axway.ats.log.autodb.exceptions.DatabaseAccessException)1 DbAppenederException (com.axway.ats.log.autodb.exceptions.DbAppenederException)1 PGDbReadAccess (com.axway.ats.log.autodb.io.PGDbReadAccess)1 SQLServerDbReadAccess (com.axway.ats.log.autodb.io.SQLServerDbReadAccess)1 HashMap (java.util.HashMap)1