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));
}
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;
}
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);
}
}
}
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));
}
}
}
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);
}
}
Aggregations