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);
}
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;
}
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;
}
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());
}
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;
}
Aggregations