Search in sources :

Example 6 with DatabaseConsumerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by dbeaver.

the class StreamProducerPageSettings method updateMultiConsumers.

private void updateMultiConsumers(DBRProgressMonitor monitor, DataTransferPipe pipe, File[] files) {
    final StreamProducerSettings producerSettings = getWizard().getPageSettings(this, StreamProducerSettings.class);
    IDataTransferConsumer<?, ?> originalConsumer = pipe.getConsumer();
    DataTransferSettings dtSettings = getWizard().getSettings();
    List<DataTransferPipe> newPipes = new ArrayList<>(dtSettings.getDataPipes());
    newPipes.remove(pipe);
    for (File file : files) {
        StreamTransferProducer producer = new StreamTransferProducer(new StreamEntityMapping(file));
        IDataTransferConsumer<?, ?> consumer = new DatabaseTransferConsumer();
        DataTransferPipe singlePipe = new DataTransferPipe(producer, consumer);
        try {
            singlePipe.initPipe(dtSettings, newPipes.size(), newPipes.size());
        } catch (DBException e) {
            log.error(e);
            continue;
        }
        newPipes.add(singlePipe);
        producerSettings.updateProducerSettingsFromStream(monitor, producer, dtSettings);
        IDataTransferSettings consumerSettings = dtSettings.getNodeSettings(dtSettings.getConsumer());
        if (consumerSettings instanceof DatabaseConsumerSettings) {
            DatabaseConsumerSettings dcs = (DatabaseConsumerSettings) consumerSettings;
            if (originalConsumer != null && originalConsumer.getTargetObjectContainer() instanceof DBSObject) {
                DBNDatabaseNode containerNode = DBNUtils.getNodeByObject(monitor, (DBSObject) originalConsumer.getTargetObjectContainer(), false);
                if (containerNode != null) {
                    dcs.setContainerNode(containerNode);
                }
            }
            DatabaseMappingContainer mapping = new DatabaseMappingContainer(dcs, producer.getDatabaseObject());
            // mapping.setTarget(null);
            mapping.setTargetName(generateTableName(producer.getInputFile()));
            dcs.addDataMappings(getWizard().getRunnableContext(), producer.getDatabaseObject(), mapping);
        }
    }
    dtSettings.setDataPipes(newPipes, false);
    dtSettings.setPipeChangeRestricted(true);
}
Also used : StreamTransferProducer(org.jkiss.dbeaver.tools.transfer.stream.StreamTransferProducer) DatabaseTransferConsumer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferConsumer) DBException(org.jkiss.dbeaver.DBException) ArrayList(java.util.ArrayList) StreamProducerSettings(org.jkiss.dbeaver.tools.transfer.stream.StreamProducerSettings) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DatabaseConsumerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings) StreamEntityMapping(org.jkiss.dbeaver.tools.transfer.stream.StreamEntityMapping) DatabaseMappingContainer(org.jkiss.dbeaver.tools.transfer.database.DatabaseMappingContainer) File(java.io.File) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 7 with DatabaseConsumerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by dbeaver.

the class StreamProducerPageSettings method deactivatePage.

@Override
public void deactivatePage() {
    // Save settings.
    // It is a producer so it must prepare data for consumers
    // Save processor properties
    propsEditor.saveEditorValues();
    Map<String, Object> processorProperties = propertySource.getPropertiesWithDefaults();
    DataTransferSettings dtSettings = getWizard().getSettings();
    dtSettings.setProcessorProperties(processorProperties);
    final StreamProducerSettings producerSettings = getWizard().getPageSettings(this, StreamProducerSettings.class);
    if (producerSettings != null) {
        producerSettings.setProcessorProperties(processorProperties);
    }
    // Update column mappings for database consumers
    IDataTransferSettings consumerSettings = getWizard().getSettings().getNodeSettings(getWizard().getSettings().getConsumer());
    try {
        getWizard().getRunnableContext().run(true, true, monitor -> {
            for (DataTransferPipe pipe : dtSettings.getDataPipes()) {
                if (pipe.getProducer() instanceof StreamTransferProducer) {
                    StreamTransferProducer producer = (StreamTransferProducer) pipe.getProducer();
                    producerSettings.updateProducerSettingsFromStream(monitor, producer, dtSettings);
                    if (consumerSettings instanceof DatabaseConsumerSettings) {
                        DatabaseMappingContainer mapping = ((DatabaseConsumerSettings) consumerSettings).getDataMapping(producer.getDatabaseObject());
                        if (mapping != null) {
                            mapping.getAttributeMappings(monitor);
                        }
                    }
                }
            }
        });
    } catch (InvocationTargetException e) {
        DBWorkbench.getPlatformUI().showError("Error updating stream settings", "Error updating settings", e.getTargetException());
    } catch (InterruptedException e) {
    // ignore
    }
    super.deactivatePage();
}
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) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DatabaseMappingContainer(org.jkiss.dbeaver.tools.transfer.database.DatabaseMappingContainer) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 8 with DatabaseConsumerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by dbeaver.

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 9 with DatabaseConsumerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by dbeaver.

the class DatabaseConsumerPageLoadSettings method loadSettings.

private void loadSettings() {
    DatabaseConsumerSettings settings = getSettings();
    if (settings.isTruncateBeforeLoad() && !confirmDataTruncate()) {
        truncateTargetTable.setSelection(false);
        settings.setTruncateBeforeLoad(false);
    }
    loadInsertMethods();
}
Also used : DatabaseConsumerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings)

Example 10 with DatabaseConsumerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings in project dbeaver by dbeaver.

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)

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