Search in sources :

Example 1 with SQLInsertReplaceMethodDescriptor

use of org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor 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 2 with SQLInsertReplaceMethodDescriptor

use of org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor in project dbeaver by serge-rider.

the class DatabaseTransferConsumer method insertBatch.

private void insertBatch(boolean force) throws DBCException {
    if (isPreview) {
        return;
    }
    boolean needCommit = force || ((rowsExported % settings.getCommitAfterRows()) == 0);
    Map<String, Object> options = new HashMap<>();
    boolean disableUsingBatches = settings.isDisableUsingBatches();
    boolean onDuplicateKeyCaseOn = settings.getOnDuplicateKeyInsertMethodId() != null && !settings.getOnDuplicateKeyInsertMethodId().equals(DBSDataManipulator.INSERT_NONE_METHOD);
    options.put(DBSDataManipulator.OPTION_DISABLE_BATCHES, disableUsingBatches);
    if (onDuplicateKeyCaseOn) {
        String insertMethodId = settings.getOnDuplicateKeyInsertMethodId();
        SQLInsertReplaceMethodDescriptor insertReplaceMethod = SQLInsertReplaceMethodRegistry.getInstance().getInsertMethod(insertMethodId);
        if (insertReplaceMethod != null) {
            try {
                DBDInsertReplaceMethod insertMethod = insertReplaceMethod.createInsertMethod();
                options.put(DBSDataManipulator.OPTION_INSERT_REPLACE_METHOD, insertMethod);
            } catch (DBException e) {
                log.debug("Can't get insert replace method", e);
            }
        }
    }
    if ((needCommit || disableUsingBatches) && executeBatch != null) {
        targetSession.getProgressMonitor().subTask("Insert rows (" + rowsExported + ")");
        boolean retryInsert;
        do {
            retryInsert = false;
            try {
                executeBatch.execute(targetSession, options);
            } catch (Throwable e) {
                log.error("Error inserting row", e);
                if (!disableUsingBatches) {
                    DBWorkbench.getPlatformUI().showError("Error inserting row", "Data transfer failed during batch insert\n" + "(you can disable batch insert in order to skip particular rows).", e);
                    throw new DBCException("Can't insert row", e);
                } else {
                    if (!ignoreErrors) {
                        switch(DBWorkbench.getPlatformUI().showErrorStopRetryIgnore(DTMessages.database_transfer_consumer_task_error_occurred_during_data_load, e, true)) {
                            case STOP:
                                // just stop execution
                                throw new DBCException("Can't insert row", e);
                            case RETRY:
                                // do it again
                                retryInsert = true;
                                break;
                            case IGNORE:
                                // Just do nothing and go to the next row
                                retryInsert = false;
                                break;
                            case IGNORE_ALL:
                                ignoreErrors = true;
                                retryInsert = false;
                                break;
                        }
                    }
                }
            }
        } while (retryInsert);
    }
    if (settings.isUseTransactions() && needCommit) {
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(targetSession.getExecutionContext());
        if (txnManager != null && txnManager.isSupportsTransactions() && !txnManager.isAutoCommit()) {
            targetSession.getProgressMonitor().subTask("Commit changes");
            txnManager.commit(targetSession);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLInsertReplaceMethodDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor) DBDInsertReplaceMethod(org.jkiss.dbeaver.model.data.DBDInsertReplaceMethod)

Example 3 with SQLInsertReplaceMethodDescriptor

use of org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor 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)

Example 4 with SQLInsertReplaceMethodDescriptor

use of org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor in project dbeaver by dbeaver.

the class DatabaseTransferConsumer method insertBatch.

private void insertBatch(boolean force) throws DBCException {
    if (isPreview) {
        return;
    }
    boolean needCommit = force || ((rowsExported % settings.getCommitAfterRows()) == 0);
    Map<String, Object> options = new HashMap<>();
    boolean disableUsingBatches = settings.isDisableUsingBatches();
    boolean onDuplicateKeyCaseOn = settings.getOnDuplicateKeyInsertMethodId() != null && !settings.getOnDuplicateKeyInsertMethodId().equals(DBSDataManipulator.INSERT_NONE_METHOD);
    options.put(DBSDataManipulator.OPTION_DISABLE_BATCHES, disableUsingBatches);
    if (onDuplicateKeyCaseOn) {
        String insertMethodId = settings.getOnDuplicateKeyInsertMethodId();
        SQLInsertReplaceMethodDescriptor insertReplaceMethod = SQLInsertReplaceMethodRegistry.getInstance().getInsertMethod(insertMethodId);
        if (insertReplaceMethod != null) {
            try {
                DBDInsertReplaceMethod insertMethod = insertReplaceMethod.createInsertMethod();
                options.put(DBSDataManipulator.OPTION_INSERT_REPLACE_METHOD, insertMethod);
            } catch (DBException e) {
                log.debug("Can't get insert replace method", e);
            }
        }
    }
    if ((needCommit || disableUsingBatches) && executeBatch != null) {
        targetSession.getProgressMonitor().subTask("Insert rows (" + rowsExported + ")");
        boolean retryInsert;
        do {
            retryInsert = false;
            try {
                executeBatch.execute(targetSession, options);
            } catch (Throwable e) {
                log.error("Error inserting row", e);
                if (!disableUsingBatches) {
                    DBWorkbench.getPlatformUI().showError("Error inserting row", "Data transfer failed during batch insert\n" + "(you can disable batch insert in order to skip particular rows).", e);
                    throw new DBCException("Can't insert row", e);
                } else {
                    if (!ignoreErrors) {
                        switch(DBWorkbench.getPlatformUI().showErrorStopRetryIgnore(DTMessages.database_transfer_consumer_task_error_occurred_during_data_load, e, true)) {
                            case STOP:
                                // just stop execution
                                throw new DBCException("Can't insert row", e);
                            case RETRY:
                                // do it again
                                retryInsert = true;
                                break;
                            case IGNORE:
                                // Just do nothing and go to the next row
                                retryInsert = false;
                                break;
                            case IGNORE_ALL:
                                ignoreErrors = true;
                                retryInsert = false;
                                break;
                        }
                    }
                }
            }
        } while (retryInsert);
    }
    if (settings.isUseTransactions() && needCommit) {
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(targetSession.getExecutionContext());
        if (txnManager != null && txnManager.isSupportsTransactions() && !txnManager.isAutoCommit()) {
            targetSession.getProgressMonitor().subTask("Commit changes");
            txnManager.commit(targetSession);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLInsertReplaceMethodDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor) DBDInsertReplaceMethod(org.jkiss.dbeaver.model.data.DBDInsertReplaceMethod)

Aggregations

SQLInsertReplaceMethodDescriptor (org.jkiss.dbeaver.model.sql.registry.SQLInsertReplaceMethodDescriptor)4 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 GridData (org.eclipse.swt.layout.GridData)2 DBException (org.jkiss.dbeaver.DBException)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBDInsertReplaceMethod (org.jkiss.dbeaver.model.data.DBDInsertReplaceMethod)2 SQLDialectDescriptor (org.jkiss.dbeaver.model.sql.registry.SQLDialectDescriptor)2 DatabaseConsumerSettings (org.jkiss.dbeaver.tools.transfer.database.DatabaseConsumerSettings)2