/* * DBeaver - Universal Database Manager * Copyright (C) 2013-2015 Denis Forveille (titou10.titou10@gmail.com) * 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.ext.db2.manager; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.jkiss.code.Nullable; import org.jkiss.dbeaver.core.DBeaverUI; import org.jkiss.dbeaver.model.DBUtils; import org.jkiss.dbeaver.model.edit.DBEPersistAction; import org.jkiss.dbeaver.ext.db2.model.DB2DataSource; import org.jkiss.dbeaver.ext.db2.model.DB2Schema; import org.jkiss.dbeaver.model.edit.DBECommandContext; import org.jkiss.dbeaver.model.impl.DBSObjectCache; import org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction; import org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.struct.DBSObject; import org.jkiss.dbeaver.ui.UITask; import org.jkiss.dbeaver.ui.UIUtils; import java.util.List; /** * DB2 Schema Manager * * @author Denis Forveille */ public class DB2SchemaManager extends SQLObjectEditor<DB2Schema, DB2DataSource> { private static final String SQL_CREATE_SCHEMA = "CREATE SCHEMA %s"; private static final String SQL_DROP_SCHEMA = "DROP SCHEMA %s RESTRICT"; @Override public long getMakerOptions() { return FEATURE_SAVE_IMMEDIATELY; } @Nullable @Override public DBSObjectCache<? extends DBSObject, DB2Schema> getObjectsCache(DB2Schema object) { return object.getDataSource().getSchemaCache(); } @Override protected DB2Schema createDatabaseObject(DBRProgressMonitor monitor, DBECommandContext context, final DB2DataSource parent, Object copyFrom) { return new UITask<DB2Schema>() { @Override protected DB2Schema runTask() { NewSchemaDialog dialog = new NewSchemaDialog(DBeaverUI.getActiveWorkbenchShell()); if (dialog.open() != IDialogConstants.OK_ID) { return null; } String schemaName = dialog.getSchemaName(); if (schemaName.length() == 0) { return null; } return new DB2Schema(parent, schemaName); } }.execute(); } @Override protected void addObjectCreateActions(List<DBEPersistAction> actions, ObjectCreateCommand command) { SQLDatabasePersistAction action = new SQLDatabasePersistAction("Create schema", String.format(SQL_CREATE_SCHEMA, DBUtils.getQuotedIdentifier(command.getObject()))); actions.add(action); } @Override protected void addObjectDeleteActions(List<DBEPersistAction> actions, ObjectDeleteCommand command) { String schemaName = command.getObject().getName(); DBEPersistAction action = new SQLDatabasePersistAction("Drop schema (SQL)", String.format(SQL_DROP_SCHEMA, DBUtils.getQuotedIdentifier(command.getObject()))); actions.add(action); } // -------- // Dialog // -------- private static class NewSchemaDialog extends Dialog { private String schemaName; public String getSchemaName() { return schemaName; } // Dialog managment private Text nameText; public NewSchemaDialog(Shell parentShell) { super(parentShell); } @Override protected boolean isResizable() { return true; } @Override protected Control createDialogArea(Composite parent) { getShell().setText("New Schema Name"); Control container = super.createDialogArea(parent); Composite composite = UIUtils.createPlaceholder((Composite) container, 2); composite.setLayoutData(new GridData(GridData.FILL_BOTH)); nameText = UIUtils.createLabelText(composite, "Schema Name", null); nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); return parent; } @Override protected void okPressed() { this.schemaName = nameText.getText().trim().toUpperCase(); super.okPressed(); } } }