fix(dameng): resolve empty schema via session current schema
This commit is contained in:
2 files changed
+197
-8
No files matched your search
@@ -569,7 +569,8 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
return queryConstrainedTables(schema, MetadataListConstraints.orNone(constraints));
|
return queryConstrainedTables(schema, MetadataListConstraints.orNone(constraints));
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<TableInfo> queryConstrainedTables(String schema, MetadataListConstraints constraints) {
|
private List<TableInfo> queryConstrainedTables(String rawSchema, MetadataListConstraints constraints) {
|
||||||
|
final String schema = normalizeSchema(rawSchema);
|
||||||
if (legacyJdbcMetadata) {
|
if (legacyJdbcMetadata) {
|
||||||
return withViewValidity(executeJdbcMetadataTables(schema, constraints), schema);
|
return withViewValidity(executeJdbcMetadataTables(schema, constraints), schema);
|
||||||
}
|
}
|
||||||
@@ -905,6 +906,43 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
&& schema.equalsIgnoreCase(connectedUsername);
|
&& schema.equalsIgnoreCase(connectedUsername);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DM resolves an unqualified object name in the session's current schema, and dbx sends an
|
||||||
|
* unqualified metadata request whenever the editor's source table has no schema selected
|
||||||
|
* (the same Oracle-like contract `oracle` and `oceanbase-oracle` follow). Looking up
|
||||||
|
* `OWNER = ''` instead reported "no columns" for tables that exist, so column comments never
|
||||||
|
* reached the result-column tooltip (issue #10221).
|
||||||
|
*/
|
||||||
|
private String normalizeSchema(String schema) {
|
||||||
|
if (schema != null && !schema.isBlank()) {
|
||||||
|
return schema;
|
||||||
|
}
|
||||||
|
return effectiveMetadataSchema(currentSchemaOrBlank(), connectedUsername);
|
||||||
|
}
|
||||||
|
|
||||||
|
static String effectiveMetadataSchema(String currentSchema, String connectedUsername) {
|
||||||
|
if (currentSchema != null && !currentSchema.isBlank()) {
|
||||||
|
return currentSchema;
|
||||||
|
}
|
||||||
|
return connectedUsername == null ? "" : connectedUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String currentSchemaOrBlank() {
|
||||||
|
try (Statement stmt = requireConnected().createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery("SELECT SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') FROM DUAL")) {
|
||||||
|
if (rs.next()) {
|
||||||
|
String current = rs.getString(1);
|
||||||
|
if (current != null && !current.isBlank()) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException error) {
|
||||||
|
// A server without SYS_CONTEXT support still resolves an unqualified name to the
|
||||||
|
// connected user, which is what the caller falls back to.
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean includesSupportedObjectTypes(MetadataListConstraints constraints) {
|
private static boolean includesSupportedObjectTypes(MetadataListConstraints constraints) {
|
||||||
return constraints.includesTableLikeTypes()
|
return constraints.includesTableLikeTypes()
|
||||||
|| constraints.objectTypeAllowed("PROCEDURE")
|
|| constraints.objectTypeAllowed("PROCEDURE")
|
||||||
@@ -1095,7 +1133,8 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
return queryConstrainedObjects(schema, MetadataListConstraints.orNone(constraints));
|
return queryConstrainedObjects(schema, MetadataListConstraints.orNone(constraints));
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<ObjectInfo> queryConstrainedObjects(String schema, MetadataListConstraints constraints) {
|
private List<ObjectInfo> queryConstrainedObjects(String rawSchema, MetadataListConstraints constraints) {
|
||||||
|
final String schema = normalizeSchema(rawSchema);
|
||||||
List<ObjectInfo> objects = queryConstrainedObjectsWithoutValidity(schema, constraints);
|
List<ObjectInfo> objects = queryConstrainedObjectsWithoutValidity(schema, constraints);
|
||||||
applyViewValidity(objects, schema);
|
applyViewValidity(objects, schema);
|
||||||
return objects;
|
return objects;
|
||||||
@@ -1349,7 +1388,8 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ObjectSource getObjectSource(String schema, String name, String objectType) {
|
public ObjectSource getObjectSource(String rawSchema, String name, String objectType) {
|
||||||
|
final String schema = normalizeSchema(rawSchema);
|
||||||
return unchecked(() -> {
|
return unchecked(() -> {
|
||||||
String dbmsType = damengDdlObjectType(objectType);
|
String dbmsType = damengDdlObjectType(objectType);
|
||||||
RuntimeException dbmsError;
|
RuntimeException dbmsError;
|
||||||
@@ -1775,7 +1815,8 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTableDdl(String schema, String table) {
|
public String getTableDdl(String rawSchema, String table) {
|
||||||
|
final String schema = normalizeSchema(rawSchema);
|
||||||
if (legacyJdbcMetadata) {
|
if (legacyJdbcMetadata) {
|
||||||
return super.getTableDdl(schema, table);
|
return super.getTableDdl(schema, table);
|
||||||
}
|
}
|
||||||
@@ -1869,7 +1910,8 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ColumnInfo> getColumns(String schema, String table) {
|
public List<ColumnInfo> getColumns(String rawSchema, String table) {
|
||||||
|
final String schema = normalizeSchema(rawSchema);
|
||||||
if (legacyJdbcMetadata) {
|
if (legacyJdbcMetadata) {
|
||||||
return StandardJdbcMetadata.INSTANCE.getColumns(
|
return StandardJdbcMetadata.INSTANCE.getColumns(
|
||||||
requireConnected(),
|
requireConnected(),
|
||||||
@@ -2175,7 +2217,8 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<IndexInfo> listIndexes(String schema, String table) {
|
public List<IndexInfo> listIndexes(String rawSchema, String table) {
|
||||||
|
final String schema = normalizeSchema(rawSchema);
|
||||||
if (legacyJdbcMetadata) {
|
if (legacyJdbcMetadata) {
|
||||||
return StandardJdbcMetadata.INSTANCE.listIndexes(
|
return StandardJdbcMetadata.INSTANCE.listIndexes(
|
||||||
requireConnected(),
|
requireConnected(),
|
||||||
@@ -2234,7 +2277,8 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ForeignKeyInfo> listForeignKeys(String schema, String table) {
|
public List<ForeignKeyInfo> listForeignKeys(String rawSchema, String table) {
|
||||||
|
final String schema = normalizeSchema(rawSchema);
|
||||||
if (legacyJdbcMetadata) {
|
if (legacyJdbcMetadata) {
|
||||||
return StandardJdbcMetadata.INSTANCE.listForeignKeys(requireConnected(), schema, table);
|
return StandardJdbcMetadata.INSTANCE.listForeignKeys(requireConnected(), schema, table);
|
||||||
}
|
}
|
||||||
@@ -2268,7 +2312,8 @@ public final class DamengAgent extends AbstractJdbcAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<TriggerInfo> listTriggers(String schema, String table) {
|
public List<TriggerInfo> listTriggers(String rawSchema, String table) {
|
||||||
|
final String schema = normalizeSchema(rawSchema);
|
||||||
if (legacyJdbcMetadata) {
|
if (legacyJdbcMetadata) {
|
||||||
return StandardJdbcMetadata.INSTANCE.listTriggers(schema, table);
|
return StandardJdbcMetadata.INSTANCE.listTriggers(schema, table);
|
||||||
}
|
}
|
||||||
|
|||||||
+144
@@ -22,6 +22,7 @@ import java.sql.DatabaseMetaData;
|
|||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -111,6 +112,80 @@ class DamengAgentMetadataTest {
|
|||||||
Assertions.assertTrue(columnsSql.startsWith("SELECT /*+ PARALLEL(1) */"), columnsSql);
|
Assertions.assertTrue(columnsSql.startsWith("SELECT /*+ PARALLEL(1) */"), columnsSql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #10221:dbx 在编辑器里的表没有选 schema 时,会以「不带 schema」的形态请求元数据
|
||||||
|
// (oracle / oceanbase-oracle 同款约定)。DM 会按会话当前 schema 解析未限定名,所以
|
||||||
|
// 这里必须先把空 schema 解析成当前 schema,否则会按 OWNER = '' 去查,列注释永远拿不到。
|
||||||
|
@Test
|
||||||
|
void resolvesBlankColumnSchemaFromTheSessionBeforeReadingComments() {
|
||||||
|
DamengAgent agent = new DamengAgent();
|
||||||
|
List<String> sessionQueries = new ArrayList<>();
|
||||||
|
List<String> boundOwners = new ArrayList<>();
|
||||||
|
TestSupport.setPrivateConnection(agent, sessionSchemaConnection("SYSDBA", sessionQueries, boundOwners));
|
||||||
|
|
||||||
|
List<ColumnInfo> columns = agent.getColumns("", "USERS");
|
||||||
|
|
||||||
|
Assertions.assertTrue(
|
||||||
|
sessionQueries.stream().anyMatch(sql -> sql.contains("SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')")),
|
||||||
|
sessionQueries.toString()
|
||||||
|
);
|
||||||
|
Assertions.assertTrue(boundOwners.contains("SYSDBA"), boundOwners.toString());
|
||||||
|
Assertions.assertFalse(boundOwners.contains(""), boundOwners.toString());
|
||||||
|
Assertions.assertEquals(1, columns.size());
|
||||||
|
Assertions.assertEquals("id comment", columns.get(0).getComment());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void resolvesBlankTableSchemaFromTheSessionBeforeListingTables() {
|
||||||
|
DamengAgent agent = new DamengAgent();
|
||||||
|
List<String> sessionQueries = new ArrayList<>();
|
||||||
|
List<String> boundOwners = new ArrayList<>();
|
||||||
|
TestSupport.setPrivateConnection(agent, sessionSchemaConnection("SYSDBA", sessionQueries, boundOwners));
|
||||||
|
|
||||||
|
List<TableInfo> tables = agent.listTables("");
|
||||||
|
|
||||||
|
Assertions.assertTrue(boundOwners.contains("SYSDBA"), boundOwners.toString());
|
||||||
|
Assertions.assertFalse(boundOwners.contains(""), boundOwners.toString());
|
||||||
|
Assertions.assertEquals(List.of("USERS"), tables.stream().map(TableInfo::getName).toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void resolvesBlankSchemaToTheConnectedUserWhenTheSessionCannotReportIt() {
|
||||||
|
DamengAgent agent = new DamengAgent();
|
||||||
|
setConnectedUsername(agent, "DBXUSER");
|
||||||
|
List<String> sessionQueries = new ArrayList<>();
|
||||||
|
List<String> boundOwners = new ArrayList<>();
|
||||||
|
TestSupport.setPrivateConnection(agent, sessionSchemaConnection(null, sessionQueries, boundOwners));
|
||||||
|
|
||||||
|
List<ColumnInfo> columns = agent.getColumns("", "USERS");
|
||||||
|
|
||||||
|
Assertions.assertTrue(boundOwners.contains("DBXUSER"), boundOwners.toString());
|
||||||
|
Assertions.assertFalse(boundOwners.contains(""), boundOwners.toString());
|
||||||
|
Assertions.assertEquals(1, columns.size());
|
||||||
|
Assertions.assertEquals("id comment", columns.get(0).getComment());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void keepsAnExplicitSchemaWithoutConsultingTheSession() {
|
||||||
|
DamengAgent agent = new DamengAgent();
|
||||||
|
List<String> sessionQueries = new ArrayList<>();
|
||||||
|
List<String> boundOwners = new ArrayList<>();
|
||||||
|
TestSupport.setPrivateConnection(agent, sessionSchemaConnection("SYSDBA", sessionQueries, boundOwners));
|
||||||
|
|
||||||
|
agent.getColumns("APP", "USERS");
|
||||||
|
|
||||||
|
Assertions.assertTrue(boundOwners.contains("APP"), boundOwners.toString());
|
||||||
|
Assertions.assertTrue(sessionQueries.isEmpty(), sessionQueries.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void effectiveMetadataSchemaPrefersTheSessionSchemaThenTheConnectedUser() {
|
||||||
|
Assertions.assertEquals("CURRENT_SCH", DamengAgent.effectiveMetadataSchema("CURRENT_SCH", "CONNECTED"));
|
||||||
|
Assertions.assertEquals("CONNECTED", DamengAgent.effectiveMetadataSchema("", "CONNECTED"));
|
||||||
|
Assertions.assertEquals("CONNECTED", DamengAgent.effectiveMetadataSchema(" ", "CONNECTED"));
|
||||||
|
Assertions.assertEquals("CONNECTED", DamengAgent.effectiveMetadataSchema(null, "CONNECTED"));
|
||||||
|
Assertions.assertEquals("", DamengAgent.effectiveMetadataSchema(null, null));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void disablesParallelExecutionForIndexMetadataQuery() {
|
void disablesParallelExecutionForIndexMetadataQuery() {
|
||||||
DamengAgent agent = new DamengAgent();
|
DamengAgent agent = new DamengAgent();
|
||||||
@@ -2699,6 +2774,75 @@ class DamengAgentMetadataTest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serves the session-schema probe, records every first bound parameter (the OWNER of the
|
||||||
|
// metadata queries under test) and answers the column/table queries these tests assert on.
|
||||||
|
private static Connection sessionSchemaConnection(
|
||||||
|
String currentSchema,
|
||||||
|
List<String> sessionQueries,
|
||||||
|
List<String> boundOwners
|
||||||
|
) {
|
||||||
|
return proxy(Connection.class, (method, args) -> {
|
||||||
|
String name = method.getName();
|
||||||
|
if ("createStatement".equals(name)) {
|
||||||
|
return proxy(Statement.class, (statementMethod, statementArgs) -> {
|
||||||
|
String statementName = statementMethod.getName();
|
||||||
|
if ("executeQuery".equals(statementName)) {
|
||||||
|
String sql = (String) statementArgs[0];
|
||||||
|
sessionQueries.add(sql);
|
||||||
|
if (sql.contains("SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')")) {
|
||||||
|
if (currentSchema == null) {
|
||||||
|
throw new SQLException("SYS_CONTEXT is not supported");
|
||||||
|
}
|
||||||
|
return metadataResultSet(List.of(List.of(currentSchema)));
|
||||||
|
}
|
||||||
|
return metadataResultSet(List.of());
|
||||||
|
}
|
||||||
|
if ("close".equals(statementName)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return defaultValue(statementMethod.getReturnType());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if ("prepareStatement".equals(name)) {
|
||||||
|
String sql = (String) args[0];
|
||||||
|
if (sql.contains("ALL_TAB_COLUMNS")) {
|
||||||
|
return ownerRecordingStatement(defaultColumnMetadataRows("id comment"), boundOwners);
|
||||||
|
}
|
||||||
|
if (sql.contains("ALL_OBJECTS") && sql.contains("ALL_TAB_COMMENTS")) {
|
||||||
|
return ownerRecordingStatement(List.of(Arrays.asList("USERS", "TABLE", "用户示例表")), boundOwners);
|
||||||
|
}
|
||||||
|
return metadataStatement(List.of());
|
||||||
|
}
|
||||||
|
if ("close".equals(name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ("isClosed".equals(name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return defaultValue(method.getReturnType());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PreparedStatement ownerRecordingStatement(List<List<Object>> rows, List<String> owners) {
|
||||||
|
return proxy(PreparedStatement.class, (method, args) -> {
|
||||||
|
String name = method.getName();
|
||||||
|
if ("executeQuery".equals(name)) {
|
||||||
|
return metadataResultSet(rows);
|
||||||
|
}
|
||||||
|
if (("setString".equals(name) || "setObject".equals(name)) && Integer.valueOf(1).equals(args[0])) {
|
||||||
|
owners.add(args[1] == null ? null : String.valueOf(args[1]));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ("setString".equals(name) || "setObject".equals(name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ("close".equals(name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return defaultValue(method.getReturnType());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private static PreparedStatement metadataStatement(List<List<Object>> rows) {
|
private static PreparedStatement metadataStatement(List<List<Object>> rows) {
|
||||||
return metadataStatement(rows, null);
|
return metadataStatement(rows, null);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user