use of com.axway.ats.core.dbaccess.postgresql.PostgreSqlDbProvider in project ats-framework by Axway.
the class DatabaseProviderFactory method getDatabaseProvider.
/**
* Creates a database provider suitable for the given input arguments
*
* @param dbType
* @param dbHost
* @param dbName
* @param dbUser
* @param dbPass
* @param dbPort
* @param customProperties
* @return
*/
public static synchronized DbProvider getDatabaseProvider(String dbType, String dbHost, String dbName, String dbUser, String dbPass, int dbPort, Map<String, Object> customProperties) {
DbProvider dbProvider;
if (dbType == null) {
throw new IllegalArgumentException("Database type is not provided");
}
dbType = dbType.toUpperCase();
if (dbName == null) {
if ("MSSQL".equals(dbType)) {
log.warn("Database name is empty! The connection will be made to the admin database!");
} else {
throw new IllegalArgumentException("Database name is not provided");
}
}
// eventual custom property value
if (dbPort > 0) {
if (customProperties == null) {
customProperties = new HashMap<String, Object>();
}
customProperties.put(DbKeys.PORT_KEY, dbPort);
}
// load a custom data provider
if (dbProviders.containsKey(dbType)) {
String[] classNames = dbProviders.get(dbType);
DbConnection dbConnection = loadDbConnection(classNames[0], dbType, dbHost, dbName, dbUser, dbPass, dbPort, customProperties);
return loadCustomDbProvider(classNames[1], dbConnection);
}
// load a common ATS supported data provider
switch(dbType) {
case DbConnSQLServer.DATABASE_TYPE:
dbProvider = new MssqlDbProvider((DbConnSQLServer) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
break;
case DbConnPostgreSQL.DATABASE_TYPE:
dbProvider = new PostgreSqlDbProvider((DbConnPostgreSQL) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
break;
case DbConnMySQL.DATABASE_TYPE:
dbProvider = new MysqlDbProvider((DbConnMySQL) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
break;
case DbConnMariaDB.DATABASE_TYPE:
dbProvider = new MariaDbDbProvider((DbConnMariaDB) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
break;
case DbConnOracle.DATABASE_TYPE:
dbProvider = new OracleDbProvider((DbConnOracle) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
break;
case DbConnCassandra.DATABASE_TYPE:
dbProvider = new CassandraDbProvider((DbConnCassandra) createDbConnection(dbType, dbHost, dbPort, dbName, dbUser, dbPass, customProperties));
break;
default:
{
// should never happen
throw new IllegalArgumentException("Database type " + dbType + " not supported");
}
}
return dbProvider;
}
use of com.axway.ats.core.dbaccess.postgresql.PostgreSqlDbProvider in project ats-framework by Axway.
the class DatabaseSnapshot method constructSelectStatement.
private String constructSelectStatement(TableDescription table, Map<String, SkipColumns> skipColumns) {
Set<String> columns = table.getColumnNames();
// Search for skip columns for this table. The search must ignore the letters case
SkipColumns skipColumnsForThisTable = null;
for (String tableName : skipColumns.keySet()) {
if (tableName.equalsIgnoreCase(table.getName())) {
skipColumnsForThisTable = skipColumns.get(tableName);
break;
}
}
if (skipColumnsForThisTable == null) {
String query = "SELECT * FROM ";
if (this.dbProvider instanceof PostgreSqlDbProvider) {
List<String> sortedColumns = new ArrayList<>(columns);
Collections.sort(sortedColumns);
query = "SELECT " + String.join(", ", sortedColumns) + " FROM ";
}
// all columns are important
if (table.getSchema() != null) {
query += table.getSchema() + ".";
}
return query + table.getName();
} else {
// some columns must be skipped
StringBuilder sql = new StringBuilder("SELECT");
// add all important columns
for (String column : columns) {
if (!skipColumnsForThisTable.isSkipColumn(column)) {
sql.append(" " + column + ",");
}
}
if ("SELECT".equals(sql.toString())) {
// so no column are left for selection, so we won't select anything
return null;
} else {
// remove last comma
sql.setLength(sql.length() - 1);
sql.append(" FROM ");
if (table.getSchema() != null) {
sql.append(table.getSchema()).append(".");
}
sql.append(table.getName());
return sql.toString();
}
}
}
Aggregations