提交 0e67f09b 编写于 作者: S Serge Rider

#5172 MySQL model. Model refactoring

上级 4ddac586
......@@ -193,7 +193,7 @@ public class DB2DataSource extends JDBCDataSource implements DBSObjectSelector,
}
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context,
boolean setActiveObject) throws DBCException
boolean setActiveObject) throws DBException
{
if (setActiveObject) {
setCurrentSchema(monitor, context, getDefaultObject());
......
......@@ -366,7 +366,7 @@ public class ExasolDataSource extends JDBCDataSource
protected void initializeContextState(@NotNull DBRProgressMonitor monitor,
@NotNull JDBCExecutionContext context, boolean setActiveObject)
throws DBCException
throws DBException
{
if (setActiveObject) {
setCurrentSchema(monitor, context, getDefaultObject());
......
......@@ -181,7 +181,7 @@ public class GenericDataSource extends JDBCDataSource implements DBPTermProvider
return new GenericExecutionContext(instance, type);
}
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBCException {
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBException {
super.initializeContextState(monitor, context, setActiveObject);
boolean hasActiveObject = false;
if (setActiveObject) {
......
......@@ -132,7 +132,7 @@ public class SQLServerDataSource extends JDBCDataSource implements DBSInstanceCo
}
@Override
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBCException {
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBException {
super.initializeContextState(monitor, context, setActiveObject);
if (setActiveObject ) {
SQLServerDatabase defaultObject = getDefaultDatabase(monitor);
......@@ -140,11 +140,7 @@ public class SQLServerDataSource extends JDBCDataSource implements DBSInstanceCo
((SQLServerExecutionContext)context).setCurrentDatabase(monitor, defaultObject);
}
} else {
try {
((SQLServerExecutionContext)context).refreshDefaults(monitor);
} catch (DBException e) {
throw new DBCException("Error reading connection defaults");
}
((SQLServerExecutionContext)context).refreshDefaults(monitor);
}
}
......
......@@ -61,7 +61,7 @@ public class MySQLTriggerManager extends SQLTriggerManager<MySQLTrigger, MySQLTa
"DROP TRIGGER IF EXISTS " + trigger.getFullyQualifiedName(DBPEvaluationContext.DDL))
);
}
MySQLCatalog curCatalog = trigger.getCatalog().getDataSource().getDefaultObject();
MySQLCatalog curCatalog = trigger.getCatalog().getDataSource().getDefaultDatabase();
if (curCatalog != trigger.getCatalog()) {
actions.add(new SQLDatabasePersistAction("Set current schema ", "USE " + DBUtils.getQuotedIdentifier(trigger.getCatalog()), false)); //$NON-NLS-2$
}
......
......@@ -31,15 +31,15 @@ import org.jkiss.dbeaver.model.admin.sessions.DBAServerSessionManager;
import org.jkiss.dbeaver.model.app.DBACertificateStorage;
import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration;
import org.jkiss.dbeaver.model.connection.DBPDriver;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.DBCQueryTransformType;
import org.jkiss.dbeaver.model.exec.DBCQueryTransformer;
import org.jkiss.dbeaver.model.exec.jdbc.*;
import org.jkiss.dbeaver.model.exec.plan.DBCQueryPlanner;
import org.jkiss.dbeaver.model.gis.GisConstants;
import org.jkiss.dbeaver.model.gis.SpatialDataProvider;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCConstants;
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.*;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCBasicDataTypeCache;
import org.jkiss.dbeaver.model.impl.jdbc.cache.JDBCObjectCache;
import org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType;
......@@ -48,7 +48,10 @@ import org.jkiss.dbeaver.model.net.DBWHandlerConfiguration;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.sql.SQLHelpProvider;
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.utils.CommonUtils;
import org.jkiss.utils.IOUtils;
......@@ -64,7 +67,7 @@ import java.util.regex.Pattern;
/**
* GenericDataSource
*/
public class MySQLDataSource extends JDBCDataSource implements DBSObjectSelector {
public class MySQLDataSource extends JDBCDataSource {
private static final Log log = Log.getLog(MySQLDataSource.class);
private final JDBCBasicDataTypeCache<MySQLDataSource, JDBCDataType> dataTypeCache;
......@@ -193,15 +196,26 @@ public class MySQLDataSource extends JDBCDataSource implements DBSObjectSelector
}
}
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBCException {
@Override
protected JDBCExecutionContext createExecutionContext(JDBCRemoteInstance instance, String type) {
return new MySQLExecutionContext(instance, type);
}
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBException {
if (setActiveObject) {
MySQLCatalog object = getDefaultObject();
MySQLCatalog object = getDefaultDatabase();
if (object != null) {
useDatabase(monitor, context, object);
((MySQLExecutionContext)context).setCurrentDatabase(monitor, object);
}
} else {
((MySQLExecutionContext)context).refreshDefaults(monitor);
}
}
public MySQLCatalog getDefaultDatabase() {
return (MySQLCatalog) DBUtils.getDefaultContext(this, true).getContextDefaults().getDefaultCatalog();
}
public String[] getTableTypes() {
return MySQLConstants.TABLE_TYPES;
}
......@@ -348,64 +362,6 @@ public class MySQLDataSource extends JDBCDataSource implements DBSObjectSelector
}
@Override
public boolean supportsDefaultChange() {
return true;
}
@Override
public MySQLCatalog getDefaultObject() {
return CommonUtils.isEmpty(activeCatalogName) ? null : getCatalog(activeCatalogName);
}
@Override
public void setDefaultObject(@NotNull DBRProgressMonitor monitor, @NotNull DBSObject object)
throws DBException {
final MySQLCatalog oldSelectedEntity = getDefaultObject();
if (!(object instanceof MySQLCatalog)) {
throw new DBException("Invalid object type: " + object);
}
for (JDBCExecutionContext context : getDefaultInstance().getAllContexts()) {
useDatabase(monitor, context, (MySQLCatalog) object);
}
activeCatalogName = object.getName();
// Send notifications
if (oldSelectedEntity != null) {
DBUtils.fireObjectSelect(oldSelectedEntity, false);
}
if (this.activeCatalogName != null) {
DBUtils.fireObjectSelect(object, true);
}
}
@Override
public boolean refreshDefaultObject(@NotNull DBCSession session) throws DBException {
final String newCatalogName = MySQLUtils.determineCurrentDatabase((JDBCSession) session);
if (!CommonUtils.equalObjects(newCatalogName, activeCatalogName)) {
final MySQLCatalog newCatalog = getCatalog(newCatalogName);
if (newCatalog != null) {
setDefaultObject(session.getProgressMonitor(), newCatalog);
return true;
}
}
return false;
}
private void useDatabase(DBRProgressMonitor monitor, JDBCExecutionContext context, MySQLCatalog catalog) throws DBCException {
if (catalog == null) {
log.debug("Null current database");
return;
}
try (JDBCSession session = context.openSession(monitor, DBCExecutionPurpose.UTIL, "Set active catalog")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("use " + DBUtils.getQuotedIdentifier(catalog))) {
dbStat.execute();
}
} catch (SQLException e) {
throw new DBCException(e, this);
}
}
@Override
protected Connection openConnection(@NotNull DBRProgressMonitor monitor, @Nullable JDBCExecutionContext context, @NotNull String purpose) throws DBCException {
Connection mysqlConnection = super.openConnection(monitor, context, purpose);
......
/*
* 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.mysql.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.ext.mysql.MySQLUtils;
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.JDBCPreparedStatement;
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.DBSSchema;
import org.jkiss.utils.CommonUtils;
import java.sql.SQLException;
/**
* MySQLExecutionContext
*/
public class MySQLExecutionContext extends JDBCExecutionContext implements DBCExecutionContextDefaults<MySQLCatalog, DBSSchema> {
private static final Log log = Log.getLog(MySQLExecutionContext.class);
//private MySQLCatalog activeDatabase;
private String activeDatabaseName;
MySQLExecutionContext(@NotNull JDBCRemoteInstance instance, String purpose) {
super(instance, purpose);
}
@NotNull
@Override
public MySQLDataSource getDataSource() {
return (MySQLDataSource) super.getDataSource();
}
@Nullable
@Override
public DBCExecutionContextDefaults getContextDefaults() {
return this;
}
public String getActiveDatabaseName() {
return activeDatabaseName;
}
@Override
public MySQLCatalog getDefaultCatalog() {
return CommonUtils.isEmpty(activeDatabaseName) ? null : getDataSource().getCatalog(activeDatabaseName);
}
@Override
public DBSSchema getDefaultSchema() {
return null;
}
@Override
public boolean supportsCatalogChange() {
return true;
}
@Override
public boolean supportsSchemaChange() {
return false;
}
@Override
public void setDefaultCatalog(DBRProgressMonitor monitor, MySQLCatalog catalog, DBSSchema schema) throws DBCException {
if (activeDatabaseName != null && activeDatabaseName.equals(catalog.getName())) {
return;
}
final MySQLCatalog oldActiveDatabase = getDefaultCatalog();
if (!setCurrentDatabase(monitor, catalog)) {
return;
}
activeDatabaseName = catalog.getName();
// Send notifications
if (oldActiveDatabase != null) {
DBUtils.fireObjectSelect(oldActiveDatabase, false);
}
DBUtils.fireObjectSelect(catalog, true);
}
@Override
public void setDefaultSchema(DBRProgressMonitor monitor, DBSSchema schema) throws DBCException {
throw new DBCException("Not supported");
}
@Override
public boolean refreshDefaults(DBRProgressMonitor monitor) throws DBException {
// Check default active schema
try (JDBCSession session = openSession(monitor, DBCExecutionPurpose.META, "Query active database")) {
activeDatabaseName = MySQLUtils.determineCurrentDatabase(session);
} catch (DBException e) {
throw new DBCException(e, getDataSource());
}
return false;
}
boolean setCurrentDatabase(DBRProgressMonitor monitor, MySQLCatalog object) throws DBCException {
if (object == null) {
log.debug("Null current database");
return false;
}
try (JDBCSession session = openSession(monitor, DBCExecutionPurpose.UTIL, "Set active catalog")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("use " + DBUtils.getQuotedIdentifier(object))) {
dbStat.execute();
}
this.activeDatabaseName = object.getName();
return true;
} catch (SQLException e) {
throw new DBCException(e, getDataSource());
}
}
}
......@@ -91,7 +91,7 @@ public class MySQLStructureAssistant extends JDBCStructureAssistant
{
MySQLCatalog catalog = parentObject instanceof MySQLCatalog ? (MySQLCatalog) parentObject : null;
if (catalog == null && !globalSearch) {
catalog = dataSource.getDefaultObject();
catalog = dataSource.getDefaultDatabase();
}
if (objectType == RelationalObjectType.TYPE_TABLE) {
findTablesByMask(session, catalog, objectNameMask, maxResults, references);
......
......@@ -200,7 +200,7 @@ public class OracleDataSource extends JDBCDataSource
}
}
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBCException {
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBException {
if (outputReader == null) {
outputReader = new OracleOutputReader();
}
......
......@@ -194,18 +194,14 @@ public class PostgreDataSource extends JDBCDataSource implements DBSInstanceCont
return new PostgreExecutionContext((PostgreDatabase) instance, type);
}
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBCException {
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBException {
if (setActiveObject) {
final PostgreSchema activeSchema = getDefaultInstance().getActiveSchema();
if (activeSchema != null) {
((PostgreExecutionContext)context).setDefaultSchema(monitor, activeSchema);
}
} else {
try {
((PostgreExecutionContext)context).refreshDefaults(monitor);
} catch (DBException e) {
throw new DBCException("Error reading connection defaults");
}
((PostgreExecutionContext)context).refreshDefaults(monitor);
}
}
......
......@@ -281,7 +281,7 @@ public abstract class JDBCDataSource
}
*/
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBCException {
protected void initializeContextState(@NotNull DBRProgressMonitor monitor, @NotNull JDBCExecutionContext context, boolean setActiveObject) throws DBException {
}
......
......@@ -138,6 +138,8 @@ public class JDBCExecutionContext extends AbstractExecutionContext<JDBCDataSourc
this.dataSource.initializeContextState(monitor, this, forceActiveObject && !connectionReadOnly);
} catch (DBCException e) {
log.error("Error while initializing context state", e);
} catch (DBException e) {
e.printStackTrace();
}
try {
......
......@@ -177,7 +177,7 @@ public class EditorUtils {
if (dataSourceId != null) {
DBPDataSourceContainer dataSource = projectMeta.getDataSourceRegistry().getDataSource(dataSourceId.toString());
if (dataSource == null) {
log.error("Datasource " + dataSourceId + " not found in project " + projectMeta.getName());
log.debug("Datasource " + dataSourceId + " not found in project " + projectMeta.getName());
}
return dataSource;
} else {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册