use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by serge-rider.
the class StreamProducerPageSettings method updateSingleConsumer.
private void updateSingleConsumer(DBRProgressMonitor monitor, DataTransferPipe pipe, File file) {
final StreamProducerSettings producerSettings = getWizard().getPageSettings(this, StreamProducerSettings.class);
final StreamTransferProducer oldProducer = pipe.getProducer() instanceof StreamTransferProducer ? (StreamTransferProducer) pipe.getProducer() : null;
final StreamTransferProducer newProducer = new StreamTransferProducer(new StreamEntityMapping(file));
pipe.setProducer(newProducer);
producerSettings.updateProducerSettingsFromStream(monitor, newProducer, getWizard().getSettings());
IDataTransferSettings consumerSettings = getWizard().getSettings().getNodeSettings(getWizard().getSettings().getConsumer());
if (consumerSettings instanceof DatabaseConsumerSettings) {
DatabaseConsumerSettings settings = (DatabaseConsumerSettings) consumerSettings;
DatabaseMappingContainer mapping = new DatabaseMappingContainer(settings, newProducer.getDatabaseObject());
if (pipe.getConsumer() != null && pipe.getConsumer().getDatabaseObject() instanceof DBSDataManipulator) {
DBSDataManipulator databaseObject = (DBSDataManipulator) pipe.getConsumer().getDatabaseObject();
DBNDatabaseNode databaseNode = DBNUtils.getNodeByObject(monitor, databaseObject.getParentObject(), false);
if (databaseNode != null) {
settings.setContainerNode(databaseNode);
}
mapping.setTarget(databaseObject);
} else {
mapping.setTarget(null);
mapping.setTargetName(generateTableName(newProducer.getInputFile()));
}
if (oldProducer != null) {
// Remove old mapping because we're just replaced file
DatabaseMappingContainer oldMappingContainer = settings.getDataMappings().remove(oldProducer.getDatabaseObject());
if (oldMappingContainer != null && oldMappingContainer.getSource() instanceof StreamEntityMapping) {
StreamEntityMapping oldEntityMapping = (StreamEntityMapping) oldMappingContainer.getSource();
// Copy mappings from old producer if columns are the same
if (oldEntityMapping.isSameColumns(newProducer.getEntityMapping())) {
StreamEntityMapping entityMapping = new StreamEntityMapping(file);
settings.addDataMappings(getWizard().getRunnableContext(), entityMapping, new DatabaseMappingContainer(oldMappingContainer, entityMapping));
StreamTransferProducer producer = new StreamTransferProducer(entityMapping);
pipe.setProducer(producer);
producerSettings.updateProducerSettingsFromStream(monitor, producer, getWizard().getSettings());
return;
}
}
}
settings.addDataMappings(getWizard().getRunnableContext(), newProducer.getDatabaseObject(), mapping);
}
}
use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by serge-rider.
the class PreviewMappingDialog method loadImportPreview.
private void loadImportPreview(DBRProgressMonitor monitor) throws DBException {
PreviewConsumer previewConsumer = new PreviewConsumer(monitor, mappingContainer);
IDataTransferProducer producer = pipe.getProducer();
IDataTransferSettings producerSettings = getNodeSettings(producer);
IDataTransferSettings consumerSettings = getNodeSettings(pipe.getConsumer());
try {
IDataTransferConsumer realConsumer = pipe.getConsumer();
try {
pipe.setConsumer(previewConsumer);
pipe.initPipe(dtSettings, 0, 1);
producer.transferData(previewConsumer.getCtlMonitor(), previewConsumer, dtSettings.getProcessor() == null ? null : dtSettings.getProcessor().getInstance(), producerSettings, null);
} finally {
pipe.setConsumer(realConsumer);
}
} finally {
previewConsumer.close();
}
List<Object[]> rows = previewConsumer.getRows();
List<String[]> strRows = new ArrayList<>(rows.size());
DBSObject target = mappingContainer.getTarget();
if (target == null) {
if (consumerSettings instanceof DatabaseConsumerSettings) {
target = ((DatabaseConsumerSettings) consumerSettings).getContainer();
}
}
if (target == null) {
throw new DBException("Can not determine target container");
}
try (DBCSession session = DBUtils.openUtilSession(monitor, target, "Generate preview values")) {
DatabaseTransferConsumer.ColumnMapping[] columnMappings = previewConsumer.getColumnMappings();
for (Object[] row : rows) {
String[] strRow = new String[row.length];
for (DatabaseTransferConsumer.ColumnMapping attr : columnMappings) {
if (attr == null) {
continue;
}
Object srcValue = row[attr.targetIndex];
Object value = attr.sourceValueHandler.getValueFromObject(session, attr.sourceAttr, srcValue, false, true);
DBSAttributeBase attrTarget = attr.targetAttr.getTarget();
if (attrTarget == null) {
// New column - it is ok for preview only
attrTarget = attr.sourceAttr;
}
String valueStr = attr.targetValueHandler.getValueDisplayString(attrTarget, value, DBDDisplayFormat.UI);
strRow[attr.targetIndex] = CommonUtils.getSingleLineString(valueStr);
}
strRows.add(strRow);
}
}
UIUtils.asyncExec(() -> {
previewTable.setRedraw(false);
try {
previewTable.removeAll();
for (TableColumn column : previewTable.getColumns()) {
column.dispose();
}
for (DatabaseTransferConsumer.ColumnMapping columnMapping : previewConsumer.getColumnMappings()) {
if (columnMapping == null) {
continue;
}
TableColumn column = new TableColumn(previewTable, SWT.NONE);
column.setText(columnMapping.targetAttr.getTargetName());
DBSAttributeBase attr = columnMapping.targetAttr.getTarget();
if (attr == null) {
// We can use icon from source attribute
attr = columnMapping.sourceAttr;
}
column.setImage(DBeaverIcons.getImage(DBValueFormatting.getObjectImage(attr)));
column.setData(columnMapping);
}
for (String[] row : strRows) {
TableItem previewItem = new TableItem(previewTable, SWT.NONE);
for (int i = 0; i < row.length; i++) {
if (row[i] != null) {
previewItem.setText(i, row[i]);
}
}
}
UIUtils.packColumns(previewTable);
} finally {
previewTable.setRedraw(true);
}
});
}
use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by serge-rider.
the class DatabaseConsumerPageLoadSettings method loadInsertMethods.
private void loadInsertMethods() {
DatabaseConsumerSettings settings = getSettings();
DBPDataSource dataSource = settings.getContainerNode().getDataSource();
List<SQLInsertReplaceMethodDescriptor> insertMethodsDescriptors = null;
if (dataSource != null) {
SQLDialectDescriptor dialectDescriptor = SQLDialectRegistry.getInstance().getDialect(dataSource.getSQLDialect().getDialectId());
insertMethodsDescriptors = dialectDescriptor.getSupportedInsertReplaceMethodsDescriptors();
}
onDuplicateKeyInsertMethods.add(DBSDataManipulator.INSERT_NONE_METHOD);
if (!CommonUtils.isEmpty(insertMethodsDescriptors)) {
boolean emptyButton = true;
for (SQLInsertReplaceMethodDescriptor insertMethod : insertMethodsDescriptors) {
onDuplicateKeyInsertMethods.add(insertMethod.getLabel());
if (insertMethod.getId().equals(settings.getOnDuplicateKeyInsertMethodId())) {
onDuplicateKeyInsertMethods.setText(insertMethod.getLabel());
emptyButton = false;
}
}
if (emptyButton) {
onDuplicateKeyInsertMethods.setText(DBSDataManipulator.INSERT_NONE_METHOD);
}
} else {
onDuplicateKeyInsertMethods.setText(DBSDataManipulator.INSERT_NONE_METHOD);
onDuplicateKeyInsertMethods.setEnabled(false);
Label descLabel = new Label(loadSettings, SWT.NONE);
descLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 2, 1));
descLabel.setText("Replace method not supported by target database");
}
List<SQLInsertReplaceMethodDescriptor> finalInsertMethodsDescriptors = insertMethodsDescriptors;
onDuplicateKeyInsertMethods.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selIndex = onDuplicateKeyInsertMethods.getSelectionIndex();
if (selIndex > 0 && !CommonUtils.isEmpty(finalInsertMethodsDescriptors)) {
SQLInsertReplaceMethodDescriptor methodDescriptor = finalInsertMethodsDescriptors.get(selIndex - 1);
settings.setOnDuplicateKeyInsertMethodId(methodDescriptor.getId());
} else {
settings.setOnDuplicateKeyInsertMethodId(onDuplicateKeyInsertMethods.getText());
}
}
});
}
use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by dbeaver.
the class DatabaseConsumerPageLoadSettings method createControl.
@Override
public void createControl(Composite parent) {
initializeDialogUnits(parent);
Composite composite = UIUtils.createComposite(parent, 1);
final DatabaseConsumerSettings settings = getSettings();
{
loadSettings = UIUtils.createControlGroup(composite, DTUIMessages.database_consumer_wizard_name, 2, GridData.FILL_HORIZONTAL, 0);
transferAutoGeneratedColumns = UIUtils.createCheckbox(loadSettings, DTUIMessages.database_consumer_wizard_transfer_checkbox_label, DTUIMessages.database_consumer_wizard_transfer_checkbox_tooltip, settings.isTransferAutoGeneratedColumns(), 2);
transferAutoGeneratedColumns.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setTransferAutoGeneratedColumns(transferAutoGeneratedColumns.getSelection());
}
});
truncateTargetTable = UIUtils.createCheckbox(loadSettings, DTUIMessages.database_consumer_wizard_truncate_checkbox_label, DTUIMessages.database_consumer_wizard_truncate_checkbox_description, settings.isTruncateBeforeLoad(), 2);
truncateTargetTable.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (truncateTargetTable.getSelection() && !confirmDataTruncate()) {
truncateTargetTable.setSelection(false);
return;
}
settings.setTruncateBeforeLoad(truncateTargetTable.getSelection());
}
});
UIUtils.createControlLabel(loadSettings, DTUIMessages.database_consumer_wizard_on_duplicate_key_insert_method_text);
onDuplicateKeyInsertMethods = new Combo(loadSettings, SWT.DROP_DOWN | SWT.READ_ONLY);
onDuplicateKeyInsertMethods.setLayoutData(new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_BEGINNING, true, false, 1, 1));
Link urlLabel = UIUtils.createLink(loadSettings, "<a href=\"" + REPLACE_METHOD_LINK + "\">" + DTUIMessages.database_consumer_wizard_link_label_replace_method_wiki + "</a>", new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
UIUtils.launchProgram(REPLACE_METHOD_LINK);
}
});
urlLabel.setLayoutData(new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 2, 1));
}
{
Group performanceSettings = UIUtils.createControlGroup(composite, DTUIMessages.database_consumer_wizard_performance_group_label, 4, GridData.FILL_HORIZONTAL, 0);
final Button newConnectionCheckbox = UIUtils.createCheckbox(performanceSettings, DTMessages.data_transfer_wizard_output_checkbox_new_connection, null, settings.isOpenNewConnections(), 4);
newConnectionCheckbox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setOpenNewConnections(newConnectionCheckbox.getSelection());
}
});
final Button useTransactionsCheck = UIUtils.createCheckbox(performanceSettings, DTUIMessages.database_consumer_wizard_transactions_checkbox_label, null, settings.isUseTransactions(), 4);
useTransactionsCheck.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setUseTransactions(useTransactionsCheck.getSelection());
}
});
final Spinner commitAfterEdit = UIUtils.createLabelSpinner(performanceSettings, DTUIMessages.database_consumer_wizard_commit_spinner_label, settings.getCommitAfterRows(), 1, Integer.MAX_VALUE);
commitAfterEdit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setCommitAfterRows(commitAfterEdit.getSelection());
}
});
commitAfterEdit.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 3, 1));
final Button useBatchCheck = UIUtils.createCheckbox(performanceSettings, DTUIMessages.database_consumer_wizard_disable_import_batches_label, DTUIMessages.database_consumer_wizard_disable_import_batches_description, settings.isDisableUsingBatches(), 4);
useBatchCheck.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setDisableUsingBatches(useBatchCheck.getSelection());
}
});
}
{
Group generalSettings = UIUtils.createControlGroup(composite, DTUIMessages.database_consumer_wizard_general_group_label, 4, GridData.FILL_HORIZONTAL, 0);
final Button showTableCheckbox = UIUtils.createCheckbox(generalSettings, DTUIMessages.database_consumer_wizard_table_checkbox_label, null, settings.isOpenTableOnFinish(), 4);
showTableCheckbox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
settings.setOpenTableOnFinish(showTableCheckbox.getSelection());
}
});
final Button showFinalMessageCheckbox = UIUtils.createCheckbox(generalSettings, DTUIMessages.database_consumer_wizard_final_message_checkbox_label, null, getWizard().getSettings().isShowFinalMessage(), 4);
showFinalMessageCheckbox.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
getWizard().getSettings().setShowFinalMessage(showFinalMessageCheckbox.getSelection());
}
});
}
setControl(composite);
}
use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by dbeaver.
the class PreviewMappingDialog method loadImportPreview.
private void loadImportPreview(DBRProgressMonitor monitor) throws DBException {
PreviewConsumer previewConsumer = new PreviewConsumer(monitor, mappingContainer);
IDataTransferProducer producer = pipe.getProducer();
IDataTransferSettings producerSettings = getNodeSettings(producer);
IDataTransferSettings consumerSettings = getNodeSettings(pipe.getConsumer());
try {
IDataTransferConsumer realConsumer = pipe.getConsumer();
try {
pipe.setConsumer(previewConsumer);
pipe.initPipe(dtSettings, 0, 1);
producer.transferData(previewConsumer.getCtlMonitor(), previewConsumer, dtSettings.getProcessor() == null ? null : dtSettings.getProcessor().getInstance(), producerSettings, null);
} finally {
pipe.setConsumer(realConsumer);
}
} finally {
previewConsumer.close();
}
List<Object[]> rows = previewConsumer.getRows();
List<String[]> strRows = new ArrayList<>(rows.size());
DBSObject target = mappingContainer.getTarget();
if (target == null) {
if (consumerSettings instanceof DatabaseConsumerSettings) {
target = ((DatabaseConsumerSettings) consumerSettings).getContainer();
}
}
if (target == null) {
throw new DBException("Can not determine target container");
}
try (DBCSession session = DBUtils.openUtilSession(monitor, target, "Generate preview values")) {
DatabaseTransferConsumer.ColumnMapping[] columnMappings = previewConsumer.getColumnMappings();
for (Object[] row : rows) {
String[] strRow = new String[row.length];
for (DatabaseTransferConsumer.ColumnMapping attr : columnMappings) {
if (attr == null) {
continue;
}
Object srcValue = row[attr.targetIndex];
Object value = attr.sourceValueHandler.getValueFromObject(session, attr.sourceAttr, srcValue, false, true);
DBSAttributeBase attrTarget = attr.targetAttr.getTarget();
if (attrTarget == null) {
// New column - it is ok for preview only
attrTarget = attr.sourceAttr;
}
String valueStr = attr.targetValueHandler.getValueDisplayString(attrTarget, value, DBDDisplayFormat.UI);
strRow[attr.targetIndex] = CommonUtils.getSingleLineString(valueStr);
}
strRows.add(strRow);
}
}
UIUtils.asyncExec(() -> {
previewTable.setRedraw(false);
try {
previewTable.removeAll();
for (TableColumn column : previewTable.getColumns()) {
column.dispose();
}
for (DatabaseTransferConsumer.ColumnMapping columnMapping : previewConsumer.getColumnMappings()) {
if (columnMapping == null) {
continue;
}
TableColumn column = new TableColumn(previewTable, SWT.NONE);
column.setText(columnMapping.targetAttr.getTargetName());
DBSAttributeBase attr = columnMapping.targetAttr.getTarget();
if (attr == null) {
// We can use icon from source attribute
attr = columnMapping.sourceAttr;
}
column.setImage(DBeaverIcons.getImage(DBValueFormatting.getObjectImage(attr)));
column.setData(columnMapping);
}
for (String[] row : strRows) {
TableItem previewItem = new TableItem(previewTable, SWT.NONE);
for (int i = 0; i < row.length; i++) {
if (row[i] != null) {
previewItem.setText(i, row[i]);
}
}
}
UIUtils.packColumns(previewTable);
} finally {
previewTable.setRedraw(true);
}
});
}
Aggregations