Search in sources :

Example 1 with DBPDataSource

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

the class DBVModel method getRealContainer.

@Override
public DBSObjectContainer getRealContainer(DBRProgressMonitor monitor) throws DBException {
    DBPDataSource dataSource = dataSourceContainer.getDataSource();
    if (dataSource instanceof DBSObjectContainer) {
        return (DBSObjectContainer) dataSource;
    }
    log.warn("Datasource '" + dataSource + "' is not an object container");
    return null;
}
Also used : DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 2 with DBPDataSource

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

the class ColumnsMappingDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    DBPDataSource targetDataSource = settings.getTargetDataSource(mapping);
    getShell().setText("Map columns of " + mapping.getTargetName());
    boldFont = UIUtils.makeBoldFont(parent.getFont());
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    new Label(composite, SWT.NONE).setText("Source entity: " + DBUtils.getObjectFullName(mapping.getSource(), DBPEvaluationContext.UI) + " [" + mapping.getSource().getDataSource().getContainer().getName() + "]");
    new Label(composite, SWT.NONE).setText("Target entity: " + mapping.getTargetName() + " [" + (targetDataSource == null ? "?" : targetDataSource.getContainer().getName()) + "]");
    mappingViewer = new TableViewer(composite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 600;
    gd.heightHint = 300;
    mappingViewer.getTable().setLayoutData(gd);
    mappingViewer.getTable().setLinesVisible(true);
    mappingViewer.getTable().setHeaderVisible(true);
    mappingViewer.setContentProvider(new ListContentProvider());
    mappingViewer.getTable().addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.character == SWT.DEL) {
                for (TableItem item : mappingViewer.getTable().getSelection()) {
                    DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) item.getData();
                    attribute.setMappingType(DatabaseMappingType.skip);
                }
                updateStatus(Status.OK_STATUS);
                mappingViewer.refresh();
            } else if (e.character == SWT.SPACE) {
                for (TableItem item : mappingViewer.getTable().getSelection()) {
                    DatabaseMappingAttribute attribute = (DatabaseMappingAttribute) item.getData();
                    attribute.setMappingType(DatabaseMappingType.existing);
                    try {
                        attribute.updateMappingType(VoidProgressMonitor.INSTANCE);
                    } catch (DBException e1) {
                        updateStatus(GeneralUtils.makeExceptionStatus(e1));
                    }
                }
                mappingViewer.refresh();
            }
        }
    });
    {
        TableViewerColumn columnSource = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnSource.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) cell.getElement();
                cell.setText(DBUtils.getObjectFullName(attrMapping.getSource(), DBPEvaluationContext.UI));
                if (attrMapping.getIcon() != null) {
                    cell.setImage(DBeaverIcons.getImage(attrMapping.getIcon()));
                }
            }
        });
        columnSource.getColumn().setText("Source Column");
        columnSource.getColumn().setWidth(170);
    }
    {
        TableViewerColumn columnSourceType = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnSourceType.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                cell.setText(((DatabaseMappingAttribute) cell.getElement()).getSourceType());
            }
        });
        columnSourceType.getColumn().setText("Source Type");
        columnSourceType.getColumn().setWidth(100);
    }
    {
        TableViewerColumn columnTarget = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnTarget.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                DatabaseMappingAttribute mapping = (DatabaseMappingAttribute) cell.getElement();
                cell.setText(mapping.getTargetName());
                if (mapping.mappingType == DatabaseMappingType.unspecified) {
                    cell.setBackground(DBeaverUI.getSharedTextColors().getColor(SharedTextColors.COLOR_WARNING));
                } else {
                    cell.setBackground(null);
                }
                cell.setFont(boldFont);
            }
        });
        columnTarget.getColumn().setText("Target Column");
        columnTarget.getColumn().setWidth(170);
        columnTarget.setEditingSupport(new EditingSupport(mappingViewer) {

            @Override
            protected CellEditor getCellEditor(Object element) {
                try {
                    java.util.List<String> items = new ArrayList<>();
                    DatabaseMappingAttribute mapping = (DatabaseMappingAttribute) element;
                    if (mapping.getParent().getMappingType() == DatabaseMappingType.existing && mapping.getParent().getTarget() instanceof DBSEntity) {
                        DBSEntity parentEntity = (DBSEntity) mapping.getParent().getTarget();
                        for (DBSEntityAttribute attr : CommonUtils.safeCollection(parentEntity.getAttributes(VoidProgressMonitor.INSTANCE))) {
                            items.add(attr.getName());
                        }
                    }
                    items.add(DatabaseMappingAttribute.TARGET_NAME_SKIP);
                    CustomComboBoxCellEditor editor = new CustomComboBoxCellEditor(mappingViewer.getTable(), items.toArray(new String[items.size()]), SWT.DROP_DOWN);
                    updateStatus(Status.OK_STATUS);
                    return editor;
                } catch (DBException e) {
                    updateStatus(GeneralUtils.makeExceptionStatus(e));
                    return null;
                }
            }

            @Override
            protected boolean canEdit(Object element) {
                return true;
            }

            @Override
            protected Object getValue(Object element) {
                return ((DatabaseMappingAttribute) element).getTargetName();
            }

            @Override
            protected void setValue(Object element, Object value) {
                try {
                    String name = CommonUtils.toString(value);
                    DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
                    if (DatabaseMappingAttribute.TARGET_NAME_SKIP.equals(name)) {
                        attrMapping.setMappingType(DatabaseMappingType.skip);
                    } else {
                        if (attrMapping.getParent().getMappingType() == DatabaseMappingType.existing && attrMapping.getParent().getTarget() instanceof DBSEntity) {
                            DBSEntity parentEntity = (DBSEntity) attrMapping.getParent().getTarget();
                            for (DBSEntityAttribute attr : CommonUtils.safeCollection(parentEntity.getAttributes(VoidProgressMonitor.INSTANCE))) {
                                if (name.equalsIgnoreCase(attr.getName())) {
                                    attrMapping.setTarget(attr);
                                    attrMapping.setMappingType(DatabaseMappingType.existing);
                                    return;
                                }
                            }
                        }
                        attrMapping.setMappingType(DatabaseMappingType.create);
                        attrMapping.setTargetName(name);
                    }
                    updateStatus(Status.OK_STATUS);
                } catch (DBException e) {
                    updateStatus(GeneralUtils.makeExceptionStatus(e));
                } finally {
                    mappingViewer.refresh();
                }
            }
        });
    }
    {
        TableViewerColumn columnTargetType = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnTargetType.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) cell.getElement();
                DBPDataSource dataSource = settings.getTargetDataSource(attrMapping);
                cell.setText(attrMapping.getTargetType(dataSource));
                cell.setFont(boldFont);
            }
        });
        columnTargetType.getColumn().setText("Target Type");
        columnTargetType.getColumn().setWidth(100);
        columnTargetType.setEditingSupport(new EditingSupport(mappingViewer) {

            @Override
            protected CellEditor getCellEditor(Object element) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
                Set<String> types = new LinkedHashSet<>();
                DBPDataSource dataSource = settings.getTargetDataSource(attrMapping);
                if (dataSource instanceof DBPDataTypeProvider) {
                    for (DBSDataType type : ((DBPDataTypeProvider) dataSource).getLocalDataTypes()) {
                        types.add(type.getName());
                    }
                }
                types.add(attrMapping.getTargetType(dataSource));
                return new CustomComboBoxCellEditor(mappingViewer.getTable(), types.toArray(new String[types.size()]), SWT.BORDER);
            }

            @Override
            protected boolean canEdit(Object element) {
                return true;
            }

            @Override
            protected Object getValue(Object element) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
                return attrMapping.getTargetType(settings.getTargetDataSource(attrMapping));
            }

            @Override
            protected void setValue(Object element, Object value) {
                DatabaseMappingAttribute attrMapping = (DatabaseMappingAttribute) element;
                attrMapping.setTargetType(CommonUtils.toString(value));
                mappingViewer.refresh(element);
            }
        });
    }
    {
        TableViewerColumn columnType = new TableViewerColumn(mappingViewer, SWT.LEFT);
        columnType.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                DatabaseMappingAttribute mapping = (DatabaseMappingAttribute) cell.getElement();
                String text = "";
                switch(mapping.getMappingType()) {
                    case unspecified:
                        text = "?";
                        break;
                    case existing:
                        text = "existing";
                        break;
                    case create:
                        text = "new";
                        break;
                    case skip:
                        text = "skip";
                        break;
                }
                cell.setText(text);
            }
        });
        columnType.getColumn().setText("Mapping");
        columnType.getColumn().setWidth(60);
    }
    mappingViewer.setInput(attributeMappings);
    return parent;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DBException(org.jkiss.dbeaver.DBException) KeyAdapter(org.eclipse.swt.events.KeyAdapter) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) ArrayList(java.util.ArrayList) CustomComboBoxCellEditor(org.jkiss.dbeaver.ui.controls.CustomComboBoxCellEditor) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) Composite(org.eclipse.swt.widgets.Composite) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) GridData(org.eclipse.swt.layout.GridData) DBPDataTypeProvider(org.jkiss.dbeaver.model.DBPDataTypeProvider) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Example 3 with DBPDataSource

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

the class DatabaseTransferConsumer method startTransfer.

@Override
public void startTransfer(DBRProgressMonitor monitor) throws DBException {
    // Create all necessary database objects
    monitor.beginTask("Create necessary database objects", 1);
    try {
        DBSObjectContainer container = settings.getContainer();
        if (container == null) {
            throw new DBException("No target datasource");
        }
        boolean hasNewObjects = false;
        DBPDataSource dataSource = container.getDataSource();
        try (DBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Create target metadata")) {
            for (DatabaseMappingContainer containerMapping : settings.getDataMappings().values()) {
                switch(containerMapping.getMappingType()) {
                    case create:
                        createTargetTable(session, containerMapping);
                        hasNewObjects = true;
                        break;
                    case existing:
                        for (DatabaseMappingAttribute attr : containerMapping.getAttributeMappings(monitor)) {
                            if (attr.getMappingType() == DatabaseMappingType.create) {
                                createTargetAttribute(session, attr);
                                hasNewObjects = true;
                            }
                        }
                        break;
                }
            }
        }
        if (hasNewObjects) {
            // Refresh node
            monitor.subTask("Refresh navigator model");
            settings.getContainerNode().refreshNode(monitor, this);
            // Reflect database changes in mappings
            for (DatabaseMappingContainer containerMapping : settings.getDataMappings().values()) {
                switch(containerMapping.getMappingType()) {
                    case create:
                        DBSObject newTarget = container.getChild(monitor, containerMapping.getTargetName());
                        if (newTarget == null) {
                            throw new DBCException("New table " + containerMapping.getTargetName() + " not found in container " + DBUtils.getObjectFullName(container, DBPEvaluationContext.UI));
                        } else if (!(newTarget instanceof DBSDataManipulator)) {
                            throw new DBCException("New table " + DBUtils.getObjectFullName(newTarget, DBPEvaluationContext.UI) + " doesn't support data manipulation");
                        }
                        containerMapping.setTarget((DBSDataManipulator) newTarget);
                        containerMapping.setMappingType(DatabaseMappingType.existing);
                    // ! Fall down is ok here
                    case existing:
                        for (DatabaseMappingAttribute attr : containerMapping.getAttributeMappings(monitor)) {
                            if (attr.getMappingType() == DatabaseMappingType.create) {
                                attr.updateMappingType(monitor);
                                if (attr.getTarget() == null) {
                                    throw new DBCException("Can't find target attribute '" + attr.getTargetName() + "' in '" + containerMapping.getTargetName() + "'");
                                }
                            }
                        }
                        break;
                }
            }
        }
    } finally {
        monitor.done();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 4 with DBPDataSource

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

the class DatabaseTransferProducer method transferData.

@Override
public void transferData(DBRProgressMonitor monitor, IDataTransferConsumer consumer, DatabaseProducerSettings settings) throws DBException {
    String contextTask = CoreMessages.data_transfer_wizard_job_task_export;
    DBPDataSource dataSource = getSourceObject().getDataSource();
    assert (dataSource != null);
    boolean newConnection = settings.isOpenNewConnections();
    DBCExecutionContext context = newConnection ? dataSource.openIsolatedContext(monitor, "Data transfer producer") : dataSource.getDefaultContext(false);
    try (DBCSession session = context.openSession(monitor, DBCExecutionPurpose.UTIL, contextTask)) {
        try {
            AbstractExecutionSource transferSource = new AbstractExecutionSource(dataContainer, context, consumer);
            session.enableLogging(false);
            if (newConnection) {
                // other complex structures only in transactional mode
                try {
                    DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
                    if (txnManager != null) {
                        txnManager.setAutoCommit(monitor, false);
                    }
                } catch (DBCException e) {
                    log.warn("Can't change auto-commit", e);
                }
            }
            long totalRows = 0;
            if (settings.isQueryRowCount() && (dataContainer.getSupportedFeatures() & DBSDataContainer.DATA_COUNT) != 0) {
                monitor.beginTask(CoreMessages.data_transfer_wizard_job_task_retrieve, 1);
                try {
                    totalRows = dataContainer.countData(transferSource, session, dataFilter);
                } catch (Throwable e) {
                    log.warn("Can't retrieve row count from '" + dataContainer.getName() + "'", e);
                    try {
                        DBCTransactionManager txnManager = DBUtils.getTransactionManager(session.getExecutionContext());
                        if (txnManager != null && !txnManager.isAutoCommit()) {
                            txnManager.rollback(session, null);
                        }
                    } catch (Throwable e1) {
                        log.warn("Error rolling back transaction", e1);
                    }
                } finally {
                    monitor.done();
                }
            }
            monitor.beginTask(CoreMessages.data_transfer_wizard_job_task_export_table_data, (int) totalRows);
            try {
                // Perform export
                if (settings.getExtractType() == DatabaseProducerSettings.ExtractType.SINGLE_QUERY) {
                    // Just do it in single query
                    dataContainer.readData(transferSource, session, consumer, dataFilter, -1, -1, DBSDataContainer.FLAG_NONE);
                } else {
                    // Read all data by segments
                    long offset = 0;
                    int segmentSize = settings.getSegmentSize();
                    for (; ; ) {
                        DBCStatistics statistics = dataContainer.readData(transferSource, session, consumer, dataFilter, offset, segmentSize, DBSDataContainer.FLAG_NONE);
                        if (statistics == null || statistics.getRowsFetched() < segmentSize) {
                            // Done
                            break;
                        }
                        offset += statistics.getRowsFetched();
                    }
                }
            } finally {
                monitor.done();
            }
        //dataContainer.readData(context, consumer, dataFilter, -1, -1);
        } finally {
            if (newConnection) {
                DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
                if (txnManager != null) {
                    try {
                        txnManager.commit(session);
                    } catch (DBCException e) {
                        log.error("Can't finish transaction in data producer connection", e);
                    }
                }
            }
            if (newConnection) {
                context.close();
            }
        }
    }
}
Also used : AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 5 with DBPDataSource

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

the class NavigatorHandlerFilterConfig method configureFilters.

public static void configureFilters(Shell shell, DBNNode node) {
    final DBNDatabaseFolder folder = (DBNDatabaseFolder) node;
    DBXTreeItem itemsMeta = folder.getItemsMeta();
    if (itemsMeta != null) {
        DBSObjectFilter objectFilter = folder.getNodeFilter(itemsMeta, true);
        if (objectFilter == null) {
            objectFilter = new DBSObjectFilter();
        }
        boolean globalFilter = folder.getValueObject() instanceof DBPDataSource;
        String parentName = "?";
        if (folder.getValueObject() instanceof DBSObject) {
            parentName = ((DBSObject) folder.getValueObject()).getName();
        }
        EditObjectFilterDialog dialog = new EditObjectFilterDialog(shell, globalFilter ? "All " + node.getNodeType() : node.getNodeType() + " of " + parentName, objectFilter, globalFilter);
        switch(dialog.open()) {
            case IDialogConstants.OK_ID:
                folder.setNodeFilter(itemsMeta, dialog.getFilter());
                NavigatorHandlerRefresh.refreshNavigator(Collections.singletonList(folder));
                break;
            case EditObjectFilterDialog.SHOW_GLOBAL_FILTERS_ID:
                objectFilter = folder.getDataSource().getContainer().getObjectFilter(folder.getChildrenClass(), null, false);
                dialog = new EditObjectFilterDialog(shell, "All " + node.getNodeType(), objectFilter != null ? objectFilter : new DBSObjectFilter(), true);
                if (dialog.open() == IDialogConstants.OK_ID) {
                    // Set global filter
                    folder.getDataSource().getContainer().setObjectFilter(folder.getChildrenClass(), null, dialog.getFilter());
                    folder.getDataSource().getContainer().persistConfiguration();
                    NavigatorHandlerRefresh.refreshNavigator(Collections.singletonList(folder));
                }
                break;
        }
    }
}
Also used : DBSObjectFilter(org.jkiss.dbeaver.model.struct.DBSObjectFilter) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBXTreeItem(org.jkiss.dbeaver.model.navigator.meta.DBXTreeItem) EditObjectFilterDialog(org.jkiss.dbeaver.ui.dialogs.connection.EditObjectFilterDialog) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)

Aggregations

DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)154 DBException (org.jkiss.dbeaver.DBException)57 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)28 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)19 InvocationTargetException (java.lang.reflect.InvocationTargetException)15 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)15 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)14 ArrayList (java.util.ArrayList)12 GridData (org.eclipse.swt.layout.GridData)12 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)12 SQLException (java.sql.SQLException)10 IEditorPart (org.eclipse.ui.IEditorPart)10 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)10 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)10 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)8 DBCExecutionContextDefaults (org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults)8 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)8 DBSCatalog (org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)8 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)8 ISelection (org.eclipse.jface.viewers.ISelection)6