fix(oracle): support locked edits and OceanBase sources

This commit is contained in:
t8y2 committed 2026-07-23 23:59:03 +08:00
1 parent 3b004abb1e
commit 2bdab48d78
8 files changed
+394 -5

No files matched your search

@@ -12,6 +12,7 @@ import com.dbx.agent.JdbcIdentifiers;
import com.dbx.agent.MultiSessionJsonRpcServer;
import com.dbx.agent.MetadataListConstraints;
import com.dbx.agent.ObjectInfo;
import com.dbx.agent.ObjectSource;
import com.dbx.agent.TableInfo;
import com.dbx.agent.TriggerInfo;
@@ -266,6 +267,114 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
return result;
}
@Override
public ObjectSource getObjectSource(String schema, String name, String objectType) {
return unchecked(() -> {
String owner = normalizeSchema(schema);
String normalizedType = normalizeObjectSourceType(objectType);
String source;
SQLException metadataError = null;
try {
source = queryDbmsMetadataSource(owner, name, normalizedType);
} catch (SQLException e) {
metadataError = e;
source = null;
}
if ((source == null || source.trim().isEmpty()) && supportsDictionarySource(normalizedType)) {
try {
// OceanBase versions and tenant privileges differ in DBMS_METADATA coverage.
// Oracle-compatible dictionary views keep schema compare and source editing usable.
source = queryDictionarySource(owner, name, normalizedType);
} catch (SQLException fallbackError) {
if (metadataError != null) {
metadataError.addSuppressed(fallbackError);
throw metadataError;
}
throw fallbackError;
}
}
if (metadataError != null && (source == null || source.trim().isEmpty())) {
throw metadataError;
}
return new ObjectSource(name, normalizedType, owner, source == null ? "" : source);
});
}
private String queryDbmsMetadataSource(String owner, String name, String objectType) throws SQLException {
String sql = "SELECT DBMS_METADATA.GET_DDL(?, ?, ?) FROM DUAL";
try (var stmt = requireConnection().prepareStatement(sql)) {
stmt.setString(1, objectType);
stmt.setString(2, name);
stmt.setString(3, owner);
try (ResultSet rs = stmt.executeQuery()) {
return rs.next() ? rs.getString(1) : null;
}
}
}
private String queryDictionarySource(String owner, String name, String objectType) throws SQLException {
if ("VIEW".equals(objectType)) {
String sql = "SELECT TEXT FROM ALL_VIEWS WHERE OWNER = ? AND VIEW_NAME = ?";
try (var stmt = requireConnection().prepareStatement(sql)) {
stmt.setString(1, owner);
stmt.setString(2, name);
try (ResultSet rs = stmt.executeQuery()) {
return rs.next() ? rs.getString(1) : null;
}
}
}
String sourceType = switch (objectType) {
case "PROCEDURE", "FUNCTION", "PACKAGE", "TRIGGER", "TYPE" -> objectType;
case "PACKAGE_BODY" -> "PACKAGE BODY";
case "TYPE_BODY" -> "TYPE BODY";
default -> throw new IllegalArgumentException("Unsupported object type: " + objectType);
};
String sql = "SELECT TEXT FROM ALL_SOURCE WHERE OWNER = ? AND NAME = ? AND TYPE = ? ORDER BY LINE";
StringBuilder source = new StringBuilder();
try (var stmt = requireConnection().prepareStatement(sql)) {
stmt.setString(1, owner);
stmt.setString(2, name);
stmt.setString(3, sourceType);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
String line = rs.getString(1);
if (line != null) {
source.append(line);
}
}
}
}
return editableOracleSource(source.toString());
}
private static String normalizeObjectSourceType(String objectType) {
String normalized = objectType == null
? ""
: objectType.trim().toUpperCase(Locale.ROOT).replace(' ', '_');
return switch (normalized) {
case "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "SEQUENCE",
"PACKAGE", "PACKAGE_BODY", "TYPE", "TYPE_BODY" -> normalized;
default -> throw new IllegalArgumentException("Unsupported object type: " + objectType);
};
}
private static boolean supportsDictionarySource(String objectType) {
return switch (objectType) {
case "VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "PACKAGE", "PACKAGE_BODY", "TYPE", "TYPE_BODY" -> true;
default -> false;
};
}
private static String editableOracleSource(String source) {
String trimmed = source.trim();
if (trimmed.isEmpty() || trimmed.regionMatches(true, 0, "CREATE ", 0, "CREATE ".length())) {
return trimmed;
}
return "CREATE OR REPLACE " + trimmed;
}
private static String placeholders(int count) {
return String.join(", ", java.util.Collections.nCopies(count, "?"));
}
@@ -5,6 +5,7 @@ import com.dbx.agent.ConnectParams;
import com.dbx.agent.ExecuteQueryOptions;
import com.dbx.agent.MetadataListConstraints;
import com.dbx.agent.ObjectInfo;
import com.dbx.agent.ObjectSource;
import com.dbx.agent.QueryPageOptions;
import com.dbx.agent.TableInfo;
import com.dbx.agent.test.TestSupport;
@@ -17,6 +18,7 @@ import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
@@ -161,6 +163,89 @@ class OceanBaseOracleAgentTest {
Assertions.assertTrue(sql.get(0).contains("ROWNUM <= ?"), sql.get(0));
}
@Test
void readsViewDdlWithDbmsMetadataForSchemaCompare() {
List<String> sql = new ArrayList<>();
List<String> params = new ArrayList<>();
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
TestSupport.setPrivateConnection(agent, objectSourceConnection(
sql,
params,
resultSet(
new String[]{"DDL"},
new Object[][]{{"CREATE OR REPLACE VIEW \"APP\".\"ACTIVE_USERS\" AS SELECT ID FROM USERS"}}
)
));
ObjectSource source = agent.getObjectSource("app", "ACTIVE_USERS", "VIEW");
Assertions.assertEquals("VIEW", source.getObject_type());
Assertions.assertEquals("app", source.getSchema());
Assertions.assertTrue(source.getSource().startsWith("CREATE OR REPLACE VIEW"), source.getSource());
Assertions.assertEquals(List.of("VIEW", "ACTIVE_USERS", "app"), params);
Assertions.assertTrue(sql.get(0).contains("DBMS_METADATA.GET_DDL"), sql.get(0));
}
@Test
void fallsBackToAllViewsWhenDbmsMetadataIsUnavailable() {
List<String> sql = new ArrayList<>();
List<String> params = new ArrayList<>();
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
TestSupport.setPrivateConnection(agent, objectSourceFallbackConnection(
sql,
params,
resultSet(new String[]{"TEXT"}, new Object[][]{{"SELECT ID FROM USERS"}})
));
ObjectSource source = agent.getObjectSource("APP", "ACTIVE_USERS", "VIEW");
Assertions.assertEquals("SELECT ID FROM USERS", source.getSource());
Assertions.assertEquals(List.of("VIEW", "ACTIVE_USERS", "APP", "APP", "ACTIVE_USERS"), params);
Assertions.assertTrue(sql.get(1).contains("ALL_VIEWS"), sql.get(1));
}
@Test
void fallsBackToAllSourceForOracleRoutineAndPackageTypes() {
for (String[] object : new String[][]{
{"PROCEDURE", "PROCEDURE"},
{"FUNCTION", "FUNCTION"},
{"PACKAGE", "PACKAGE"},
{"PACKAGE_BODY", "PACKAGE BODY"}
}) {
List<String> sql = new ArrayList<>();
List<String> params = new ArrayList<>();
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
TestSupport.setPrivateConnection(agent, objectSourceFallbackConnection(
sql,
params,
resultSet(
new String[]{"TEXT"},
new Object[][]{{object[1] + " ACCOUNT_API AS\n"}, {"END ACCOUNT_API;\n"}}
)
));
ObjectSource source = agent.getObjectSource("APP", "ACCOUNT_API", object[0]);
Assertions.assertEquals(object[0], source.getObject_type());
Assertions.assertTrue(source.getSource().startsWith("CREATE OR REPLACE " + object[1]), source.getSource());
Assertions.assertEquals(object[1], params.get(params.size() - 1));
Assertions.assertTrue(sql.get(1).contains("ALL_SOURCE"), sql.get(1));
Assertions.assertTrue(sql.get(1).contains("ORDER BY LINE"), sql.get(1));
}
}
@Test
void rejectsUnsupportedObjectSourceTypesBeforeQuerying() {
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
IllegalArgumentException error = Assertions.assertThrows(
IllegalArgumentException.class,
() -> agent.getObjectSource("APP", "USERS", "TABLE")
);
Assertions.assertTrue(error.getMessage().contains("Unsupported object type: TABLE"), error.getMessage());
}
@Test
void getColumnsIncludesDefaultAndCommentMetadata() {
List<String> sql = new ArrayList<>();
@@ -261,6 +346,54 @@ class OceanBaseOracleAgentTest {
});
}
private static Connection objectSourceConnection(List<String> sql, List<String> params, ResultSet resultSet) {
PreparedStatement statement = objectSourceStatement(params, resultSet, false);
return proxy(Connection.class, (method, args) -> {
if ("prepareStatement".equals(method.getName())) {
sql.add(String.valueOf(args[0]));
return statement;
}
if ("isClosed".equals(method.getName())) {
return false;
}
return defaultValue(method.getReturnType());
});
}
private static Connection objectSourceFallbackConnection(List<String> sql, List<String> params, ResultSet fallbackResultSet) {
int[] statementIndex = {0};
return proxy(Connection.class, (method, args) -> {
if ("prepareStatement".equals(method.getName())) {
sql.add(String.valueOf(args[0]));
boolean fail = statementIndex[0]++ == 0;
return objectSourceStatement(params, fallbackResultSet, fail);
}
if ("isClosed".equals(method.getName())) {
return false;
}
return defaultValue(method.getReturnType());
});
}
private static PreparedStatement objectSourceStatement(List<String> params, ResultSet resultSet, boolean fail) {
return proxy(PreparedStatement.class, (method, args) -> {
if ("executeQuery".equals(method.getName())) {
if (fail) {
throw new SQLException("DBMS_METADATA is unavailable");
}
return resultSet;
}
if ("setString".equals(method.getName())) {
params.add(String.valueOf(args[1]));
return null;
}
if ("close".equals(method.getName())) {
return null;
}
return defaultValue(method.getReturnType());
});
}
private static Connection executionConnection(List<String> sql) {
Statement statement = proxy(Statement.class, (method, args) -> {
if ("execute".equals(method.getName())) {