Search in sources :

Example 1 with DatabaseConsumerSettings

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);
    }
}
Also used : StreamTransferProducer(org.jkiss.dbeaver.tools.transfer.stream.StreamTransferProducer) StreamProducerSettings(org.jkiss.dbeaver.tools.transfer.stream.StreamProducerSettings) DatabaseConsumerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings) DBSDataManipulator(org.jkiss.dbeaver.model.struct.DBSDataManipulator) StreamEntityMapping(org.jkiss.dbeaver.tools.transfer.stream.StreamEntityMapping) DatabaseMappingContainer(org.jkiss.dbeaver.tools.transfer.database.DatabaseMappingContainer) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 2 with DatabaseConsumerSettings

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);
        }
    });
}
Also used : DBException(org.jkiss.dbeaver.DBException) DatabaseTransferConsumer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferConsumer) ArrayList(java.util.ArrayList) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DatabaseConsumerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Example 3 with DatabaseConsumerSettings

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());
            }
        }
    });
}
Also used : SQLDialectDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLDialectDescriptor) SQLInsertReplaceMethodDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DatabaseConsumerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 4 with DatabaseConsumerSettings

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);
}
Also used : SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DatabaseConsumerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings) SelectionEvent(org.eclipse.swt.events.SelectionEvent) GridData(org.eclipse.swt.layout.GridData)

Example 5 with DatabaseConsumerSettings

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);
        }
    });
}
Also used : DBException(org.jkiss.dbeaver.DBException) DatabaseTransferConsumer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferConsumer) ArrayList(java.util.ArrayList) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DatabaseConsumerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Aggregations

DatabaseConsumerSettings (org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings)14 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)6 DatabaseMappingContainer (org.jkiss.dbeaver.tools.transfer.database.DatabaseMappingContainer)6 StreamProducerSettings (org.jkiss.dbeaver.tools.transfer.stream.StreamProducerSettings)6 StreamTransferProducer (org.jkiss.dbeaver.tools.transfer.stream.StreamTransferProducer)6 ArrayList (java.util.ArrayList)4 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)4 SelectionEvent (org.eclipse.swt.events.SelectionEvent)4 GridData (org.eclipse.swt.layout.GridData)4 DBException (org.jkiss.dbeaver.DBException)4 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)4 DatabaseTransferConsumer (org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferConsumer)4 StreamEntityMapping (org.jkiss.dbeaver.tools.transfer.stream.StreamEntityMapping)4 File (java.io.File)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBCSession (org.jkiss.dbeaver.model.exec.DBCSession)2 SQLDialectDescriptor (org.jkiss.dbeaver.model.sql.registry.SQLDialectDescriptor)2 SQLInsertReplaceMethodDescriptor (org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor)2 DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)2