use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.
the class BinaryAttributeTransformer method transformAttribute.
@Override
public void transformAttribute(@NotNull DBCSession session, @NotNull DBDAttributeBinding attribute, @NotNull List<Object[]> rows, @NotNull Map<String, String> options) throws DBException {
DBPDataSource dataSource = session.getDataSource();
String formatterId = options.getOrDefault(PROP_FORMAT, FORMAT_HEX);
DBDBinaryFormatter formatter;
if (FORMAT_NATIVE.equals(formatterId) && dataSource instanceof SQLDataSource) {
formatter = ((SQLDataSource) dataSource).getSQLDialect().getNativeBinaryFormatter();
} else {
formatter = DBValueFormatting.getBinaryPresentation(formatterId);
}
if (formatter == null) {
formatter = new BinaryFormatterString();
}
String encodingName = options.getOrDefault(PROP_ENCODING, GeneralUtils.UTF8_ENCODING);
Charset charset;
try {
charset = Charset.forName(encodingName);
} catch (Exception e) {
log.warn(e);
charset = Charset.defaultCharset();
}
attribute.setTransformHandler(new BinaryValueHandler(attribute.getValueHandler(), charset, formatter));
}
use of org.jkiss.dbeaver.model.DBPDataSource 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;
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.
the class DatabaseConsumerPageMapping method createMappingsTree.
private void createMappingsTree(Composite composite) {
// Mapping table
mappingViewer = new TreeViewer(composite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
mappingViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
mappingViewer.getTree().setLinesVisible(true);
mappingViewer.getTree().setHeaderVisible(true);
TreeViewerColumn columnSource = new TreeViewerColumn(mappingViewer, SWT.LEFT);
columnSource.setLabelProvider(new MappingLabelProvider() {
@Override
public void update(ViewerCell cell) {
DatabaseMappingObject mapping = (DatabaseMappingObject) cell.getElement();
cell.setText(DBUtils.getObjectFullName(mapping.getSource(), DBPEvaluationContext.UI));
if (mapping.getIcon() != null) {
cell.setImage(DBeaverIcons.getImage(mapping.getIcon()));
}
super.update(cell);
}
});
columnSource.getColumn().setText("Source");
TreeViewerColumn columnTarget = new TreeViewerColumn(mappingViewer, SWT.LEFT);
columnTarget.setLabelProvider(new MappingLabelProvider() {
@Override
public void update(ViewerCell cell) {
DatabaseMappingObject mapping = (DatabaseMappingObject) cell.getElement();
cell.setText(mapping.getTargetName());
if (mapping.getMappingType() == DatabaseMappingType.unspecified) {
cell.setBackground(DBeaverUI.getSharedTextColors().getColor(SharedTextColors.COLOR_WARNING));
} else {
cell.setBackground(null);
}
super.update(cell);
}
});
columnTarget.getColumn().setText("Target");
columnTarget.setEditingSupport(new EditingSupport(mappingViewer) {
@Override
protected CellEditor getCellEditor(Object element) {
try {
return createTargetEditor(element);
} catch (DBException e) {
setErrorMessage(e.getMessage());
return null;
}
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
DatabaseMappingObject mapping = (DatabaseMappingObject) element;
if (mapping.getMappingType() == DatabaseMappingType.unspecified) {
return transformTargetName(mapping.getSource().getName());
}
if (mapping instanceof DatabaseMappingContainer) {
if (mapping.getMappingType() == DatabaseMappingType.existing) {
return ((DatabaseMappingContainer) mapping).getTarget();
}
return mapping.getTargetName();
} else {
if (mapping.getMappingType() == DatabaseMappingType.existing) {
return ((DatabaseMappingAttribute) mapping).getTarget();
}
return mapping.getTargetName();
}
}
@Override
protected void setValue(final Object element, Object value) {
try {
final DatabaseConsumerSettings settings = getDatabaseConsumerSettings();
String name = CommonUtils.toString(value);
DBPDataSource dataSource = settings.getTargetDataSource((DatabaseMappingObject) element);
if (!name.equals(DatabaseMappingAttribute.TARGET_NAME_SKIP) && !name.equals(TARGET_NAME_BROWSE) && dataSource != null) {
name = DBObjectNameCaseTransformer.transformName(dataSource, name);
}
setMappingTarget((DatabaseMappingObject) element, name);
// mappingViewer.setSelection(mappingViewer.getSelection());
mappingViewer.update(element, null);
updatePageCompletion();
} catch (DBException e) {
DBUserInterface.getInstance().showError("Mapping error", "Error setting target table", e);
}
}
});
// TreeViewerEditor.create(mappingViewer, new TreeViewerFocusCellManager(), ColumnViewerEditor.TABBING_CYCLE_IN_ROW);
TreeViewerColumn columnMapping = new TreeViewerColumn(mappingViewer, SWT.LEFT);
columnMapping.setLabelProvider(new MappingLabelProvider() {
@Override
public void update(ViewerCell cell) {
DatabaseMappingObject mapping = (DatabaseMappingObject) cell.getElement();
cell.setText(mapping.getMappingType().name());
super.update(cell);
}
});
columnMapping.getColumn().setText("Mapping");
columnMapping.setEditingSupport(new EditingSupport(mappingViewer) {
@Override
protected CellEditor getCellEditor(Object element) {
List<String> mappingTypes = new ArrayList<>();
mappingTypes.add(DatabaseMappingType.skip.name());
DatabaseMappingObject mapping = (DatabaseMappingObject) element;
if (mapping instanceof DatabaseMappingAttribute) {
mappingTypes.add(((DatabaseMappingAttribute) mapping).getParent().getMappingType().name());
} else {
mappingTypes.add(mapping.getMappingType().name());
}
return new CustomComboBoxCellEditor(mappingViewer, mappingViewer.getTree(), mappingTypes.toArray(new String[mappingTypes.size()]), SWT.DROP_DOWN | SWT.READ_ONLY);
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
DatabaseMappingObject mapping = (DatabaseMappingObject) element;
return mapping.getMappingType().name();
}
@Override
protected void setValue(Object element, Object value) {
try {
DatabaseMappingObject mapping = (DatabaseMappingObject) element;
DatabaseMappingType mappingType = DatabaseMappingType.valueOf(value.toString());
if (mapping instanceof DatabaseMappingAttribute) {
((DatabaseMappingAttribute) mapping).setMappingType(mappingType);
} else {
((DatabaseMappingContainer) mapping).refreshMappingType(getWizard().getContainer(), mappingType);
}
mappingViewer.refresh();
setErrorMessage(null);
} catch (DBException e) {
setErrorMessage(e.getMessage());
}
}
});
mappingViewer.setContentProvider(new TreeContentProvider() {
@Override
public boolean hasChildren(Object element) {
return element instanceof DatabaseMappingContainer;
}
@Override
public Object[] getChildren(Object parentElement) {
if (parentElement instanceof DatabaseMappingContainer) {
return ((DatabaseMappingContainer) parentElement).getAttributeMappings(getContainer()).toArray();
}
return null;
}
});
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.
the class DatabaseTransferConsumer method startTransfer.
@Override
public void startTransfer(DBRProgressMonitor monitor) throws DBException {
// Create all necessary database objects
monitor.beginTask("Create necessary database objects", 1);
try {
DBSObjectContainer container = settings.getContainer();
if (container == null) {
throw new DBException("No target datasource");
}
boolean hasNewObjects = false;
DBPDataSource dataSource = container.getDataSource();
try (DBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Create target metadata")) {
for (DatabaseMappingContainer containerMapping : settings.getDataMappings().values()) {
switch(containerMapping.getMappingType()) {
case create:
createTargetTable(session, containerMapping);
hasNewObjects = true;
break;
case existing:
for (DatabaseMappingAttribute attr : containerMapping.getAttributeMappings(monitor)) {
if (attr.getMappingType() == DatabaseMappingType.create) {
createTargetAttribute(session, attr);
hasNewObjects = true;
}
}
break;
}
}
}
if (hasNewObjects) {
// Refresh node
monitor.subTask("Refresh navigator model");
settings.getContainerNode().refreshNode(monitor, this);
// Reflect database changes in mappings
for (DatabaseMappingContainer containerMapping : settings.getDataMappings().values()) {
switch(containerMapping.getMappingType()) {
case create:
DBSObject newTarget = container.getChild(monitor, containerMapping.getTargetName());
if (newTarget == null) {
throw new DBCException("New table " + containerMapping.getTargetName() + " not found in container " + DBUtils.getObjectFullName(container, DBPEvaluationContext.UI));
} else if (!(newTarget instanceof DBSDataManipulator)) {
throw new DBCException("New table " + DBUtils.getObjectFullName(newTarget, DBPEvaluationContext.UI) + " doesn't support data manipulation");
}
containerMapping.setTarget((DBSDataManipulator) newTarget);
containerMapping.setMappingType(DatabaseMappingType.existing);
// ! Fall down is ok here
case existing:
for (DatabaseMappingAttribute attr : containerMapping.getAttributeMappings(monitor)) {
if (attr.getMappingType() == DatabaseMappingType.create) {
attr.updateMappingType(monitor);
if (attr.getTarget() == null) {
throw new DBCException("Can't find target attribute '" + attr.getTargetName() + "' in '" + containerMapping.getTargetName() + "'");
}
}
}
break;
}
}
}
} finally {
monitor.done();
}
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.
the class DatabaseTransferConsumer method initExporter.
private void initExporter(DBRProgressMonitor monitor) throws DBCException {
containerMapping = settings.getDataMapping(sourceObject);
if (containerMapping == null) {
throw new DBCException("Can't find container mapping for " + DBUtils.getObjectFullName(sourceObject, DBPEvaluationContext.UI));
}
DBPDataSource dataSource = containerMapping.getTarget().getDataSource();
assert (dataSource != null);
try {
targetContext = settings.isOpenNewConnections() ? dataSource.openIsolatedContext(monitor, "Data transfer consumer") : dataSource.getDefaultContext(false);
} catch (DBException e) {
throw new DBCException("Error opening new connection", e);
}
targetSession = targetContext.openSession(monitor, DBCExecutionPurpose.UTIL, "Data load");
targetSession.enableLogging(false);
if (settings.isUseTransactions()) {
DBCTransactionManager txnManager = DBUtils.getTransactionManager(targetSession.getExecutionContext());
if (txnManager != null) {
txnManager.setAutoCommit(monitor, false);
}
}
}
Aggregations