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

Retain query messages when switching tabs #17999

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/controllers/queryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface IResultSet {
export default class QueryRunner {
// MEMBER VARIABLES ////////////////////////////////////////////////////
private _batchSets: BatchSummary[] = [];
private _batchSetMessages: { [batchId: number] : IResultMessage[]; } = {};
private _isExecuting: boolean;
private _resultLineOffset: number;
private _totalElapsedMilliseconds: number;
Expand Down Expand Up @@ -109,12 +110,12 @@ export default class QueryRunner {
this._batchSets = batchSets;
}

get isExecutingQuery(): boolean {
return this._isExecuting;
get batchSetMessages(): { [batchId: number] : IResultMessage[]; } {
return this._batchSetMessages;
}

get hasCompleted(): boolean {
return this._hasCompleted;
get isExecutingQuery(): boolean {
return this._isExecuting;
}

get isSqlCmd(): boolean {
Expand All @@ -125,6 +126,10 @@ export default class QueryRunner {
this._isSqlCmd = value;
}

get hasCompleted(): boolean {
return this._hasCompleted;
}

set hasCompleted(value: boolean) {
this._hasCompleted = value;
}
Expand Down Expand Up @@ -279,6 +284,9 @@ export default class QueryRunner {
// Set the result sets as an empty array so that as result sets complete we can add to the list
batch.resultSetSummaries = [];

// Set the batch messages to an empty array
this._batchSetMessages[batch.id] = [];

// Store the batch
this._batchSets[batch.id] = batch;
this.eventEmitter.emit('batchStart', batch);
Expand Down Expand Up @@ -312,6 +320,16 @@ export default class QueryRunner {
// send a time message in the format used for query complete
this.sendBatchTimeMessage(batchSet.id, Utils.parseNumAsTimeString(executionTime));
}

// replay the messages for the current batch
const messages = this._batchSetMessages[batchId];
if (messages !== undefined) {
for (let messageId = 0; messageId < messages.length; ++messageId) {
// Send the message to the results pane
this.eventEmitter.emit('message', messages[messageId]);
}
}

this.eventEmitter.emit('batchComplete', batchSet);
for (let resultSetId = 0; resultSetId < batchSet.resultSetSummaries.length; resultSetId++) {
let resultSet = batchSet.resultSetSummaries[resultSetId];
Expand Down Expand Up @@ -339,6 +357,11 @@ export default class QueryRunner {
let message = obj.message;
message.time = new Date(message.time).toLocaleTimeString();

// save the message into the batch summary so it can be restored on view refresh
if (message.batchId >= 0 && this._batchSetMessages[message.batchId] !== undefined) {
this._batchSetMessages[message.batchId].push(message);
}

// Send the message to the results pane
this.eventEmitter.emit('message', message);

Expand Down
11 changes: 10 additions & 1 deletion test/unit/queryRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ suite('Query Runner tests', () => {
queryRunner.eventEmitter = mockEventEmitter.object;
queryRunner.handleBatchStart(batchStart);

// Then: It should store the batch and emit a batch start
// Then: It should store the batch, messages and emit a batch start
assert.equal(queryRunner.batchSets.indexOf(batchStart.batchSummary), 0);
assert.ok(queryRunner.batchSetMessages[batchStart.batchSummary.id]);
mockEventEmitter.verify(x => x.emit('batchStart', TypeMoq.It.isAny()), TypeMoq.Times.once());
});

Expand Down Expand Up @@ -236,6 +237,8 @@ suite('Query Runner tests', () => {
selection: { startLine: 0, endLine: 0, startColumn: 3, endColumn: 3 },
resultSetSummaries: []
};
queryRunner.batchSetMessages[queryRunner.batchSets[0].id] = [];

let mockEventEmitter = TypeMoq.Mock.ofType(EventEmitter, TypeMoq.MockBehavior.Strict);
mockEventEmitter.setup(x => x.emit('batchComplete', TypeMoq.It.isAny()));
mockEventEmitter.setup(x => x.emit('message', TypeMoq.It.isAny(), TypeMoq.It.isAny()));
Expand All @@ -250,6 +253,9 @@ suite('Query Runner tests', () => {
assert.equal(typeof (storedBatch.executionStart), typeof (batchComplete.batchSummary.executionStart));
assert.equal(storedBatch.hasError, batchComplete.batchSummary.hasError);

// ... Messages should be empty since batch time messages are stored separately
assert.equal(queryRunner.batchSetMessages[queryRunner.batchSets[0].id].length, 0);

// ... Result sets should not be set by the batch complete notification
assert.equal(typeof (storedBatch.resultSetSummaries), typeof ([]));
assert.equal(storedBatch.resultSetSummaries.length, 0);
Expand Down Expand Up @@ -381,13 +387,16 @@ suite('Query Runner tests', () => {
testStatusView.object, testSqlToolsServerClient.object,
testQueryNotificationHandler.object, testVscodeWrapper.object
);
queryRunner.batchSetMessages[message.message.batchId] = [];
queryRunner.eventEmitter = mockEventEmitter.object;

// ... And I ask to handle a message
queryRunner.handleMessage(message);

// Then: A message event should have been emitted
mockEventEmitter.verify(x => x.emit('message', TypeMoq.It.isAny()), TypeMoq.Times.once());
// ... Result set message cache contains one entry
assert.equal(queryRunner.batchSetMessages[message.message.batchId].length, 1);
});

test('Notification - Query complete', () => {
Expand Down
Loading