Fix: preserve query input when switching dataverse; replace leading USE statement (ASTERIXDB-3124)#44
Conversation
…SE statement (ASTERIXDB-3124)
There was a problem hiding this comment.
Pull request overview
This PR updates the Angular dashboard query editor’s dataverse-switching behavior to avoid overwriting the user’s in-progress query, by updating (or removing) a leading USE <dataverse>; statement instead.
Changes:
- Preserve existing
queryStringwhen changing the selected dataverse. - Replace an existing leading
USE ...;statement if present, otherwise prepend a new one. - Remove a leading
USE ...;when switching back toDefault/None.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const useStmt = 'USE ' + this.selected + ';\n'; | ||
| let qs = this.queryString || ''; | ||
| const leadingUseRegex = /^\s*USE\s+[^;]+;\s*/i; | ||
|
|
||
| if (this.selected === 'None' || this.selected === 'Default') { | ||
| qs = qs.replace(leadingUseRegex, ''); | ||
| this.selected = 'Default'; | ||
| } else { | ||
| this.queryString = 'USE ' + this.selected + '; \n' + this.queryString; | ||
| if (leadingUseRegex.test(qs)) { | ||
| qs = qs.replace(leadingUseRegex, useStmt); | ||
| } else { | ||
| qs = useStmt + qs; | ||
| } |
There was a problem hiding this comment.
leadingUseRegex only matches a USE ...; that appears at the very start of the query (ignoring whitespace). If the user has a leading SQL++ comment/header (e.g., -- ... or /* ... */) before the USE statement, the regex won’t match and switching dataverses will prepend a new USE while leaving the old one in place, potentially resulting in the query still running under the old dataverse (since the later USE can override). Consider expanding the match to allow leading comments before the first USE, or stripping/replacing the first USE statement that occurs before any non-comment tokens.
This PR fixes an issue where selecting a different dataverse from the dropdown would overwrite the current query in the editor with a USE statement, causing loss of user input.
With this change, the query editor now preserves the existing query text when the dataverse selection changes. Instead of replacing the query, the logic updates or prepends the appropriate USE statement only when necessary.
🔧 Changes Made
Updated the dataverseSelected() handler in the Angular dashboard.
Preserved the existing query string instead of overwriting it.
Replaced the leading USE statement if present, otherwise prepended it.
Removed the USE statement when switching to Default or None.
✅ How to Test
Open the AsterixDB dashboard (Angular UI).
Enter a query in the editor (e.g., SELECT * FROM Metadata.Dataset;).
Change the dataverse using the dropdown.
Verify:
The existing query is preserved.
The USE ; statement is correctly updated or inserted.
Switching to Default removes any existing USE statement.