use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.
the class EditVirtualEntityDialog method okPressed.
@Override
protected void okPressed() {
if (editUniqueKeyPage != null) {
Collection<DBSEntityAttribute> uniqueAttrs = editUniqueKeyPage.getSelectedAttributes();
uniqueConstraint.setName(editUniqueKeyPage.getConstraintName());
uniqueConstraint.setUseAllColumns(editUniqueKeyPage.isUseAllColumns());
uniqueConstraint.setAttributes(uniqueConstraint.isUseAllColumns() ? Collections.emptyList() : uniqueAttrs);
DBDRowIdentifier virtualEntityIdentifier = viewer.getVirtualEntityIdentifier();
if (virtualEntityIdentifier != null) {
try {
virtualEntityIdentifier.reloadAttributes(new VoidProgressMonitor(), viewer.getModel().getAttributes());
} catch (DBException e) {
log.error(e);
}
}
}
if (editDictionaryPage != null) {
editDictionaryPage.saveDictionarySettings();
}
vEntity.persistConfiguration();
if (structChanged || columnsPage.isStructChanged()) {
viewer.refreshData(null);
}
super.okPressed();
}
use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.
the class SQLAttributeResolver method resolveAll.
@Override
protected String[] resolveAll(final TemplateContext context) {
final DBCExecutionContext executionContext = ((DBPContextProvider) context).getExecutionContext();
if (executionContext == null) {
return super.resolveAll(context);
}
TemplateVariable tableVariable = ((SQLContext) context).getTemplateVariable("table");
final String tableName = tableVariable == null ? null : tableVariable.getDefaultValue();
if (!CommonUtils.isEmpty(tableName)) {
final List<DBSEntityAttribute> attributes = new ArrayList<>();
DBRRunnableWithProgress runnable = new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
List<DBSEntity> entities = new ArrayList<>();
SQLEntityResolver.resolveTables(monitor, executionContext, context, entities);
if (!CommonUtils.isEmpty(entities)) {
DBSEntity table = DBUtils.findObject(entities, tableName);
if (table != null) {
attributes.addAll(CommonUtils.safeCollection(table.getAttributes(monitor)));
}
}
} catch (DBException e) {
throw new InvocationTargetException(e);
}
}
};
RuntimeUtils.runTask(runnable, "Resolve attributes", 1000);
if (!CommonUtils.isEmpty(attributes)) {
String[] result = new String[attributes.size()];
for (int i = 0; i < attributes.size(); i++) {
DBSEntityAttribute entity = attributes.get(i);
result[i] = entity.getName();
}
return result;
}
}
return super.resolveAll(context);
}
use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by serge-rider.
the class SQLForeignKeyManager method getNestedDeclaration.
@Override
protected StringBuilder getNestedDeclaration(TABLE_TYPE owner, DBECommandAbstract<OBJECT_TYPE> command) {
OBJECT_TYPE foreignKey = command.getObject();
boolean legacySyntax = isLegacyForeignKeySyntax(owner);
// Create column
String constraintName = DBUtils.getQuotedIdentifier(foreignKey.getDataSource(), foreignKey.getName());
StringBuilder decl = new StringBuilder(40);
if (!legacySyntax || !foreignKey.isPersisted()) {
decl.append("CONSTRAINT ");
}
if (!legacySyntax) {
//$NON-NLS-1$
decl.append(constraintName).append(" ");
}
//$NON-NLS-1$
decl.append(foreignKey.getConstraintType().getName().toUpperCase(Locale.ENGLISH)).append(//$NON-NLS-1$
" (");
try {
// Get columns using void monitor
final Collection<? extends DBSEntityAttributeRef> columns = command.getObject().getAttributeReferences(VoidProgressMonitor.INSTANCE);
boolean firstColumn = true;
for (DBSEntityAttributeRef constraintColumn : CommonUtils.safeCollection(columns)) {
final DBSEntityAttribute attribute = constraintColumn.getAttribute();
//$NON-NLS-1$
if (!firstColumn)
decl.append(",");
firstColumn = false;
if (attribute != null) {
decl.append(DBUtils.getQuotedIdentifier(attribute));
}
}
} catch (DBException e) {
log.error("Can't obtain reference attributes", e);
}
final DBSEntityConstraint refConstraint = foreignKey.getReferencedConstraint();
//$NON-NLS-1$ //$NON-NLS-2$
decl.append(") REFERENCES ").append(refConstraint == null ? "<?>" : DBUtils.getObjectFullName(refConstraint.getParentObject(), DBPEvaluationContext.DDL)).append("(");
if (refConstraint instanceof DBSEntityReferrer) {
try {
boolean firstColumn = true;
List<? extends DBSEntityAttributeRef> columns = ((DBSEntityReferrer) refConstraint).getAttributeReferences(VoidProgressMonitor.INSTANCE);
for (DBSEntityAttributeRef constraintColumn : CommonUtils.safeCollection(columns)) {
//$NON-NLS-1$
if (!firstColumn)
decl.append(",");
firstColumn = false;
final DBSEntityAttribute attribute = constraintColumn.getAttribute();
if (attribute != null) {
decl.append(DBUtils.getQuotedIdentifier(attribute));
}
}
} catch (DBException e) {
log.error("Can't obtain ref constraint reference attributes", e);
}
}
//$NON-NLS-1$
decl.append(")");
if (foreignKey.getDeleteRule() != null && !CommonUtils.isEmpty(foreignKey.getDeleteRule().getClause())) {
//$NON-NLS-1$
decl.append(" ON DELETE ").append(foreignKey.getDeleteRule().getClause());
}
if (foreignKey.getUpdateRule() != null && !CommonUtils.isEmpty(foreignKey.getUpdateRule().getClause())) {
//$NON-NLS-1$
decl.append(" ON UPDATE ").append(foreignKey.getUpdateRule().getClause());
}
if (legacySyntax) {
//$NON-NLS-1$
decl.append(" CONSTRAINT ").append(constraintName);
}
return decl;
}
use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.
the class ExasolCreateForeignKeyDialog method handleUniqueKeySelect.
private void handleUniqueKeySelect() {
curConstraint = null;
fkColumns.clear();
ownColumns = null;
columnsTable.removeAll();
if (curConstraints.isEmpty() || uniqueKeyCombo.getSelectionIndex() < 0) {
return;
}
curConstraint = curConstraints.get(uniqueKeyCombo.getSelectionIndex());
try {
// Read column nodes with void monitor because we already cached them above
for (DBSEntityAttributeRef pkColumn : curConstraint.getAttributeReferences(new VoidProgressMonitor())) {
FKColumnInfo fkColumnInfo = new FKColumnInfo(pkColumn.getAttribute());
// Try to find matched column in own table
Collection<? extends DBSEntityAttribute> tmpColumns = ownTable.getAttributes(new VoidProgressMonitor());
ownColumns = tmpColumns == null ? Collections.<DBSTableColumn>emptyList() : new ArrayList<>(ownTable.getAttributes(new VoidProgressMonitor()));
if (!CommonUtils.isEmpty(ownColumns)) {
for (DBSEntityAttribute ownColumn : ownColumns) {
if (ownColumn.getName().equals(pkColumn.getAttribute().getName()) && ownTable != pkColumn.getAttribute().getParentObject()) {
fkColumnInfo.ownColumn = ownColumn;
break;
}
}
}
fkColumns.add(fkColumnInfo);
TableItem item = new TableItem(columnsTable, SWT.NONE);
if (fkColumnInfo.ownColumn != null) {
item.setText(0, fkColumnInfo.ownColumn.getName());
item.setImage(0, getColumnIcon(fkColumnInfo.ownColumn));
item.setText(1, fkColumnInfo.ownColumn.getFullTypeName());
}
item.setText(2, pkColumn.getAttribute().getName());
item.setImage(2, getColumnIcon(pkColumn.getAttribute()));
item.setText(3, pkColumn.getAttribute().getFullTypeName());
item.setData(fkColumnInfo);
}
} catch (DBException e) {
DBUserInterface.getInstance().showError(CoreMessages.dialog_struct_edit_fk_error_load_constraint_columns_title, CoreMessages.dialog_struct_edit_fk_error_load_constraint_columns_message, e);
}
UIUtils.packColumns(columnsTable, true);
}
use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.
the class ColumnsMappingDialog method createDialogArea.
@Override
protected Control createDialogArea(Composite parent) {
DBPDataSource targetDataSource = settings.getTargetDataSource(mapping);
getShell().setText("Map columns of " + mapping.getTargetName());
boldFont = UIUtils.makeBoldFont(parent.getFont());
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
new Label(composite, SWT.NONE).setText("Source entity: " + DBUtils.getObjectFullName(mapping.getSource(), DBPEvaluationContext.UI) + " [" + mapping.getSource().getDataSource().getContainer().getName() + "]");
new Label(composite, SWT.NONE).setText("Target entity: " + mapping.getTargetName() + " [" + (targetDataSource == null ? "?" : targetDataSource.getContainer().getName()) + "]");
mappingViewer = new TableViewer(composite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.widthHint = 600;
gd.heightHint = 300;
mappingViewer.getTable().setLayoutData(gd);
mappingViewer.getTable().setLinesVisible(true);
mappingViewer.getTable().setHeaderVisible(true);
mappingViewer.setContentProvider(new ListContentProvider());
mappingViewer.getTable().addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.character == SWT.DEL) {
for (TableItem item : mappingViewer.getTable().getSelection()) {
DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) item.getData();
attribute.setMappingType(DatabaseMappingType.skip);
}
updateStatus(Status.OK_STATUS);
mappingViewer.refresh();
} else if (e.character == SWT.SPACE) {
for (TableItem item : mappingViewer.getTable().getSelection()) {
DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) item.getData();
attribute.setMappingType(DatabaseMappingType.existing);
try {
attribute.updateMappingType(new VoidProgressMonitor());
} catch (DBException e1) {
updateStatus(GeneralUtils.makeExceptionStatus(e1));
}
}
mappingViewer.refresh();
}
}
});
{
TableViewerColumn columnSource = new TableViewerColumn(mappingViewer, SWT.LEFT);
columnSource.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) cell.getElement();
cell.setText(DBUtils.getObjectFullName(attrMapping.getSource(), DBPEvaluationContext.UI));
if (attrMapping.getIcon() != null) {
cell.setImage(DBeaverIcons.getImage(attrMapping.getIcon()));
}
}
});
columnSource.getColumn().setText("Source Column");
columnSource.getColumn().setWidth(170);
}
{
TableViewerColumn columnSourceType = new TableViewerColumn(mappingViewer, SWT.LEFT);
columnSourceType.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
cell.setText(((DatabaseMappingAttribute) cell.getElement()).getSourceType());
}
});
columnSourceType.getColumn().setText("Source Type");
columnSourceType.getColumn().setWidth(100);
}
{
TableViewerColumn columnTarget = new TableViewerColumn(mappingViewer, SWT.LEFT);
columnTarget.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
DatabaseMappingAttribute mapping = (DatabaseMappingAttribute) cell.getElement();
cell.setText(mapping.getTargetName());
if (mapping.mappingType == DatabaseMappingType.unspecified) {
cell.setBackground(DBeaverUI.getSharedTextColors().getColor(SharedTextColors.COLOR_WARNING));
} else {
cell.setBackground(null);
}
cell.setFont(boldFont);
}
});
columnTarget.getColumn().setText("Target Column");
columnTarget.getColumn().setWidth(170);
columnTarget.setEditingSupport(new EditingSupport(mappingViewer) {
@Override
protected CellEditor getCellEditor(Object element) {
try {
java.util.List<String> items = new ArrayList<>();
DatabaseMappingAttribute mapping = (DatabaseMappingAttribute) element;
if (mapping.getParent().getMappingType() == DatabaseMappingType.existing && mapping.getParent().getTarget() instanceof DBSEntity) {
DBSEntity parentEntity = (DBSEntity) mapping.getParent().getTarget();
for (DBSEntityAttribute attr : CommonUtils.safeCollection(parentEntity.getAttributes(new VoidProgressMonitor()))) {
items.add(attr.getName());
}
}
items.add(DatabaseMappingAttribute.TARGET_NAME_SKIP);
CustomComboBoxCellEditor editor = new CustomComboBoxCellEditor(mappingViewer, mappingViewer.getTable(), items.toArray(new String[items.size()]), SWT.DROP_DOWN);
updateStatus(Status.OK_STATUS);
return editor;
} catch (DBException e) {
updateStatus(GeneralUtils.makeExceptionStatus(e));
return null;
}
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
return ((DatabaseMappingAttribute) element).getTargetName();
}
@Override
protected void setValue(Object element, Object value) {
try {
String name = CommonUtils.toString(value);
DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
if (DatabaseMappingAttribute.TARGET_NAME_SKIP.equals(name)) {
attrMapping.setMappingType(DatabaseMappingType.skip);
} else {
if (attrMapping.getParent().getMappingType() == DatabaseMappingType.existing && attrMapping.getParent().getTarget() instanceof DBSEntity) {
DBSEntity parentEntity = (DBSEntity) attrMapping.getParent().getTarget();
for (DBSEntityAttribute attr : CommonUtils.safeCollection(parentEntity.getAttributes(new VoidProgressMonitor()))) {
if (name.equalsIgnoreCase(attr.getName())) {
attrMapping.setTarget(attr);
attrMapping.setMappingType(DatabaseMappingType.existing);
return;
}
}
}
attrMapping.setMappingType(DatabaseMappingType.create);
attrMapping.setTargetName(name);
}
updateStatus(Status.OK_STATUS);
} catch (DBException e) {
updateStatus(GeneralUtils.makeExceptionStatus(e));
} finally {
mappingViewer.refresh();
}
}
});
}
{
TableViewerColumn columnTargetType = new TableViewerColumn(mappingViewer, SWT.LEFT);
columnTargetType.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) cell.getElement();
DBPDataSource dataSource = settings.getTargetDataSource(attrMapping);
cell.setText(attrMapping.getTargetType(dataSource));
cell.setFont(boldFont);
}
});
columnTargetType.getColumn().setText("Target Type");
columnTargetType.getColumn().setWidth(100);
columnTargetType.setEditingSupport(new EditingSupport(mappingViewer) {
@Override
protected CellEditor getCellEditor(Object element) {
DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
Set<String> types = new LinkedHashSet<>();
DBPDataSource dataSource = settings.getTargetDataSource(attrMapping);
if (dataSource instanceof DBPDataTypeProvider) {
for (DBSDataType type : ((DBPDataTypeProvider) dataSource).getLocalDataTypes()) {
types.add(type.getName());
}
}
types.add(attrMapping.getTargetType(dataSource));
return new CustomComboBoxCellEditor(mappingViewer, mappingViewer.getTable(), types.toArray(new String[types.size()]), SWT.BORDER);
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
return attrMapping.getTargetType(settings.getTargetDataSource(attrMapping));
}
@Override
protected void setValue(Object element, Object value) {
DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
attrMapping.setTargetType(CommonUtils.toString(value));
mappingViewer.refresh(element);
}
});
}
{
TableViewerColumn columnType = new TableViewerColumn(mappingViewer, SWT.LEFT);
columnType.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
DatabaseMappingAttribute mapping = (DatabaseMappingAttribute) cell.getElement();
String text = "";
switch(mapping.getMappingType()) {
case unspecified:
text = "?";
break;
case existing:
text = "existing";
break;
case create:
text = "new";
break;
case skip:
text = "skip";
break;
}
cell.setText(text);
}
});
columnType.getColumn().setText("Mapping");
columnType.getColumn().setWidth(60);
}
mappingViewer.setInput(attributeMappings);
return parent;
}
Aggregations