in src/main/java/com/datacompare/service/FetchMetadata.java [432:559]
private void fetchTableColumns(String sourceDBType, Connection connection, String schemaName,
String tableName, List<String> columnList, boolean ignoreColumns) {
ResultSet rs = null;
try {
rs = connection.getMetaData().getColumns(null, schemaName, tableName, null);
rs.setFetchSize(getFetchSize());
while (rs.next()) {
String col = null;
String columnType = rs.getString("TYPE_NAME");
String columnName = rs.getString("COLUMN_NAME");
String columnSize = rs.getString("COLUMN_SIZE");
int colSize = (columnSize != null && !columnSize.equals("null")) ? Integer.parseInt(columnSize) : 0;
if(column(columnList, columnName, ignoreColumns)) continue;
if (binaryColumnType(columnType) || varcharLargeSize(columnType, colSize)) {/*
*//** In case if source db is Oracle, it will check length for binary column types.
* If source db is SQLServer, it will check MD5 hash string for binary column types.*//*
if("ORACLE".equals(getDbType())) {
if(varcharLargeSize(columnType, colSize)) {
col = "LOWER(STANDARD_HASH(NVL(" + columnName
+ ",'NULL'), 'MD5')) AS " + columnName;
} else {
col = "DBMS_LOB.GETLENGTH(" + columnName + ") AS " + columnName;
}
} else if("POSTGRESQL".equals(getDbType()) && "ORACLE".equals(sourceDBType) && !varcharLargeSize(columnType, colSize)) {
col = "LENGTH(" + columnName + ") as " + columnName;
} else if("SQLSERVER".equals(getDbType())) {
col = "HashBytes('MD5', COALESCE(" + columnName + ",'NULL')) as " + columnName;
} else if("POSTGRESQL".equals(getDbType()) && ("SQLSERVER".equals(sourceDBType) || varcharLargeSize(columnType, colSize))) {
col = "MD5(COALESCE (" + columnName + ",'NULL')) as " + columnName;
} else {
}*/
col = columnName;
//col = "LENGTH(" + columnName + ") as " + columnName;
} else {
col = columnName;
}
boolean isNullable = rs.getString("IS_NULLABLE").toUpperCase().equals("YES");
String decimalDigits = rs.getString("DECIMAL_DIGITS");
int noOfDecimals = (decimalDigits != null && !decimalDigits.equals("null"))
? Integer.parseInt(decimalDigits)
: 0;
String decimalFormat = "";
if (columnType.compareTo("DECIMAL") == 0 || columnType.compareTo("NUMBER") == 0
|| columnType.compareTo("numeric") == 0 || columnType.contains("float")
|| columnType.contains("FLOAT") || columnType.compareTo("DOUBLE") == 0) {
for (int i = 0; i < colSize; i++) {
decimalFormat = decimalFormat + "#";
}
}
if (noOfDecimals > getMaxDecimals() && getMaxDecimals() > 0) {
decimalFormat = (decimalFormat.trim().length() > 0) ? decimalFormat + "." : "#.";
for (int j = 0; j < getMaxDecimals(); j++) {
decimalFormat = decimalFormat + "0";
}
} else if (noOfDecimals > 0) {
decimalFormat = (decimalFormat.trim().length() > 0) ? decimalFormat + "." : "#.";
for (int j = 0; j < noOfDecimals; j++) {
decimalFormat = decimalFormat + "0";
}
}
decimalFormat = decimalFormat.replace("#.", "0.");
TableColumnMetadata tableColumnMetadata = new TableColumnMetadata();
tableColumnMetadata.setColumnType(columnType);
tableColumnMetadata.setColumnName(columnName);
tableColumnMetadata.setNullable(isNullable);
tableColumnMetadata.setNoOfDecimals(noOfDecimals);
tableColumnMetadata.setColSize(colSize);
tableColumnMetadata.setDecimalFormat(decimalFormat);
tableColumnMetadata.setColumnAs(col);
tableColumnMetadata.setMaxTextSize(getMaxTextSize());
tableMetadataMap.put(columnName, tableColumnMetadata);
}
if (tableMetadataMap.isEmpty()) {
throw new Exception(getDbType() + " Database Table " + schemaName + "." + tableName + " has no columns.");
}
} catch (Exception e) {
logger.error(getDbType(), e);
} finally {
new JdbcUtil().closeResultSet(rs);
}
}