Skip to content
Open
Show file tree
Hide file tree
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.oceanbase.odc.service.connection.model.ConnectionConfig;
import com.oceanbase.odc.service.connection.table.model.QueryTableParams;
import com.oceanbase.odc.service.connection.table.model.Table;
import com.oceanbase.odc.service.connection.util.ConnectionMapper;
import com.oceanbase.odc.service.db.DBMaterializedViewService;
import com.oceanbase.odc.service.db.schema.DBSchemaSyncService;
import com.oceanbase.odc.service.db.schema.syncer.DBSchemaSyncer;
Expand Down Expand Up @@ -130,7 +131,8 @@ public List<Table> list(@NonNull @Valid QueryTableParams params) throws SQLExcep
return Collections.emptyList();
}
Database database = databaseService.detail(params.getDatabaseId());
ConnectionConfig dataSource = database.getDataSource();
ConnectionConfig dataSource = ConnectionMapper.INSTANCE.clone(database.getDataSource());
OBConsoleDataSourceFactory.applyPostgresCatalogForDatabase(dataSource, database.getName());
OBConsoleDataSourceFactory factory = new OBConsoleDataSourceFactory(dataSource, true);
List<Table> tables = new ArrayList<>();
try (SingleConnectionDataSource ds = (SingleConnectionDataSource) factory.getDataSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.oceanbase.odc.service.connection.ConnectionService;
import com.oceanbase.odc.service.connection.database.model.Database;
import com.oceanbase.odc.service.connection.model.ConnectionConfig;
import com.oceanbase.odc.service.connection.util.ConnectionMapper;
import com.oceanbase.odc.service.db.schema.syncer.DBSchemaSyncer;
import com.oceanbase.odc.service.session.factory.OBConsoleDataSourceFactory;

Expand Down Expand Up @@ -85,7 +86,9 @@ public boolean sync(@NonNull Database database) throws InterruptedException, SQL
}
try {
ConnectionConfig config = connectionService.getForConnectionSkipPermissionCheck(dataSourceId);
OBConsoleDataSourceFactory factory = new OBConsoleDataSourceFactory(config, true);
ConnectionConfig connectConfig = ConnectionMapper.INSTANCE.clone(config);
OBConsoleDataSourceFactory.applyPostgresCatalogForDatabase(connectConfig, database.getName());
OBConsoleDataSourceFactory factory = new OBConsoleDataSourceFactory(connectConfig, true);
try (SingleConnectionDataSource dataSource = (SingleConnectionDataSource) factory.getDataSource();
Connection conn = dataSource.getConnection()) {
boolean success = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ public ConnectionSession create(@NotNull CreateSessionReq req) {
connection.setDefaultSchema(StringUtils.isNotBlank(catalogAndSchema[1])
? catalogAndSchema[1]
: schemaName);
} else if (DialectType.POSTGRESQL == connection.getDialectType()
|| DialectType.GAUSSDB == connection.getDialectType()) {
// Tree node is a catalog; reconnect JDBC URL to that database (PG cannot USE mid-session).
OBConsoleDataSourceFactory.applyPostgresCatalogForDatabase(connection, schemaName);
} else {
connection.setDefaultSchema(schemaName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,30 @@ public static String[] resolveSqlServerCatalogAndSchema(String catalogName, Stri
return new String[] {null, defaultSchema};
}

/**
* Bind a resource-tree database node to the PostgreSQL/GaussDB JDBC catalog.
* <p>
* PG cannot {@code USE} another database mid-connection; the catalog must be present in the JDBC
* URL. Tree nodes after multi-database sync are catalogs (from {@code pg_database}), so selecting
* node {@code wms_ftest} must reconnect to that catalog. Object metadata still lives under schemas
* (default {@code public}), which becomes {@code currentSchema}/{@code search_path}.
* </p>
*
* @param connectionConfig connection to mutate (caller should clone if shared)
* @param databaseName resource-tree database node name (catalog)
*/
public static void applyPostgresCatalogForDatabase(ConnectionConfig connectionConfig, String databaseName) {
if (connectionConfig == null || StringUtils.isBlank(databaseName)) {
return;
}
DialectType dialectType = connectionConfig.getDialectType();
if (DialectType.POSTGRESQL != dialectType && DialectType.GAUSSDB != dialectType) {
return;
}
connectionConfig.setCatalogName(databaseName);
connectionConfig.setDefaultSchema(OdcConstants.POSTGRESQL_DEFAULT_SCHEMA);
}

public static String getUsername(@NonNull ConnectionConfig connectionConfig) {
String username = getDbUser(connectionConfig);
if (DialectType.OB_ORACLE.equals(connectionConfig.getDialectType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public String getKillSessionSql(@NonNull String connectionId) {

@Override
public void switchSchema(Connection connection, String schemaName) throws SQLException {
// Align with PostgresSessionExtension: tree nodes are catalogs after multi-DB sync.
String currentDatabase = JdbcOperationsUtil.getJdbcOperations(connection)
.queryForObject("SELECT current_database()", String.class);
if (schemaName != null && schemaName.equals(currentDatabase)) {
schemaName = "public";
}
String sql = "SET search_path TO " + quoteIdentifier(schemaName);
JdbcOperationsUtil.getJdbcOperations(connection).execute(sql);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public class PostgresSessionExtension extends OBMySQLSessionExtension {
*/
@Override
public void switchSchema(Connection connection, String schemaName) throws SQLException {
// Resource-tree nodes are catalogs; map catalog name to default schema public.
String currentDatabase = getCurrentDatabase(connection);
if (Objects.equals(schemaName, currentDatabase)) {
schemaName = "public";
}
String currentSchema = getCurrentSchema(connection);
if (Objects.equals(currentSchema, schemaName)) {
return;
Expand Down