use of org.jkiss.dbeaver.model.edit.DBECommand in project dbeaver by serge-rider.
the class EntityEditor method saveCommandContext.
private boolean saveCommandContext(final DBRProgressMonitor monitor, Map<String, Object> options) {
monitor.beginTask("Save entity", 1);
Throwable error = null;
final DBECommandContext commandContext = getCommandContext();
if (commandContext == null) {
log.warn("Null command context");
return true;
}
DBCExecutionContext executionContext = getExecutionContext();
if (executionContext == null) {
log.warn("Null execution context");
return true;
}
boolean isNewObject = getDatabaseObject() == null || !getDatabaseObject().isPersisted();
if (!isNewObject) {
// Check for any new nested objects
for (DBECommand cmd : commandContext.getFinalCommands()) {
if (cmd.getObject() instanceof DBSObject && !((DBSObject) cmd.getObject()).isPersisted()) {
isNewObject = true;
break;
}
}
}
try {
DBExecUtils.tryExecuteRecover(monitor, executionContext.getDataSource(), param -> {
try {
commandContext.saveChanges(monitor, options);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (DBException e) {
error = e;
}
if (getDatabaseObject() instanceof DBPStatefulObject) {
try {
((DBPStatefulObject) getDatabaseObject()).refreshObjectState(monitor);
} catch (DBCException e) {
// Just report an error
log.error(e);
}
}
if (error == null) {
// Refresh underlying node
// It'll refresh database object and all it's descendants
// So we'll get actual data from database
final DBNDatabaseNode treeNode = getEditorInput().getNavigatorNode();
try {
boolean doRefresh = isNewObject;
UIUtils.runInProgressService(monitor1 -> {
try {
treeNode.refreshNode(monitor1, doRefresh ? DBNEvent.FORCE_REFRESH : DBNEvent.UPDATE_ON_SAVE);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (InvocationTargetException e) {
error = e.getTargetException();
} catch (InterruptedException e) {
// ok
}
}
monitor.done();
if (error == null) {
return true;
} else {
// Try to handle error in nested editors
final Throwable vError = error;
UIUtils.syncExec(() -> {
final IErrorVisualizer errorVisualizer = getAdapter(IErrorVisualizer.class);
if (errorVisualizer != null) {
errorVisualizer.visualizeError(monitor, vError);
}
});
// Show error dialog
UIUtils.asyncExec(() -> DBWorkbench.getPlatformUI().showError("Can't save '" + getDatabaseObject().getName() + "'", null, vError));
return false;
}
}
use of org.jkiss.dbeaver.model.edit.DBECommand in project dbeaver by dbeaver.
the class NavigatorHandlerObjectRename method renameDatabaseObject.
public static boolean renameDatabaseObject(IWorkbenchWindow workbenchWindow, DBNDatabaseNode node, String newName) {
try {
if (node.getParentNode() instanceof DBNContainer) {
DBSObject object = node.getObject();
if (object != null) {
DBEObjectRenamer objectRenamer = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectRenamer.class);
if (objectRenamer != null) {
CommandTarget commandTarget = getCommandTarget(workbenchWindow, node.getParentNode(), object.getClass(), false);
objectRenamer.renameObject(commandTarget.getContext(), object, newName);
if (object.isPersisted() && commandTarget.getEditor() == null) {
Map<String, Object> options = DBPScriptObject.EMPTY_OPTIONS;
if (!showScript(workbenchWindow, commandTarget.getContext(), options, "Rename script")) {
commandTarget.getContext().resetChanges(true);
return false;
} else {
ObjectSaver renamer = new ObjectSaver(commandTarget.getContext(), options);
TasksJob.runTask("Rename object '" + object.getName() + "'", renamer);
}
} else {
for (DBECommand command : commandTarget.getContext().getFinalCommands()) {
// System.out.println(command);
}
}
return true;
}
}
}
} catch (Throwable e) {
DBWorkbench.getPlatformUI().showError("Rename object", "Can't rename object '" + node.getNodeName() + "'", e);
return false;
}
return false;
}
use of org.jkiss.dbeaver.model.edit.DBECommand in project dbeaver by dbeaver.
the class EntityEditor method showChanges.
public int showChanges(boolean allowSave) {
DBECommandContext commandContext = getCommandContext();
if (commandContext == null) {
return IDialogConstants.CANCEL_ID;
}
Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
if (CommonUtils.isEmpty(commands)) {
return IDialogConstants.IGNORE_ID;
}
StringBuilder script = new StringBuilder();
try {
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(monitor, validateOptions);
} catch (final DBException e) {
throw new InvocationTargetException(e);
}
Map<String, Object> options = new HashMap<>();
options.put(DBPScriptObject.OPTION_OBJECT_SAVE, true);
DBPDataSource dataSource = getDatabaseObject().getDataSource();
try {
DBEPersistAction[] persistActions = command.getPersistActions(monitor, getExecutionContext(), options);
script.append(SQLUtils.generateScript(dataSource, persistActions, false));
} catch (DBException e) {
throw new InvocationTargetException(e);
}
monitor.worked(1);
}
monitor.done();
});
} catch (InterruptedException e) {
return IDialogConstants.CANCEL_ID;
} catch (InvocationTargetException e) {
log.error(e);
DBWorkbench.getPlatformUI().showError("Script generate error", "Couldn't generate alter script", e.getTargetException());
return IDialogConstants.CANCEL_ID;
} finally {
saveInProgress = false;
}
if (script.length() == 0) {
return IDialogConstants.PROCEED_ID;
}
ChangesPreviewer changesPreviewer = new ChangesPreviewer(script, allowSave);
UIUtils.syncExec(changesPreviewer);
return changesPreviewer.getResult();
}
use of org.jkiss.dbeaver.model.edit.DBECommand in project dbeaver by dbeaver.
the class EntityEditor method saveCommandContext.
private boolean saveCommandContext(final DBRProgressMonitor monitor, Map<String, Object> options) {
monitor.beginTask("Save entity", 1);
Throwable error = null;
final DBECommandContext commandContext = getCommandContext();
if (commandContext == null) {
log.warn("Null command context");
return true;
}
DBCExecutionContext executionContext = getExecutionContext();
if (executionContext == null) {
log.warn("Null execution context");
return true;
}
boolean isNewObject = getDatabaseObject() == null || !getDatabaseObject().isPersisted();
if (!isNewObject) {
// Check for any new nested objects
for (DBECommand cmd : commandContext.getFinalCommands()) {
if (cmd.getObject() instanceof DBSObject && !((DBSObject) cmd.getObject()).isPersisted()) {
isNewObject = true;
break;
}
}
}
try {
DBExecUtils.tryExecuteRecover(monitor, executionContext.getDataSource(), param -> {
try {
commandContext.saveChanges(monitor, options);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (DBException e) {
error = e;
}
if (getDatabaseObject() instanceof DBPStatefulObject) {
try {
((DBPStatefulObject) getDatabaseObject()).refreshObjectState(monitor);
} catch (DBCException e) {
// Just report an error
log.error(e);
}
}
if (error == null) {
// Refresh underlying node
// It'll refresh database object and all it's descendants
// So we'll get actual data from database
final DBNDatabaseNode treeNode = getEditorInput().getNavigatorNode();
try {
boolean doRefresh = isNewObject;
UIUtils.runInProgressService(monitor1 -> {
try {
treeNode.refreshNode(monitor1, doRefresh ? DBNEvent.FORCE_REFRESH : DBNEvent.UPDATE_ON_SAVE);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (InvocationTargetException e) {
error = e.getTargetException();
} catch (InterruptedException e) {
// ok
}
}
monitor.done();
if (error == null) {
return true;
} else {
// Try to handle error in nested editors
final Throwable vError = error;
UIUtils.syncExec(() -> {
final IErrorVisualizer errorVisualizer = getAdapter(IErrorVisualizer.class);
if (errorVisualizer != null) {
errorVisualizer.visualizeError(monitor, vError);
}
});
// Show error dialog
UIUtils.asyncExec(() -> DBWorkbench.getPlatformUI().showError("Can't save '" + getDatabaseObject().getName() + "'", null, vError));
return false;
}
}
use of org.jkiss.dbeaver.model.edit.DBECommand in project dbeaver by serge-rider.
the class EntityEditor method showChanges.
public int showChanges(boolean allowSave) {
DBECommandContext commandContext = getCommandContext();
if (commandContext == null) {
return IDialogConstants.CANCEL_ID;
}
Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
if (CommonUtils.isEmpty(commands)) {
return IDialogConstants.IGNORE_ID;
}
StringBuilder script = new StringBuilder();
try {
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(monitor, validateOptions);
} catch (final DBException e) {
throw new InvocationTargetException(e);
}
Map<String, Object> options = new HashMap<>();
options.put(DBPScriptObject.OPTION_OBJECT_SAVE, true);
DBPDataSource dataSource = getDatabaseObject().getDataSource();
try {
DBEPersistAction[] persistActions = command.getPersistActions(monitor, getExecutionContext(), options);
script.append(SQLUtils.generateScript(dataSource, persistActions, false));
} catch (DBException e) {
throw new InvocationTargetException(e);
}
monitor.worked(1);
}
monitor.done();
});
} catch (InterruptedException e) {
return IDialogConstants.CANCEL_ID;
} catch (InvocationTargetException e) {
log.error(e);
DBWorkbench.getPlatformUI().showError("Script generate error", "Couldn't generate alter script", e.getTargetException());
return IDialogConstants.CANCEL_ID;
} finally {
saveInProgress = false;
}
if (script.length() == 0) {
return IDialogConstants.PROCEED_ID;
}
ChangesPreviewer changesPreviewer = new ChangesPreviewer(script, allowSave);
UIUtils.syncExec(changesPreviewer);
return changesPreviewer.getResult();
}
Aggregations