提交 d8938f20 编写于 作者: S Serge Rider

#5384 FK create editor (add/remove)

上级 b91eca6b
......@@ -165,8 +165,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
}
@Nullable
public DBSEntity getRealEntity(DBRProgressMonitor monitor) throws DBException
{
public DBSEntity getRealEntity(DBRProgressMonitor monitor) throws DBException {
DBSObjectContainer realContainer = container.getRealContainer(monitor);
if (realContainer == null) {
return null;
......@@ -187,50 +186,42 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
@Nullable
@Override
public String getDescription()
{
public String getDescription() {
return description;
}
@Override
public DBVContainer getParentObject()
{
public DBVContainer getParentObject() {
return container;
}
@NotNull
@Override
public DBPDataSource getDataSource()
{
public DBPDataSource getDataSource() {
return container == null ? null : container.getDataSource();
}
public void setDescription(String description)
{
public void setDescription(String description) {
this.description = description;
}
@Override
public boolean isPersisted()
{
public boolean isPersisted() {
return true;
}
@NotNull
@Override
public DBSEntityType getEntityType()
{
public DBSEntityType getEntityType() {
return DBSEntityType.VIRTUAL_ENTITY;
}
@Nullable
public String getProperty(String name)
{
public String getProperty(String name) {
return CommonUtils.isEmpty(properties) ? null : properties.get(name);
}
public void setProperty(String name, @Nullable String value)
{
public void setProperty(String name, @Nullable String value) {
if (properties == null) {
properties = new LinkedHashMap<>();
}
......@@ -243,8 +234,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
@NotNull
@Override
public Collection<? extends DBSEntityAttribute> getAttributes(@NotNull DBRProgressMonitor monitor) throws DBException
{
public Collection<? extends DBSEntityAttribute> getAttributes(@NotNull DBRProgressMonitor monitor) throws DBException {
DBSEntity realEntity = getRealEntity(monitor);
if (realEntity != null) {
final Collection<? extends DBSEntityAttribute> realAttributes = realEntity.getAttributes(monitor);
......@@ -257,8 +247,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
@Nullable
@Override
public DBSEntityAttribute getAttribute(@NotNull DBRProgressMonitor monitor, @NotNull String attributeName)
{
public DBSEntityAttribute getAttribute(@NotNull DBRProgressMonitor monitor, @NotNull String attributeName) {
try {
return DBUtils.findObject(getAttributes(monitor), attributeName);
} catch (DBException e) {
......@@ -268,8 +257,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
}
@Nullable
public DBVEntityAttribute getVirtualAttribute(DBDAttributeBinding binding, boolean create)
{
public DBVEntityAttribute getVirtualAttribute(DBDAttributeBinding binding, boolean create) {
if (entityAttributes != null || create) {
if (entityAttributes == null) {
entityAttributes = new ArrayList<>();
......@@ -313,13 +301,11 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
@Nullable
@Override
public Collection<? extends DBVEntityConstraint> getConstraints(@NotNull DBRProgressMonitor monitor) throws DBException
{
public Collection<? extends DBVEntityConstraint> getConstraints(@NotNull DBRProgressMonitor monitor) throws DBException {
return entityConstraints;
}
public DBVEntityConstraint getBestIdentifier()
{
public DBVEntityConstraint getBestIdentifier() {
if (entityConstraints == null) {
entityConstraints = new ArrayList<>();
}
......@@ -334,8 +320,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
return entityConstraints.get(0);
}
void addConstraint(DBVEntityConstraint constraint)
{
void addConstraint(DBVEntityConstraint constraint) {
if (entityConstraints == null) {
entityConstraints = new ArrayList<>();
}
......@@ -344,23 +329,31 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
@Nullable
@Override
public Collection<DBVEntityForeignKey> getAssociations(@NotNull DBRProgressMonitor monitor) throws DBException
{
public synchronized List<DBVEntityForeignKey> getAssociations(@NotNull DBRProgressMonitor monitor) throws DBException {
return entityForeignKeys;
}
void addForeignKey(DBVEntityForeignKey foreignKey)
{
@NotNull
public synchronized List<DBVEntityForeignKey> getForeignKeys() {
return entityForeignKeys != null ? entityForeignKeys : Collections.emptyList();
}
public synchronized void addForeignKey(@NotNull DBVEntityForeignKey foreignKey) {
if (entityForeignKeys == null) {
entityForeignKeys = new ArrayList<>();
}
entityForeignKeys.add(foreignKey);
}
public synchronized void removeForeignKey(@NotNull DBVEntityForeignKey foreignKey) {
if (entityForeignKeys != null) {
entityForeignKeys.remove(foreignKey);
}
}
@Nullable
@Override
public Collection<? extends DBSEntityAssociation> getReferences(@NotNull DBRProgressMonitor monitor) throws DBException
{
public Collection<? extends DBSEntityAssociation> getReferences(@NotNull DBRProgressMonitor monitor) throws DBException {
return null;
}
......@@ -368,20 +361,17 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
return descriptionColumnNames;
}
public void setDescriptionColumnNames(String descriptionColumnNames)
{
public void setDescriptionColumnNames(String descriptionColumnNames) {
this.descriptionColumnNames = descriptionColumnNames;
}
public Collection<DBSEntityAttribute> getDescriptionColumns(DBRProgressMonitor monitor, DBSEntity entity)
throws DBException
{
throws DBException {
return getDescriptionColumns(monitor, entity, descriptionColumnNames);
}
public static Collection<DBSEntityAttribute> getDescriptionColumns(DBRProgressMonitor monitor, DBSEntity entity, String descColumns)
throws DBException
{
throws DBException {
if (CommonUtils.isEmpty(descColumns)) {
return Collections.emptyList();
}
......@@ -402,8 +392,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
}
public static String getDefaultDescriptionColumn(DBRProgressMonitor monitor, DBSEntityAttribute keyColumn)
throws DBException
{
throws DBException {
assert keyColumn.getParentObject() != null;
Collection<? extends DBSEntityAttribute> allColumns = keyColumn.getParentObject().getAttributes(monitor);
......@@ -419,8 +408,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
if (column != keyColumn &&
column.getDataKind() == DBPDataKind.STRING &&
column.getMaxLength() < MAX_DESC_COLUMN_LENGTH &&
column.getMaxLength() >= MIN_DESC_COLUMN_LENGTH)
{
column.getMaxLength() >= MIN_DESC_COLUMN_LENGTH) {
stringColumns.put(column.getName(), column);
}
}
......@@ -466,7 +454,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
final DBVColorOverride co = new DBVColorOverride(
attrName,
DBCLogicalOperator.EQUALS,
new Object[] { value },
new Object[]{value},
foreground,
background);
......@@ -539,8 +527,7 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec
@NotNull
@Override
public String getFullyQualifiedName(DBPEvaluationContext context)
{
public String getFullyQualifiedName(DBPEvaluationContext context) {
return DBUtils.getFullQualifiedName(getDataSource(),
container,
this);
......
......@@ -71,6 +71,10 @@ public class DBVEntityForeignKey implements DBSEntityConstraint, DBSEntityReferr
return attributes;
}
public List<DBVEntityForeignKeyColumn> getAttributes() {
return attributes;
}
public synchronized void setAttributes(List<DBVEntityForeignKeyColumn> attrs) {
attributes.clear();
attributes.addAll(attrs);
......
......@@ -373,7 +373,10 @@ public class EditForeignKeyPage extends BaseObjectEditPage {
break;
}
if (childNode instanceof DBNDatabaseFolder) {
loadEntities(monitor, entities, (DBNDatabaseFolder)childNode);
Class<?> childrenClass = ((DBNDatabaseFolder) childNode).getChildrenClass(((DBNDatabaseFolder) childNode).getItemsMeta());
if (DBSEntity.class.isAssignableFrom(childrenClass)) {
loadEntities(monitor, entities, (DBNDatabaseFolder) childNode);
}
} else {
if (childNode instanceof DBNDatabaseNode) {
DBSObject object = ((DBNDatabaseNode) childNode).getObject();
......
......@@ -28,13 +28,13 @@ import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.model.DBValueFormatting;
import org.jkiss.dbeaver.model.data.DBDAttributeBinding;
import org.jkiss.dbeaver.model.data.DBDAttributeTransformerDescriptor;
import org.jkiss.dbeaver.model.data.DBDRowIdentifier;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSEntity;
import org.jkiss.dbeaver.model.struct.DBSEntityAssociation;
import org.jkiss.dbeaver.model.struct.DBSEntityAttribute;
import org.jkiss.dbeaver.model.struct.rdb.DBSForeignKeyModifyRule;
import org.jkiss.dbeaver.model.virtual.*;
......@@ -49,6 +49,8 @@ import org.jkiss.utils.CommonUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
class EditVirtualEntityDialog extends BaseDialog {
......@@ -170,24 +172,54 @@ class EditVirtualEntityDialog extends BaseDialog {
UIUtils.createTableColumn(fkTable, SWT.LEFT, "Ref Table");
UIUtils.createTableColumn(fkTable, SWT.LEFT, "Columns");
Composite buttonsPanel = UIUtils.createComposite(panel, 2);
buttonsPanel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
Button btnAdd = createButton(buttonsPanel, ID_CREATE_FOREIGN_KEY, "Add", false);
btnAdd.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
for (DBVEntityForeignKey fk : vEntity.getForeignKeys()) {
createForeignKeyItem(fkTable, fk);
}
DBSEntityAssociation virtualFK = new DBVEntityForeignKey(vEntity);
EditForeignKeyPage editDialog = new EditForeignKeyPage(
"Define virtual foreign keys", virtualFK, new DBSForeignKeyModifyRule[] { DBSForeignKeyModifyRule.NO_ACTION });
if (!editDialog.edit()) {
return;
{
Composite buttonsPanel = UIUtils.createComposite(panel, 2);
buttonsPanel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
Button btnAdd = createButton(buttonsPanel, ID_CREATE_FOREIGN_KEY, "Add", false);
btnAdd.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DBVEntityForeignKey virtualFK = new DBVEntityForeignKey(vEntity);
EditForeignKeyPage editDialog = new EditForeignKeyPage(
"Define virtual foreign keys", virtualFK, new DBSForeignKeyModifyRule[]{DBSForeignKeyModifyRule.NO_ACTION});
if (!editDialog.edit()) {
return;
}
// Save
virtualFK.setReferencedConstraint(editDialog.getUniqueConstraint());
List<DBVEntityForeignKeyColumn> columns = new ArrayList<>();
for (EditForeignKeyPage.FKColumnInfo tableColumn : editDialog.getColumns()) {
columns.add(
new DBVEntityForeignKeyColumn(
virtualFK, tableColumn.getOwnColumn().getName(), tableColumn.getRefColumn().getName()));
}
virtualFK.setAttributes(columns);
vEntity.addForeignKey(virtualFK);
createForeignKeyItem(fkTable, virtualFK);
}
// Save
}
});
Button btnRemove = createButton(buttonsPanel, ID_REMOVE_FOREIGN_KEY, "Remove", false);
btnRemove.setEnabled(false);
});
Button btnRemove = createButton(buttonsPanel, ID_REMOVE_FOREIGN_KEY, "Remove", false);
btnRemove.setEnabled(false);
btnRemove.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DBVEntityForeignKey virtualFK = (DBVEntityForeignKey) fkTable.getSelection()[0].getData();
if (!UIUtils.confirmAction(getShell(),
"Delete virtual FK",
"Are you sure you want to delete virtual foreign key '" + virtualFK.getName() + "'?")) {
return;
}
vEntity.removeForeignKey(virtualFK);
fkTable.remove(fkTable.getSelectionIndices());
}
});
}
fkTable.addSelectionListener(new SelectionAdapter() {
@Override
......@@ -198,6 +230,18 @@ class EditVirtualEntityDialog extends BaseDialog {
});
}
private void createForeignKeyItem(Table fkTable, DBVEntityForeignKey fk) {
TableItem item = new TableItem(fkTable, SWT.NONE);
item.setImage(0, DBeaverIcons.getImage(DBIcon.TREE_FOREIGN_KEY));
if (fk.getReferencedConstraint() != null) {
item.setText(0, fk.getReferencedConstraint().getParentObject().getName());
}
String attrNames = fk.getAttributes().stream().map(DBVEntityForeignKeyColumn::getAttributeName)
.collect(Collectors.joining(","));
item.setText(1, attrNames);
item.setData(fk);
}
private void createColumnsPage(TabFolder tabFolder) {
TabItem colItem = new TabItem(tabFolder, SWT.NONE);
colItem.setText("Columns view");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册