use of org.jkiss.dbeaver.model.edit.DBECommand in project dbeaver by serge-rider.
the class NavigatorHandlerObjectRename method renameDatabaseObject.
public static boolean renameDatabaseObject(IWorkbenchWindow workbenchWindow, DBNDatabaseNode node, String newName) {
try {
if (node.getParentNode() instanceof DBNContainer) {
final DBNContainer container = (DBNContainer) node.getParentNode();
DBSObject object = node.getObject();
if (object != null) {
DBEObjectRenamer objectRenamer = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectRenamer.class);
if (objectRenamer != null) {
CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, object.getClass(), false);
objectRenamer.renameObject(commandTarget.getContext(), object, newName);
if (object.isPersisted() && commandTarget.getEditor() == null) {
if (!showScript(workbenchWindow, commandTarget.getContext(), "Rename script")) {
commandTarget.getContext().resetChanges();
return false;
} else {
ObjectSaver renamer = new ObjectSaver(commandTarget.getContext());
TasksJob.runTask("Rename object '" + object.getName() + "'", renamer);
}
} else {
for (DBECommand command : commandTarget.getContext().getFinalCommands()) {
//System.out.println(command);
}
}
return true;
}
}
}
} catch (Throwable e) {
UIUtils.showErrorDialog(workbenchWindow.getShell(), "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 serge-rider.
the class NavigatorHandlerObjectBase method showScript.
protected static boolean showScript(IWorkbenchWindow workbenchWindow, DBECommandContext commandContext, String dialogTitle) {
Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
StringBuilder script = new StringBuilder();
for (DBECommand command : commands) {
script.append(SQLUtils.generateScript(commandContext.getExecutionContext().getDataSource(), command.getPersistActions(), false));
}
DatabaseNavigatorView view = UIUtils.findView(workbenchWindow, DatabaseNavigatorView.class);
if (view != null) {
ViewSQLDialog dialog = new ViewSQLDialog(view.getSite(), commandContext.getExecutionContext(), dialogTitle, UIIcon.SQL_PREVIEW, script.toString());
dialog.setShowSaveButton(true);
return dialog.open() == IDialogConstants.PROCEED_ID;
} else {
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();
for (DBECommand command : commands) {
try {
command.validateCommand();
} catch (final DBException e) {
log.debug(e);
DBeaverUI.syncExec(() -> DBUserInterface.getInstance().showError("Validation", e.getMessage()));
return IDialogConstants.CANCEL_ID;
}
script.append(SQLUtils.generateScript(commandContext.getExecutionContext().getDataSource(), command.getPersistActions(DBPScriptObject.EMPTY_OPTIONS), false));
}
if (script.length() == 0) {
return IDialogConstants.PROCEED_ID;
}
ChangesPreviewer changesPreviewer = new ChangesPreviewer(script, allowSave);
DBeaverUI.syncExec(changesPreviewer);
return changesPreviewer.getResult();
}
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) {
final DBNContainer container = (DBNContainer) node.getParentNode();
DBSObject object = node.getObject();
if (object != null) {
DBEObjectRenamer objectRenamer = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectRenamer.class);
if (objectRenamer != null) {
CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, 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();
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) {
DBUserInterface.getInstance().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 serge-rider.
the class NavigatorObjectsDeleter method appendScript.
private void appendScript(final DBRProgressMonitor monitor, final StringBuilder sql, final DBNDatabaseNode node) throws InvocationTargetException {
if (!(node.getParentNode() instanceof DBNContainer)) {
return;
}
final DBSObject object = node.getObject();
if (object == null) {
return;
}
final DBEObjectMaker objectMaker = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectMaker.class);
if (objectMaker == null) {
return;
}
final boolean supportsCascade = (objectMaker.getMakerOptions(object.getDataSource()) & DBEObjectMaker.FEATURE_DELETE_CASCADE) != 0;
final NavigatorHandlerObjectBase.CommandTarget commandTarget;
try {
commandTarget = NavigatorHandlerObjectBase.getCommandTarget(window, node.getParentNode(), object.getClass(), false);
} catch (DBException e) {
log.warn(e);
return;
}
if (commandContext == null) {
commandContext = commandTarget.getContext();
}
if (!object.isPersisted() || commandTarget.getEditor() != null) {
return;
}
final Map<String, Object> deleteOptions;
if (supportsCascade && deleteCascade) {
deleteOptions = OPTIONS_CASCADE;
} else {
deleteOptions = Collections.emptyMap();
}
try {
objectMaker.deleteObject(commandTarget.getContext(), node.getObject(), deleteOptions);
} catch (DBException e) {
log.warn(e);
return;
}
final StringBuilder script = new StringBuilder();
final DBECommandContext commandContext = commandTarget.getContext();
Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
try {
for (DBECommand command : commands) {
final DBEPersistAction[] persistActions = command.getPersistActions(monitor, commandContext.getExecutionContext(), deleteOptions);
script.append(SQLUtils.generateScript(commandContext.getExecutionContext().getDataSource(), persistActions, false));
if (script.length() == 0) {
script.append(SQLUtils.generateComments(commandContext.getExecutionContext().getDataSource(), persistActions, false));
}
}
} catch (DBException e) {
throw new InvocationTargetException(e);
}
commandTarget.getContext().resetChanges(true);
if (sql.length() != 0) {
sql.append("\n");
}
sql.append(script);
}
Aggregations