Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#16219 Add popup suggested to reload metadata if ddl query was executed #18754

Draft
wants to merge 8 commits into
base: devel
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,8 @@
PostgreConstants.TYPE_GEOGRAPHY.equals(typeName);
}

public static String getRealSchemaName(PostgreDatabase database, String name) {
return name.replace(PostgreConstants.USER_VARIABLE, database.getMetaContext().getActiveUser());
public static String getRealSchemaName(PostgreExecutionContext metaContext, String name) {

Check warning on line 811 in plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/PostgreUtils.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/PostgreUtils.java#L811

Missing a Javadoc comment.
return name.replace(PostgreConstants.USER_VARIABLE, metaContext.getActiveUser());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
} else if (!params.isGlobalSearch()) {
// Limit object search with search path
for (String sn : executionContext.getSearchPath()) {
PostgreSchema schema = database.getSchema(monitor, PostgreUtils.getRealSchemaName(database, sn));
PostgreSchema schema = database.getSchema(monitor, PostgreUtils.getRealSchemaName(executionContext, sn));
if (schema != null) {
nsList.add(schema);
}
Expand Down Expand Up @@ -190,7 +190,6 @@
}
queryParams.setMaxResults(params.getMaxResults() - objects.size());
String sql = buildFindQuery(queryParams);

// Load tables
try (JDBCPreparedStatement dbStat = session.prepareStatement(sql)) {
fillParams(dbStat, params, schemas, params.isSearchInDefinitions());
Expand All @@ -213,7 +212,7 @@
public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
PostgreTableBase table = tableSchema.getTable(monitor, tableId);
if (table == null) {
throw new DBException("Table '" + tableName + "' not found in schema '" + tableSchema.getName() + "'");
throw new DBException("Table '" + tableName + "' not found in schema '" + tableSchema.getName() + "' by id " + tableId);

Check warning on line 215 in plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreStructureAssistant.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreStructureAssistant.java#L215

Line is longer than 140 characters (found 152).
}
return table;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
}
}

@NotNull
@Nullable
@Override
public JDBCExecutionContext getDefaultContext(DBRProgressMonitor monitor, boolean meta) {
if (sharedInstance != null) {
Expand All @@ -189,7 +189,7 @@
return getDefaultContext(meta);
}

@NotNull
@Nullable

Check warning on line 192 in plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCRemoteInstance.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/JDBCRemoteInstance.java#L192

Missing a Javadoc comment.
public JDBCExecutionContext getDefaultContext(boolean meta) {
if (sharedInstance != null) {
return sharedInstance.getDefaultContext(meta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public DBNDatabaseNode getNodeByObject(DBSObject object)
}
}
}
// Get just first one
// Get just the first one
return nodeList.get(0);
} else {
// Never be here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.jkiss.dbeaver.model.impl.DefaultServerOutputReader;
import org.jkiss.dbeaver.model.impl.sql.SQLQueryTransformerCount;
import org.jkiss.dbeaver.model.messages.ModelMessages;
import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode;
import org.jkiss.dbeaver.model.navigator.DBNUtils;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.model.qm.QMUtils;
Expand All @@ -95,6 +96,8 @@
import org.jkiss.dbeaver.model.struct.DBSInstance;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.DBSObjectState;
import org.jkiss.dbeaver.model.struct.rdb.DBSCatalog;
import org.jkiss.dbeaver.model.struct.rdb.DBSSchema;
import org.jkiss.dbeaver.registry.DataSourceUtils;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.ui.DBPPlatformUI.UserChoiceResponse;
Expand Down Expand Up @@ -128,6 +131,8 @@
import org.jkiss.dbeaver.ui.editors.sql.variables.SQLVariablesPanel;
import org.jkiss.dbeaver.ui.editors.text.ScriptPositionColumn;
import org.jkiss.dbeaver.ui.navigator.INavigatorModelView;
import org.jkiss.dbeaver.ui.navigator.actions.NavigatorHandlerRefresh;
import org.jkiss.dbeaver.ui.notifications.NotificationUtils;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.dbeaver.utils.PrefUtils;
import org.jkiss.dbeaver.utils.ResourceUtils;
Expand All @@ -144,6 +149,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* SQL Executor
Expand Down Expand Up @@ -184,7 +190,7 @@

public static final String VIEW_PART_PROP_NAME = "org.jkiss.dbeaver.ui.editors.sql.SQLEditor";


private static final String REFRESH_DATA_SOURCE_ON_DDL_QUERY_POPUP_ID_PART = "refreshDataSourceOnDdlQuery";

public static final String DEFAULT_TITLE_PATTERN = "<${" + SQLPreferenceConstants.VAR_CONNECTION_NAME + "}> ${" + SQLPreferenceConstants.VAR_FILE_NAME + "}";
public static final String DEFAULT_SCRIPT_FILE_NAME = "Script";
Expand Down Expand Up @@ -325,7 +331,13 @@
return executionContextProvider.getExecutionContext();
}
if (dataSourceContainer != null && !SQLEditorUtils.isOpenSeparateConnection(dataSourceContainer)) {
return DBUtils.getDefaultContext(getDataSource(), false);
try {
return DBUtils.getDefaultContext(getDataSource(), false);
} catch (IllegalStateException e) {
// Probably we are in the metadata refresh state in the single connection mode
log.debug(e);
return null;
}
}
return null;
}
Expand Down Expand Up @@ -4152,6 +4164,7 @@
private final ITextSelection originalSelection = (ITextSelection) getSelectionProvider().getSelection();
private int topOffset, visibleLength;
private boolean closeTabOnError;
private boolean ddlQueryMet = false;
private SQLQueryListener extListener;

private SQLEditorQueryListener(QueryProcessor queryProcessor, boolean closeTabOnError) {
Expand Down Expand Up @@ -4227,8 +4240,10 @@
@Override
public void onEndQuery(final DBCSession session, final SQLQueryResult result, DBCStatistics statistics) {
try {
SQLQuery query = result.getStatement();
ddlQueryMet |= query.getType() == SQLQueryType.DDL;
synchronized (runningQueries) {
runningQueries.remove(result.getStatement());
runningQueries.remove(query);
}
queryProcessor.curJobRunning.decrementAndGet();
if (getTotalQueryRunning() <= 0) {
Expand All @@ -4240,7 +4255,9 @@
updateDirtyFlag();
});
}

if (ddlQueryMet && query.equals(queryProcessor.curJob.getLastQuery())) {
showMetadataChangedNotification();
}
if (isDisposed()) {
return;
}
Expand All @@ -4260,6 +4277,74 @@
}
}
}

private void showMetadataChangedNotification() {
DBPDataSource dataSource = getDataSource();
if (dataSource == null) {
return;
}
UIUtils.runInUI(m -> {
NotificationUtils.sendNotification(
REFRESH_DATA_SOURCE_ON_DDL_QUERY_POPUP_ID_PART + dataSource.getContainer().getId(),
NLS.bind(SQLEditorMessages.sql_editor_refresh_data_source_on_ddl_query_popup_title, dataSource.getName()),
SQLEditorMessages.sql_editor_refresh_data_source_on_ddl_query_popup_text,
DBPMessageType.WARNING,
() -> {
UIUtils.runUIJob("Refreshing data source metadata after statement execution", monitor -> {
DBSInstance defaultObject = getExecutionContext().getOwnerInstance();
DBNDatabaseNode defObjNode = DBNUtils.getNodeByObject(monitor, defaultObject, true);
DBSObject oldDefaultObject = null;
if (defaultObject != null) {
if (defObjNode != null) {
oldDefaultObject = defObjNode.getObject();
} else {
log.warn("Failed to resolve node for default object. Default object wouldn't be refreshed.");
}
}
DBNDatabaseNode dsNode = DBNUtils.getNodeByObject(monitor, dataSource, true);

if (NavigatorHandlerRefresh.refresh(SQLEditor.this, List.of(dsNode)).join(0, monitor.getNestedMonitor())) {
if (oldDefaultObject != null) {
List<DBCExecutionContext> executionContexts = Stream.of(
getExecutionContext(),
dataSource.getDefaultInstance().getDefaultContext(monitor, true),
dataSource.getDefaultInstance().getDefaultContext(monitor, false)
).distinct().collect(Collectors.toList());
for (DBCExecutionContext executionContext: executionContexts) {

Check warning on line 4313 in plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java#L4313

WhitespaceAround: : is not preceded with whitespace.
DBCExecutionContextDefaults editorContextDefaults = executionContext.getContextDefaults();
if (editorContextDefaults != null) {
DBSObject newDefaultObject = defObjNode.getObject();
if (defObjNode.getObject() != oldDefaultObject
&& getExecutionContext().getOwnerInstance() == oldDefaultObject
) {
try {
if (oldDefaultObject instanceof DBSCatalog
&& oldDefaultObject == editorContextDefaults.getDefaultCatalog()
) {
monitor.subTask("Change default catalog");
editorContextDefaults.setDefaultCatalog(monitor, (DBSCatalog) newDefaultObject, null);

Check warning on line 4325 in plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java#L4325

Line is longer than 140 characters (found 142).
} else if (oldDefaultObject instanceof DBSSchema
&& oldDefaultObject == editorContextDefaults.getDefaultSchema()
) {
monitor.subTask("Change default schema");
editorContextDefaults.setDefaultSchema(monitor, (DBSSchema) newDefaultObject);
}
} catch (Throwable ex) {
log.error("Failed to refresh execution context default object", ex);
}
}
} else {
log.trace("Execution context doesn't support explicit default object refresh."
+ "Data context node object identity is mandatory.");
}
}
}
}
});
}
);
});
}

private void processQueryResult(DBRProgressMonitor monitor, SQLQueryResult result, DBCStatistics statistics) {
dumpQueryServerOutput(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void setFetchResultSets(boolean fetchResultSets)

public SQLScriptElement getLastQuery()
{
return queries.isEmpty() ? null : queries.get(0);
return queries.isEmpty() ? null : queries.get(queries.size() - 1);
ShadelessFox marked this conversation as resolved.
Show resolved Hide resolved
}

public SQLScriptElement getLastGoodQuery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ public class SQLEditorMessages extends NLS {

public static String sql_editor_panel_output_filter_message;
public static String sql_editor_panel_output_filter_hint;
public static String sql_editor_refresh_data_source_on_ddl_query_popup_title;
public static String sql_editor_refresh_data_source_on_ddl_query_popup_text;

static {
// initialize resource bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ sql_editor_confirm_no_fetch_result_for_big_script_question = Large script execut
sql_editor_confirm_no_fetch_result_for_big_script_yes = Yes
sql_editor_confirm_no_fetch_result_for_big_script_no = No
sql_editor_confirm_no_fetch_result_for_big_script_remember = Don't show again for this editor till the end of the session

sql_editor_panel_output_filter_message = Enter a part of a message to search for here
sql_editor_panel_output_filter_hint = Filter visible output
sql_editor_refresh_data_source_on_ddl_query_popup_title = Click to reload metadata for {0}
sql_editor_refresh_data_source_on_ddl_query_popup_text = Some DDL statements were executed
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.IDialogConstants;
Expand All @@ -30,6 +32,7 @@
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.navigator.*;
Expand Down Expand Up @@ -64,7 +67,7 @@

// If navigator refresh is possible then do not refresh active part directly
// Because active part should be refresh in navigator event handler
if (refreshInNavigator(event, workbenchPart)) {
if (refreshInNavigator(event, workbenchPart).initiated) {
return null;
}

Expand All @@ -83,18 +86,17 @@
if (((IRefreshablePart) workbenchPart).refreshPart(this, true) == IRefreshablePart.RefreshResult.CANCELED) {
return null;
}
//return null;
}

return null;
}

private boolean refreshInNavigator(ExecutionEvent event, IWorkbenchPart workbenchPart) {
private RefreshStatus refreshInNavigator(ExecutionEvent event, IWorkbenchPart workbenchPart) {
// Try to get navigator view and refresh node
INavigatorModelView navigatorView = GeneralUtils.adapt(workbenchPart, INavigatorModelView.class);
if (navigatorView == null) {
// Nothing to refresh
return false;
return RefreshStatus.skipped();
}
final List<DBNNode> refreshObjects = new ArrayList<>();
final ISelection selection = HandlerUtil.getCurrentSelection(event);
Expand All @@ -118,18 +120,28 @@
}
}
}


return prepareRefresh(this, refreshObjects);
}

@NotNull

Check warning on line 127 in plugins/org.jkiss.dbeaver.ui.navigator/src/org/jkiss/dbeaver/ui/navigator/actions/NavigatorHandlerRefresh.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.ui.navigator/src/org/jkiss/dbeaver/ui/navigator/actions/NavigatorHandlerRefresh.java#L127

Missing a Javadoc comment.
public static RefreshStatus refresh(@NotNull Object source, @NotNull List<? extends DBNNode> refreshObjects) {
return prepareRefresh(source, refreshObjects);
}

@NotNull
private static RefreshStatus prepareRefresh(@NotNull Object source, @NotNull List<? extends DBNNode> refreshObjects) {
// Check for open editors with selected objects
if (!refreshObjects.isEmpty()) {
for (IEditorReference er : UIUtils.getActiveWorkbenchWindow().getActivePage().getEditorReferences()) {
IEditorPart editorPart = er.getEditor(false);
if (editorPart instanceof IRefreshablePart && editorPart.getEditorInput() instanceof DatabaseEditorInput && editorPart.isDirty()) {
DBNDatabaseNode editorNode = ((DatabaseEditorInput<?>) editorPart.getEditorInput()).getNavigatorNode();
for (Iterator<DBNNode> iter = refreshObjects.iterator(); iter.hasNext(); ) {
for (Iterator<? extends DBNNode> iter = refreshObjects.iterator(); iter.hasNext();) {
DBNNode nextNode = iter.next();
if (nextNode == editorNode || editorNode.isChildOf(nextNode) || nextNode.isChildOf(editorNode)) {
if (((IRefreshablePart) editorPart).refreshPart(this, true) == IRefreshablePart.RefreshResult.CANCELED) {
return true;
if (((IRefreshablePart) editorPart).refreshPart(source, true) == IRefreshablePart.RefreshResult.CANCELED) {
return RefreshStatus.completed();
}
if (nextNode == editorNode) {
iter.remove();
Expand All @@ -142,12 +154,12 @@

// Refresh objects
if (!refreshObjects.isEmpty()) {
return refreshNavigator(refreshObjects);
return RefreshStatus.initiated(refreshNavigator(refreshObjects));
}
return false;
return RefreshStatus.skipped();
}

public static boolean refreshNavigator(final Collection<? extends DBNNode> refreshObjects)
public static Job refreshNavigator(final Collection<? extends DBNNode> refreshObjects)

Check warning on line 162 in plugins/org.jkiss.dbeaver.ui.navigator/src/org/jkiss/dbeaver/ui/navigator/actions/NavigatorHandlerRefresh.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Report

plugins/org.jkiss.dbeaver.ui.navigator/src/org/jkiss/dbeaver/ui/navigator/actions/NavigatorHandlerRefresh.java#L162

Missing a Javadoc comment.
{
Job refreshJob = new AbstractJob("Refresh navigator object(s)") {
@Override
Expand Down Expand Up @@ -215,7 +227,7 @@
refreshJob.setUser(true);
refreshJob.schedule();

return true;
return refreshJob;
}

private static boolean showConfirmation(DBNNode node) {
Expand Down