Search in sources :

Example 36 with DBSDataType

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

the class JDBCStructValueHandler method getValueFromObject.

@Override
public Object getValueFromObject(@NotNull DBCSession session, @NotNull DBSTypedObject type, Object object, boolean copy, boolean validateValue) throws DBCException {
    if (object instanceof JDBCComposite) {
        return copy ? ((JDBCComposite) object).cloneValue(session.getProgressMonitor()) : object;
    }
    String typeName;
    try {
        if (object instanceof Struct) {
            typeName = ((Struct) object).getSQLTypeName();
        } else {
            typeName = type.getTypeName();
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getExecutionContext());
    }
    DBSDataType dataType = null;
    try {
        dataType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), typeName);
    } catch (DBException e) {
        log.debug("Error resolving data type '" + typeName + "'", e);
    }
    if (dataType == null) {
        if (object instanceof Struct) {
            return new JDBCCompositeDynamic(session, (Struct) object, null);
        } else {
            return new JDBCCompositeUnknown(session, object);
        }
    }
    if (object == null) {
        return new JDBCCompositeStatic(session, dataType, new JDBCStructImpl(dataType.getTypeName(), null, ""));
    } else if (object instanceof Struct) {
        return new JDBCCompositeStatic(session, dataType, (Struct) object);
    } else {
        return new JDBCCompositeUnknown(session, object);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCCompositeStatic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeStatic) SQLException(java.sql.SQLException) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) JDBCCompositeDynamic(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeDynamic) JDBCStructImpl(org.jkiss.dbeaver.model.impl.jdbc.JDBCStructImpl) JDBCCompositeUnknown(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCCompositeUnknown) DBCException(org.jkiss.dbeaver.model.exec.DBCException) JDBCComposite(org.jkiss.dbeaver.model.impl.jdbc.data.JDBCComposite) Struct(java.sql.Struct)

Example 37 with DBSDataType

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

the class EditVirtualAttributePage method createPageContents.

@Override
protected Control createPageContents(Composite parent) {
    final Composite dialogArea = UIUtils.createComposite(parent, 1);
    dialogArea.setLayoutData(new GridData(GridData.FILL_BOTH));
    DBPDataSource dataSource = vAttr.getEntity().getDataSource();
    Composite panel = UIUtils.createComposite(dialogArea, 2);
    panel.setLayoutData(new GridData(GridData.FILL_BOTH));
    String name = vAttr.getName();
    int index = 1;
    for (; ; ) {
        DBVEntityAttribute vAttr2 = vAttr.getEntity().getVirtualAttribute(name);
        if (vAttr2 == null || vAttr2 == vAttr) {
            break;
        }
        index++;
        name = vAttr.getName() + index;
    }
    nameText = UIUtils.createLabelText(panel, "Column Name", name);
    typeCombo = UIUtils.createLabelCombo(panel, "Type Name", "Column type name", SWT.BORDER | SWT.DROP_DOWN);
    {
        DBPDataTypeProvider dataTypeProvider = DBUtils.getAdapter(DBPDataTypeProvider.class, dataSource);
        if (dataTypeProvider != null) {
            List<DBSDataType> localDataTypes = new ArrayList<>(dataTypeProvider.getLocalDataTypes());
            localDataTypes.sort(Comparator.comparing(DBSDataType::getFullTypeName));
            for (DBSDataType dataType : localDataTypes) {
                typeCombo.add(dataType.getFullTypeName());
            }
            String defTypeName = vAttr.getTypeName();
            if (CommonUtils.isEmpty(defTypeName)) {
                defTypeName = dataTypeProvider.getDefaultDataTypeName(DBPDataKind.STRING);
                vAttr.setTypeName(defTypeName);
                DBSDataType dataType = dataTypeProvider.getLocalDataType(defTypeName);
                if (dataType != null) {
                    vAttr.setDataKind(dataType.getDataKind());
                }
            }
            if (!CommonUtils.isEmpty(defTypeName)) {
                typeCombo.setText(defTypeName);
            }
            typeCombo.addModifyListener(e -> {
                DBSDataType dataType = dataTypeProvider.getLocalDataType(typeCombo.getText());
                if (dataType != null) {
                    kindCombo.setText(dataType.getDataKind().name());
                }
            });
        } else {
            typeCombo.setText(CommonUtils.notEmpty(vAttr.getTypeName()));
        }
        ContentAssistUtils.installContentProposal(typeCombo, new ComboContentAdapter(), new StringContentProposalProvider(typeCombo.getItems()));
    }
    kindCombo = UIUtils.createLabelCombo(panel, "Data Kind", "Column data kind", SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
    for (DBPDataKind dataKind : DBPDataKind.values()) {
        if (dataKind != DBPDataKind.UNKNOWN) {
            kindCombo.add(dataKind.name());
        }
    }
    kindCombo.setText(vAttr.getDataKind().name());
    expressionText = UIUtils.createLabelText(panel, "Expression", CommonUtils.notEmpty(vAttr.getExpression()), SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 300;
    gd.heightHint = expressionText.getLineHeight() * 5;
    expressionText.setLayoutData(gd);
    List<String> expressionProposals = new ArrayList<>();
    if (viewer != null) {
        for (DBDAttributeBinding attr : viewer.getModel().getAttributes()) {
            expressionProposals.add(attr.getLabel());
        }
    }
    ContentAssistUtils.installContentProposal(expressionText, new SmartTextContentAdapter(), new StringContentProposalProvider(expressionProposals.toArray(new String[0])));
    previewText = UIUtils.createLabelText(panel, "Preview", "", SWT.BORDER | SWT.READ_ONLY);
    previewText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    expressionText.addModifyListener(e -> generatePreviewValue());
    generatePreviewValue();
    return dialogArea;
}
Also used : StringContentProposalProvider(org.jkiss.dbeaver.ui.contentassist.StringContentProposalProvider) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) BaseObjectEditPage(org.jkiss.dbeaver.ui.editors.object.struct.BaseObjectEditPage) ArrayList(java.util.ArrayList) IHelpContextIdProvider(org.jkiss.dbeaver.ui.IHelpContextIdProvider) DBVUtils(org.jkiss.dbeaver.model.virtual.DBVUtils) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) Composite(org.eclipse.swt.widgets.Composite) UIUtils(org.jkiss.dbeaver.ui.UIUtils) ResultSetViewer(org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer) GridData(org.eclipse.swt.layout.GridData) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) ComboContentAdapter(org.eclipse.jface.fieldassist.ComboContentAdapter) GeneralUtils(org.jkiss.dbeaver.utils.GeneralUtils) CommonUtils(org.jkiss.utils.CommonUtils) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) SmartTextContentAdapter(org.jkiss.dbeaver.ui.contentassist.SmartTextContentAdapter) ContentAssistUtils(org.jkiss.dbeaver.ui.contentassist.ContentAssistUtils) List(java.util.List) DBException(org.jkiss.dbeaver.DBException) SWT(org.eclipse.swt.SWT) DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) JexlExpression(org.apache.commons.jexl3.JexlExpression) Comparator(java.util.Comparator) Control(org.eclipse.swt.widgets.Control) ComboContentAdapter(org.eclipse.jface.fieldassist.ComboContentAdapter) SmartTextContentAdapter(org.jkiss.dbeaver.ui.contentassist.SmartTextContentAdapter) Composite(org.eclipse.swt.widgets.Composite) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) ArrayList(java.util.ArrayList) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) StringContentProposalProvider(org.jkiss.dbeaver.ui.contentassist.StringContentProposalProvider) GridData(org.eclipse.swt.layout.GridData) ArrayList(java.util.ArrayList) List(java.util.List)

Example 38 with DBSDataType

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

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 39 with DBSDataType

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

the class EditVirtualAttributePage method createPageContents.

@Override
protected Control createPageContents(Composite parent) {
    final Composite dialogArea = UIUtils.createComposite(parent, 1);
    dialogArea.setLayoutData(new GridData(GridData.FILL_BOTH));
    DBPDataSource dataSource = vAttr.getEntity().getDataSource();
    Composite panel = UIUtils.createComposite(dialogArea, 2);
    panel.setLayoutData(new GridData(GridData.FILL_BOTH));
    String name = vAttr.getName();
    int index = 1;
    for (; ; ) {
        DBVEntityAttribute vAttr2 = vAttr.getEntity().getVirtualAttribute(name);
        if (vAttr2 == null || vAttr2 == vAttr) {
            break;
        }
        index++;
        name = vAttr.getName() + index;
    }
    nameText = UIUtils.createLabelText(panel, "Column Name", name);
    typeCombo = UIUtils.createLabelCombo(panel, "Type Name", "Column type name", SWT.BORDER | SWT.DROP_DOWN);
    {
        DBPDataTypeProvider dataTypeProvider = DBUtils.getAdapter(DBPDataTypeProvider.class, dataSource);
        if (dataTypeProvider != null) {
            List<DBSDataType> localDataTypes = new ArrayList<>(dataTypeProvider.getLocalDataTypes());
            localDataTypes.sort(Comparator.comparing(DBSDataType::getFullTypeName));
            for (DBSDataType dataType : localDataTypes) {
                typeCombo.add(dataType.getFullTypeName());
            }
            String defTypeName = vAttr.getTypeName();
            if (CommonUtils.isEmpty(defTypeName)) {
                defTypeName = dataTypeProvider.getDefaultDataTypeName(DBPDataKind.STRING);
                vAttr.setTypeName(defTypeName);
                DBSDataType dataType = dataTypeProvider.getLocalDataType(defTypeName);
                if (dataType != null) {
                    vAttr.setDataKind(dataType.getDataKind());
                }
            }
            if (!CommonUtils.isEmpty(defTypeName)) {
                typeCombo.setText(defTypeName);
            }
            typeCombo.addModifyListener(e -> {
                DBSDataType dataType = dataTypeProvider.getLocalDataType(typeCombo.getText());
                if (dataType != null) {
                    kindCombo.setText(dataType.getDataKind().name());
                }
            });
        } else {
            typeCombo.setText(CommonUtils.notEmpty(vAttr.getTypeName()));
        }
        ContentAssistUtils.installContentProposal(typeCombo, new ComboContentAdapter(), new StringContentProposalProvider(typeCombo.getItems()));
    }
    kindCombo = UIUtils.createLabelCombo(panel, "Data Kind", "Column data kind", SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
    for (DBPDataKind dataKind : DBPDataKind.values()) {
        if (dataKind != DBPDataKind.UNKNOWN) {
            kindCombo.add(dataKind.name());
        }
    }
    kindCombo.setText(vAttr.getDataKind().name());
    expressionText = UIUtils.createLabelText(panel, "Expression", CommonUtils.notEmpty(vAttr.getExpression()), SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 300;
    gd.heightHint = expressionText.getLineHeight() * 5;
    expressionText.setLayoutData(gd);
    List<String> expressionProposals = new ArrayList<>();
    if (viewer != null) {
        for (DBDAttributeBinding attr : viewer.getModel().getAttributes()) {
            expressionProposals.add(attr.getLabel());
        }
    }
    ContentAssistUtils.installContentProposal(expressionText, new SmartTextContentAdapter(), new StringContentProposalProvider(expressionProposals.toArray(new String[0])));
    previewText = UIUtils.createLabelText(panel, "Preview", "", SWT.BORDER | SWT.READ_ONLY);
    previewText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    expressionText.addModifyListener(e -> generatePreviewValue());
    generatePreviewValue();
    return dialogArea;
}
Also used : StringContentProposalProvider(org.jkiss.dbeaver.ui.contentassist.StringContentProposalProvider) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) BaseObjectEditPage(org.jkiss.dbeaver.ui.editors.object.struct.BaseObjectEditPage) ArrayList(java.util.ArrayList) IHelpContextIdProvider(org.jkiss.dbeaver.ui.IHelpContextIdProvider) DBVUtils(org.jkiss.dbeaver.model.virtual.DBVUtils) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) Composite(org.eclipse.swt.widgets.Composite) UIUtils(org.jkiss.dbeaver.ui.UIUtils) ResultSetViewer(org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer) GridData(org.eclipse.swt.layout.GridData) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) ComboContentAdapter(org.eclipse.jface.fieldassist.ComboContentAdapter) GeneralUtils(org.jkiss.dbeaver.utils.GeneralUtils) CommonUtils(org.jkiss.utils.CommonUtils) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) SmartTextContentAdapter(org.jkiss.dbeaver.ui.contentassist.SmartTextContentAdapter) ContentAssistUtils(org.jkiss.dbeaver.ui.contentassist.ContentAssistUtils) List(java.util.List) DBException(org.jkiss.dbeaver.DBException) SWT(org.eclipse.swt.SWT) DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) JexlExpression(org.apache.commons.jexl3.JexlExpression) Comparator(java.util.Comparator) Control(org.eclipse.swt.widgets.Control) ComboContentAdapter(org.eclipse.jface.fieldassist.ComboContentAdapter) SmartTextContentAdapter(org.jkiss.dbeaver.ui.contentassist.SmartTextContentAdapter) Composite(org.eclipse.swt.widgets.Composite) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) ArrayList(java.util.ArrayList) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) StringContentProposalProvider(org.jkiss.dbeaver.ui.contentassist.StringContentProposalProvider) GridData(org.eclipse.swt.layout.GridData) ArrayList(java.util.ArrayList) List(java.util.List)

Example 40 with DBSDataType

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

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)

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