Skip to content

Commit

Permalink
Issue #32. Remove empty tables from analysis. This feature is managed…
Browse files Browse the repository at this point in the history
… by "skip-empty-tables" attribute in db.properties property file.
  • Loading branch information
armenak committed Aug 25, 2015
1 parent 081928c commit dbf4dd1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@
public class DataDiscoverer extends Discoverer {

private static Logger log = getLogger(DataDiscoverer.class);
// TODO: Uncomment back in if/when we need it
// private static List firstAndLastNames = new ArrayList();


@Override
public List<MatchMetaData> discover(IDBFactory factory, Properties dataDiscoveryProperties, Set<String> tables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@
*/
package com.strider.dataanonymizer.database.metadata;

import static org.apache.log4j.Logger.getLogger;

import com.strider.dataanonymizer.utils.SQLToJavaMapping;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;
import static org.apache.log4j.Logger.getLogger;
import static org.apache.log4j.Logger.getLogger;
import static org.apache.log4j.Logger.getLogger;
import static org.apache.log4j.Logger.getLogger;

import com.strider.dataanonymizer.utils.SQLToJavaMapping;

/**
* Class to hold common logic between different metadata implementations.
Expand Down Expand Up @@ -62,12 +66,15 @@ public List<MatchMetaData> getMetaData(String columnType) {
protected ResultSet getTableRS(DatabaseMetaData md) throws SQLException {
return md.getTables(null, schema, null, new String[] {"TABLE"});
}

protected ResultSet getPKRS(DatabaseMetaData md, String tableName) throws SQLException {
return md.getPrimaryKeys(null, schema, tableName);
}

protected ResultSet getColumnRS(DatabaseMetaData md, String tableName) throws SQLException {
return md.getColumns(null, schema, tableName, null);
}

protected String getColumnName(ResultSet columnRS) throws SQLException {
return columnRS.getString(4);
}
Expand All @@ -82,11 +89,20 @@ public List<MatchMetaData> getMetaData() {
DatabaseMetaData md = connection.getMetaData();

String schemaName = databaseProperties.getProperty("schema");
String skipEmptyTables = databaseProperties.getProperty("skip-empty-tables");

log.info("Fetching table names from schema " + schemaName);
try (ResultSet tableRS = getTableRS(md)) {
while (tableRS.next()) {
String tableName = tableRS.getString(3);

// Skip table if it is empty
if ( (skipEmptyTables != null && skipEmptyTables.equals("true")) && (getRowNumber(tableName) == 0) ) {
log.info("Skipping empty table " + tableName);
continue;
}
log.info("Processing table " + tableName);

List<String> pkeys = new ArrayList<>();
try (ResultSet pkRS = getPKRS(md, tableName)) {
while (pkRS.next()) {
Expand Down Expand Up @@ -120,4 +136,19 @@ String getColumnType(ResultSet columnRS) throws SQLException {
}
return colType;
}

private int getRowNumber(String table) {
int rowNum = 0;

try (Statement stmt = connection.createStatement();) {
try (ResultSet rs = stmt.executeQuery("SELECT count(*) FROM " + table);) {
rs.next();
rowNum = rs.getInt(1);
}
} catch (SQLException sqle) {
log.error(sqle.toString());
}

return rowNum;
}
}

0 comments on commit dbf4dd1

Please sign in to comment.