Search in sources :

Example 6 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class DatabaseBackupRestoreTests method init.

@BeforeMethod
public void init() {
    serverBox = new TestBox();
    serverBox.setHost(configuration.getDatabaseHost());
    serverBox.setDbType(configuration.getDatabaseType());
    serverBox.setDbName(configuration.getBackupDatabaseName());
    serverBox.setDbUser(configuration.getDatabaseUser());
    serverBox.setDbPass(configuration.getDatabasePassword());
    serverBox.setDbPort(String.valueOf(configuration.getDbPort()));
    // establish DB connection
    dbOperations = new DatabaseOperations(serverBox);
    dbConn = new DbConnPostgreSQL(configuration.getDatabaseHost(), port, configuration.getBackupDatabaseName(), configuration.getDatabaseUser(), configuration.getDatabasePassword(), null);
    // establish tables which to be inspected
    dbTables = new ArrayList<>();
    dbTables.add(new DbTable(TABLE));
}
Also used : TestBox(com.axway.ats.harness.config.TestBox) DatabaseOperations(com.axway.ats.action.dbaccess.DatabaseOperations) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL) DbTable(com.axway.ats.environment.database.model.DbTable)

Example 7 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class DbEventRequestProcessor method getLatestRun.

private Run getLatestRun() throws SQLException {
    PreparedStatement stmt = null;
    Run run = new Run();
    Connection tmpConn = null;
    try {
        tmpConn = ConnectionPool.getConnection(dbConnection);
        stmt = tmpConn.prepareStatement("SELECT * FROM tRuns WHERE runId=" + eventProcessorState.getRunId());
        if (dbConnection instanceof DbConnPostgreSQL) {
            stmt = tmpConn.prepareStatement("SELECT * FROM \"tRuns\" WHERE runId=" + eventProcessorState.getRunId());
        }
        ResultSet rs = stmt.executeQuery();
        rs.next();
        run.runId = rs.getString("runId");
        run.productName = rs.getString("productName");
        run.versionName = rs.getString("versionName");
        run.buildName = rs.getString("buildName");
        run.runName = rs.getString("runName");
        run.os = rs.getString("OS");
        run.hostName = rs.getString("hostName");
        if (run.hostName == null) {
            run.hostName = "";
        }
        run.userNote = rs.getString("userNote");
        if (run.userNote == null) {
            run.userNote = "";
        }
    } finally {
        DbUtils.closeConnection(tmpConn);
        DbUtils.closeStatement(stmt);
    }
    return run;
}
Also used : Connection(java.sql.Connection) DbConnection(com.axway.ats.core.dbaccess.DbConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Run(com.axway.ats.log.autodb.entities.Run) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL)

Example 8 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class DbAccessFactory method getNewDbWriteAccessObject.

/**
 * Retrieves the DB info from the log4j2 system and then creates a new
 * instance for writing into the DB
 *
 * @return
 * @throws DatabaseAccessException
 */
public SQLServerDbWriteAccess getNewDbWriteAccessObject() throws DatabaseAccessException {
    // Our DB appender keeps the DB connection info
    ActiveDbAppender loggingAppender = ActiveDbAppender.getCurrentInstance();
    if (loggingAppender == null) {
        throw new DatabaseAccessException("Unable to initialize connection to the logging database as the ATS ActiveDbAppender is not attached to log4j2 system");
    }
    DbConnection dbConnection = null;
    Exception mssqlException = DbUtils.isMSSQLDatabaseAvailable(loggingAppender.getHost(), Integer.parseInt(loggingAppender.getPort()), loggingAppender.getDatabase(), loggingAppender.getUser(), loggingAppender.getPassword());
    SQLServerDbWriteAccess writeAccess = null;
    if (mssqlException == null) {
        // Create the database access layer
        if (DbKeys.SQL_SERVER_DRIVER_MICROSOFT.equalsIgnoreCase(loggingAppender.getAppenderConfig().getDriver())) {
            // Create DB connection based on the log4j2 system settings
            Map<String, Object> props = new HashMap<>();
            props.put(DbKeys.DRIVER, DbKeys.SQL_SERVER_DRIVER_MICROSOFT);
            dbConnection = new DbConnSQLServer(loggingAppender.getHost(), Integer.parseInt(loggingAppender.getPort()), loggingAppender.getDatabase(), loggingAppender.getUser(), loggingAppender.getPassword(), props);
            writeAccess = new SQLServerDbWriteAccessMSSQL(dbConnection, false);
            writeAccess.setMaxNumberOfCachedEvents(Integer.parseInt(loggingAppender.getAppenderConfig().getChunkSize()));
            return writeAccess;
        } else if (DbKeys.SQL_SERVER_DRIVER_JTDS.equalsIgnoreCase(loggingAppender.getAppenderConfig().getDriver())) {
            // Create DB connection based on the log4j2 system settings
            Map<String, Object> props = new HashMap<>();
            props.put(DbKeys.DRIVER, DbKeys.SQL_SERVER_DRIVER_JTDS);
            dbConnection = new DbConnSQLServer(loggingAppender.getHost(), Integer.parseInt(loggingAppender.getPort()), loggingAppender.getDatabase(), loggingAppender.getUser(), loggingAppender.getPassword(), props);
            writeAccess = new SQLServerDbWriteAccess(dbConnection, false);
            writeAccess.setMaxNumberOfCachedEvents(Integer.parseInt(loggingAppender.getAppenderConfig().getChunkSize()));
            return writeAccess;
        } else {
            throw new IllegalArgumentException("Appender configuration specified SQL Server driver to be '" + loggingAppender.getAppenderConfig().getDriver() + "' which is not supported");
        }
    } else {
        Exception pgsqlException = DbUtils.isPostgreSQLDatabaseAvailable(loggingAppender.getHost(), Integer.parseInt(loggingAppender.getPort()), loggingAppender.getDatabase(), loggingAppender.getUser(), loggingAppender.getPassword());
        if (pgsqlException == null) {
            // Create DB connection based on the log4j2 system settings
            dbConnection = new DbConnPostgreSQL(loggingAppender.getHost(), Integer.parseInt(loggingAppender.getPort()), loggingAppender.getDatabase(), loggingAppender.getUser(), loggingAppender.getPassword(), null);
            // Create the database access layer
            writeAccess = new PGDbWriteAccess(dbConnection, false);
            writeAccess.setMaxNumberOfCachedEvents(Integer.parseInt(loggingAppender.getAppenderConfig().getChunkSize()));
            return writeAccess;
        } else {
            String errMsg = "Neither MSSQL, nor PostgreSQL server at '" + loggingAppender.getHost() + ":" + loggingAppender.getPort() + "' has database with name '" + loggingAppender.getDatabase() + "'. Exception for MSSQL is : \n\t" + mssqlException + "\n\nException for PostgreSQL is: \n\t" + pgsqlException;
            throw new DatabaseAccessException(errMsg);
        }
    }
}
Also used : DbConnSQLServer(com.axway.ats.core.dbaccess.mssql.DbConnSQLServer) HashMap(java.util.HashMap) DbConnection(com.axway.ats.core.dbaccess.DbConnection) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException) ActiveDbAppender(com.axway.ats.log.appenders.ActiveDbAppender) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL) Map(java.util.Map) HashMap(java.util.HashMap) DatabaseAccessException(com.axway.ats.log.autodb.exceptions.DatabaseAccessException)

Example 9 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class AbstractDbProvider method extractTableColumns.

private void extractTableColumns(String tableName, DatabaseMetaData databaseMetaData, String schemaPattern, List<String> columnDescription) throws SQLException {
    // Get info about all columns in the specified table.
    // Specifying the user name is needed for Oracle, otherwise returns info
    // about the specified table in all DBs.
    // We can use an method overriding instead of checking this instance
    // type.
    // Map to hold the table's columns in sorted (natural-order) manner. Special case for PostgreSQL.
    Map<String, String> columns = null;
    ResultSet columnInformation = databaseMetaData.getColumns(null, schemaPattern, tableName, "%");
    while (columnInformation.next()) {
        StringBuilder sb = new StringBuilder();
        String columnName = columnInformation.getString("COLUMN_NAME");
        sb.append("name=" + columnName);
        String type = SQL_COLUMN_TYPES.get(columnInformation.getInt("DATA_TYPE"));
        sb.append(", type=" + type);
        sb.append(extractTableAttributeValue(columnInformation, "IS_AUTOINCREMENT", "auto increment", tableName, columnName));
        if ("BIT".equalsIgnoreCase(type)) {
            sb.append(extractBooleanResultSetAttribute(columnInformation, "COLUMN_DEF", "default"));
        } else {
            sb.append(extractTableAttributeValue(columnInformation, "COLUMN_DEF", "default", tableName, columnName));
        }
        sb.append(extractTableAttributeValue(columnInformation, "IS_NULLABLE", "nullable", tableName, columnName));
        sb.append(extractTableAttributeValue(columnInformation, "COLUMN_SIZE", "size", tableName, columnName));
        // columnInformation.getShort( "SOURCE_DATA_TYPE" ) ) );
        if (this.dbConnection instanceof DbConnPostgreSQL) {
            if (columns == null) {
                columns = new HashMap<String, String>();
            }
            columns.put(columnName, sb.toString());
        } else {
            columnDescription.add(sb.toString());
        }
    }
    if (columns != null) {
        List<String> sortedKeySet = new ArrayList<>(columns.keySet());
        Collections.sort(sortedKeySet);
        for (String key : sortedKeySet) {
            columnDescription.add(columns.get(key));
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL)

Example 10 with DbConnPostgreSQL

use of com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL in project ats-framework by Axway.

the class DbUtils method isPostgreSQLDatabaseAvailable.

/**
 * Check if ATS log PostgreSQL database is available for connection
 * @param dbHost the database host
 * @param dbPort the database port
 * @param dbName the database name
 * @param dbUser the database user name used for login
 * @param dbPassword the database password used for login
 * @return null if PostgreSQL database is available, and an Exception if PostgreSQL database is NOT available
 */
public static Exception isPostgreSQLDatabaseAvailable(String dbHost, int dbPort, String dbName, String dbUser, String dbPassword) {
    Connection sqlConnection = null;
    DbConnPostgreSQL postgreConnection = null;
    PreparedStatement ps = null;
    try {
        postgreConnection = new DbConnPostgreSQL(dbHost, dbPort, dbName, dbUser, dbPassword, null);
        sqlConnection = postgreConnection.getDataSource().getConnection();
        ps = sqlConnection.prepareStatement("SELECT value FROM \"tInternal\" WHERE key = 'version'");
        ResultSet rs = ps.executeQuery();
        // we expect only one record
        if (rs.next()) {
            // execute it just to be sure that the database we found is ATS Log database as much as possible
            rs.getString(1);
        } else {
            throw new Exception("Could not fetch the database version from PostgreSQL database using URL '" + postgreConnection.getURL() + "'");
        }
        return null;
    } catch (Exception e) {
        return e;
    } finally {
        closeStatement(ps);
        closeConnection(sqlConnection);
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnPostgreSQL(com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL) SQLException(java.sql.SQLException)

Aggregations

DbConnPostgreSQL (com.axway.ats.core.dbaccess.postgresql.DbConnPostgreSQL)10 DbConnection (com.axway.ats.core.dbaccess.DbConnection)4 DbConnSQLServer (com.axway.ats.core.dbaccess.mssql.DbConnSQLServer)4 HashMap (java.util.HashMap)4 DatabaseAccessException (com.axway.ats.log.autodb.exceptions.DatabaseAccessException)3 ResultSet (java.sql.ResultSet)3 BaseTest (com.axway.ats.core.BaseTest)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Map (java.util.Map)2 Test (org.junit.Test)2 DatabaseOperations (com.axway.ats.action.dbaccess.DatabaseOperations)1 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)1 DbConnCassandra (com.axway.ats.core.dbaccess.cassandra.DbConnCassandra)1 DbConnMariaDB (com.axway.ats.core.dbaccess.mariadb.DbConnMariaDB)1 MariaDbDbProvider (com.axway.ats.core.dbaccess.mariadb.MariaDbDbProvider)1 MssqlDbProvider (com.axway.ats.core.dbaccess.mssql.MssqlDbProvider)1 DbConnMySQL (com.axway.ats.core.dbaccess.mysql.DbConnMySQL)1 MysqlDbProvider (com.axway.ats.core.dbaccess.mysql.MysqlDbProvider)1 DbConnOracle (com.axway.ats.core.dbaccess.oracle.DbConnOracle)1