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

#5172 Exasol: default schema

上级 0397ce1b
......@@ -46,7 +46,7 @@ import org.jkiss.dbeaver.model.exec.plan.DBCPlanStyle;
import org.jkiss.dbeaver.model.exec.plan.DBCQueryPlanner;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCDataSource;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCExecutionContext;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCRemoteInstance;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCObjectSimpleCache;
import org.jkiss.dbeaver.model.impl.sql.QueryTransformerLimit;
import org.jkiss.dbeaver.model.meta.Association;
......@@ -54,27 +54,20 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSDataType;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.DBSObjectSelector;
import org.jkiss.dbeaver.model.struct.DBSStructureAssistant;
import org.jkiss.dbeaver.model.struct.cache.DBSObjectCache;
import org.jkiss.utils.CommonUtils;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExasolDataSource extends JDBCDataSource
implements DBSObjectSelector, DBCQueryPlanner, IAdaptable {
public class ExasolDataSource extends JDBCDataSource implements DBCQueryPlanner, IAdaptable {
private static final Log LOG = Log.getLog(ExasolDataSource.class);
private static final String GET_CURRENT_SCHEMA = "SELECT CURRENT_SCHEMA";
private static final String SET_CURRENT_SCHEMA = "OPEN SCHEMA \"%s\"";
private static final String GET_CURRENT_SESSION = "SELECT CURRENT_SESSION";
private DBSObjectCache<ExasolDataSource, ExasolSchema> schemaCache;
private DBSObjectCache<ExasolDataSource, ExasolVirtualSchema> virtualSchemaCache;
......@@ -97,9 +90,6 @@ public class ExasolDataSource extends JDBCDataSource
private Properties addMetaProps = new Properties();
private int driverMajorVersion = 5;
private String activeSchemaName;
// -----------------------
// Constructors
......@@ -121,15 +111,10 @@ public class ExasolDataSource extends JDBCDataSource
{
super.initialize(monitor);
try (JDBCSession session = DBUtils.openMetaSession(monitor, this,
"Load data source meta info")) {
// First try to get active schema from special register 'CURRENT
// SCHEMA'
this.activeSchemaName = determineActiveSchema(session);
this.exasolCurrentUserPrivileges = new ExasolCurrentUserPrivileges(
monitor, session, this);
try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load data source meta info")) {
this.exasolCurrentUserPrivileges = new ExasolCurrentUserPrivileges(monitor, session, this);
this.driverMajorVersion = session.getMetaData().getDriverMajorVersion();
} catch (SQLException e) {
......@@ -357,41 +342,25 @@ public class ExasolDataSource extends JDBCDataSource
positions.add(pos);
}
if (!positions.isEmpty()) {
return positions.toArray(new ErrorPosition[positions.size()]);
return positions.toArray(new ErrorPosition[0]);
}
}
return null;
}
@Override
protected JDBCExecutionContext createExecutionContext(JDBCRemoteInstance instance, String type) {
return new ExasolExecutionContext(instance, type);
}
protected void initializeContextState(@NotNull DBRProgressMonitor monitor,
@NotNull JDBCExecutionContext context, boolean setActiveObject)
@NotNull JDBCExecutionContext context, boolean setActiveObject)
throws DBException
{
if (setActiveObject) {
setCurrentSchema(monitor, context, getDefaultObject());
}
}
private String determineActiveSchema(JDBCSession session)
throws SQLException
{
// First try to get active schema from special register 'CURRENT SCHEMA'
String defSchema = JDBCUtils.queryString(session, GET_CURRENT_SCHEMA);
if (defSchema == null) {
return null;
}
return defSchema.trim();
}
public BigDecimal getCurrentSessionId(JDBCSession session)
throws DBException
{
try {
return (BigDecimal) JDBCUtils.queryObject(session, GET_CURRENT_SESSION);
} catch (SQLException e) {
throw new DBCException(e, this);
((ExasolExecutionContext)context).setCurrentSchema(monitor, getDefaultSchema());
} else {
((ExasolExecutionContext)context).refreshDefaults(monitor);
}
}
......@@ -490,24 +459,15 @@ public class ExasolDataSource extends JDBCDataSource
// --------------------------
@Override
public boolean supportsDefaultChange()
{
return true;
}
@Override
public Class<? extends ExasolSchema> getChildType(
@NotNull DBRProgressMonitor monitor) throws DBException
public Class<? extends ExasolSchema> getChildType(@NotNull DBRProgressMonitor monitor) throws DBException
{
return ExasolSchema.class;
}
@Override
public Collection<ExasolSchema> getChildren(
@NotNull DBRProgressMonitor monitor) throws DBException
public Collection<ExasolSchema> getChildren(@NotNull DBRProgressMonitor monitor) throws DBException
{
Collection<ExasolSchema> totalList = getSchemas(monitor);
return totalList;
return getSchemas(monitor);
}
@Override
......@@ -519,74 +479,8 @@ public class ExasolDataSource extends JDBCDataSource
return getSchema(monitor, childName);
}
@Override
public ExasolSchema getDefaultObject()
{
return activeSchemaName == null ? null : schemaCache.getCachedObject(activeSchemaName);
}
@Override
public void setDefaultObject(@NotNull DBRProgressMonitor monitor,
@NotNull DBSObject object) throws DBException
{
final ExasolSchema oldSelectedEntity = getDefaultObject();
if (!(object instanceof ExasolSchema)) {
throw new IllegalArgumentException(
"Invalid object type: " + object);
}
for (JDBCExecutionContext context : getDefaultInstance().getAllContexts()) {
setCurrentSchema(monitor, context, (ExasolSchema) 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 newSchemaName = determineActiveSchema(
(JDBCSession) session);
if (!CommonUtils.equalObjects(newSchemaName, activeSchemaName)) {
final ExasolSchema newSchema = schemaCache
.getCachedObject(newSchemaName);
if (newSchema != null) {
setDefaultObject(session.getProgressMonitor(), newSchema);
return true;
}
}
return false;
} catch (Exception e) {
throw new DBException(e, this);
}
}
private void setCurrentSchema(DBRProgressMonitor monitor,
JDBCExecutionContext executionContext, ExasolSchema object)
throws DBCException
{
if (object == null) {
LOG.debug("Null current schema");
return;
}
try (JDBCSession session = executionContext.openSession(monitor,
DBCExecutionPurpose.UTIL, "Set active schema")) {
JDBCUtils.executeSQL(session,
String.format(SET_CURRENT_SCHEMA, object.getName()));
} catch (SQLException e) {
throw new DBCException(e, this);
}
public ExasolSchema getDefaultSchema() {
return (ExasolSchema) DBUtils.getDefaultContext(this, true).getContextDefaults().getDefaultSchema();
}
// --------------
......
/*
* 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.exasol.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.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.rdb.DBSCatalog;
import java.sql.SQLException;
/**
* ExasolExecutionContext
*/
public class ExasolExecutionContext extends JDBCExecutionContext implements DBCExecutionContextDefaults<DBSCatalog, ExasolSchema> {
private static final Log log = Log.getLog(ExasolExecutionContext.class);
private static final String GET_CURRENT_SCHEMA = "SELECT CURRENT_SCHEMA";
private static final String SET_CURRENT_SCHEMA = "OPEN SCHEMA \"%s\"";
private String activeSchemaName;
ExasolExecutionContext(@NotNull JDBCRemoteInstance instance, String purpose) {
super(instance, purpose);
}
@NotNull
@Override
public ExasolDataSource getDataSource() {
return (ExasolDataSource) super.getDataSource();
}
@Nullable
@Override
public DBCExecutionContextDefaults getContextDefaults() {
return this;
}
public String getActiveSchemaName() {
return activeSchemaName;
}
@Override
public DBSCatalog getDefaultCatalog() {
return null;
}
@Override
public ExasolSchema getDefaultSchema() {
return activeSchemaName == null ? null : getDataSource().getSchemaCache().getCachedObject(activeSchemaName);
}
@Override
public boolean supportsCatalogChange() {
return false;
}
@Override
public boolean supportsSchemaChange() {
return true;
}
@Override
public void setDefaultCatalog(DBRProgressMonitor monitor, DBSCatalog catalog, ExasolSchema schema) throws DBCException {
throw new DBCException("Not supported");
}
@Override
public void setDefaultSchema(DBRProgressMonitor monitor, ExasolSchema schema) throws DBCException {
final ExasolSchema 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 = determineActiveSchema(session);
} catch (Exception e) {
throw new DBCException(e, getDataSource());
}
return false;
}
void setCurrentSchema(DBRProgressMonitor monitor, ExasolSchema object) throws DBCException {
if (object == null) {
log.debug("Null current schema");
return;
}
try (JDBCSession session = openSession(monitor, DBCExecutionPurpose.UTIL, "Set active schema")) {
JDBCUtils.executeSQL(session, String.format(SET_CURRENT_SCHEMA, object.getName()));
this.activeSchemaName = object.getName();
} catch (SQLException e) {
throw new DBCException(e, getDataSource());
}
}
private String determineActiveSchema(JDBCSession session)
throws SQLException
{
// First try to get active schema from special register 'CURRENT SCHEMA'
String defSchema = JDBCUtils.queryString(session, GET_CURRENT_SCHEMA);
if (defSchema == null) {
return null;
}
return defSchema.trim();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册