Search in sources :

Example 11 with TableDescription

use of com.axway.ats.common.dbaccess.snapshot.TableDescription in project ats-framework by Axway.

the class AbstractDbProvider method getTableDescriptions.

/**
 * @param tablesToSkip list of some tables we are not interested in
 * @return description about all important tables
 */
@Override
public List<TableDescription> getTableDescriptions(List<String> tablesToSkip) {
    Connection connection = ConnectionPool.getConnection(dbConnection);
    List<TableDescription> tables = new ArrayList<TableDescription>();
    if (tablesToSkip == null) {
        tablesToSkip = new ArrayList<>();
    }
    try {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        // ORACLE -> The USER NAME is the TABLE SCHEMA
        String schemaPattern = (this instanceof OracleDbProvider ? dbConnection.getUser() : null);
        // MySQL/MariaDB -> The DB NAME is the TABLE SCHEMA
        schemaPattern = (this instanceof MysqlDbProvider || this instanceof MariaDbDbProvider ? dbConnection.getDb() : schemaPattern);
        ResultSet tablesResultSet = databaseMetaData.getTables(null, schemaPattern, null, new String[] { "TABLE" });
        while (tablesResultSet.next()) {
            String tableName = tablesResultSet.getString(3);
            // check for tables the DB driver rejects to return
            if (!isTableAccepted(tablesResultSet, dbConnection.db, tableName)) {
                continue;
            }
            // check for tables the user is not interested in
            boolean skipThisTable = false;
            for (String tableToSkip : tablesToSkip) {
                if (tableName.equalsIgnoreCase(tableToSkip)) {
                    skipThisTable = true;
                    break;
                }
            }
            if (skipThisTable) {
                continue;
            }
            log.debug("Extracting description about '" + tableName + "' table");
            TableDescription table = new TableDescription();
            table.setName(tableName);
            table.setSchema(tablesResultSet.getString("TABLE_SCHEM"));
            table.setPrimaryKeyColumn(extractPrimaryKeyColumn(tableName, databaseMetaData, schemaPattern));
            table.setIndexes(extractTableIndexes(tableName, databaseMetaData, connection.getCatalog()));
            List<String> columnDescriptions = new ArrayList<>();
            extractTableColumns(tableName, databaseMetaData, schemaPattern, columnDescriptions);
            table.setColumnDescriptions(columnDescriptions);
            tables.add(table);
        }
    } catch (SQLException sqle) {
        throw new DbException("Error extracting DB schema information", sqle);
    }
    return tables;
}
Also used : OracleDbProvider(com.axway.ats.core.dbaccess.oracle.OracleDbProvider) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DatabaseMetaData(java.sql.DatabaseMetaData) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) MysqlDbProvider(com.axway.ats.core.dbaccess.mysql.MysqlDbProvider) ResultSet(java.sql.ResultSet) MariaDbDbProvider(com.axway.ats.core.dbaccess.mariadb.MariaDbDbProvider)

Aggregations

TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)11 ArrayList (java.util.ArrayList)5 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)4 SkipColumns (com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)3 SkipContent (com.axway.ats.action.dbaccess.snapshot.rules.SkipContent)3 PublicAtsApi (com.axway.ats.common.PublicAtsApi)3 SkipIndexAttributes (com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes)2 SkipRows (com.axway.ats.action.dbaccess.snapshot.rules.SkipRows)2 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)2 OracleDbProvider (com.axway.ats.core.dbaccess.oracle.OracleDbProvider)2 Connection (java.sql.Connection)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 IndexMatcher (com.axway.ats.common.dbaccess.snapshot.IndexMatcher)1 IndexNameMatcher (com.axway.ats.common.dbaccess.snapshot.IndexNameMatcher)1 DatabaseEqualityState (com.axway.ats.common.dbaccess.snapshot.equality.DatabaseEqualityState)1 EqualityState (com.axway.ats.common.dbaccess.snapshot.equality.EqualityState)1 MariaDbDbProvider (com.axway.ats.core.dbaccess.mariadb.MariaDbDbProvider)1