Search in sources :

Example 21 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by serge-rider.

the class SearchDataQuery method findRows.

private DBCStatistics findRows(@NotNull DBCSession session, @NotNull DBSDataContainer dataContainer, @NotNull TestDataReceiver dataReceiver) throws DBCException {
    DBSEntity entity;
    if (dataContainer instanceof DBSEntity) {
        entity = (DBSEntity) dataContainer;
    } else {
        log.warn("Data container " + dataContainer + " isn't entity");
        return null;
    }
    try {
        List<DBDAttributeConstraint> constraints = new ArrayList<>();
        for (DBSEntityAttribute attribute : CommonUtils.safeCollection(entity.getAttributes(session.getProgressMonitor()))) {
            if (params.fastSearch) {
                if (DBUtils.findAttributeIndex(session.getProgressMonitor(), attribute) == null) {
                    continue;
                }
            }
            if (DBUtils.isPseudoAttribute(attribute) || DBUtils.isHiddenObject(attribute)) {
                continue;
            }
            DBCLogicalOperator[] supportedOperators = DBUtils.getAttributeOperators(attribute);
            DBCLogicalOperator operator;
            Object value;
            switch(attribute.getDataKind()) {
                case BOOLEAN:
                    continue;
                case NUMERIC:
                    if (!params.searchNumbers) {
                        continue;
                    }
                    if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
                        continue;
                    }
                    operator = DBCLogicalOperator.EQUALS;
                    try {
                        value = Integer.valueOf(params.searchString);
                    } catch (NumberFormatException e) {
                        try {
                            value = Long.valueOf(params.searchString);
                        } catch (NumberFormatException e1) {
                            try {
                                value = Double.valueOf(params.searchString);
                            } catch (NumberFormatException e2) {
                                try {
                                    value = new BigDecimal(params.searchString);
                                } catch (Exception e3) {
                                    // Not a number
                                    continue;
                                }
                            }
                        }
                    }
                    break;
                case CONTENT:
                case BINARY:
                    if (!params.searchLOBs) {
                        continue;
                    }
                case STRING:
                    if (!params.isCaseSensitive() && ArrayUtils.contains(supportedOperators, DBCLogicalOperator.ILIKE)) {
                        operator = DBCLogicalOperator.ILIKE;
                        value = "%" + params.searchString + "%";
                    } else if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.LIKE)) {
                        operator = DBCLogicalOperator.LIKE;
                        value = "%" + params.searchString + "%";
                    } else if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
                        operator = DBCLogicalOperator.EQUALS;
                        value = params.searchString;
                    } else {
                        continue;
                    }
                    break;
                default:
                    {
                        // On success search by exact match
                        if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
                            continue;
                        }
                        String typeName = attribute.getTypeName();
                        if (typeName.equals(DBConstants.TYPE_NAME_UUID) || typeName.equals(DBConstants.TYPE_NAME_UUID2)) {
                            try {
                                UUID uuid = UUID.fromString(params.searchString);
                                operator = DBCLogicalOperator.EQUALS;
                                value = uuid.toString();
                            } catch (Exception e) {
                                // No a UUID
                                continue;
                            }
                        } else {
                            continue;
                        }
                    }
            }
            DBDAttributeConstraint constraint = new DBDAttributeConstraint(attribute, constraints.size());
            constraint.setOperator(operator);
            constraint.setValue(value);
            constraint.setVisible(true);
            constraints.add(constraint);
        }
        if (constraints.isEmpty()) {
            return null;
        }
        dataReceiver.filter = new DBDDataFilter(constraints);
        dataReceiver.filter.setAnyConstraint(true);
        DBCExecutionSource searchSource = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), this);
        return dataContainer.readData(searchSource, session, dataReceiver, dataReceiver.filter, -1, -1, 0, 0);
    } catch (DBException e) {
        throw new DBCException("Error finding rows", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) BigDecimal(java.math.BigDecimal) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) DBException(org.jkiss.dbeaver.DBException) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Example 22 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by serge-rider.

the class PostgreConstraintConfigurator method configureObject.

@Override
public PostgreTableConstraint configureObject(DBRProgressMonitor monitor, Object parent, PostgreTableConstraint constraint) {
    return UITask.run(() -> {
        EditConstraintPage editPage = new EditConstraintPage(PostgreMessages.edit_constraint_page_add_constraint, constraint, new DBSEntityConstraintType[] { DBSEntityConstraintType.PRIMARY_KEY, DBSEntityConstraintType.UNIQUE_KEY, DBSEntityConstraintType.CHECK });
        if (!editPage.edit()) {
            return null;
        }
        constraint.setName(editPage.getConstraintName());
        constraint.setConstraintType(editPage.getConstraintType());
        if (constraint.getConstraintType().isCustom()) {
            constraint.setSource(editPage.getConstraintExpression());
        } else {
            int colIndex = 1;
            for (DBSEntityAttribute tableColumn : editPage.getSelectedAttributes()) {
                constraint.addColumn(new PostgreTableConstraintColumn(constraint, (PostgreAttribute) tableColumn, colIndex++));
            }
        }
        return constraint;
    });
}
Also used : DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) PostgreTableConstraintColumn(org.jkiss.dbeaver.ext.postgresql.model.PostgreTableConstraintColumn) EditConstraintPage(org.jkiss.dbeaver.ui.editors.object.struct.EditConstraintPage) PostgreTableConstraint(org.jkiss.dbeaver.ext.postgresql.model.PostgreTableConstraint) PostgreAttribute(org.jkiss.dbeaver.ext.postgresql.model.PostgreAttribute)

Example 23 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by serge-rider.

the class MySQLIndexConfigurator method configureObject.

@Override
public MySQLTableIndex configureObject(DBRProgressMonitor monitor, Object parent, MySQLTableIndex index) {
    return UITask.run(() -> {
        MyEditIndexPage editPage = new MyEditIndexPage(index);
        if (!editPage.edit()) {
            return null;
        }
        StringBuilder idxName = new StringBuilder(64);
        idxName.append(CommonUtils.escapeIdentifier(index.getParentObject().getName()));
        int colIndex = 1;
        for (DBSEntityAttribute tableColumn : editPage.getSelectedAttributes()) {
            if (colIndex == 1) {
                // $NON-NLS-1$
                idxName.append("_").append(CommonUtils.escapeIdentifier(tableColumn.getName()));
            }
            Integer length = (Integer) editPage.getAttributeProperty(tableColumn, MyEditIndexPage.PROP_LENGTH);
            index.addColumn(new MySQLTableIndexColumn(index, (MySQLTableColumn) tableColumn, colIndex++, !Boolean.TRUE.equals(editPage.getAttributeProperty(tableColumn, EditIndexPage.PROP_DESC)), false, length == null ? null : String.valueOf(length)));
        }
        // $NON-NLS-1$
        idxName.append("_IDX");
        index.setName(DBObjectNameCaseTransformer.transformObjectName(index, idxName.toString()));
        index.setName(idxName.toString());
        index.setIndexType(editPage.getIndexType());
        index.setUnique(editPage.isUnique());
        return index;
    });
}
Also used : MySQLTableIndexColumn(org.jkiss.dbeaver.ext.mysql.model.MySQLTableIndexColumn) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) MySQLTableColumn(org.jkiss.dbeaver.ext.mysql.model.MySQLTableColumn)

Example 24 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by serge-rider.

the class MySQLTableColumnManager method createDatabaseObject.

@Override
protected MySQLTableColumn createDatabaseObject(final DBRProgressMonitor monitor, final DBECommandContext context, final Object container, Object copyFrom, Map<String, Object> options) throws DBException {
    MySQLTable table = (MySQLTable) container;
    MySQLTableColumn column;
    if (copyFrom instanceof DBSEntityAttribute) {
        column = new MySQLTableColumn(monitor, table, (DBSEntityAttribute) copyFrom);
    } else {
        column = new MySQLTableColumn(table);
        // $NON-NLS-1$
        DBSDataType columnType = findBestDataType(table.getDataSource(), "varchar");
        column.setName(getNewColumnName(monitor, context, table));
        final String typeName = columnType == null ? "integer" : columnType.getName().toLowerCase();
        // $NON-NLS-1$
        column.setTypeName(typeName);
        column.setMaxLength(columnType != null && columnType.getDataKind() == DBPDataKind.STRING ? 100 : 0);
        column.setValueType(columnType == null ? Types.INTEGER : columnType.getTypeID());
        column.setOrdinalPosition(table.getCachedAttributes().size() + 1);
        column.setFullTypeName(DBUtils.getFullTypeName(column));
    }
    return column;
}
Also used : DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) MySQLTableColumn(org.jkiss.dbeaver.ext.mysql.model.MySQLTableColumn) MySQLTable(org.jkiss.dbeaver.ext.mysql.model.MySQLTable)

Example 25 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.

the class ColumnsMappingDialog method createDialogArea.

@Override
protected Composite createDialogArea(Composite parent) {
    DBPDataSource targetDataSource = settings.getTargetDataSource(mapping);
    boldFont = UIUtils.makeBoldFont(parent.getFont());
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    DBPDataSource sourceDataSource = mapping.getSource().getDataSource();
    UIUtils.createLabelText(composite, DTUIMessages.columns_mapping_dialog_composite_label_text_source_container, sourceDataSource == null ? "" : sourceDataSource.getContainer().getName(), SWT.BORDER | SWT.READ_ONLY);
    Text sourceEntity = UIUtils.createLabelText(composite, DTUIMessages.columns_mapping_dialog_composite_label_text_source_entity, DBUtils.getObjectFullName(mapping.getSource(), DBPEvaluationContext.UI), SWT.BORDER | SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL);
    ((GridData) sourceEntity.getLayoutData()).widthHint = 600;
    ((GridData) sourceEntity.getLayoutData()).heightHint = UIUtils.getFontHeight(sourceEntity) * 3;
    UIUtils.createLabelText(composite, DTUIMessages.columns_mapping_dialog_composite_label_text_target_container, (targetDataSource == null ? "?" : targetDataSource.getContainer().getName()), SWT.BORDER | SWT.READ_ONLY);
    Text targetEntity = UIUtils.createLabelText(composite, DTUIMessages.columns_mapping_dialog_composite_label_text_target_entity, mapping.getTargetName(), SWT.BORDER | SWT.READ_ONLY);
    ((GridData) targetEntity.getLayoutData()).widthHint = 600;
    ((GridData) targetEntity.getLayoutData()).heightHint = UIUtils.getFontHeight(sourceEntity) * 3;
    mappingViewer = new TableViewer(composite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 600;
    gd.heightHint = 300;
    gd.horizontalSpan = 2;
    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);
                }
                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) {
                        DBWorkbench.getPlatformUI().showError("Bad mapping", "Invalid column mapping", 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(DTUIMessages.columns_mapping_dialog_column_source_text);
        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(DTUIMessages.columns_mapping_dialog_column_source_type_text);
        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.getMappingType() == DatabaseMappingType.unspecified) {
                    cell.setBackground(UIUtils.getSharedTextColors().getColor(SharedTextColors.COLOR_WARNING));
                } else {
                    cell.setBackground(null);
                }
                cell.setFont(boldFont);
            }
        });
        columnTarget.getColumn().setText(DTUIMessages.columns_mapping_dialog_column_target_text);
        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);
                    return new CustomComboBoxCellEditor(mappingViewer, mappingViewer.getTable(), items.toArray(new String[0]), SWT.DROP_DOWN);
                } catch (DBException e) {
                    DBWorkbench.getPlatformUI().showError("Bad value", "Wrong target column", 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);
                                    attrMapping.setTargetName(name);
                                    return;
                                }
                            }
                        }
                        attrMapping.setMappingType(DatabaseMappingType.create);
                        attrMapping.setTargetName(name);
                    }
                } catch (DBException e) {
                    DBWorkbench.getPlatformUI().showError("Bad value", "Wrong target", 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, true));
                cell.setFont(boldFont);
            }
        });
        columnTargetType.getColumn().setText(DTUIMessages.columns_mapping_dialog_column_target_type_text);
        columnTargetType.getColumn().setWidth(100);
        columnTargetType.setEditingSupport(new EditingSupport(mappingViewer) {

            @Override
            protected CellEditor getCellEditor(Object element) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
                Set<String> types = new TreeSet<>();
                DBPDataSource dataSource = settings.getTargetDataSource(attrMapping);
                if (dataSource instanceof DBPDataTypeProvider) {
                    for (DBSDataType type : ((DBPDataTypeProvider) dataSource).getLocalDataTypes()) {
                        types.add(type.getName());
                    }
                }
                types.add(attrMapping.getTargetType(dataSource, true));
                return new CustomComboBoxCellEditor(mappingViewer, mappingViewer.getTable(), types.toArray(new String[0]), 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), true);
            }

            @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 = DTUIMessages.columns_mapping_dialog_cell_text_existing;
                        break;
                    case create:
                        text = DTUIMessages.columns_mapping_dialog_cell_text_new;
                        break;
                    case skip:
                        text = DTUIMessages.columns_mapping_dialog_cell_text_skip;
                        break;
                }
                cell.setText(text);
            }
        });
        columnType.getColumn().setText(DTUIMessages.columns_mapping_dialog_column_type_text_mapping);
        columnType.getColumn().setWidth(60);
    }
    mappingViewer.setInput(attributeMappings);
    return parent;
}
Also used : DBException(org.jkiss.dbeaver.DBException) KeyAdapter(org.eclipse.swt.events.KeyAdapter) TableItem(org.eclipse.swt.widgets.TableItem) CustomComboBoxCellEditor(org.jkiss.dbeaver.ui.controls.CustomComboBoxCellEditor) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) Composite(org.eclipse.swt.widgets.Composite) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) Text(org.eclipse.swt.widgets.Text) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DatabaseMappingAttribute(org.jkiss.dbeaver.tools.transfer.database.DatabaseMappingAttribute) java.util(java.util) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) GridData(org.eclipse.swt.layout.GridData) DBPDataTypeProvider(org.jkiss.dbeaver.model.DBPDataTypeProvider) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Aggregations

DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)77 DBException (org.jkiss.dbeaver.DBException)25 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)16 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)14 ArrayList (java.util.ArrayList)11 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)10 DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)10 EditIndexPage (org.jkiss.dbeaver.ui.editors.object.struct.EditIndexPage)10 DBSEntityAttributeRef (org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef)9 MySQLTableColumn (org.jkiss.dbeaver.ext.mysql.model.MySQLTableColumn)6 DBDRowIdentifier (org.jkiss.dbeaver.model.data.DBDRowIdentifier)6 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)6 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)6 EditConstraintPage (org.jkiss.dbeaver.ui.editors.object.struct.EditConstraintPage)6 TableItem (org.eclipse.swt.widgets.TableItem)5 BigDecimal (java.math.BigDecimal)4 SQLServerTableColumn (org.jkiss.dbeaver.ext.mssql.model.SQLServerTableColumn)4 KeyAdapter (org.eclipse.swt.events.KeyAdapter)3 KeyEvent (org.eclipse.swt.events.KeyEvent)3 GridData (org.eclipse.swt.layout.GridData)3