提交 c40e7a97 编写于 作者: S serge-rider

#3244 PG: fix search path update on active schema change


Former-commit-id: ce6f352c
上级 e4731220
......@@ -66,6 +66,7 @@ public class PostgreDataSource extends JDBCDataSource implements DBSObjectSelect
private String activeDatabaseName;
private String activeSchemaName;
private final List<String> searchPath = new ArrayList<>();
private final List<String> defaultSearchPath = new ArrayList<>();
private String activeUser;
public PostgreDataSource(DBRProgressMonitor monitor, DBPDataSourceContainer container)
......@@ -174,6 +175,8 @@ public class PostgreDataSource extends JDBCDataSource implements DBSObjectSelect
} catch (Exception e) {
log.debug(e);
}
defaultSearchPath.clear();
defaultSearchPath.addAll(searchPath);
// Read databases
databaseCache.getAllObjects(monitor, this);
......@@ -286,6 +289,12 @@ public class PostgreDataSource extends JDBCDataSource implements DBSObjectSelect
conConfig.setUrl(getContainer().getDriver().getDataSourceProvider().getConnectionURL(getContainer().getDriver(), conConfig));
getContainer().getRegistry().flushConfig();
try (JDBCSession session = getDefaultContext(false).openSession(monitor, DBCExecutionPurpose.UTIL, "Update object state")) {
determineDefaultObjects(session);
} catch (SQLException e) {
throw new DBException(e, this);
}
// Notify UI
if (oldDatabase != null) {
DBUtils.fireObjectSelect(oldDatabase, false);
......@@ -334,6 +343,10 @@ public class PostgreDataSource extends JDBCDataSource implements DBSObjectSelect
return searchPath;
}
List<String> getDefaultSearchPath() {
return defaultSearchPath;
}
public void setSearchPath(String path) {
searchPath.clear();
searchPath.add(path);
......
......@@ -48,12 +48,10 @@ import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.LongKeyMap;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* PostgreDatabase
......@@ -443,8 +441,25 @@ public class PostgreDatabase implements DBSInstance, DBSCatalog, DBPRefreshableO
}
void setSearchPath(DBRProgressMonitor monitor, PostgreSchema schema, JDBCExecutionContext context) throws DBCException {
// Construct search path from current search path but put default schema first
List<String> newSearchPath = new ArrayList<>(dataSource.getDefaultSearchPath());
{
String defSchemaName = DBUtils.getQuotedIdentifier(schema);
int schemaIndex = newSearchPath.indexOf(defSchemaName);
if (schemaIndex == 0) {
// Already default schema
} else {
if (schemaIndex > 0) {
// Remove from previous position
newSearchPath.remove(schemaIndex);
}
// Add it first
newSearchPath.add(0, defSchemaName);
}
}
try (JDBCSession session = context.openSession(monitor, DBCExecutionPurpose.UTIL, "Change search path")) {
JDBCUtils.executeSQL(session, "SET search_path = \"$user\"," + DBUtils.getQuotedIdentifier(schema));
String sp = newSearchPath.stream().collect(Collectors.joining(","));
JDBCUtils.executeSQL(session, "SET search_path = " + sp);
} catch (SQLException e) {
throw new DBCException("Error setting search path", e, dataSource);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册