Skip to content

Commit

Permalink
Including schema in table name for SQL Server and PDW. Fixes #108
Browse files Browse the repository at this point in the history
  • Loading branch information
schuemie committed Feb 21, 2019
1 parent d09e6d5 commit 7a9ecc9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
30 changes: 15 additions & 15 deletions src/org/ohdsi/databases/RichConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public List<String> getTableNames(String database) {
if (dbType == DbType.MYSQL) {
query = "SHOW TABLES IN " + database;
} else if (dbType == DbType.MSSQL || dbType == DbType.PDW) {
query = "SELECT name FROM " + database + ".sys.tables ORDER BY name";
query = "SELECT CONCAT(schemas.name, '.', tables.name) FROM " + database + ".sys.tables INNER JOIN " + database + ".sys.schemas ON tables.schema_id = schemas.schema_id ORDER BY schemas.name, tables.name";
} else if (dbType == DbType.ORACLE) {
query = "SELECT table_name FROM all_tables WHERE owner='" + database.toUpperCase() + "'";
} else if (dbType == DbType.POSTGRESQL || dbType == DbType.REDSHIFT) {
Expand All @@ -161,19 +161,19 @@ public List<String> getTableNames(String database) {
return names;
}

public List<String> getFieldNames(String table) {
List<String> names = new ArrayList<String>();
if (dbType == DbType.MSSQL || dbType == DbType.PDW) {
for (Row row : query("SELECT name FROM syscolumns WHERE id=OBJECT_ID('" + table + "')"))
names.add(row.get("name"));
} else if (dbType == DbType.MYSQL)
for (Row row : query("SHOW COLUMNS FROM " + table))
names.add(row.get("COLUMN_NAME"));
else
throw new RuntimeException("DB type not supported");

return names;
}
// public List<String> getFieldNames(String table) {
// List<String> names = new ArrayList<String>();
// if (dbType == DbType.MSSQL || dbType == DbType.PDW) {
// for (Row row : query("SELECT name FROM syscolumns WHERE id=OBJECT_ID('" + table + "')"))
// names.add(row.get("name"));
// } else if (dbType == DbType.MYSQL)
// for (Row row : query("SHOW COLUMNS FROM " + table))
// names.add(row.get("COLUMN_NAME"));
// else
// throw new RuntimeException("DB type not supported");
//
// return names;
// }

public ResultSet getMsAccessFieldNames(String table) {
if (dbType == DbType.MSACCESS) {
Expand All @@ -197,7 +197,7 @@ public long getTableSize(String tableName) {
QueryResult qr = null;
Long returnVal = null;
if (dbType == DbType.MSSQL || dbType == DbType.PDW)
qr = query("SELECT COUNT_BIG(*) FROM [" + tableName + "];");
qr = query("SELECT COUNT_BIG(*) FROM [" + tableName.replaceAll("\\.", "].[") + "];");
else if (dbType == DbType.MSACCESS)
qr = query("SELECT COUNT(*) FROM [" + tableName + "];");
else
Expand Down
13 changes: 8 additions & 5 deletions src/org/ohdsi/whiteRabbit/scan/SourceDataScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,19 @@ private QueryResult fetchRowsFromTable(RichConnection connection, String table,
String query = null;

if (sampleSize == -1) {
if (dbType == DbType.MSSQL || dbType == DbType.PDW || dbType == DbType.MSACCESS)
if (dbType == DbType.MSACCESS)
query = "SELECT * FROM [" + table + "]";
else if (dbType == DbType.MSSQL || dbType == DbType.PDW)
query = "SELECT * FROM [" + table.replaceAll("\\.", "].[") + "]";
else
query = "SELECT * FROM " + table;
} else {
if (dbType == DbType.MSSQL)
query = "SELECT * FROM [" + table + "] TABLESAMPLE (" + sampleSize + " ROWS)";
query = "SELECT * FROM [" + table.replaceAll("\\.", "].[") + "] TABLESAMPLE (" + sampleSize + " ROWS)";
else if (dbType == DbType.MYSQL)
query = "SELECT * FROM " + table + " ORDER BY RAND() LIMIT " + sampleSize;
else if (dbType == DbType.PDW)
query = "SELECT TOP " + sampleSize + " * FROM [" + table + "] ORDER BY RAND()";
query = "SELECT TOP " + sampleSize + " * FROM [" + table.replaceAll("\\.", "].[") + "] ORDER BY RAND()";
else if (dbType == DbType.ORACLE) {
if (sampleSize < rowCount) {
double percentage = 100 * sampleSize / (double) rowCount;
Expand Down Expand Up @@ -280,8 +282,9 @@ else if (dbType == DbType.MSSQL || dbType == DbType.PDW) {
String trimmedDatabase = database;
if (database.startsWith("[") && database.endsWith("]"))
trimmedDatabase = database.substring(1, database.length() - 1);
query = "SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG='" + trimmedDatabase + "' AND TABLE_NAME='" + table
+ "';";
String[] parts = table.split("\\.");
query = "SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG='" + trimmedDatabase + "' AND TABLE_SCHEMA='" + parts[0] +
"' AND TABLE_NAME='" + parts[1] + "';";
} else if (dbType == DbType.MYSQL)
query = "SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" + database + "' AND TABLE_NAME = '" + table
+ "';";
Expand Down

0 comments on commit 7a9ecc9

Please sign in to comment.