Search in sources :

Example 6 with SQLScriptElement

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

the class SQLScriptExecuteHandler method processScript.

private void processScript(DBRProgressMonitor monitor, DBTTask task, SQLScriptExecuteSettings settings, DBCExecutionContext executionContext, String filePath, String sqlScriptContent, Log log, PrintStream logStream) throws DBException {
    PrintWriter logWriter = new PrintWriter(logStream, true);
    List<SQLScriptElement> scriptElements = SQLScriptParser.parseScript(executionContext, sqlScriptContent);
    SQLScriptContext scriptContext = new SQLScriptContext(null, () -> executionContext, null, logWriter, null);
    scriptContext.setVariables(DBTaskUtils.getVariables(task));
    SQLScriptDataReceiver dataReceiver = new SQLScriptDataReceiver();
    SQLScriptProcessor scriptProcessor = new SQLScriptProcessor(executionContext, scriptElements, scriptContext, dataReceiver, log);
    scriptProcessor.setCommitType(settings.isAutoCommit() ? SQLScriptCommitType.AUTOCOMMIT : SQLScriptCommitType.AT_END);
    scriptProcessor.setErrorHandling(settings.isIgnoreErrors() ? SQLScriptErrorHandling.IGNORE : SQLScriptErrorHandling.STOP_ROLLBACK);
    if (settings.isDumpQueryResultsToLog()) {
        dataReceiver.setDumpWriter(logWriter);
    }
    scriptProcessor.runScript(monitor);
}
Also used : SQLScriptProcessor(org.jkiss.dbeaver.model.sql.exec.SQLScriptProcessor) SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) SQLScriptContext(org.jkiss.dbeaver.model.sql.SQLScriptContext)

Example 7 with SQLScriptElement

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

the class ResultSetViewer method getActiveQueryText.

@NotNull
public String getActiveQueryText() {
    DBCStatistics statistics = getModel().getStatistics();
    String queryText = statistics == null ? null : statistics.getQueryText();
    if (queryText == null || queryText.isEmpty()) {
        DBSDataContainer dataContainer = getDataContainer();
        if (dataContainer != null) {
            if (dataContainer instanceof SQLQueryContainer) {
                SQLScriptElement query = ((SQLQueryContainer) dataContainer).getQuery();
                if (query != null) {
                    return query.getText();
                }
            }
            return dataContainer.getName();
        }
        queryText = DEFAULT_QUERY_TEXT;
    }
    return queryText;
}
Also used : SQLQueryContainer(org.jkiss.dbeaver.model.sql.SQLQueryContainer) SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) NotNull(org.jkiss.code.NotNull)

Example 8 with SQLScriptElement

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

the class SQLCompletionProcessor method computeContextInformation.

/**
 * This method is incomplete in that it does not implement logic to produce
 * some context help relevant to SQL. It just hard codes two strings to
 * demonstrate the action
 *
 * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(ITextViewer,
 *      int)
 */
@Nullable
@Override
public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
    SQLScriptElement statementInfo = editor.extractQueryAtPos(documentOffset);
    if (statementInfo == null || CommonUtils.isEmpty(statementInfo.getText())) {
        return null;
    }
    IContextInformation[] result = new IContextInformation[1];
    result[0] = new ContextInformation(statementInfo.getText(), statementInfo.getText());
    return result;
}
Also used : SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) Nullable(org.jkiss.code.Nullable)

Example 9 with SQLScriptElement

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

the class SQLReconcilingStrategy method reconcile.

private void reconcile(int damagedRegionOffset, int damagedRegionLength) {
    if (!editor.isFoldingEnabled()) {
        return;
    }
    ProjectionAnnotationModel model = editor.getAnnotationModel();
    if (model == null) {
        return;
    }
    SQLScriptElementImpl leftBound = cache.lower(new SQLScriptElementImpl(damagedRegionOffset, damagedRegionLength));
    if (leftBound != null) {
        leftBound = cache.lower(leftBound);
    }
    SQLScriptElementImpl rightBound = cache.ceiling(new SQLScriptElementImpl(damagedRegionOffset + damagedRegionLength, 0));
    if (leftBound == null) {
        damagedRegionOffset = 0;
    } else {
        damagedRegionOffset = leftBound.getOffset() + leftBound.getLength();
    }
    if (rightBound == null) {
        damagedRegionLength = document.getLength();
    } else {
        damagedRegionLength = rightBound.getOffset() + rightBound.getLength() - damagedRegionOffset;
    }
    List<SQLScriptElement> parsedQueries = extractQueries(damagedRegionOffset, damagedRegionLength);
    if (parsedQueries == null) {
        return;
    }
    if (rightBound != null && !parsedQueries.isEmpty()) {
        SQLScriptElement rightmostParsedQuery = parsedQueries.get(parsedQueries.size() - 1);
        if (!rightBound.equals(new SQLScriptElementImpl(rightmostParsedQuery.getOffset(), expandQueryLength(rightmostParsedQuery)))) {
            parsedQueries = extractQueries(damagedRegionOffset, document.getLength());
            if (parsedQueries == null) {
                return;
            }
            rightBound = null;
        }
    }
    Collection<SQLScriptElementImpl> cachedQueries;
    if (leftBound == null && rightBound == null) {
        cachedQueries = Collections.unmodifiableNavigableSet(cache);
    } else if (leftBound == null) {
        cachedQueries = Collections.unmodifiableNavigableSet(cache.headSet(rightBound, true));
    } else if (rightBound == null) {
        cachedQueries = Collections.unmodifiableNavigableSet(cache.tailSet(leftBound, false));
    } else {
        cachedQueries = Collections.unmodifiableNavigableSet(cache.subSet(leftBound, false, rightBound, true));
    }
    List<SQLScriptElementImpl> parsedElements = parsedQueries.stream().filter(this::deservesFolding).map(element -> new SQLScriptElementImpl(element.getOffset(), expandQueryLength(element))).collect(Collectors.toList());
    Map<Annotation, SQLScriptElementImpl> additions = new HashMap<>();
    for (SQLScriptElementImpl element : parsedElements) {
        if (!cachedQueries.contains(element)) {
            element.setAnnotation(new ProjectionAnnotation());
            additions.put(element.getAnnotation(), element);
        }
    }
    Collection<SQLScriptElementImpl> deletedPositions = cachedQueries.stream().filter(element -> !parsedElements.contains(element)).collect(Collectors.toList());
    Annotation[] deletions = deletedPositions.stream().map(SQLScriptElementImpl::getAnnotation).toArray(Annotation[]::new);
    model.modifyAnnotations(deletions, additions, null);
    cache.removeAll(deletedPositions);
    cache.addAll(additions.values());
}
Also used : java.util(java.util) org.eclipse.jface.text(org.eclipse.jface.text) ProjectionAnnotationModel(org.eclipse.jface.text.source.projection.ProjectionAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) Nullable(org.jkiss.code.Nullable) SQLEditorBase(org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase) Collectors(java.util.stream.Collectors) NotNull(org.jkiss.code.NotNull) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) DirtyRegion(org.eclipse.jface.text.reconciler.DirtyRegion) IReconcilingStrategyExtension(org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension) IReconcilingStrategy(org.eclipse.jface.text.reconciler.IReconcilingStrategy) SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) ProjectionAnnotationModel(org.eclipse.jface.text.source.projection.ProjectionAnnotationModel) ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement) Annotation(org.eclipse.jface.text.source.Annotation) ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation)

Example 10 with SQLScriptElement

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

the class CopySourceCodeHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    SQLEditor editor = RuntimeUtils.getObjectAdapter(HandlerUtil.getActiveEditor(event), SQLEditor.class);
    if (editor == null) {
        return null;
    }
    ISelection selection = editor.getSelectionProvider().getSelection();
    if (selection.isEmpty() || !(selection instanceof TextSelection)) {
        return null;
    }
    TextSelection textSelection = (TextSelection) selection;
    if (textSelection.getLength() < 2) {
        // Use active query
        SQLScriptElement activeQuery = editor.extractActiveQuery();
        if (activeQuery != null && activeQuery.getLength() > 1) {
            textSelection = new TextSelection(editor.getDocument(), activeQuery.getOffset(), activeQuery.getLength());
        }
    }
    TargetFormatDialog dialog = new TargetFormatDialog(editor, textSelection);
    if (dialog.open() != IDialogConstants.OK_ID) {
        return null;
    }
    UIUtils.setClipboardContents(Display.getCurrent(), TextTransfer.getInstance(), dialog.getConvertedText());
    return null;
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) SQLScriptElement(org.jkiss.dbeaver.model.sql.SQLScriptElement)

Aggregations

SQLScriptElement (org.jkiss.dbeaver.model.sql.SQLScriptElement)10 Nullable (org.jkiss.code.Nullable)3 SQLEditorBase (org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase)3 Annotation (org.eclipse.jface.text.source.Annotation)2 ProjectionAnnotation (org.eclipse.jface.text.source.projection.ProjectionAnnotation)2 ProjectionAnnotationModel (org.eclipse.jface.text.source.projection.ProjectionAnnotationModel)2 IEditorPart (org.eclipse.ui.IEditorPart)2 NotNull (org.jkiss.code.NotNull)2 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 org.eclipse.jface.text (org.eclipse.jface.text)1 IRegion (org.eclipse.jface.text.IRegion)1 Position (org.eclipse.jface.text.Position)1 Region (org.eclipse.jface.text.Region)1 TextSelection (org.eclipse.jface.text.TextSelection)1 DirtyRegion (org.eclipse.jface.text.reconciler.DirtyRegion)1 IReconcilingStrategy (org.eclipse.jface.text.reconciler.IReconcilingStrategy)1 IReconcilingStrategyExtension (org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension)1 ISelection (org.eclipse.jface.viewers.ISelection)1