Search in sources :

Example 6 with DBSDataType

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

the class GenericTableColumnManager method createDatabaseObject.

@Override
protected GenericTableColumn createDatabaseObject(DBRProgressMonitor monitor, DBECommandContext context, GenericTable parent, Object copyFrom) {
    DBSDataType columnType = findBestDataType(parent.getDataSource(), DBConstants.DEFAULT_DATATYPE_NAMES);
    final GenericTableColumn column = new GenericTableColumn(parent);
    column.setName(getNewColumnName(monitor, context, parent));
    column.setTypeName(columnType == null ? "INTEGER" : columnType.getName());
    column.setMaxLength(columnType != null && columnType.getDataKind() == DBPDataKind.STRING ? 100 : 0);
    column.setValueType(columnType == null ? Types.INTEGER : columnType.getTypeID());
    column.setOrdinalPosition(-1);
    return column;
}
Also used : DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) GenericTableColumn(org.jkiss.dbeaver.ext.generic.model.GenericTableColumn)

Example 7 with DBSDataType

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

the class MySQLTableColumn method loadInfo.

private void loadInfo(ResultSet dbResult) throws DBException {
    name = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_NAME);
    ordinalPosition = JDBCUtils.safeGetInt(dbResult, MySQLConstants.COL_ORDINAL_POSITION);
    String typeName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_DATA_TYPE);
    assert typeName != null;
    String keyTypeName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_KEY);
    if (!CommonUtils.isEmpty(keyTypeName)) {
        try {
            keyType = KeyType.valueOf(keyTypeName);
        } catch (IllegalArgumentException e) {
            log.debug(e);
        }
    }
    setTypeName(typeName);
    setValueType(MySQLUtils.typeNameToValueType(typeName));
    DBSDataType dataType = getDataSource().getLocalDataType(typeName);
    this.charLength = JDBCUtils.safeGetLong(dbResult, MySQLConstants.COL_CHARACTER_MAXIMUM_LENGTH);
    if (this.charLength <= 0) {
        if (dataType != null) {
            setMaxLength(CommonUtils.toInt(dataType.getPrecision()));
        }
    } else {
        setMaxLength(this.charLength);
    }
    this.comment = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_COMMENT);
    this.required = !"YES".equals(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_IS_NULLABLE));
    this.setScale(JDBCUtils.safeGetInteger(dbResult, MySQLConstants.COL_NUMERIC_SCALE));
    this.setPrecision(JDBCUtils.safeGetInteger(dbResult, MySQLConstants.COL_NUMERIC_PRECISION));
    String defaultValue = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_DEFAULT);
    if (defaultValue != null) {
        switch(getDataKind()) {
            case STRING:
                // Although I didn't reproduce that locally - perhaps depends on server config.
                if (!SQLConstants.NULL_VALUE.equals(defaultValue) && !SQLUtils.isStringQuoted(defaultValue)) {
                    defaultValue = SQLUtils.quoteString(getDataSource(), defaultValue);
                }
                break;
            case DATETIME:
                if (!defaultValue.isEmpty() && Character.isDigit(defaultValue.charAt(0))) {
                    defaultValue = "'" + defaultValue + "'";
                }
                break;
        }
        setDefaultValue(defaultValue);
    }
    this.collation = getDataSource().getCollation(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLLATION_NAME));
    this.extraInfo = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_EXTRA);
    this.autoGenerated = extraInfo != null && extraInfo.contains(MySQLConstants.EXTRA_AUTO_INCREMENT);
    this.fullTypeName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_TYPE);
    if (!CommonUtils.isEmpty(fullTypeName) && (isTypeEnum() || isTypeSet())) {
        enumValues = parseEnumValues(fullTypeName);
    }
    if (!getDataSource().isMariaDB() && getDataSource().isServerVersionAtLeast(5, 7)) {
        genExpression = JDBCUtils.safeGetString(dbResult, "GENERATION_EXPRESSION");
    }
    for (String modifier : CommonUtils.notEmpty(fullTypeName).toLowerCase().split(" ")) {
        switch(modifier) {
            case "zerofill":
                modifiers |= DBSTypedObject.TYPE_MOD_NUMBER_LEADING_ZEROES;
                break;
            case "unsigned":
                modifiers |= DBSTypedObject.TYPE_MOD_NUMBER_UNSIGNED;
                break;
        }
    }
}
Also used : DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType)

Example 8 with DBSDataType

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

the class SQLServerTableColumnManager method createDatabaseObject.

@Override
protected SQLServerTableColumn createDatabaseObject(DBRProgressMonitor monitor, DBECommandContext context, Object container, Object copyFrom, Map<String, Object> options) {
    SQLServerTable table = (SQLServerTable) container;
    // $NON-NLS-1$
    DBSDataType columnType = findBestDataType(table.getDataSource(), "varchar");
    final SQLServerTableColumn column = new SQLServerTableColumn(table);
    column.setName(getNewColumnName(monitor, context, table));
    column.setDataType((SQLServerDataType) columnType);
    // $NON-NLS-1$
    column.setTypeName(columnType == null ? "varchar" : columnType.getName());
    column.setMaxLength(columnType != null && columnType.getDataKind() == DBPDataKind.STRING ? 100 : 0);
    column.setValueType(columnType == null ? Types.VARCHAR : columnType.getTypeID());
    column.setOrdinalPosition(-1);
    return column;
}
Also used : DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType)

Example 9 with DBSDataType

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

the class SQLDataTypeResolver method resolveAll.

@Override
protected String[] resolveAll(final TemplateContext context) {
    final DBCExecutionContext executionContext = ((DBPContextProvider) context).getExecutionContext();
    if (executionContext == null) {
        return super.resolveAll(context);
    }
    DBPDataTypeProvider dataTypeProvider = DBUtils.getAdapter(DBPDataTypeProvider.class, executionContext.getDataSource());
    if (dataTypeProvider != null) {
        final Collection<? extends DBSDataType> localDataTypes = dataTypeProvider.getLocalDataTypes();
        if (!CommonUtils.isEmpty(localDataTypes)) {
            String[] result = new String[localDataTypes.size()];
            int index = 0;
            for (DBSDataType dataType : localDataTypes) {
                result[index++] = dataType.getName();
            }
            return result;
        }
    }
    return super.resolveAll(context);
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) DBPDataTypeProvider(org.jkiss.dbeaver.model.DBPDataTypeProvider)

Example 10 with DBSDataType

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

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

DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)46 DBException (org.jkiss.dbeaver.DBException)10 DBCException (org.jkiss.dbeaver.model.exec.DBCException)8 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)8 DBSTypedObjectEx (org.jkiss.dbeaver.model.struct.DBSTypedObjectEx)8 ArrayList (java.util.ArrayList)7 SQLException (java.sql.SQLException)6 GridData (org.eclipse.swt.layout.GridData)6 Composite (org.eclipse.swt.widgets.Composite)6 DBPDataTypeProvider (org.jkiss.dbeaver.model.DBPDataTypeProvider)6 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)6 Text (org.eclipse.swt.widgets.Text)4 GenericTableColumn (org.jkiss.dbeaver.ext.generic.model.GenericTableColumn)4 OracleTableColumn (org.jkiss.dbeaver.ext.oracle.model.OracleTableColumn)4 PostgreDataType (org.jkiss.dbeaver.ext.postgresql.model.PostgreDataType)4 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)4 KeyAdapter (org.eclipse.swt.events.KeyAdapter)3 KeyEvent (org.eclipse.swt.events.KeyEvent)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 TableItem (org.eclipse.swt.widgets.TableItem)3