Search in sources :

Example 1 with OracleDbProvider

use of com.axway.ats.core.dbaccess.oracle.OracleDbProvider in project ats-framework by Axway.

the class AbstractDbProvider method getTableDescriptions.

/**
     * @return description about all tables present in the database
     */
@Override
public List<TableDescription> getTableDescriptions() {
    ResultSet tablesResultSet = null;
    List<TableDescription> tables = new ArrayList<TableDescription>();
    try (Connection connection = ConnectionPool.getConnection(dbConnection)) {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        tablesResultSet = databaseMetaData.getTables(null, null, null, new String[] { "TABLE" });
        while (tablesResultSet.next()) {
            if (this instanceof OracleDbProvider && !tablesResultSet.getString(2).equalsIgnoreCase(dbConnection.user)) {
                // Oracle gives us all tables from all databases, we filter here only ours
                continue;
            }
            String tableName = tablesResultSet.getString("TABLE_NAME");
            if (!isTableAccepted(tablesResultSet, dbConnection.db, tableName)) {
                // Table is skipped
                continue;
            }
            log.debug("Extracting description about '" + tableName + "' table");
            TableDescription table = new TableDescription();
            table.setName(tableName);
            table.setSchema(tablesResultSet.getString("TABLE_SCHEM"));
            table.setPrimaryKeyColumn(exctractPrimaryKeyColumn(tableName, databaseMetaData));
            table.setIndexes(extractTableIndexes(tableName, databaseMetaData, connection.getCatalog()));
            List<String> columnDescriptions = new ArrayList<>();
            extractTableColumns(tableName, databaseMetaData, columnDescriptions);
            table.setColumnDescriptions(columnDescriptions);
            tables.add(table);
        }
    } catch (SQLException sqle) {
        throw new DbException("Error extracting DB schema information", sqle);
    } finally {
        if (tablesResultSet != null) {
            try {
                tablesResultSet.close();
            } catch (SQLException e) {
                log.warn("Result set resouce could not be closed!", e);
            }
        }
    }
    return tables;
}
Also used : OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 2 with OracleDbProvider

use of com.axway.ats.core.dbaccess.oracle.OracleDbProvider in project ats-framework by Axway.

the class AbstractDbProvider method extractTableColumns.

private void extractTableColumns(String tableName, DatabaseMetaData databaseMetaData, List<String> columnDescription) throws SQLException {
    ResultSet columnInformation = null;
    try {
        // 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.
        columnInformation = databaseMetaData.getColumns(null, (this instanceof OracleDbProvider ? dbConnection.getUser() : null), 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(extractResultSetAttribute(columnInformation, "IS_AUTOINCREMENT", "auto increment", tableName));
            if ("BIT".equalsIgnoreCase(type)) {
                sb.append(extractBooleanResultSetAttribute(columnInformation, "COLUMN_DEF", "default"));
            } else {
                sb.append(extractResultSetAttribute(columnInformation, "COLUMN_DEF", "default", tableName));
            }
            sb.append(extractResultSetAttribute(columnInformation, "IS_NULLABLE", "nullable", tableName));
            sb.append(extractResultSetAttribute(columnInformation, "COLUMN_SIZE", "size", tableName));
            //            sb.append( extractResultSetAttribute( columnInformation, "DECIMAL_DIGITS", "decimal digits" ) );
            //            sb.append( extractResultSetAttribute( columnInformation, "NUM_PREC_RADIX", "radix" ) );
            //            sb.append( extractResultSetAttribute( columnInformation, "ORDINAL_POSITION", "sequence number" ) );
            //            sb.append( ", source data type=" + sqlTypeToString( columnInformation.getShort( "SOURCE_DATA_TYPE" ) ) );
            columnDescription.add(sb.toString());
        }
    } finally {
        if (columnInformation != null) {
            columnInformation.close();
        }
    }
}
Also used : OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) ResultSet(java.sql.ResultSet)

Example 3 with OracleDbProvider

use of com.axway.ats.core.dbaccess.oracle.OracleDbProvider 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");
    }
    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, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnMySQL.DATABASE_TYPE:
            dbProvider = new MysqlDbProvider((DbConnMySQL) createDbConnection(dbType, dbHost, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnOracle.DATABASE_TYPE:
            dbProvider = new OracleDbProvider((DbConnOracle) createDbConnection(dbType, dbHost, dbName, dbUser, dbPass, customProperties));
            break;
        case DbConnCassandra.DATABASE_TYPE:
            dbProvider = new CassandraDbProvider((DbConnCassandra) createDbConnection(dbType, dbHost, dbName, dbUser, dbPass, customProperties));
            break;
        default:
            {
                // should never happen
                throw new IllegalArgumentException("Database type " + dbType + " not supported");
            }
    }
    return dbProvider;
}
Also used : OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) MssqlDbProvider(com.axway.ats.core.dbaccess.mssql.MssqlDbProvider) DbConnSQLServer(com.axway.ats.core.dbaccess.mssql.DbConnSQLServer) MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) DbConnMySQL(com.axway.ats.core.dbaccess.mysql.DbConnMySQL) DbConnOracle(com.axway.ats.core.dbaccess.oracle.DbConnOracle) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider) OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) MssqlDbProvider(com.axway.ats.core.dbaccess.mssql.MssqlDbProvider) MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) DbConnCassandra(com.axway.ats.core.dbaccess.cassandra.DbConnCassandra) CassandraDbProvider(com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)

Aggregations

OracleDbProvider (com.axway.ats.core.dbaccess.oracle.OracleDbProvider)3 ResultSet (java.sql.ResultSet)2 TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)1 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)1 DbConnCassandra (com.axway.ats.core.dbaccess.cassandra.DbConnCassandra)1 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 DbConnSQLServer (com.axway.ats.core.dbaccess.mssql.DbConnSQLServer)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 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1