use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by serge-rider.
the class SQLEditorBase method scrollCursorToError.
/**
* Error handling
*/
boolean scrollCursorToError(@NotNull DBRProgressMonitor monitor, @NotNull SQLQuery query, @NotNull Throwable error) {
try {
DBCExecutionContext context = getExecutionContext();
if (context == null) {
return false;
}
boolean scrolled = false;
DBPErrorAssistant errorAssistant = DBUtils.getAdapter(DBPErrorAssistant.class, context.getDataSource());
if (errorAssistant != null) {
DBPErrorAssistant.ErrorPosition[] positions = errorAssistant.getErrorPosition(monitor, context, query.getText(), error);
if (positions != null && positions.length > 0) {
int queryStartOffset = query.getOffset();
int queryLength = query.getLength();
DBPErrorAssistant.ErrorPosition pos = positions[0];
if (pos.line < 0) {
if (pos.position >= 0) {
// Only position
getSelectionProvider().setSelection(new TextSelection(queryStartOffset + pos.position, 0));
scrolled = true;
}
} else {
// Line + position
IDocument document = getDocument();
if (document != null) {
int startLine = document.getLineOfOffset(queryStartOffset);
int errorOffset = document.getLineOffset(startLine + pos.line);
int errorLength;
if (pos.position >= 0) {
errorOffset += pos.position;
errorLength = 1;
} else {
errorLength = document.getLineLength(startLine + pos.line);
}
if (errorOffset < queryStartOffset)
errorOffset = queryStartOffset;
if (errorLength > queryLength)
errorLength = queryLength;
if (errorOffset >= queryStartOffset + queryLength) {
// This may happen if error position was incorrectly detected.
// E.g. in SQL Server when actual error happened in some stored procedure.
errorOffset = queryStartOffset + queryLength - 1;
}
getSelectionProvider().setSelection(new TextSelection(errorOffset, errorLength));
scrolled = true;
}
}
}
}
return scrolled;
// if (!scrolled) {
// // Can't position on error - let's just select entire problem query
// showStatementInEditor(result.getStatement(), true);
// }
} catch (Exception e) {
log.warn("Error positioning on query error", e);
return false;
}
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by serge-rider.
the class NavigatorHandlerObjectGoto method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
DBCExecutionContext context = null;
DBSObject container = null;
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
if (activePart instanceof DBPContextProvider) {
context = ((DBPContextProvider) activePart).getExecutionContext();
} else if (activePart instanceof INavigatorModelView) {
final ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection) selection).getFirstElement();
if (element instanceof DBSWrapper) {
DBSObject object = ((DBSWrapper) element).getObject();
if (object != null) {
container = object;
while (container instanceof DBSFolder) {
container = container.getParentObject();
}
DBPDataSource dataSource = object.getDataSource();
if (dataSource != null) {
context = dataSource.getDefaultContext(true);
}
}
}
}
}
if (context == null) {
DBUserInterface.getInstance().showError("Go to object", "No active datasource");
return null;
}
IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
GotoObjectDialog dialog = new GotoObjectDialog(HandlerUtil.getActiveShell(event), context, container);
dialog.open();
Object[] objectsToOpen = dialog.getResult();
if (!ArrayUtils.isEmpty(objectsToOpen)) {
Collection<DBNDatabaseNode> nodes = NavigatorHandlerObjectBase.getNodesByObjects(Arrays.asList(objectsToOpen));
for (DBNDatabaseNode node : nodes) {
NavigatorUtils.openNavigatorNode(node, workbenchWindow);
}
}
return null;
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by serge-rider.
the class BaseSQLDialog method createSQLPanel.
protected Composite createSQLPanel(Composite parent) {
Composite panel = UIUtils.createPlaceholder(parent, 1);
panel.setLayoutData(new GridData(GridData.FILL_BOTH));
if (isLabelVisible()) {
UIUtils.createControlLabel(panel, "SQL Preview");
}
// new Label(panel, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Composite editorPH = new Composite(panel, SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.verticalIndent = 3;
gd.horizontalSpan = 1;
gd.minimumHeight = 100;
gd.minimumWidth = 100;
editorPH.setLayoutData(gd);
editorPH.setLayout(new FillLayout());
sqlViewer = new SQLEditorBase() {
@NotNull
@Override
protected SQLDialect getSQLDialect() {
return BaseSQLDialog.this.getSQLDialect();
}
@Override
public DBCExecutionContext getExecutionContext() {
return BaseSQLDialog.this.getExecutionContext();
}
};
updateSQL();
sqlViewer.createPartControl(editorPH);
if (isWordWrap()) {
Object text = sqlViewer.getAdapter(Control.class);
if (text instanceof StyledText) {
((StyledText) text).setWordWrap(true);
}
}
sqlViewer.reloadSyntaxRules();
parent.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
sqlViewer.dispose();
}
});
return panel;
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by serge-rider.
the class InvalidateJob method invalidateDataSource.
public static List<ContextInvalidateResult> invalidateDataSource(DBRProgressMonitor monitor, DBPDataSource dataSource) {
long timeSpent = 0;
List<ContextInvalidateResult> invalidateResults = new ArrayList<>();
DBPDataSourceContainer container = dataSource.getContainer();
DBWNetworkHandler[] activeHandlers = container.getActiveNetworkHandlers();
boolean networkOK = true;
boolean hasGoodContexts = false;
if (activeHandlers != null && activeHandlers.length > 0) {
for (DBWNetworkHandler nh : activeHandlers) {
monitor.subTask("Invalidate network [" + container.getName() + "]");
try {
nh.invalidateHandler(monitor);
} catch (Exception e) {
invalidateResults.add(new ContextInvalidateResult(DBCExecutionContext.InvalidateResult.ERROR, e));
networkOK = false;
break;
}
}
}
if (networkOK) {
// Invalidate datasource
monitor.subTask("Invalidate connections of [" + container.getName() + "]");
DBCExecutionContext[] allContexts = dataSource.getAllContexts();
for (DBCExecutionContext context : allContexts) {
long startTime = System.currentTimeMillis();
try {
final DBCExecutionContext.InvalidateResult result = context.invalidateContext(monitor);
if (result != DBCExecutionContext.InvalidateResult.ERROR) {
hasGoodContexts = true;
}
invalidateResults.add(new ContextInvalidateResult(result, null));
} catch (Exception e) {
invalidateResults.add(new ContextInvalidateResult(DBCExecutionContext.InvalidateResult.ERROR, e));
} finally {
timeSpent += (System.currentTimeMillis() - startTime);
}
}
}
if (!hasGoodContexts) {
// Close whole datasource. Target host seems to be unavailable
try {
container.disconnect(monitor);
} catch (Exception e) {
log.error("Error closing unaccessible datasource", e);
}
StringBuilder msg = new StringBuilder();
for (ContextInvalidateResult result : invalidateResults) {
if (result.error != null) {
if (msg.length() > 0)
msg.append("\n");
msg.append(result.error.getMessage());
}
}
UIUtils.showErrorDialog(null, "Forced disconnect", "Datasource '" + container.getName() + "' was disconnected: destination database unreachable.\n" + msg);
}
return invalidateResults;
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContext in project dbeaver by serge-rider.
the class GlobalProxySelector method select.
@Override
public List<Proxy> select(URI uri) {
String scheme = uri.getScheme();
if (CommonUtils.isEmpty(scheme)) {
return parent.select(uri);
}
if (scheme.startsWith("http")) {
// 1. Check for drivers download proxy
}
if (SocksConstants.SOCKET_SCHEME.equals(scheme)) {
// 2. Check for connections' proxy config
DBCExecutionContext activeContext = DBExecUtils.findConnectionContext(uri.getHost(), uri.getPort(), uri.getPath());
if (activeContext != null) {
List<Proxy> proxies = null;
DBPDataSourceContainer container = activeContext.getDataSource().getContainer();
for (DBWHandlerConfiguration networkHandler : container.getConnectionConfiguration().getDeclaredHandlers()) {
if (networkHandler.isEnabled() && networkHandler.getType() == DBWHandlerType.PROXY) {
Map<String, String> proxyProps = networkHandler.getProperties();
String proxyHost = proxyProps.get(SocksConstants.PROP_HOST);
String proxyPort = proxyProps.get(SocksConstants.PROP_PORT);
if (!CommonUtils.isEmpty(proxyHost)) {
int portNumber = SocksConstants.DEFAULT_SOCKS_PORT;
if (!CommonUtils.isEmpty(proxyPort)) {
try {
portNumber = Integer.parseInt(proxyPort);
} catch (NumberFormatException e) {
log.warn("Bad proxy port number", e);
}
}
InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, portNumber);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
if (proxies == null) {
proxies = new ArrayList<>();
}
proxies.add(proxy);
log.debug("Use SOCKS proxy [" + proxyAddr + "]");
}
}
}
if (proxies != null) {
return proxies;
}
}
}
return parent.select(uri);
}
Aggregations