Search in sources :

Example 21 with DBRRunnableWithProgress

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

the class TasksJob method run.

@Override
protected IStatus run(DBRProgressMonitor monitor) {
    monitor.beginTask(getName(), tasks.size());
    boolean ignoreErrors = false;
    for (int i = 0; i < tasks.size(); ) {
        DBRRunnableWithProgress task = tasks.get(i);
        if (monitor.isCanceled()) {
            break;
        }
        try {
            task.run(monitor);
        } catch (InvocationTargetException e) {
            if (tasks.size() == 1) {
                DBWorkbench.getPlatformUI().showError(getName(), null, e.getTargetException());
            } else if (!ignoreErrors) {
                boolean keepRunning = true;
                switch(DBWorkbench.getPlatformUI().showErrorStopRetryIgnore(getName(), e.getTargetException(), true)) {
                    case STOP:
                        keepRunning = false;
                        break;
                    case RETRY:
                        // just make it again
                        continue;
                    case IGNORE:
                        // Just do nothing
                        break;
                    case IGNORE_ALL:
                        ignoreErrors = true;
                        break;
                }
                if (!keepRunning) {
                    break;
                }
            }
        } catch (InterruptedException e) {
        // Ignore
        }
        monitor.worked(1);
        i++;
    }
    monitor.done();
    return Status.OK_STATUS;
}
Also used : DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 22 with DBRRunnableWithProgress

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

the class StreamProducerPageSettings method chooseSourceFile.

private void chooseSourceFile(DataTransferPipe pipe) {
    List<String> extensions = new ArrayList<>();
    String extensionProp = CommonUtils.toString(propertySource.getPropertyValue(null, "extension"));
    for (String ext : extensionProp.split(",")) {
        extensions.add("*." + ext);
    }
    extensions.add("*");
    DBRRunnableWithProgress initializer = null;
    if (pipe.getConsumer() != null && pipe.getConsumer().getTargetObjectContainer() != null) {
        File[] files = DialogUtils.openFileList(getShell(), "Select input files", extensions.toArray(new String[0]));
        if (files != null && files.length > 0) {
            initializer = monitor -> updateMultiConsumers(monitor, pipe, files);
        }
    } else {
        File file = DialogUtils.openFile(getShell(), extensions.toArray(new String[0]));
        if (file != null) {
            initializer = monitor -> updateSingleConsumer(monitor, pipe, file);
        }
    }
    if (initializer != null) {
        try {
            getWizard().getRunnableContext().run(true, true, initializer);
        } catch (InvocationTargetException e) {
            DBWorkbench.getPlatformUI().showError("Column mappings error", "Error reading column mappings from stream", e.getTargetException());
            return;
        } catch (InterruptedException e) {
        // ignore
        }
    }
    reloadPipes();
    updatePageCompletion();
}
Also used : ArrayList(java.util.ArrayList) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 23 with DBRRunnableWithProgress

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

the class ResultSetFilterPanel method getProposals.

@Override
public IContentProposal[] getProposals(String contents, int position) {
    if (!viewer.getPreferenceStore().getBoolean(ResultSetPreferences.RESULT_SET_FILTER_AUTO_COMPLETE_PROPOSIAL)) {
        return null;
    }
    SQLSyntaxManager syntaxManager = new SQLSyntaxManager();
    DBPDataSource dataSource = viewer.getDataSource();
    if (dataSource != null) {
        syntaxManager.init(dataSource);
    }
    SQLWordPartDetector wordDetector = new SQLWordPartDetector(new Document(contents), syntaxManager, position);
    String word = wordDetector.getFullWord();
    final List<IContentProposal> proposals = new ArrayList<>();
    if (CommonUtils.isEmptyTrimmed(word))
        word = contents;
    word = word.toLowerCase(Locale.ENGLISH);
    String attrName = word;
    final DBRRunnableWithProgress reader = monitor -> {
        DBDAttributeBinding[] attributes = viewer.getModel().getAttributes();
        for (DBDAttributeBinding attribute : attributes) {
            if (attribute.isCustom()) {
                continue;
            }
            final String name = DBUtils.getUnQuotedIdentifier(attribute.getDataSource(), attribute.getName());
            if (CommonUtils.isEmpty(attrName) || name.toLowerCase(Locale.ENGLISH).startsWith(attrName)) {
                final String content = DBUtils.getQuotedIdentifier(attribute) + " ";
                proposals.add(new ContentProposalExt(content, attribute.getName(), DBInfoUtils.makeObjectDescription(monitor, attribute.getAttribute(), false), content.length(), DBValueFormatting.getObjectImage(attribute)));
            }
        }
    };
    SystemJob searchJob = new SystemJob("Extract attribute proposals", reader);
    searchJob.schedule();
    UIUtils.waitJobCompletion(searchJob);
    String[] filterKeywords = { SQLConstants.KEYWORD_AND, SQLConstants.KEYWORD_OR, SQLConstants.KEYWORD_IS, SQLConstants.KEYWORD_NOT, SQLConstants.KEYWORD_NULL };
    for (String kw : filterKeywords) {
        if (attrName.isEmpty() || kw.startsWith(attrName.toUpperCase())) {
            if (dataSource != null) {
                kw = dataSource.getSQLDialect().storesUnquotedCase().transform(kw);
            }
            proposals.add(new ContentProposal(kw + " ", kw + ": SQL expression keyword"));
        }
    }
    return proposals.toArray(new IContentProposal[0]);
}
Also used : ResultSetHandlerMain(org.jkiss.dbeaver.ui.controls.resultset.handler.ResultSetHandlerMain) org.jkiss.dbeaver.ui(org.jkiss.dbeaver.ui) StyledText(org.eclipse.swt.custom.StyledText) IWorkbenchCommandConstants(org.eclipse.ui.IWorkbenchCommandConstants) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) ContentProposal(org.eclipse.jface.fieldassist.ContentProposal) Matcher(java.util.regex.Matcher) PartInitException(org.eclipse.ui.PartInitException) Locale(java.util.Locale) ContentProposalAdapter(org.eclipse.jface.fieldassist.ContentProposalAdapter) IContentProposal(org.eclipse.jface.fieldassist.IContentProposal) SystemJob(org.jkiss.dbeaver.model.runtime.SystemJob) IAdaptable(org.eclipse.core.runtime.IAdaptable) CommonUtils(org.jkiss.utils.CommonUtils) org.eclipse.swt.graphics(org.eclipse.swt.graphics) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) StyledTextUtils(org.jkiss.dbeaver.ui.controls.StyledTextUtils) Collection(java.util.Collection) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBStyles(org.jkiss.dbeaver.ui.css.DBStyles) ContentAssistUtils(org.jkiss.dbeaver.ui.contentassist.ContentAssistUtils) List(java.util.List) DBException(org.jkiss.dbeaver.DBException) SWT(org.eclipse.swt.SWT) SQLSyntaxManager(org.jkiss.dbeaver.model.sql.SQLSyntaxManager) SQLUtils(org.jkiss.dbeaver.model.sql.SQLUtils) Pattern(java.util.regex.Pattern) SQLWordPartDetector(org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector) ContentProposalExt(org.jkiss.dbeaver.ui.contentassist.ContentProposalExt) DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) SQLConstants(org.jkiss.dbeaver.model.sql.SQLConstants) Nullable(org.jkiss.code.Nullable) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) NotNull(org.jkiss.code.NotNull) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) Log(org.jkiss.dbeaver.Log) ResultSetMessages(org.jkiss.dbeaver.ui.controls.resultset.internal.ResultSetMessages) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity) org.eclipse.swt.events(org.eclipse.swt.events) IContentProposalProvider(org.eclipse.jface.fieldassist.IContentProposalProvider) IUndoManager(org.eclipse.jface.text.IUndoManager) TextEditorUtils(org.jkiss.dbeaver.ui.editors.TextEditorUtils) org.eclipse.swt.widgets(org.eclipse.swt.widgets) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) TextViewerUndoManager(org.eclipse.jface.text.TextViewerUndoManager) CSSUtils(org.jkiss.dbeaver.ui.css.CSSUtils) TextViewer(org.eclipse.jface.text.TextViewer) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) GridLayout(org.eclipse.swt.layout.GridLayout) SQLWordPartDetector(org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector) ContentProposalExt(org.jkiss.dbeaver.ui.contentassist.ContentProposalExt) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) SystemJob(org.jkiss.dbeaver.model.runtime.SystemJob) ContentProposal(org.eclipse.jface.fieldassist.ContentProposal) IContentProposal(org.eclipse.jface.fieldassist.IContentProposal) IContentProposal(org.eclipse.jface.fieldassist.IContentProposal) SQLSyntaxManager(org.jkiss.dbeaver.model.sql.SQLSyntaxManager) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)

Example 24 with DBRRunnableWithProgress

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

the class ResultSetReferenceMenu method fillRefTablesActions.

static void fillRefTablesActions(@Nullable DBRProgressMonitor extMonitor, ResultSetViewer viewer, List<ResultSetRow> rows, DBSEntity singleSource, IMenuManager manager, boolean openInNewWindow) {
    final List<DBSEntityAssociation> references = new ArrayList<>();
    final List<DBSEntityAssociation> associations = new ArrayList<>();
    DBRRunnableWithProgress refCollector = monitor -> {
        try {
            monitor.beginTask("Read references", 1);
            Collection<? extends DBSEntityAssociation> refs = DBVUtils.getAllReferences(monitor, singleSource);
            {
                monitor.beginTask("Check references", refs.size());
                for (DBSEntityAssociation ref : refs) {
                    if (monitor.isCanceled()) {
                        return;
                    }
                    monitor.subTask("Check references " + ref.getName());
                    boolean allMatch = true;
                    DBSEntityConstraint ownConstraint = ref.getReferencedConstraint();
                    if (ownConstraint instanceof DBSEntityReferrer) {
                        List<? extends DBSEntityAttributeRef> attributeReferences = ((DBSEntityReferrer) ownConstraint).getAttributeReferences(monitor);
                        if (attributeReferences != null) {
                            for (DBSEntityAttributeRef ownAttrRef : attributeReferences) {
                                if (viewer.getModel().getAttributeBinding(ownAttrRef.getAttribute()) == null) {
                                    // Attribute is not in the list - skip this association
                                    allMatch = false;
                                    break;
                                }
                            }
                        } else {
                            allMatch = false;
                        }
                    }
                    if (allMatch) {
                        references.add(ref);
                    }
                    monitor.worked(1);
                }
            }
            monitor.done();
            if (monitor.isCanceled()) {
                return;
            }
            monitor.beginTask("Read associations", 1);
            Collection<? extends DBSEntityAssociation> fks = singleSource.getAssociations(monitor);
            if (fks != null) {
                monitor.beginTask("Check associations", fks.size());
                for (DBSEntityAssociation fk : fks) {
                    if (monitor.isCanceled()) {
                        return;
                    }
                    monitor.subTask("Check association " + fk.getName());
                    boolean allMatch = true;
                    if (fk instanceof DBSEntityReferrer && fk.getReferencedConstraint() != null) {
                        for (DBSEntityAttributeRef ownAttr : ((DBSEntityReferrer) fk).getAttributeReferences(monitor)) {
                            if (viewer.getModel().getAttributeBinding(ownAttr.getAttribute()) == null) {
                                // Attribute is not in the list - skip this association
                                allMatch = false;
                                break;
                            }
                        }
                    }
                    if (allMatch) {
                        associations.add(fk);
                    }
                    monitor.worked(1);
                }
            }
            monitor.done();
        } catch (DBException e) {
            throw new InvocationTargetException(e);
        }
    };
    try {
        if (extMonitor != null) {
            refCollector.run(extMonitor);
        } else {
            UIUtils.runInProgressService(refCollector);
        }
    } catch (InvocationTargetException e) {
        DBWorkbench.getPlatformUI().showError("Table References", "Error reading referencing tables for '" + singleSource.getName() + "'", e.getTargetException());
        return;
    } catch (InterruptedException e) {
        return;
    }
    manager.removeAll();
    if (CommonUtils.isEmpty(associations)) {
        manager.add(NOFKS_ACTION);
    } else {
        manager.add(FKS_TITLE_ACTION);
        manager.add(new Separator());
        ArrayList<Action> entries = new ArrayList<>(associations.size());
        for (DBSEntityAssociation association : associations) {
            DBSEntity refTable = association.getReferencedConstraint().getParentObject();
            entries.add(new Action(DBUtils.getObjectFullName(refTable, DBPEvaluationContext.UI) + " (" + association.getName() + ")", DBeaverIcons.getImageDescriptor(DBSEntityType.TABLE.getIcon())) {

                @Override
                public void run() {
                    new AbstractJob("Navigate association") {

                        @Override
                        protected IStatus run(DBRProgressMonitor monitor) {
                            try {
                                viewer.navigateAssociation(new VoidProgressMonitor(), viewer.getModel(), association, rows, openInNewWindow);
                            } catch (DBException e) {
                                return GeneralUtils.makeExceptionStatus(e);
                            }
                            return Status.OK_STATUS;
                        }
                    }.schedule();
                }
            });
        }
        entries.sort(Comparator.comparing(Action::getText));
        entries.forEach(manager::add);
    }
    manager.add(new Separator());
    if (CommonUtils.isEmpty(references)) {
        manager.add(NOREFS_ACTION);
    } else {
        manager.add(REFS_TITLE_ACTION);
        manager.add(new Separator());
        ArrayList<Action> entries = new ArrayList<>(references.size());
        for (DBSEntityAssociation refAssociation : references) {
            DBSEntity refTable = refAssociation.getParentObject();
            entries.add(new Action(DBUtils.getObjectFullName(refTable, DBPEvaluationContext.UI) + " (" + refAssociation.getName() + ")", DBeaverIcons.getImageDescriptor(DBSEntityType.TABLE.getIcon())) {

                @Override
                public void run() {
                    new AbstractJob("Navigate reference") {

                        @Override
                        protected IStatus run(DBRProgressMonitor monitor) {
                            try {
                                viewer.navigateReference(new VoidProgressMonitor(), viewer.getModel(), refAssociation, rows, openInNewWindow);
                            } catch (DBException e) {
                                return GeneralUtils.makeExceptionStatus(e);
                            }
                            return Status.OK_STATUS;
                        }
                    }.schedule();
                }
            });
        }
        entries.sort(Comparator.comparing(Action::getText));
        entries.forEach(manager::add);
    }
}
Also used : DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) Nullable(org.jkiss.code.Nullable) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) EmptyAction(org.jkiss.dbeaver.ui.EmptyAction) ArrayList(java.util.ArrayList) IStatus(org.eclipse.core.runtime.IStatus) DBVUtils(org.jkiss.dbeaver.model.virtual.DBVUtils) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) UIUtils(org.jkiss.dbeaver.ui.UIUtils) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) ResultSetMessages(org.jkiss.dbeaver.ui.controls.resultset.internal.ResultSetMessages) GeneralUtils(org.jkiss.dbeaver.utils.GeneralUtils) CommonUtils(org.jkiss.utils.CommonUtils) Separator(org.eclipse.jface.action.Separator) org.jkiss.dbeaver.model.struct(org.jkiss.dbeaver.model.struct) DBUtils(org.jkiss.dbeaver.model.DBUtils) Collection(java.util.Collection) Status(org.eclipse.core.runtime.Status) Action(org.eclipse.jface.action.Action) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) DBException(org.jkiss.dbeaver.DBException) IMenuManager(org.eclipse.jface.action.IMenuManager) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Comparator(java.util.Comparator) DBPEvaluationContext(org.jkiss.dbeaver.model.DBPEvaluationContext) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBException(org.jkiss.dbeaver.DBException) EmptyAction(org.jkiss.dbeaver.ui.EmptyAction) Action(org.eclipse.jface.action.Action) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Collection(java.util.Collection) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) Separator(org.eclipse.jface.action.Separator)

Example 25 with DBRRunnableWithProgress

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

the class SQLAttributeResolver method resolveAll.

@Override
protected String[] resolveAll(final TemplateContext context) {
    final DBCExecutionContext executionContext = ((DBPContextProvider) context).getExecutionContext();
    if (executionContext == null) {
        return super.resolveAll(context);
    }
    TemplateVariable tableVariable = ((SQLContext) context).getTemplateVariable("table");
    final String tableName = tableVariable == null ? null : tableVariable.getDefaultValue();
    if (!CommonUtils.isEmpty(tableName)) {
        final List<DBSEntityAttribute> attributes = new ArrayList<>();
        DBRRunnableWithProgress runnable = new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    List<DBSEntity> entities = new ArrayList<>();
                    SQLEntityResolver.resolveTables(monitor, executionContext, context, entities);
                    if (!CommonUtils.isEmpty(entities)) {
                        DBSEntity table = DBUtils.findObject(entities, tableName);
                        if (table != null) {
                            attributes.addAll(CommonUtils.safeCollection(table.getAttributes(monitor)));
                        }
                    }
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        RuntimeUtils.runTask(runnable, "Resolve attributes", 1000);
        if (!CommonUtils.isEmpty(attributes)) {
            String[] result = new String[attributes.size()];
            for (int i = 0; i < attributes.size(); i++) {
                DBSEntityAttribute entity = attributes.get(i);
                result[i] = entity.getName();
            }
            return result;
        }
    }
    return super.resolveAll(context);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Aggregations

DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)64 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)60 InvocationTargetException (java.lang.reflect.InvocationTargetException)58 DBException (org.jkiss.dbeaver.DBException)36 ArrayList (java.util.ArrayList)11 IFile (org.eclipse.core.resources.IFile)11 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)11 CoreException (org.eclipse.core.runtime.CoreException)10 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)10 List (java.util.List)9 NotNull (org.jkiss.code.NotNull)9 Nullable (org.jkiss.code.Nullable)9 CommonUtils (org.jkiss.utils.CommonUtils)8 File (java.io.File)7 Collection (java.util.Collection)7 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)7 Log (org.jkiss.dbeaver.Log)6 InputStream (java.io.InputStream)5 IResource (org.eclipse.core.resources.IResource)5 SWT (org.eclipse.swt.SWT)5