From c40e7a9793939ed0575d3a12b02d2b3bb9638b8a Mon Sep 17 00:00:00 2001 From: serge-rider Date: Fri, 13 Apr 2018 22:44:20 +0300 Subject: [PATCH] #3244 PG: fix search path update on active schema change Former-commit-id: ce6f352ca2d233e60bf7c7ea6437a894cc5f431e --- .../postgresql/model/PostgreDataSource.java | 13 ++++++++++ .../ext/postgresql/model/PostgreDatabase.java | 25 +++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreDataSource.java b/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreDataSource.java index dd97c1537d..5b06f5ae45 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreDataSource.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreDataSource.java @@ -66,6 +66,7 @@ public class PostgreDataSource extends JDBCDataSource implements DBSObjectSelect private String activeDatabaseName; private String activeSchemaName; private final List searchPath = new ArrayList<>(); + private final List 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 getDefaultSearchPath() { + return defaultSearchPath; + } + public void setSearchPath(String path) { searchPath.clear(); searchPath.add(path); diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreDatabase.java b/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreDatabase.java index a1f9a4d413..c5cde6cf4c 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreDatabase.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreDatabase.java @@ -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 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); } -- GitLab