Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -63,8 +64,9 @@ public List<PhysicalColumnInfo> getTableSchema(Datasource datasource, String tab
javax.sql.DataSource ds = dynamicDataSourceManager.getOrCreateDataSource(datasource);

try (Connection conn = ds.getConnection()) {
Set<String> primaryKeys = getPrimaryKeys(conn, tableName);
return getColumns(conn, tableName, primaryKeys);
String metadataTableName = resolveMetadataTableName(conn, tableName);
Set<String> primaryKeys = getPrimaryKeys(conn, metadataTableName);
return getColumns(conn, metadataTableName, primaryKeys);
} catch (SQLException e) {
log.error("Failed to read schema for table {}: {}", tableName, e.getMessage(), e);
throw BusinessException.of(
Expand All @@ -80,8 +82,10 @@ public Map<String, Set<String>> getTableColumnNames(
return Map.of();
}
Map<String, Set<String>> columnNamesByTable = new LinkedHashMap<>();
Map<String, String> tableNameLookup = new LinkedHashMap<>();
for (String tableName : tableNames) {
columnNamesByTable.putIfAbsent(tableName, new LinkedHashSet<>());
tableNameLookup.putIfAbsent(caseInsensitiveKey(tableName), tableName);
}

javax.sql.DataSource ds = dynamicDataSourceManager.getOrCreateDataSource(datasource);
Expand All @@ -91,7 +95,12 @@ public Map<String, Set<String>> getTableColumnNames(
try (ResultSet rs =
metaData.getColumns(conn.getCatalog(), conn.getSchema(), "%", null)) {
while (rs.next()) {
Set<String> columnNames = columnNamesByTable.get(rs.getString("TABLE_NAME"));
String metadataTableName = rs.getString("TABLE_NAME");
String tableName =
columnNamesByTable.containsKey(metadataTableName)
? metadataTableName
: tableNameLookup.get(caseInsensitiveKey(metadataTableName));
Set<String> columnNames = columnNamesByTable.get(tableName);
if (columnNames != null) {
columnNames.add(rs.getString("COLUMN_NAME"));
}
Expand Down Expand Up @@ -123,6 +132,32 @@ private List<PhysicalTableInfo> getTables(Connection conn) throws SQLException {
return tables;
}

private String resolveMetadataTableName(Connection conn, String tableName) throws SQLException {
DatabaseMetaData metaData = conn.getMetaData();
String catalog = conn.getCatalog();
String schema = conn.getSchema();
String[] types = new String[] {"TABLE"};

try (ResultSet rs = metaData.getTables(catalog, schema, tableName, types)) {
if (rs.next()) {
return rs.getString("TABLE_NAME");
}
}
try (ResultSet rs = metaData.getTables(catalog, schema, "%", types)) {
while (rs.next()) {
String metadataTableName = rs.getString("TABLE_NAME");
if (metadataTableName != null && metadataTableName.equalsIgnoreCase(tableName)) {
return metadataTableName;
}
}
}
return tableName;
}

private String caseInsensitiveKey(String value) {
return value == null ? null : value.toLowerCase(Locale.ROOT);
}

private Set<String> getPrimaryKeys(Connection conn, String tableName) throws SQLException {
Set<String> pkColumns = new HashSet<>();
DatabaseMetaData metaData = conn.getMetaData();
Expand Down
Loading