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);
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations