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