提交 7bc36ec2 编写于 作者: S serge-rider

#2051 Command context model refactoring. DB lazy instance. PostgreSQL database altering.


Former-commit-id: 0dbb99f2
上级 0c3ea4e8
......@@ -59,7 +59,7 @@ public class DB2SequenceManager extends SQLObjectEditor<DB2Sequence, DB2Schema>
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command) throws DBException
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options) throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
throw new DBException("Sequence name cannot be empty");
......
......@@ -47,7 +47,7 @@ public class ExasolFunctionManager extends SQLObjectEditor<ExasolFunction, Exaso
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException {
if (CommonUtils.isEmpty(command.getObject().getName()))
{
......
......@@ -47,7 +47,7 @@ public class ExasolScriptManager extends SQLObjectEditor<ExasolScript, ExasolSch
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException {
if (CommonUtils.isEmpty(command.getObject().getName()))
{
......
......@@ -47,7 +47,7 @@ public class ExasolViewManager
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
ExasolTableBase object = command.getObject();
......
......@@ -59,7 +59,7 @@ public class MySQLCommandChangeUser extends DBECommandComposite<MySQLUser, UserP
}
@Override
public void validateCommand() throws DBException
public void validateCommand(Map<String, Object> options) throws DBException
{
String passValue = CommonUtils.toString(getProperty(UserPropertyHandler.PASSWORD));
String confirmValue = CommonUtils.toString(getProperty(UserPropertyHandler.PASSWORD_CONFIRM));
......
......@@ -55,7 +55,7 @@ public class MySQLProcedureManager extends SQLObjectEditor<MySQLProcedure, MySQL
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
......
......@@ -50,7 +50,7 @@ public class MySQLViewManager extends MySQLTableManager {
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
MySQLTableBase object = command.getObject();
......
......@@ -49,7 +49,7 @@ public class OracleMaterializedViewManager extends SQLObjectEditor<OracleMateria
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
......
......@@ -45,7 +45,7 @@ public class OracleQueueManager extends SQLObjectEditor<OracleQueue, OracleSchem
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command) throws DBException
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options) throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
throw new DBException("Queue name cannot be empty");
......
......@@ -32,7 +32,7 @@ public class OracleSequenceManager extends SQLObjectEditor<OracleSequence, Oracl
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command) throws DBException
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options) throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
throw new DBException("Sequence name cannot be empty");
......
......@@ -48,7 +48,7 @@ public class OracleViewManager extends SQLObjectEditor<OracleView, OracleSchema>
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
......
......@@ -23,6 +23,7 @@ import org.jkiss.dbeaver.ext.postgresql.PostgreMessages;
import org.jkiss.dbeaver.ext.postgresql.model.PostgreDataSource;
import org.jkiss.dbeaver.ext.postgresql.model.PostgreDatabase;
import org.jkiss.dbeaver.ext.postgresql.ui.PostgreCreateDatabaseDialog;
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.edit.DBECommandContext;
......@@ -134,6 +135,38 @@ public class PostgreDatabaseManager extends SQLObjectEditor<PostgreDatabase, Pos
);
}
@Override
protected void addObjectModifyActions(DBRProgressMonitor monitor, List<DBEPersistAction> actionList, ObjectChangeCommand command, Map<String, Object> options)
{
if (command.getProperties().size() > 1 || command.getProperty(DBConstants.PROP_ID_DESCRIPTION) == null) {
try {
generateAlterActions(monitor, actionList, command);
} catch (DBException e) {
log.error(e);
}
}
}
private void generateAlterActions(DBRProgressMonitor monitor, List<DBEPersistAction> actionList, ObjectChangeCommand command) throws DBException {
final PostgreDatabase database = command.getObject();
final String alterPrefix = "ALTER DATABASE " + DBUtils.getQuotedIdentifier(command.getObject()) + " ";
if (command.hasProperty("defaultTablespace")) {
actionList.add(new SQLDatabasePersistAction(alterPrefix + "SET TABLESPACE " + DBUtils.getQuotedIdentifier(database.getDefaultTablespace(monitor))));
}
if (command.hasProperty("defaultEncoding")) {
actionList.add(new SQLDatabasePersistAction(alterPrefix + "SET ENCODING " + DBUtils.getQuotedIdentifier(database.getDefaultEncoding(monitor))));
}
if (command.hasProperty("dba")) {
actionList.add(new SQLDatabasePersistAction(alterPrefix + "OWNER TO " + DBUtils.getQuotedIdentifier(database.getDBA(monitor))));
}
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options) throws DBException {
super.validateObjectProperties(command, options);
options.put(DBECommandContext.OPTION_AVOID_TRANSACTIONS, true);
}
private static class DeleteDatabaseAction extends SQLDatabasePersistActionAtomic {
private final PostgreDatabase database;
......@@ -169,5 +202,6 @@ public class PostgreDatabaseManager extends SQLObjectEditor<PostgreDatabase, Pos
}
}
}
}
......@@ -63,7 +63,7 @@ public class PostgreProcedureManager extends SQLObjectEditor<PostgreProcedure, P
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
......
......@@ -58,7 +58,7 @@ public class PostgreSequenceManager extends SQLObjectEditor<PostgreTableBase, Po
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
......
......@@ -55,7 +55,7 @@ public class PostgreViewManager extends PostgreTableManagerBase implements DBEOb
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
PostgreTableBase object = command.getObject();
......
......@@ -146,7 +146,7 @@ public class PostgreDataSource extends JDBCDataSource implements DBSObjectSelect
}
databaseCache.setCache(dbList);
// Initiate default context
getDefaultInstance().checkDatabaseConnection(monitor);
getDefaultInstance().checkInstanceConnection(monitor);
}
@Override
......
......@@ -39,12 +39,11 @@ 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.JDBCObjectLookupCache;
import org.jkiss.dbeaver.model.meta.Association;
import org.jkiss.dbeaver.model.meta.IPropertyValueListProvider;
import org.jkiss.dbeaver.model.meta.Property;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.DBSObjectFilter;
import org.jkiss.dbeaver.model.struct.DBSObjectSelector;
import org.jkiss.dbeaver.model.struct.DBSObjectState;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.model.struct.rdb.DBSCatalog;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.LongKeyMap;
......@@ -59,7 +58,15 @@ import java.util.List;
/**
* PostgreDatabase
*/
public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> implements DBSCatalog, DBPRefreshableObject, DBPStatefulObject, DBPNamedObject2, PostgreObject, DBSObjectSelector {
public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource>
implements
DBSCatalog,
DBPRefreshableObject,
DBPStatefulObject,
DBPNamedObject2,
PostgreObject,
DBSObjectSelector,
DBSInstanceLazy {
private static final Log log = Log.getLog(PostgreDatabase.class);
......@@ -118,7 +125,7 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
// We need to set name first
this.name = databaseName;
this.initCaches();
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
readDatabaseInfo(monitor);
}
......@@ -165,7 +172,8 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
return initialEncoding;
}
void checkDatabaseConnection(DBRProgressMonitor monitor) throws DBException {
@Override
public void checkInstanceConnection(DBRProgressMonitor monitor) throws DBException {
if (executionContext == null) {
initializeMainContext(monitor);
initializeMetaContext(monitor);
......@@ -250,12 +258,16 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
}
@Nullable
@Property(order = 3)
@Property(editable = true, updatable = true, order = 3, listProvider = RoleListProvider.class)
public PostgreRole getDBA(DBRProgressMonitor monitor) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return getRoleById(monitor, ownerId);
}
public void setDBA(PostgreRole owner) {
this.ownerId = owner.getObjectId();
}
@Nullable
public PostgreRole getRoleById(DBRProgressMonitor monitor, long roleId) throws DBException {
if (!getDataSource().getServerType().supportsRoles()) {
......@@ -274,15 +286,19 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
return roleCache.getObject(monitor, owner, roleName);
}
@Property(order = 5)
@Property(editable = false, updatable = false, order = 5/*, listProvider = CharsetListProvider.class*/)
public PostgreCharset getDefaultEncoding(DBRProgressMonitor monitor) throws DBException {
if (!getDataSource().getServerType().supportsEncodings()) {
return null;
}
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return PostgreUtils.getObjectById(monitor, encodingCache, this, encodingId);
}
public void setDefaultEncoding(PostgreCharset charset) throws DBException {
this.encodingId = charset.getObjectId();
}
@Property(order = 10)
public String getCollate() {
return collate;
......@@ -313,31 +329,31 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
@Association
public Collection<PostgreRole> getAuthIds(DBRProgressMonitor monitor) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return roleCache.getAllObjects(monitor, this);
}
@Association
public Collection<PostgreAccessMethod> getAccessMethods(DBRProgressMonitor monitor) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return accessMethodCache.getAllObjects(monitor, this);
}
@Association
public Collection<PostgreForeignDataWrapper> getForeignDataWrappers(DBRProgressMonitor monitor) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return foreignDataWrapperCache.getAllObjects(monitor, this);
}
@Association
public Collection<PostgreForeignServer> getForeignServers(DBRProgressMonitor monitor) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return foreignServerCache.getAllObjects(monitor, this);
}
@Association
public Collection<PostgreLanguage> getLanguages(DBRProgressMonitor monitor) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return languageCache.getAllObjects(monitor, this);
}
......@@ -346,7 +362,7 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
if (!getDataSource().getServerType().supportsEncodings()) {
return null;
}
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return encodingCache.getAllObjects(monitor, this);
}
......@@ -355,18 +371,22 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
@Association
public Collection<PostgreTablespace> getTablespaces(DBRProgressMonitor monitor) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return tablespaceCache.getAllObjects(monitor, this);
}
@Property(order = 4)
@Property(editable = true, updatable = true, order = 4, listProvider = TablespaceListProvider.class)
public PostgreTablespace getDefaultTablespace(DBRProgressMonitor monitor) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return PostgreUtils.getObjectById(monitor, tablespaceCache, this, tablespaceId);
}
public void setDefaultTablespace(PostgreTablespace tablespace) throws DBException {
this.tablespaceId = tablespace.getObjectId();
}
public PostgreTablespace getTablespace(DBRProgressMonitor monitor, long tablespaceId) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
for (PostgreTablespace ts : tablespaceCache.getAllObjects(monitor, this)) {
if (ts.getObjectId() == tablespaceId) {
return ts;
......@@ -380,7 +400,7 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
@Association
public Collection<PostgreSchema> getSchemas(DBRProgressMonitor monitor) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
// Get all schemas
return schemaCache.getAllObjects(monitor, this);
}
......@@ -408,12 +428,12 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
}
public PostgreSchema getSchema(DBRProgressMonitor monitor, String name) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return schemaCache.getObject(monitor, this, name);
}
public PostgreSchema getSchema(DBRProgressMonitor monitor, long oid) throws DBException {
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
for (PostgreSchema schema : schemaCache.getAllObjects(monitor, this)) {
if (schema.getObjectId() == oid) {
return schema;
......@@ -488,7 +508,7 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
if (!getDataSource().getServerType().supportsRoles()) {
return Collections.emptyList();
}
checkDatabaseConnection(monitor);
checkInstanceConnection(monitor);
return roleCache.getAllObjects(monitor, this);
}
......@@ -914,4 +934,64 @@ public class PostgreDatabase extends JDBCRemoteInstance<PostgreDataSource> imple
return new PostgreSchema(owner, name, resultSet);
}
}
public static class TablespaceListProvider implements IPropertyValueListProvider<PostgreDatabase> {
@Override
public boolean allowCustomValue()
{
return false;
}
@Override
public Object[] getPossibleValues(PostgreDatabase object)
{
try {
Collection<PostgreTablespace> tablespaces = object.getTablespaces(new VoidProgressMonitor());
return tablespaces.toArray(new Object[tablespaces.size()]);
} catch (DBException e) {
log.error(e);
return new Object[0];
}
}
}
public static class RoleListProvider implements IPropertyValueListProvider<PostgreDatabase> {
@Override
public boolean allowCustomValue()
{
return false;
}
@Override
public Object[] getPossibleValues(PostgreDatabase object)
{
try {
Collection<PostgreRole> roles = object.getAuthIds(new VoidProgressMonitor());
return roles.toArray(new Object[roles.size()]);
} catch (DBException e) {
log.error(e);
return new Object[0];
}
}
}
public static class CharsetListProvider implements IPropertyValueListProvider<PostgreDatabase> {
@Override
public boolean allowCustomValue()
{
return false;
}
@Override
public Object[] getPossibleValues(PostgreDatabase object)
{
try {
Collection<PostgreCharset> tablespaces = object.getEncodings(new VoidProgressMonitor());
return tablespaces.toArray(new Object[tablespaces.size()]);
} catch (DBException e) {
log.error(e);
return new Object[0];
}
}
}
}
......@@ -38,8 +38,9 @@ public interface DBECommand<OBJECT_TYPE extends DBPObject> {
* Validates command.
* If command is fine then just returns, otherwise throws an exception
* @throws DBException contains information about invalid command state
* @param options
*/
void validateCommand() throws DBException;
void validateCommand(Map<String, Object> options) throws DBException;
void updateModel();
......
......@@ -31,6 +31,9 @@ import java.util.Map;
*/
public interface DBECommandContext extends DBPContextProvider {
// Do not use transactions in conect save
String OPTION_AVOID_TRANSACTIONS = "avoidTransactions";
boolean isDirty();
DBECommand getUndoCommand();
......
......@@ -97,7 +97,7 @@ public class DBECommandProperty<OBJECT_TYPE extends DBPObject> extends DBEComman
}
@Override
public void validateCommand() throws DBException
public void validateCommand(Map<String, Object> options) throws DBException
{
if (handler instanceof DBEPropertyValidator) {
((DBEPropertyValidator<OBJECT_TYPE>)handler).validate(getObject(), newValue);
......
......@@ -80,12 +80,25 @@ public abstract class AbstractCommandContext implements DBECommandContext {
// Execute commands in transaction
DBCTransactionManager txnManager = DBUtils.getTransactionManager(executionContext);
boolean useAutoCommit = false;
// Validate commands
{
Map<String, Object> validateOptions = new HashMap<>();
for (CommandQueue queue : commandQueues) {
for (CommandInfo cmd : queue.commands) {
cmd.command.validateCommand(validateOptions);
}
}
useAutoCommit = CommonUtils.getOption(validateOptions, OPTION_AVOID_TRANSACTIONS);
}
boolean oldAutoCommit = false;
if (txnManager != null) {
oldAutoCommit = txnManager.isAutoCommit();
if (oldAutoCommit) {
if (oldAutoCommit != useAutoCommit) {
try {
txnManager.setAutoCommit(monitor, false);
txnManager.setAutoCommit(monitor, useAutoCommit);
} catch (DBCException e) {
log.warn("Can't switch to transaction mode", e);
}
......@@ -95,7 +108,7 @@ public abstract class AbstractCommandContext implements DBECommandContext {
executeCommands(monitor, options);
// Commit changes
if (txnManager != null) {
if (txnManager != null && !useAutoCommit) {
try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.UTIL, "Commit script transaction")) {
txnManager.commit(session);
} catch (DBCException e1) {
......@@ -106,7 +119,7 @@ public abstract class AbstractCommandContext implements DBECommandContext {
clearCommandQueues();
} catch (Throwable e) {
// Rollback changes
if (txnManager != null) {
if (txnManager != null && !useAutoCommit) {
try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.UTIL, "Rollback script transaction")) {
txnManager.rollback(session, null);
} catch (DBCException e1) {
......@@ -115,9 +128,9 @@ public abstract class AbstractCommandContext implements DBECommandContext {
}
throw e;
} finally {
if (txnManager != null && oldAutoCommit) {
if (txnManager != null && oldAutoCommit != useAutoCommit) {
try {
txnManager.setAutoCommit(monitor, true);
txnManager.setAutoCommit(monitor, oldAutoCommit);
} catch (DBCException e) {
log.warn("Can't switch back to auto-commit mode", e);
}
......@@ -128,13 +141,6 @@ public abstract class AbstractCommandContext implements DBECommandContext {
private void executeCommands(DBRProgressMonitor monitor, Map<String, Object> options) throws DBException {
List<CommandQueue> commandQueues = getCommandQueues();
// Validate commands
for (CommandQueue queue : commandQueues) {
for (CommandInfo cmd : queue.commands) {
cmd.command.validateCommand();
}
}
// Execute commands
List<CommandInfo> executedCommands = new ArrayList<>();
try {
......
......@@ -56,7 +56,7 @@ public class DBECommandAbstract<OBJECT_TYPE extends DBPObject> implements DBECom
}
@Override
public void validateCommand() throws DBException
public void validateCommand(Map<String, Object> options) throws DBException
{
// do nothing by default
}
......
......@@ -161,7 +161,7 @@ public abstract class SQLObjectEditor<OBJECT_TYPE extends DBSObject, CONTAINER_T
}
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
......@@ -266,9 +266,9 @@ public abstract class SQLObjectEditor<OBJECT_TYPE extends DBSObject, CONTAINER_T
}
@Override
public void validateCommand() throws DBException
public void validateCommand(Map<String, Object> options) throws DBException
{
validateObjectProperties(this);
validateObjectProperties(this, options);
}
@Override
......
......@@ -217,7 +217,7 @@ public abstract class SQLTableColumnManager<OBJECT_TYPE extends JDBCTableColumn<
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
......
......@@ -45,7 +45,7 @@ public abstract class SQLTriggerManager<OBJECT_TYPE extends DBSTrigger, CONTAINE
}
@Override
protected void validateObjectProperties(ObjectChangeCommand command)
protected void validateObjectProperties(ObjectChangeCommand command, Map<String, Object> options)
throws DBException
{
if (CommonUtils.isEmpty(command.getObject().getName())) {
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 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.model.struct;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
/**
* Instance with lazy initialization
*/
public interface DBSInstanceLazy extends DBSInstance
{
/**
* Check instance connection
*/
void checkInstanceConnection(@NotNull DBRProgressMonitor monitor) throws DBException;
}
......@@ -22,6 +22,7 @@ import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.WorkbenchAdapter;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.IDataSourceContainerProvider;
......@@ -30,6 +31,8 @@ import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.navigator.DBNDataSource;
import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode;
import org.jkiss.dbeaver.model.preferences.DBPPropertySource;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSInstanceLazy;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.properties.PropertySourceEditable;
......@@ -45,6 +48,8 @@ import java.util.Map;
*/
public abstract class DatabaseEditorInput<NODE extends DBNDatabaseNode> implements IPersistableElement, IDatabaseEditorInput, IDataSourceContainerProvider
{
private static final Log log = Log.getLog(DatabaseEditorInput.class);
private final NODE node;
private final DBCExecutionContext executionContext;
private final DBECommandContext commandContext;
......@@ -63,6 +68,9 @@ public abstract class DatabaseEditorInput<NODE extends DBNDatabaseNode> implemen
DBSObject object = node == null ? null : node.getObject();
if (object != null) {
this.executionContext = DBUtils.getDefaultContext(object, false);
if (this.executionContext == null) {
log.error("Database object is not associated with any execution context");
}
this.commandContext = commandContext != null ?
commandContext :
new SimpleCommandContext(
......
......@@ -428,10 +428,11 @@ public class EntityEditor extends MultiPageDatabaseEditor
saveInProgress = true;
UIUtils.runInProgressService(monitor -> {
monitor.beginTask("Generate SQL script", commands.size());
Map<String, Object> validateOptions = new HashMap<>();
for (DBECommand command : commands) {
monitor.subTask(command.getTitle());
try {
command.validateCommand();
command.validateCommand(validateOptions);
} catch (final DBException e) {
throw new InvocationTargetException(e);
}
......
......@@ -34,12 +34,16 @@ import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.edit.DBEObjectEditor;
import org.jkiss.dbeaver.model.navigator.*;
import org.jkiss.dbeaver.model.struct.DBSInstanceLazy;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.DBSObjectContainer;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.TasksJob;
import org.jkiss.dbeaver.runtime.ui.UIServiceConnections;
import org.jkiss.dbeaver.runtime.ui.UIServiceSQL;
import org.jkiss.dbeaver.ui.IRefreshablePart;
......@@ -180,13 +184,30 @@ public class NavigatorHandlerObjectOpen extends NavigatorHandlerObjectBase imple
objectNode.getMeta().getEditorId());
} else if (selectedNode instanceof DBNDatabaseNode) {
DBNDatabaseNode dnNode = (DBNDatabaseNode) selectedNode;
if (dnNode.getObject() != null) {
if (!dnNode.getObject().isPersisted()) {
DBSObject databaseObject = dnNode.getObject();
if (databaseObject != null) {
if (!databaseObject.isPersisted()) {
return null;
}
if (DBUtils.getDefaultContext(databaseObject, false) == null) {
// Not connected - try to connect
if (databaseObject instanceof DBSInstanceLazy) {
if (!RuntimeUtils.runTask(monitor -> {
try {
((DBSInstanceLazy) databaseObject).checkInstanceConnection(monitor);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
}, "Initiate instance connection",
dnNode.getDataSourceContainer().getPreferenceStore().getInt(ModelPreferences.CONNECTION_OPEN_TIMEOUT))) {
return null;
}
}
//
}
DatabaseNodeEditorInput editorInput = new DatabaseNodeEditorInput(dnNode);
if (DBWorkbench.getPlatform().getPreferenceStore().getBoolean(NavigatorPreferences.NAVIGATOR_REFRESH_EDITORS_ON_OPEN)) {
if (dnNode.getObject() instanceof DBSObjectContainer) {
if (databaseObject instanceof DBSObjectContainer) {
// do not auto-refresh object containers (too expensive)
} else {
refreshDatabaseNode(dnNode);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册