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

#5172 Oracle: default schema

上级 4ca72137
...@@ -32,10 +32,7 @@ import org.jkiss.dbeaver.model.connection.DBPDriver; ...@@ -32,10 +32,7 @@ import org.jkiss.dbeaver.model.connection.DBPDriver;
import org.jkiss.dbeaver.model.exec.*; import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.exec.jdbc.*; import org.jkiss.dbeaver.model.exec.jdbc.*;
import org.jkiss.dbeaver.model.exec.plan.DBCQueryPlanner; import org.jkiss.dbeaver.model.exec.plan.DBCQueryPlanner;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCDataSource; import org.jkiss.dbeaver.model.impl.jdbc.*;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCDataSourceInfo;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCExecutionContext;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCObjectCache; import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCObjectCache;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCStructCache; import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCStructCache;
import org.jkiss.dbeaver.model.meta.Association; import org.jkiss.dbeaver.model.meta.Association;
...@@ -43,7 +40,10 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; ...@@ -43,7 +40,10 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLConstants; import org.jkiss.dbeaver.model.sql.SQLConstants;
import org.jkiss.dbeaver.model.sql.SQLQueryResult; import org.jkiss.dbeaver.model.sql.SQLQueryResult;
import org.jkiss.dbeaver.model.sql.SQLState; import org.jkiss.dbeaver.model.sql.SQLState;
import org.jkiss.dbeaver.model.struct.*; import org.jkiss.dbeaver.model.struct.DBSDataType;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.DBSObjectFilter;
import org.jkiss.dbeaver.model.struct.DBSStructureAssistant;
import org.jkiss.dbeaver.runtime.DBWorkbench; import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.utils.GeneralUtils; import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.BeanUtils; import org.jkiss.utils.BeanUtils;
...@@ -59,8 +59,7 @@ import java.util.regex.Pattern; ...@@ -59,8 +59,7 @@ import java.util.regex.Pattern;
/** /**
* GenericDataSource * GenericDataSource
*/ */
public class OracleDataSource extends JDBCDataSource public class OracleDataSource extends JDBCDataSource implements IAdaptable {
implements DBSObjectSelector, IAdaptable {
private static final Log log = Log.getLog(OracleDataSource.class); private static final Log log = Log.getLog(OracleDataSource.class);
final public SchemaCache schemaCache = new SchemaCache(); final public SchemaCache schemaCache = new SchemaCache();
...@@ -72,7 +71,6 @@ public class OracleDataSource extends JDBCDataSource ...@@ -72,7 +71,6 @@ public class OracleDataSource extends JDBCDataSource
private OracleOutputReader outputReader; private OracleOutputReader outputReader;
private OracleSchema publicSchema; private OracleSchema publicSchema;
private String activeSchemaName;
private boolean isAdmin; private boolean isAdmin;
private boolean isAdminVisible; private boolean isAdminVisible;
private String planTableName; private String planTableName;
...@@ -200,6 +198,11 @@ public class OracleDataSource extends JDBCDataSource ...@@ -200,6 +198,11 @@ public class OracleDataSource extends JDBCDataSource
} }
} }
@Override
protected JDBCExecutionContext createExecutionContext(JDBCRemoteInstance instance, String type) {
return new OracleExecutionContext(instance, type);
}
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBException { protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBException {
if (outputReader == null) { if (outputReader == null) {
outputReader = new OracleOutputReader(); outputReader = new OracleOutputReader();
...@@ -210,7 +213,9 @@ public class OracleDataSource extends JDBCDataSource ...@@ -210,7 +213,9 @@ public class OracleDataSource extends JDBCDataSource
context, context,
outputReader.isServerOutputEnabled()); outputReader.isServerOutputEnabled());
if (setActiveObject) { if (setActiveObject) {
setCurrentSchema(monitor, context, getDefaultObject()); ((OracleExecutionContext)context).setCurrentSchema(monitor, getDefaultSchema());
} else {
((OracleExecutionContext)context).refreshDefaults(monitor);
} }
{ {
...@@ -265,6 +270,10 @@ public class OracleDataSource extends JDBCDataSource ...@@ -265,6 +270,10 @@ public class OracleDataSource extends JDBCDataSource
} }
} }
public OracleSchema getDefaultSchema() {
return (OracleSchema) DBUtils.getDefaultContext(this, true).getContextDefaults().getDefaultSchema();
}
@Override @Override
protected String getConnectionUserName(@NotNull DBPConnectionConfiguration connectionInfo) { protected String getConnectionUserName(@NotNull DBPConnectionConfiguration connectionInfo) {
final String role = connectionInfo.getProviderProperty(OracleConstants.PROP_INTERNAL_LOGON); final String role = connectionInfo.getProviderProperty(OracleConstants.PROP_INTERNAL_LOGON);
...@@ -417,15 +426,6 @@ public class OracleDataSource extends JDBCDataSource ...@@ -417,15 +426,6 @@ public class OracleDataSource extends JDBCDataSource
isAdminVisible = CommonUtils.getBoolean(showAdmin, false); isAdminVisible = CommonUtils.getBoolean(showAdmin, false);
} }
} }
// Get active schema
this.activeSchemaName = OracleUtils.getCurrentSchema(session);
if (this.activeSchemaName != null) {
if (this.activeSchemaName.isEmpty()) {
this.activeSchemaName = null;
}
}
} catch (SQLException e) { } catch (SQLException e) {
//throw new DBException(e); //throw new DBException(e);
log.warn(e); log.warn(e);
...@@ -453,7 +453,6 @@ public class OracleDataSource extends JDBCDataSource ...@@ -453,7 +453,6 @@ public class OracleDataSource extends JDBCDataSource
this.userCache.clearCache(); this.userCache.clearCache();
this.profileCache.clearCache(); this.profileCache.clearCache();
this.roleCache.clearCache(); this.roleCache.clearCache();
this.activeSchemaName = null;
this.initialize(monitor); this.initialize(monitor);
...@@ -484,67 +483,6 @@ public class OracleDataSource extends JDBCDataSource ...@@ -484,67 +483,6 @@ public class OracleDataSource extends JDBCDataSource
} }
@Override
public boolean supportsDefaultChange() {
return true;
}
@Nullable
@Override
public OracleSchema getDefaultObject() {
return activeSchemaName == null ? null : schemaCache.getCachedObject(activeSchemaName);
}
@Override
public void setDefaultObject(@NotNull DBRProgressMonitor monitor, @NotNull DBSObject object)
throws DBException {
final OracleSchema oldSelectedEntity = getDefaultObject();
if (!(object instanceof OracleSchema)) {
throw new IllegalArgumentException("Invalid object type: " + object);
}
for (JDBCExecutionContext context : getDefaultInstance().getAllContexts()) {
setCurrentSchema(monitor, context, (OracleSchema) object);
}
activeSchemaName = object.getName();
// Send notifications
if (oldSelectedEntity != null) {
DBUtils.fireObjectSelect(oldSelectedEntity, false);
}
if (this.activeSchemaName != null) {
DBUtils.fireObjectSelect(object, true);
}
}
@Override
public boolean refreshDefaultObject(@NotNull DBCSession session) throws DBException {
try {
final String currentSchema = OracleUtils.getCurrentSchema((JDBCSession) session);
if (currentSchema != null && !CommonUtils.equalObjects(currentSchema, activeSchemaName)) {
final OracleSchema newSchema = schemaCache.getCachedObject(currentSchema);
if (newSchema != null) {
setDefaultObject(session.getProgressMonitor(), newSchema);
return true;
}
}
return false;
} catch (SQLException e) {
throw new DBException(e, this);
}
}
private void setCurrentSchema(DBRProgressMonitor monitor, JDBCExecutionContext executionContext, OracleSchema object) throws DBCException {
if (object == null) {
log.debug("Null current schema");
return;
}
try (JDBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.UTIL, "Set active schema")) {
OracleUtils.setCurrentSchema(session, object.getName());
} catch (SQLException e) {
throw new DBCException(e, this);
}
}
@Nullable @Nullable
@Override @Override
public <T> T getAdapter(Class<T> adapter) { public <T> T getAdapter(Class<T> adapter) {
...@@ -880,11 +818,6 @@ public class OracleDataSource extends JDBCDataSource ...@@ -880,11 +818,6 @@ public class OracleDataSource extends JDBCDataSource
@Override @Override
protected void invalidateObjects(DBRProgressMonitor monitor, OracleDataSource owner, Iterator<OracleSchema> objectIter) { protected void invalidateObjects(DBRProgressMonitor monitor, OracleDataSource owner, Iterator<OracleSchema> objectIter) {
setListOrderComparator(DBUtils.<OracleSchema>nameComparator()); setListOrderComparator(DBUtils.<OracleSchema>nameComparator());
// Add predefined types
if (!CommonUtils.isEmpty(owner.activeSchemaName) && getCachedObject(owner.activeSchemaName) == null) {
cacheObject(
new OracleSchema(owner, 100, owner.activeSchemaName));
}
} }
} }
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ext.oracle.model;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults;
import org.jkiss.dbeaver.model.exec.DBCExecutionPurpose;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCExecutionContext;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCRemoteInstance;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.rdb.DBSCatalog;
import java.sql.SQLException;
/**
* OracleExecutionContext
*/
public class OracleExecutionContext extends JDBCExecutionContext implements DBCExecutionContextDefaults<DBSCatalog, OracleSchema> {
private static final Log log = Log.getLog(OracleExecutionContext.class);
private String activeSchemaName;
OracleExecutionContext(@NotNull JDBCRemoteInstance instance, String purpose) {
super(instance, purpose);
}
@NotNull
@Override
public OracleDataSource getDataSource() {
return (OracleDataSource) super.getDataSource();
}
@Nullable
@Override
public DBCExecutionContextDefaults getContextDefaults() {
return this;
}
public String getActiveSchemaName() {
return activeSchemaName;
}
@Override
public DBSCatalog getDefaultCatalog() {
return null;
}
@Override
public OracleSchema getDefaultSchema() {
return activeSchemaName == null ? null : getDataSource().schemaCache.getCachedObject(activeSchemaName);
}
@Override
public boolean supportsCatalogChange() {
return false;
}
@Override
public boolean supportsSchemaChange() {
return true;
}
@Override
public void setDefaultCatalog(DBRProgressMonitor monitor, DBSCatalog catalog, OracleSchema schema) throws DBCException {
throw new DBCException("Not supported");
}
@Override
public void setDefaultSchema(DBRProgressMonitor monitor, OracleSchema schema) throws DBCException {
final OracleSchema oldSelectedEntity = getDefaultSchema();
if (schema == null || oldSelectedEntity == schema) {
return;
}
setCurrentSchema(monitor, schema);
activeSchemaName = schema.getName();
// Send notifications
if (oldSelectedEntity != null) {
DBUtils.fireObjectSelect(oldSelectedEntity, false);
}
DBUtils.fireObjectSelect(schema, true);
}
@Override
public boolean refreshDefaults(DBRProgressMonitor monitor) throws DBException {
// Check default active schema
try (JDBCSession session = openSession(monitor, DBCExecutionPurpose.META, "Query active schema")) {
// Get active schema
this.activeSchemaName = OracleUtils.getCurrentSchema(session);
if (this.activeSchemaName != null) {
if (this.activeSchemaName.isEmpty()) {
this.activeSchemaName = null;
}
}
} catch (Exception e) {
throw new DBCException(e, getDataSource());
}
return false;
}
void setCurrentSchema(DBRProgressMonitor monitor, OracleSchema object) throws DBCException {
if (object == null) {
log.debug("Null current schema");
return;
}
try (JDBCSession session = openSession(monitor, DBCExecutionPurpose.UTIL, "Set active schema")) {
OracleUtils.setCurrentSchema(session, object.getName());
this.activeSchemaName = object.getName();
} catch (SQLException e) {
throw new DBCException(e, getDataSource());
}
}
}
...@@ -34,7 +34,9 @@ import org.jkiss.utils.ArrayUtils; ...@@ -34,7 +34,9 @@ import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils; import org.jkiss.utils.CommonUtils;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** /**
* OracleStructureAssistant * OracleStructureAssistant
...@@ -121,7 +123,7 @@ public class OracleStructureAssistant implements DBSStructureAssistant ...@@ -121,7 +123,7 @@ public class OracleStructureAssistant implements DBSStructureAssistant
findConstraintsByMask(session, schema, objectNameMask, objectTypes, maxResults, objects); findConstraintsByMask(session, schema, objectNameMask, objectTypes, maxResults, objects);
} }
// Sort objects. Put ones in the current schema first // Sort objects. Put ones in the current schema first
final OracleSchema activeSchema = dataSource.getDefaultObject(); final OracleSchema activeSchema = dataSource.getDefaultSchema();
objects.sort((o1, o2) -> { objects.sort((o1, o2) -> {
if (CommonUtils.equalObjects(o1.getContainer(), o2.getContainer())) { if (CommonUtils.equalObjects(o1.getContainer(), o2.getContainer())) {
return o1.getName().compareTo(o2.getName()); return o1.getName().compareTo(o2.getName());
......
...@@ -200,14 +200,19 @@ public class OracleUtils { ...@@ -200,14 +200,19 @@ public class OracleUtils {
public static void addSchemaChangeActions(List<DBEPersistAction> actions, OracleSourceObject object) public static void addSchemaChangeActions(List<DBEPersistAction> actions, OracleSourceObject object)
{ {
OracleSchema schema = object.getSchema();
if (schema == null) {
return;
}
actions.add(0, new SQLDatabasePersistAction( actions.add(0, new SQLDatabasePersistAction(
"Set target schema", "Set target schema",
"ALTER SESSION SET CURRENT_SCHEMA=" + object.getSchema().getName(), "ALTER SESSION SET CURRENT_SCHEMA=" + schema.getName(),
DBEPersistAction.ActionType.INITIALIZER)); DBEPersistAction.ActionType.INITIALIZER));
if (object.getSchema() != object.getDataSource().getDefaultObject()) { OracleSchema defaultSchema = object.getDataSource().getDefaultSchema();
if (schema != defaultSchema) {
actions.add(new SQLDatabasePersistAction( actions.add(new SQLDatabasePersistAction(
"Set current schema", "Set current schema",
"ALTER SESSION SET CURRENT_SCHEMA=" + object.getDataSource().getDefaultObject().getName(), "ALTER SESSION SET CURRENT_SCHEMA=" + defaultSchema.getName(),
DBEPersistAction.ActionType.FINALIZER)); DBEPersistAction.ActionType.FINALIZER));
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册