Skip to content

Commit

Permalink
#5172 PG: context-specific active schema. Model refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-rider committed Dec 13, 2019
1 parent 6cccab5 commit 1cf3f78
Show file tree
Hide file tree
Showing 14 changed files with 432 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public class PostgreDebugSession extends DBGJDBCSession {
JDBCDataSource src = this.controllerConnection.getDataSource();
if (src instanceof PostgreDataSource) {
PostgreDataSource pgSrc = (PostgreDataSource) src;
log.debug(String.format("Active user %s", instance.getActiveUser()));
log.debug(String.format("Active schema %s", instance.getActiveSchemaName()));
log.debug(String.format("Active user %s", instance.getDefaultContext().getActiveUser()));
log.debug(String.format("Active schema %s", instance.getDefaultContext().getDefaultSchema()));
if (pgSrc.getInfo() instanceof JDBCDataSourceInfo) {
JDBCDataSourceInfo JDBCinfo = (JDBCDataSourceInfo) pgSrc.getInfo();
log.debug("------------DATABASE DRIVER INFO---------------");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,6 @@ public static boolean isGISDataType(String typeName) {
}

public static String getRealSchemaName(PostgreDatabase database, String name) {
return name.replace("$user", database.getActiveUser());
return name.replace("$user", database.getDefaultContext().getActiveUser());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
/**
* PostgreDataSource
*/
public class PostgreDataSource extends JDBCDataSource implements DBSObjectSelector, DBSInstanceContainer, IAdaptable {
public class PostgreDataSource extends JDBCDataSource implements DBSInstanceContainer, IAdaptable {

private static final Log log = Log.getLog(PostgreDataSource.class);

Expand Down Expand Up @@ -189,24 +189,22 @@ private void initSSL(Map<String, String> props, DBWHandlerConfiguration sslConfi
props.put("sslpasswordcallback", DefaultCallbackHandler.class.getName());
}

@Override
protected PostgreExecutionContext createExecutionContext(JDBCRemoteInstance instance, String type) {
return new PostgreExecutionContext((PostgreDatabase) instance, type);
}

protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBCException {
if (setActiveObject) {
PostgreDatabase activeDatabase = getDefaultObject();
if (activeDatabase != null) {
final PostgreSchema activeSchema = activeDatabase.getDefaultObject();
if (activeSchema != null) {

// Check default active schema
String curDefSchema;
try (JDBCSession session = context.openSession(monitor, DBCExecutionPurpose.META, "Get context active schema")) {
curDefSchema = JDBCUtils.queryString(session, "SELECT current_schema()");
} catch (SQLException e) {
throw new DBCException(e, getDataSource());
}
if (curDefSchema == null || !curDefSchema.equals(activeSchema.getName())) {
activeDatabase.setSearchPath(monitor, activeSchema, context);
}
}
final PostgreSchema activeSchema = getDefaultInstance().getActiveSchema();
if (activeSchema != null) {
((PostgreExecutionContext)context).setDefaultSchema(monitor, activeSchema);
}
} else {
try {
((PostgreExecutionContext)context).refreshDefaults(monitor);
} catch (DBException e) {
log.debug("Error reading connection defaults");
}
}
}
Expand Down Expand Up @@ -287,60 +285,6 @@ public void cacheStructure(@NotNull DBRProgressMonitor monitor, int scope)
databaseCache.getAllObjects(monitor, this);
}

@Override
public boolean supportsDefaultChange()
{
return true;
}

////////////////////////////////////////////////////
// Default schema and search path

@Override
public PostgreDatabase getDefaultObject()
{
return getDefaultInstance();
}

@Override
public void setDefaultObject(@NotNull DBRProgressMonitor monitor, @NotNull DBSObject object)
throws DBException
{
final PostgreDatabase oldDatabase = getDefaultObject();
if (!(object instanceof PostgreDatabase)) {
throw new IllegalArgumentException("Invalid object type: " + object);
}
final PostgreDatabase newDatabase = (PostgreDatabase) object;
if (oldDatabase == newDatabase) {
// The same
return;
}

PostgreDatabase oldActiveInstance = getDefaultInstance();

PostgreDatabase newActiveInstance = (PostgreDatabase) object;
newActiveInstance.initializeMetaContext(monitor);
newActiveInstance.cacheDataTypes(monitor, false);

activeDatabaseName = newActiveInstance.getName();

// Notify UI
if (oldDatabase != null) {
DBUtils.fireObjectSelect(oldDatabase, false);
}
DBUtils.fireObjectSelect(newDatabase, true);

if (oldActiveInstance != newActiveInstance) {
// Close all database connections but meta (we need it to browse metadata like navigator tree)
oldActiveInstance.shutdown(monitor, true);
}
}

@Override
public boolean refreshDefaultObject(@NotNull DBCSession session) throws DBException {
return true;
}

////////////////////////////////////////////
// Connections

Expand Down Expand Up @@ -454,6 +398,41 @@ public List<PostgreDatabase> getAvailableInstances() {
return databaseCache.getCachedObjects();
}

public void setDefaultInstance(@NotNull DBRProgressMonitor monitor, @NotNull PostgreDatabase newDatabase, PostgreSchema schema)
throws DBException
{
final PostgreDatabase oldDatabase = getDefaultInstance();
if (oldDatabase == newDatabase) {
// The same
return;
}

newDatabase.initializeMetaContext(monitor);
newDatabase.cacheDataTypes(monitor, false);

PostgreSchema oldDefaultSchema = null;
if (schema != null) {
oldDefaultSchema = newDatabase.getDefaultContext().getDefaultSchema();
newDatabase.getDefaultContext().setDefaultSchema(monitor, schema, false);
}

activeDatabaseName = newDatabase.getName();

// Notify UI
DBUtils.fireObjectSelect(oldDatabase, false);
DBUtils.fireObjectSelect(newDatabase, true);

if (schema != null && schema != oldDefaultSchema) {
if (oldDefaultSchema != null) {
DBUtils.fireObjectSelect(oldDefaultSchema, false);
}
DBUtils.fireObjectSelect(schema, true);
}

// Close all database connections but meta (we need it to browse metadata like navigator tree)
oldDatabase.shutdown(monitor, true);
}

public List<String> getTemplateDatabases(DBRProgressMonitor monitor) throws DBException {
try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load template databases")) {
try (PreparedStatement dbStat = session.prepareStatement("SELECT db.datname FROM pg_catalog.pg_database db WHERE datistemplate")) {
Expand Down
Loading

0 comments on commit 1cf3f78

Please sign in to comment.