Skip to content

Commit bd8acf2

Browse files
authored
fix: add datasource compatible (#119)
1 parent 6faf380 commit bd8acf2

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

  • data-agent-backend/src/main/java/io/github/malonetalk/agent/datasource

‎data-agent-backend/src/main/java/io/github/malonetalk/agent/datasource/SchemaReader.java‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.LinkedHashMap;
3333
import java.util.LinkedHashSet;
3434
import java.util.List;
35+
import java.util.Locale;
3536
import java.util.Map;
3637
import java.util.Set;
3738
import lombok.AllArgsConstructor;
@@ -63,8 +64,9 @@ public List<PhysicalColumnInfo> getTableSchema(Datasource datasource, String tab
6364
javax.sql.DataSource ds = dynamicDataSourceManager.getOrCreateDataSource(datasource);
6465

6566
try (Connection conn = ds.getConnection()) {
66-
Set<String> primaryKeys = getPrimaryKeys(conn, tableName);
67-
return getColumns(conn, tableName, primaryKeys);
67+
String metadataTableName = resolveMetadataTableName(conn, tableName);
68+
Set<String> primaryKeys = getPrimaryKeys(conn, metadataTableName);
69+
return getColumns(conn, metadataTableName, primaryKeys);
6870
} catch (SQLException e) {
6971
log.error("Failed to read schema for table {}: {}", tableName, e.getMessage(), e);
7072
throw BusinessException.of(
@@ -80,8 +82,10 @@ public Map<String, Set<String>> getTableColumnNames(
8082
return Map.of();
8183
}
8284
Map<String, Set<String>> columnNamesByTable = new LinkedHashMap<>();
85+
Map<String, String> tableNameLookup = new LinkedHashMap<>();
8386
for (String tableName : tableNames) {
8487
columnNamesByTable.putIfAbsent(tableName, new LinkedHashSet<>());
88+
tableNameLookup.putIfAbsent(caseInsensitiveKey(tableName), tableName);
8589
}
8690

8791
javax.sql.DataSource ds = dynamicDataSourceManager.getOrCreateDataSource(datasource);
@@ -91,7 +95,12 @@ public Map<String, Set<String>> getTableColumnNames(
9195
try (ResultSet rs =
9296
metaData.getColumns(conn.getCatalog(), conn.getSchema(), "%", null)) {
9397
while (rs.next()) {
94-
Set<String> columnNames = columnNamesByTable.get(rs.getString("TABLE_NAME"));
98+
String metadataTableName = rs.getString("TABLE_NAME");
99+
String tableName =
100+
columnNamesByTable.containsKey(metadataTableName)
101+
? metadataTableName
102+
: tableNameLookup.get(caseInsensitiveKey(metadataTableName));
103+
Set<String> columnNames = columnNamesByTable.get(tableName);
95104
if (columnNames != null) {
96105
columnNames.add(rs.getString("COLUMN_NAME"));
97106
}
@@ -123,6 +132,32 @@ private List<PhysicalTableInfo> getTables(Connection conn) throws SQLException {
123132
return tables;
124133
}
125134

135+
private String resolveMetadataTableName(Connection conn, String tableName) throws SQLException {
136+
DatabaseMetaData metaData = conn.getMetaData();
137+
String catalog = conn.getCatalog();
138+
String schema = conn.getSchema();
139+
String[] types = new String[] {"TABLE"};
140+
141+
try (ResultSet rs = metaData.getTables(catalog, schema, tableName, types)) {
142+
if (rs.next()) {
143+
return rs.getString("TABLE_NAME");
144+
}
145+
}
146+
try (ResultSet rs = metaData.getTables(catalog, schema, "%", types)) {
147+
while (rs.next()) {
148+
String metadataTableName = rs.getString("TABLE_NAME");
149+
if (metadataTableName != null && metadataTableName.equalsIgnoreCase(tableName)) {
150+
return metadataTableName;
151+
}
152+
}
153+
}
154+
return tableName;
155+
}
156+
157+
private String caseInsensitiveKey(String value) {
158+
return value == null ? null : value.toLowerCase(Locale.ROOT);
159+
}
160+
126161
private Set<String> getPrimaryKeys(Connection conn, String tableName) throws SQLException {
127162
Set<String> pkColumns = new HashSet<>();
128163
DatabaseMetaData metaData = conn.getMetaData();

0 commit comments

Comments
 (0)