Skip to content

Commit

Permalink
Handling case when Oracle table smaller than sample size. Also catchi…
Browse files Browse the repository at this point in the history
…ng exceptions when getting values from fields.
  • Loading branch information
schuemie committed Nov 2, 2017
1 parent c968284 commit 2da78b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
36 changes: 21 additions & 15 deletions src/org/ohdsi/databases/RichConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ public void execute(String sql) {
System.err.println(sql);
e.printStackTrace();
} finally {
if (statement != null) {
if (statement != null) {
try {
statement.close();
}
catch (SQLException e) {
} catch (SQLException e) {
// TODO Auto-generated catch block
System.err.println(e.getMessage());
}
Expand Down Expand Up @@ -132,11 +131,11 @@ public void use(String database) {
execute("ALTER SESSION SET current_schema = " + database);
else if (dbType == DbType.POSTGRESQL || dbType == DbType.REDSHIFT)
execute("SET search_path TO " + database);
else if (dbType == DbType.MSACCESS); // NOOP
else if (dbType == DbType.MSACCESS)
; // NOOP
else if (dbType == DbType.TERADATA) {
execute("database " + database);
}
else
} else
execute("USE " + database);
}

Expand Down Expand Up @@ -175,16 +174,16 @@ public List<String> getFieldNames(String table) {

return names;
}
public ResultSet getMsAccessFieldNames(String table){
if(dbType == DbType.MSACCESS){

public ResultSet getMsAccessFieldNames(String table) {
if (dbType == DbType.MSACCESS) {
try {
DatabaseMetaData metadata = connection.getMetaData();
return metadata.getColumns(null, null, table, null);
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
}else
} else
throw new RuntimeException("DB is not of type MS Access");
}

Expand All @@ -197,9 +196,9 @@ public ResultSet getMsAccessFieldNames(String table){
public long getTableSize(String tableName) {
QueryResult qr = null;
Long returnVal = null;
if (dbType == DbType.MSSQL|| dbType == DbType.PDW)
if (dbType == DbType.MSSQL || dbType == DbType.PDW)
qr = query("SELECT COUNT_BIG(*) FROM [" + tableName + "];");
else if (dbType == DbType.MSACCESS )
else if (dbType == DbType.MSACCESS)
qr = query("SELECT COUNT(*) FROM [" + tableName + "];");
else
qr = query("SELECT COUNT(*) FROM " + tableName + ";");
Expand All @@ -208,9 +207,11 @@ else if (dbType == DbType.MSACCESS )
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (qr != null) { qr.close(); }
if (qr != null) {
qr.close();
}
}
return returnVal;
return returnVal;
}

/**
Expand Down Expand Up @@ -493,7 +494,12 @@ public Row next() {
for (int i = 1; i < metaData.getColumnCount() + 1; i++) {
String columnName = metaData.getColumnName(i);
if (columnNames.add(columnName)) {
String value = resultSet.getString(i);
String value;
try {
value = resultSet.getString(i);
} catch (Exception e) {
value = "";
}
if (value == null)
value = "";

Expand Down
2 changes: 1 addition & 1 deletion src/org/ohdsi/utilities/files/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public String toString() {
}

public void remove(String field) {
Integer index = fieldName2ColumnIndex.remove(field);
int index = fieldName2ColumnIndex.remove(field);
cells.remove(index);
Map<String, Integer> tempMap = new HashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : fieldName2ColumnIndex.entrySet())
Expand Down
2 changes: 2 additions & 0 deletions src/org/ohdsi/whiteRabbit/scan/SourceDataScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ else if (dbType == DbType.ORACLE) {
double percentage = 100 * sampleSize / (double) rowCount;
if (percentage < 100)
query = "SELECT * FROM " + table + " SAMPLE(" + percentage + ")";
} else {
query = "SELECT * FROM " + table;
}
} else if (dbType == DbType.POSTGRESQL || dbType == DbType.REDSHIFT)
query = "SELECT * FROM " + table + " ORDER BY RANDOM() LIMIT " + sampleSize;
Expand Down

0 comments on commit 2da78b5

Please sign in to comment.