Search in sources :

Example 86 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.

the class EntityEditor method saveCommandContext.

private boolean saveCommandContext(final DBRProgressMonitor monitor, Map<String, Object> options) {
    monitor.beginTask("Save entity", 1);
    Throwable error = null;
    final DBECommandContext commandContext = getCommandContext();
    if (commandContext == null) {
        log.warn("Null command context");
        return true;
    }
    DBCExecutionContext executionContext = getExecutionContext();
    if (executionContext == null) {
        log.warn("Null execution context");
        return true;
    }
    boolean isNewObject = getDatabaseObject() == null || !getDatabaseObject().isPersisted();
    if (!isNewObject) {
        // Check for any new nested objects
        for (DBECommand cmd : commandContext.getFinalCommands()) {
            if (cmd.getObject() instanceof DBSObject && !((DBSObject) cmd.getObject()).isPersisted()) {
                isNewObject = true;
                break;
            }
        }
    }
    try {
        DBExecUtils.tryExecuteRecover(monitor, executionContext.getDataSource(), param -> {
            try {
                commandContext.saveChanges(monitor, options);
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        });
    } catch (DBException e) {
        error = e;
    }
    if (getDatabaseObject() instanceof DBPStatefulObject) {
        try {
            ((DBPStatefulObject) getDatabaseObject()).refreshObjectState(monitor);
        } catch (DBCException e) {
            // Just report an error
            log.error(e);
        }
    }
    if (error == null) {
        // Refresh underlying node
        // It'll refresh database object and all it's descendants
        // So we'll get actual data from database
        final DBNDatabaseNode treeNode = getEditorInput().getNavigatorNode();
        try {
            boolean doRefresh = isNewObject;
            UIUtils.runInProgressService(monitor1 -> {
                try {
                    treeNode.refreshNode(monitor1, doRefresh ? DBNEvent.FORCE_REFRESH : DBNEvent.UPDATE_ON_SAVE);
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            });
        } catch (InvocationTargetException e) {
            error = e.getTargetException();
        } catch (InterruptedException e) {
        // ok
        }
    }
    monitor.done();
    if (error == null) {
        return true;
    } else {
        // Try to handle error in nested editors
        final Throwable vError = error;
        UIUtils.syncExec(() -> {
            final IErrorVisualizer errorVisualizer = getAdapter(IErrorVisualizer.class);
            if (errorVisualizer != null) {
                errorVisualizer.visualizeError(monitor, vError);
            }
        });
        // Show error dialog
        UIUtils.asyncExec(() -> DBWorkbench.getPlatformUI().showError("Can't save '" + getDatabaseObject().getName() + "'", null, vError));
        return false;
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext)

Example 87 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.

the class DatabaseProducerPageInputObjects method autoAssignMappings.

private void autoAssignMappings(List<DBSObject> containerObjects) {
    boolean chooseConsumer = getWizard().getSettings().isConsumerOptional();
    for (TableItem item : mappingTable.getItems()) {
        DataTransferPipe pipe = (DataTransferPipe) item.getData();
        if ((chooseConsumer && (pipe.getConsumer() == null || pipe.getConsumer().getDatabaseObject() == null)) || (!chooseConsumer && (pipe.getProducer() == null || pipe.getProducer().getDatabaseObject() == null))) {
            DBSObject objectToMap = chooseConsumer ? pipe.getProducer().getDatabaseObject() : pipe.getConsumer().getDatabaseObject();
            if (objectToMap == null) {
                continue;
            }
            DBSObject object = DBUtils.findObject(containerObjects, objectToMap.getName());
            if (object != null) {
                if (chooseConsumer) {
                    if (object instanceof DBSDataManipulator) {
                        pipe.setConsumer(new DatabaseTransferConsumer((DBSDataManipulator) object));
                    }
                } else {
                    if (object instanceof DBSDataContainer) {
                        pipe.setProducer(new DatabaseTransferProducer((DBSDataContainer) object));
                    }
                }
                updateItemData(item, pipe);
            }
        }
    }
    updatePageCompletion();
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DatabaseTransferConsumer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferConsumer) DBSDataManipulator(org.jkiss.dbeaver.model.struct.DBSDataManipulator) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) DataTransferPipe(org.jkiss.dbeaver.tools.transfer.DataTransferPipe)

Example 88 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject 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 89 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.

the class DataTransferPagePipes method loadConsumers.

private void loadConsumers() {
    DataTransferSettings settings = getWizard().getSettings();
    Collection<DBSObject> objects = settings.getSourceObjects();
    List<TransferTarget> transferTargets = new ArrayList<>();
    for (DataTransferNodeDescriptor consumer : DataTransferRegistry.getInstance().getAvailableConsumers(objects)) {
        Collection<DataTransferProcessorDescriptor> processors = consumer.getAvailableProcessors(objects);
        if (CommonUtils.isEmpty(processors)) {
            transferTargets.add(new TransferTarget(consumer, null));
        } else {
            for (DataTransferProcessorDescriptor processor : processors) {
                transferTargets.add(new TransferTarget(consumer, processor));
            }
        }
    }
    nodesTable.setInput(transferTargets);
}
Also used : DataTransferSettings(org.jkiss.dbeaver.tools.transfer.DataTransferSettings) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ArrayList(java.util.ArrayList) DataTransferNodeDescriptor(org.jkiss.dbeaver.tools.transfer.registry.DataTransferNodeDescriptor) DataTransferProcessorDescriptor(org.jkiss.dbeaver.tools.transfer.registry.DataTransferProcessorDescriptor)

Example 90 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by serge-rider.

the class DataTransferSettings method setDataPipes.

public void setDataPipes(List<DataTransferPipe> dataPipes, boolean isExport) {
    this.dataPipes = dataPipes;
    // Now determine main producer and consumer and processor
    DataTransferRegistry registry = DataTransferRegistry.getInstance();
    this.consumerOptional = isExport;
    this.producerOptional = !isExport;
    // Don't update producer and consumer if it's not required (#9687)
    if (!nodeUpdateRestricted) {
        this.producer = null;
        this.consumer = null;
        if (!dataPipes.isEmpty()) {
            DataTransferPipe pipe = dataPipes.get(0);
            this.producer = pipe.getProducer() == null ? null : registry.getNodeByType(pipe.getProducer().getClass());
            this.consumer = pipe.getConsumer() == null ? null : registry.getNodeByType(pipe.getConsumer().getClass());
        }
    }
    DataTransferProcessorDescriptor savedProcessor = this.processor;
    if (this.consumerOptional && this.consumer != null) {
        this.selectConsumer(this.consumer, savedProcessor, false);
    }
    if (this.producerOptional && this.producer != null) {
        this.selectProducer(this.producer, savedProcessor, false);
    }
    // Collect objects
    initObjects.clear();
    for (DataTransferPipe pipe : dataPipes) {
        DBSObject object = isExport ? pipe.getProducer().getDatabaseObject() : pipe.getConsumer().getDatabaseObject();
        if (object != null) {
            initObjects.add(object);
        }
    }
}
Also used : DataTransferRegistry(org.jkiss.dbeaver.tools.transfer.registry.DataTransferRegistry) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DataTransferProcessorDescriptor(org.jkiss.dbeaver.tools.transfer.registry.DataTransferProcessorDescriptor)

Aggregations

DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)374 DBException (org.jkiss.dbeaver.DBException)129 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)66 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)66 ArrayList (java.util.ArrayList)54 InvocationTargetException (java.lang.reflect.InvocationTargetException)52 ISelection (org.eclipse.jface.viewers.ISelection)46 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)46 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)44 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)38 GridData (org.eclipse.swt.layout.GridData)38 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)38 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)30 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)29 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)29 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)28 Composite (org.eclipse.swt.widgets.Composite)27 IEditorPart (org.eclipse.ui.IEditorPart)26 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)24 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)22