From 0e67f09beb058c7af560230808e21cc46cf11d48 Mon Sep 17 00:00:00 2001 From: Serge Rider Date: Sat, 14 Dec 2019 15:34:22 +0300 Subject: [PATCH] #5172 MySQL model. Model refactoring --- .../dbeaver/ext/db2/model/DB2DataSource.java | 2 +- .../ext/exasol/model/ExasolDataSource.java | 2 +- .../ext/generic/model/GenericDataSource.java | 2 +- .../ext/mssql/model/SQLServerDataSource.java | 8 +- .../ext/mysql/edit/MySQLTriggerManager.java | 2 +- .../ext/mysql/model/MySQLDataSource.java | 92 +++--------- .../mysql/model/MySQLExecutionContext.java | 139 ++++++++++++++++++ .../mysql/model/MySQLStructureAssistant.java | 2 +- .../ext/oracle/model/OracleDataSource.java | 2 +- .../postgresql/model/PostgreDataSource.java | 8 +- .../model/impl/jdbc/JDBCDataSource.java | 2 +- .../model/impl/jdbc/JDBCExecutionContext.java | 2 + .../jkiss/dbeaver/ui/editors/EditorUtils.java | 2 +- 13 files changed, 177 insertions(+), 88 deletions(-) create mode 100644 plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/model/MySQLExecutionContext.java diff --git a/plugins/org.jkiss.dbeaver.ext.db2/src/org/jkiss/dbeaver/ext/db2/model/DB2DataSource.java b/plugins/org.jkiss.dbeaver.ext.db2/src/org/jkiss/dbeaver/ext/db2/model/DB2DataSource.java index 1a24334002..c8b883e989 100644 --- a/plugins/org.jkiss.dbeaver.ext.db2/src/org/jkiss/dbeaver/ext/db2/model/DB2DataSource.java +++ b/plugins/org.jkiss.dbeaver.ext.db2/src/org/jkiss/dbeaver/ext/db2/model/DB2DataSource.java @@ -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()); diff --git a/plugins/org.jkiss.dbeaver.ext.exasol/src/org/jkiss/dbeaver/ext/exasol/model/ExasolDataSource.java b/plugins/org.jkiss.dbeaver.ext.exasol/src/org/jkiss/dbeaver/ext/exasol/model/ExasolDataSource.java index 647e10f99d..50445dbd2e 100644 --- a/plugins/org.jkiss.dbeaver.ext.exasol/src/org/jkiss/dbeaver/ext/exasol/model/ExasolDataSource.java +++ b/plugins/org.jkiss.dbeaver.ext.exasol/src/org/jkiss/dbeaver/ext/exasol/model/ExasolDataSource.java @@ -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()); diff --git a/plugins/org.jkiss.dbeaver.ext.generic/src/org/jkiss/dbeaver/ext/generic/model/GenericDataSource.java b/plugins/org.jkiss.dbeaver.ext.generic/src/org/jkiss/dbeaver/ext/generic/model/GenericDataSource.java index 6a952c1dca..2d7ddc9ffa 100644 --- a/plugins/org.jkiss.dbeaver.ext.generic/src/org/jkiss/dbeaver/ext/generic/model/GenericDataSource.java +++ b/plugins/org.jkiss.dbeaver.ext.generic/src/org/jkiss/dbeaver/ext/generic/model/GenericDataSource.java @@ -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) { diff --git a/plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/model/SQLServerDataSource.java b/plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/model/SQLServerDataSource.java index 7caa0ceaed..06ea309e69 100644 --- a/plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/model/SQLServerDataSource.java +++ b/plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/model/SQLServerDataSource.java @@ -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); } } diff --git a/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/edit/MySQLTriggerManager.java b/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/edit/MySQLTriggerManager.java index 01590c1c6c..64efc74943 100644 --- a/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/edit/MySQLTriggerManager.java +++ b/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/edit/MySQLTriggerManager.java @@ -61,7 +61,7 @@ public class MySQLTriggerManager extends SQLTriggerManager 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); diff --git a/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/model/MySQLExecutionContext.java b/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/model/MySQLExecutionContext.java new file mode 100644 index 0000000000..6e3d21cb89 --- /dev/null +++ b/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/model/MySQLExecutionContext.java @@ -0,0 +1,139 @@ +/* + * 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 { + 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()); + } + } + +} diff --git a/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/model/MySQLStructureAssistant.java b/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/model/MySQLStructureAssistant.java index 495e16f710..1fc9d6654d 100644 --- a/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/model/MySQLStructureAssistant.java +++ b/plugins/org.jkiss.dbeaver.ext.mysql/src/org/jkiss/dbeaver/ext/mysql/model/MySQLStructureAssistant.java @@ -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); diff --git a/plugins/org.jkiss.dbeaver.ext.oracle/src/org/jkiss/dbeaver/ext/oracle/model/OracleDataSource.java b/plugins/org.jkiss.dbeaver.ext.oracle/src/org/jkiss/dbeaver/ext/oracle/model/OracleDataSource.java index 43ec54574e..8b24c1525f 100644 --- a/plugins/org.jkiss.dbeaver.ext.oracle/src/org/jkiss/dbeaver/ext/oracle/model/OracleDataSource.java +++ b/plugins/org.jkiss.dbeaver.ext.oracle/src/org/jkiss/dbeaver/ext/oracle/model/OracleDataSource.java @@ -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(); } 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 41eeb8e121..68ee82e59c 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 @@ -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); } } diff --git a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCDataSource.java b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCDataSource.java index 8c01d35a27..2638b5bacf 100644 --- a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCDataSource.java +++ b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCDataSource.java @@ -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 { } diff --git a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCExecutionContext.java b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCExecutionContext.java index b3ac9df8a9..838925ace5 100644 --- a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCExecutionContext.java +++ b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCExecutionContext.java @@ -138,6 +138,8 @@ public class JDBCExecutionContext extends AbstractExecutionContext