fix(dameng): separate materialized view metadata
This commit is contained in:
1 parent
ff810f3058
commit
73120883b6
15 files changed
+475
-58
No files matched your search
@@ -30,7 +30,9 @@ import java.sql.Types;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@@ -44,6 +46,7 @@ public final class DamengAgent extends BaseDatabaseAgent {
|
||||
);
|
||||
|
||||
private Connection connection;
|
||||
private String connectedUsername;
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
@@ -56,6 +59,7 @@ public final class DamengAgent extends BaseDatabaseAgent {
|
||||
withSuppressedStdout(() -> {
|
||||
Class.forName("dm.jdbc.driver.DmDriver");
|
||||
connection = DriverManager.getConnection(buildUrl(params), params.getUsername(), params.getPassword());
|
||||
connectedUsername = params.getUsername();
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -136,31 +140,191 @@ public final class DamengAgent extends BaseDatabaseAgent {
|
||||
|
||||
@Override
|
||||
public List<TableInfo> listTables(String schema) {
|
||||
return listTables(schema, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableInfo> listTables(String schema, List<String> objectTypes) {
|
||||
return unchecked(() -> {
|
||||
List<TableInfo> result = new ArrayList<>();
|
||||
String sql = """
|
||||
SELECT TABLE_NAME, 'TABLE' AS TABLE_TYPE, COMMENTS
|
||||
FROM ALL_TAB_COMMENTS
|
||||
WHERE OWNER = ? AND TABLE_TYPE = 'TABLE'
|
||||
UNION ALL
|
||||
SELECT TABLE_NAME, 'VIEW' AS TABLE_TYPE, COMMENTS
|
||||
FROM ALL_TAB_COMMENTS
|
||||
WHERE OWNER = ? AND TABLE_TYPE = 'VIEW'
|
||||
ORDER BY 1
|
||||
""".stripIndent().trim();
|
||||
try (PreparedStatement stmt = requireConnected().prepareStatement(sql)) {
|
||||
stmt.setString(1, schema);
|
||||
stmt.setString(2, schema);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(new TableInfo(rs.getString(1), rs.getString(2), rs.getString(3)));
|
||||
}
|
||||
}
|
||||
Map<String, TableInfo> tablesByName = new LinkedHashMap<>();
|
||||
if (objectTypesInclude(objectTypes, "TABLE")) {
|
||||
loadTableOrView(schema, "TABLE", tablesByName);
|
||||
}
|
||||
if (objectTypesInclude(objectTypes, "VIEW")) {
|
||||
loadTableOrView(schema, "VIEW", tablesByName);
|
||||
}
|
||||
if (objectTypesInclude(objectTypes, "MATERIALIZED_VIEW")) {
|
||||
loadMaterializedViews(schema, tablesByName);
|
||||
}
|
||||
List<TableInfo> result = new ArrayList<>(tablesByName.values());
|
||||
result.sort(Comparator.comparing(TableInfo::getName));
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private void loadTableOrView(String schema, String tableType, Map<String, TableInfo> tablesByName) {
|
||||
if (!loadTableOrViewFromAllObjects(schema, tableType, tablesByName)) {
|
||||
loadTableOrViewFromComments(schema, tableType, tablesByName);
|
||||
}
|
||||
if ("VIEW".equals(tableType)) {
|
||||
removeMaterializedViewsFromRegularViews(schema, tablesByName);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean loadTableOrViewFromAllObjects(String schema, String tableType, Map<String, TableInfo> tablesByName) {
|
||||
String sql = ("""
|
||||
SELECT o.OBJECT_NAME AS TABLE_NAME, c.COMMENTS
|
||||
FROM ALL_OBJECTS o
|
||||
LEFT JOIN ALL_TAB_COMMENTS c ON c.OWNER = o.OWNER AND c.TABLE_NAME = o.OBJECT_NAME
|
||||
WHERE o.OWNER = ? AND o.OBJECT_TYPE = '%s'
|
||||
ORDER BY o.OBJECT_NAME
|
||||
""").formatted(tableType).stripIndent().trim();
|
||||
try (PreparedStatement stmt = requireConnected().prepareStatement(sql)) {
|
||||
stmt.setString(1, schema);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
addTableInfo(tablesByName, rs.getString(1), tableType, rs.getString(2));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ignored) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void loadTableOrViewFromComments(String schema, String tableType, Map<String, TableInfo> tablesByName) {
|
||||
String sql = ("""
|
||||
SELECT TABLE_NAME, COMMENTS
|
||||
FROM ALL_TAB_COMMENTS
|
||||
WHERE OWNER = ? AND TABLE_TYPE = '%s'
|
||||
ORDER BY TABLE_NAME
|
||||
""").formatted(tableType).stripIndent().trim();
|
||||
try (PreparedStatement stmt = requireConnected().prepareStatement(sql)) {
|
||||
stmt.setString(1, schema);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
addTableInfo(tablesByName, rs.getString(1), tableType, rs.getString(2));
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// Keep metadata browsing usable even when optional catalog views are restricted.
|
||||
}
|
||||
}
|
||||
|
||||
private void loadMaterializedViews(String schema, Map<String, TableInfo> tablesByName) {
|
||||
loadMaterializedViewsFromAllObjects(schema, tablesByName);
|
||||
loadMaterializedViewsFromUserMviews(schema, tablesByName);
|
||||
}
|
||||
|
||||
private void loadMaterializedViewsFromAllObjects(String schema, Map<String, TableInfo> tablesByName) {
|
||||
String sql = """
|
||||
SELECT o.OBJECT_NAME AS TABLE_NAME, c.COMMENTS
|
||||
FROM ALL_OBJECTS o
|
||||
LEFT JOIN ALL_TAB_COMMENTS c ON c.OWNER = o.OWNER AND c.TABLE_NAME = o.OBJECT_NAME
|
||||
WHERE o.OWNER = ? AND o.OBJECT_TYPE IN ('MATERIALIZED VIEW', 'MATERIALIZED_VIEW')
|
||||
ORDER BY o.OBJECT_NAME
|
||||
""".stripIndent().trim();
|
||||
try (PreparedStatement stmt = requireConnected().prepareStatement(sql)) {
|
||||
stmt.setString(1, schema);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
addTableInfo(tablesByName, rs.getString(1), "MATERIALIZED_VIEW", rs.getString(2));
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// Older or restricted Dameng catalogs may not expose this object type.
|
||||
}
|
||||
}
|
||||
|
||||
private void loadMaterializedViewsFromUserMviews(String schema, Map<String, TableInfo> tablesByName) {
|
||||
if (!schemaMatchesConnectedUser(schema)) {
|
||||
return;
|
||||
}
|
||||
loadUserMviews("SELECT MVIEW_NAME FROM USER_MVIEWS", tablesByName);
|
||||
}
|
||||
|
||||
private boolean loadUserMviews(String sql, Map<String, TableInfo> tablesByName) {
|
||||
try (PreparedStatement stmt = requireConnected().prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
addTableInfo(tablesByName, rs.getString(1), "MATERIALIZED_VIEW", null);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ignored) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void removeMaterializedViewsFromRegularViews(String schema, Map<String, TableInfo> tablesByName) {
|
||||
if (!schemaMatchesConnectedUser(schema)) {
|
||||
return;
|
||||
}
|
||||
for (String name : listUserMviewNames()) {
|
||||
tablesByName.remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> listUserMviewNames() {
|
||||
Set<String> names = new java.util.HashSet<>();
|
||||
String sql = "SELECT MVIEW_NAME FROM USER_MVIEWS";
|
||||
try (PreparedStatement stmt = requireConnected().prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String key = metadataNameKey(rs.getString(1));
|
||||
if (!key.isEmpty()) {
|
||||
names.add(key);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// Some Dameng versions or users do not expose USER_MVIEWS.
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
private boolean schemaMatchesConnectedUser(String schema) {
|
||||
return connectedUsername != null
|
||||
&& schema != null
|
||||
&& !connectedUsername.isBlank()
|
||||
&& schema.equalsIgnoreCase(connectedUsername);
|
||||
}
|
||||
|
||||
private static void addTableInfo(Map<String, TableInfo> tablesByName, String name, String tableType, String comment) {
|
||||
String key = metadataNameKey(name);
|
||||
if (!key.isEmpty()) {
|
||||
tablesByName.put(key, new TableInfo(name, tableType, comment));
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean objectTypesInclude(List<String> objectTypes, String expectedType) {
|
||||
if (objectTypes == null || objectTypes.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
for (String objectType : objectTypes) {
|
||||
if (expectedType.equals(normalizeObjectType(objectType))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String normalizeObjectType(String value) {
|
||||
String upper = value == null ? "" : value.trim().toUpperCase(Locale.ROOT).replace(' ', '_').replace('-', '_');
|
||||
if (upper.contains("MATERIALIZED") && upper.contains("VIEW")) {
|
||||
return "MATERIALIZED_VIEW";
|
||||
}
|
||||
if (upper.contains("VIEW")) {
|
||||
return "VIEW";
|
||||
}
|
||||
if (upper.contains("TABLE")) {
|
||||
return "TABLE";
|
||||
}
|
||||
return upper;
|
||||
}
|
||||
|
||||
private static String metadataNameKey(String value) {
|
||||
return value == null ? "" : value.trim().toUpperCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ObjectInfo> listObjects(String schema) {
|
||||
return unchecked(() -> {
|
||||
@@ -190,6 +354,7 @@ public final class DamengAgent extends BaseDatabaseAgent {
|
||||
return unchecked(() -> {
|
||||
String dbmsType = switch (objectType.toUpperCase(Locale.ROOT)) {
|
||||
case "VIEW" -> "VIEW";
|
||||
case "MATERIALIZED_VIEW", "MATERIALIZED VIEW" -> "MATERIALIZED_VIEW";
|
||||
case "PROCEDURE" -> "PROCEDURE";
|
||||
case "FUNCTION" -> "FUNCTION";
|
||||
default -> throw new IllegalArgumentException("Unsupported object type: " + objectType);
|
||||
|
||||
+130
-5
@@ -2,18 +2,21 @@ package com.dbx.agent.dameng;
|
||||
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.ObjectInfo;
|
||||
import com.dbx.agent.ObjectSource;
|
||||
import com.dbx.agent.TableInfo;
|
||||
import com.dbx.agent.test.JdbcMetadataSqlFake;
|
||||
import com.dbx.agent.test.TestSupport;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@@ -43,7 +46,11 @@ class DamengAgentMetadataTest {
|
||||
.filter(sql -> sql.contains("ALL_TAB_COMMENTS"))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
String allTablesSql = String.join("\n", JdbcMetadataSqlFake.statements);
|
||||
Assertions.assertTrue(tablesSql.contains("COMMENTS"), tablesSql);
|
||||
Assertions.assertTrue(allTablesSql.contains("ALL_OBJECTS"), allTablesSql);
|
||||
Assertions.assertTrue(allTablesSql.contains("MATERIALIZED_VIEW"), allTablesSql);
|
||||
Assertions.assertFalse(allTablesSql.contains("ALL_MVIEWS"), allTablesSql);
|
||||
Assertions.assertFalse(tablesSql.contains("ALL_TABLES"), tablesSql);
|
||||
Assertions.assertFalse(tablesSql.contains("ALL_VIEWS"), tablesSql);
|
||||
}
|
||||
@@ -72,6 +79,55 @@ class DamengAgentMetadataTest {
|
||||
Assertions.assertEquals("用户示例表", objects.get(0).getComment());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapsMaterializedViewsFromMetadata() {
|
||||
DamengAgent agent = new DamengAgent();
|
||||
TestSupport.setPrivateConnection(agent, metadataConnection("id comment", null, true));
|
||||
|
||||
List<TableInfo> tables = agent.listTables("APP");
|
||||
List<ObjectInfo> objects = agent.listObjects("APP");
|
||||
|
||||
Assertions.assertTrue(tables.stream().anyMatch(table ->
|
||||
"USER_SUMMARY_MV".equals(table.getName()) && "MATERIALIZED_VIEW".equals(table.getTable_type())
|
||||
));
|
||||
Assertions.assertFalse(tables.stream().anyMatch(table ->
|
||||
"USER_SUMMARY_MV".equals(table.getName()) && "VIEW".equals(table.getTable_type())
|
||||
));
|
||||
Assertions.assertTrue(objects.stream().anyMatch(object ->
|
||||
"USER_SUMMARY_MV".equals(object.getName()) && "MATERIALIZED_VIEW".equals(object.getObject_type())
|
||||
));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filtersTableListingByRequestedObjectTypes() {
|
||||
DamengAgent agent = new DamengAgent();
|
||||
TestSupport.setPrivateConnection(agent, metadataConnection("id comment", null, true));
|
||||
setConnectedUsername(agent, "APP");
|
||||
|
||||
List<TableInfo> views = agent.listTables("APP", List.of("VIEW"));
|
||||
List<TableInfo> materializedViews = agent.listTables("APP", List.of("MATERIALIZED_VIEW"));
|
||||
|
||||
Assertions.assertTrue(views.stream().noneMatch(table -> "MATERIALIZED_VIEW".equals(table.getTable_type())));
|
||||
Assertions.assertTrue(views.stream().noneMatch(table -> "USER_SUMMARY_MV".equals(table.getName())));
|
||||
Assertions.assertEquals(List.of("USER_SUMMARY_MV"), materializedViews.stream().map(TableInfo::getName).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsMaterializedViewSourceWithDbmsMetadataType() {
|
||||
DamengAgent agent = new DamengAgent();
|
||||
List<String> params = new ArrayList<>();
|
||||
TestSupport.setPrivateConnection(
|
||||
agent,
|
||||
objectSourceConnection(params, "CREATE MATERIALIZED VIEW \"APP\".\"USER_SUMMARY_MV\" AS SELECT 1 AS ID FROM DUAL")
|
||||
);
|
||||
|
||||
ObjectSource source = agent.getObjectSource("APP", "USER_SUMMARY_MV", "MATERIALIZED_VIEW");
|
||||
|
||||
Assertions.assertEquals(List.of("MATERIALIZED_VIEW", "USER_SUMMARY_MV", "APP"), params);
|
||||
Assertions.assertEquals("MATERIALIZED_VIEW", source.getObject_type());
|
||||
Assertions.assertTrue(source.getSource().contains("CREATE MATERIALIZED VIEW"), source.getSource());
|
||||
}
|
||||
|
||||
@Test
|
||||
void triggerMetadataDoesNotRequireOracleTriggerTypeColumn() {
|
||||
DamengAgent agent = new DamengAgent();
|
||||
@@ -137,6 +193,10 @@ class DamengAgentMetadataTest {
|
||||
}
|
||||
|
||||
private static Connection metadataConnection(String allColumnComment, String fallbackColumnComment) {
|
||||
return metadataConnection(allColumnComment, fallbackColumnComment, false);
|
||||
}
|
||||
|
||||
private static Connection metadataConnection(String allColumnComment, String fallbackColumnComment, boolean includeMaterializedView) {
|
||||
return proxy(Connection.class, (method, args) -> {
|
||||
String name = method.getName();
|
||||
if ("prepareStatement".equals(name)) {
|
||||
@@ -159,15 +219,44 @@ class DamengAgentMetadataTest {
|
||||
if (sql.contains("SYSCOLUMNCOMMENTS")) {
|
||||
return metadataStatement(List.of());
|
||||
}
|
||||
if (sql.contains("ALL_TAB_COMMENTS")) {
|
||||
return metadataStatement(List.of(List.of("USERS", "TABLE", "用户示例表")));
|
||||
if (sql.contains("ALL_OBJECTS") && sql.contains("OBJECT_TYPE = 'TABLE'")) {
|
||||
return metadataStatement(List.of(List.of("USERS", "用户示例表")));
|
||||
}
|
||||
if (sql.contains("ALL_COL_COMMENTS") && !sql.contains("ALL_TAB_COLUMNS")) {
|
||||
return metadataStatement(List.of());
|
||||
if (sql.contains("ALL_OBJECTS") && sql.contains("OBJECT_TYPE = 'VIEW'")) {
|
||||
return metadataStatement(
|
||||
includeMaterializedView
|
||||
? List.of(Arrays.asList("USER_SUMMARY_MV", "mv comment"))
|
||||
: List.of()
|
||||
);
|
||||
}
|
||||
if (sql.contains("ALL_OBJECTS") && sql.contains("MATERIALIZED VIEW")) {
|
||||
return metadataStatement(
|
||||
includeMaterializedView
|
||||
? List.of(Arrays.asList("USER_SUMMARY_MV", "mv comment"))
|
||||
: List.of()
|
||||
);
|
||||
}
|
||||
if (sql.contains("ALL_OBJECTS")) {
|
||||
return metadataStatement(List.of());
|
||||
}
|
||||
if (sql.contains("ALL_TAB_COMMENTS")) {
|
||||
List<List<Object>> rows = new ArrayList<>();
|
||||
rows.add(List.of("USERS", "TABLE", "用户示例表"));
|
||||
if (includeMaterializedView) {
|
||||
rows.add(Arrays.asList("USER_SUMMARY_MV", "VIEW", "mv comment"));
|
||||
}
|
||||
return metadataStatement(rows);
|
||||
}
|
||||
if (sql.contains("USER_MVIEWS")) {
|
||||
return metadataStatement(
|
||||
includeMaterializedView
|
||||
? List.of(List.of("USER_SUMMARY_MV"))
|
||||
: List.of()
|
||||
);
|
||||
}
|
||||
if (sql.contains("ALL_COL_COMMENTS") && !sql.contains("ALL_TAB_COLUMNS")) {
|
||||
return metadataStatement(List.of());
|
||||
}
|
||||
if (sql.contains("ALL_TAB_COLUMNS")) {
|
||||
return metadataStatement(List.of(Arrays.asList(
|
||||
"ID",
|
||||
@@ -191,13 +280,49 @@ class DamengAgentMetadataTest {
|
||||
});
|
||||
}
|
||||
|
||||
private static void setConnectedUsername(DamengAgent agent, String username) {
|
||||
try {
|
||||
Field field = DamengAgent.class.getDeclaredField("connectedUsername");
|
||||
field.setAccessible(true);
|
||||
field.set(agent, username);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new IllegalStateException("Unable to set connected username", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Connection objectSourceConnection(List<String> params, String source) {
|
||||
return proxy(Connection.class, (method, args) -> {
|
||||
String name = method.getName();
|
||||
if ("prepareStatement".equals(name)) {
|
||||
return metadataStatement(List.of(List.of(source)), params);
|
||||
}
|
||||
if ("close".equals(name)) {
|
||||
return null;
|
||||
}
|
||||
if ("isClosed".equals(name)) {
|
||||
return false;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
}
|
||||
|
||||
private static PreparedStatement metadataStatement(List<List<Object>> rows) {
|
||||
return metadataStatement(rows, null);
|
||||
}
|
||||
|
||||
private static PreparedStatement metadataStatement(List<List<Object>> rows, List<String> params) {
|
||||
return proxy(PreparedStatement.class, (method, args) -> {
|
||||
String name = method.getName();
|
||||
if ("executeQuery".equals(name)) {
|
||||
return metadataResultSet(rows);
|
||||
}
|
||||
if ("setString".equals(name) || "close".equals(name)) {
|
||||
if ("setString".equals(name)) {
|
||||
if (params != null) {
|
||||
params.add(String.valueOf(args[1]));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if ("close".equals(name)) {
|
||||
return null;
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
|
||||
Reference in new issue
Block a user