mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-16 01:25:36 -05:00
Query Runner Tests (#10252)
* rework some code and write an inital test * fix strict * add more to standard test * add to existing workflow test * fix tests * simplify the code * add more tests * remove bad import * fix compile * fix timestampiong
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
import * as GridContentEvents from 'sql/workbench/services/query/common/gridContentEvents';
|
||||
import QueryRunner from 'sql/workbench/services/query/common/queryRunner';
|
||||
import { ResultSetSubset } from 'sql/workbench/services/query/common/query';
|
||||
import { DataService } from 'sql/workbench/services/query/common/dataService';
|
||||
import { IQueryModelService, IQueryEvent } from 'sql/workbench/services/query/common/queryModel';
|
||||
|
||||
@@ -17,6 +18,8 @@ import * as strings from 'vs/base/common/strings';
|
||||
import * as types from 'vs/base/common/types';
|
||||
import { INotificationService } from 'vs/platform/notification/common/notification';
|
||||
import Severity from 'vs/base/common/severity';
|
||||
import EditQueryRunner from 'sql/workbench/services/editData/common/editQueryRunner';
|
||||
import { IRange } from 'vs/editor/common/core/range';
|
||||
|
||||
const selectionSnippetMaxLen = 100;
|
||||
|
||||
@@ -29,10 +32,10 @@ export interface QueryEvent {
|
||||
* Holds information about the state of a query runner
|
||||
*/
|
||||
export class QueryInfo {
|
||||
public queryRunner?: QueryRunner;
|
||||
public queryRunner?: EditQueryRunner;
|
||||
public dataService?: DataService;
|
||||
public queryEventQueue?: QueryEvent[];
|
||||
public selection?: Array<azdata.ISelectionData>;
|
||||
public range?: Array<IRange>;
|
||||
public selectionSnippet?: string;
|
||||
|
||||
// Notes if the angular components have obtained the DataService. If not, all messages sent
|
||||
@@ -42,7 +45,7 @@ export class QueryInfo {
|
||||
constructor() {
|
||||
this.dataServiceReady = false;
|
||||
this.queryEventQueue = [];
|
||||
this.selection = [];
|
||||
this.range = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,10 +131,10 @@ export class QueryModelService implements IQueryModelService {
|
||||
/**
|
||||
* Get more data rows from the current resultSets from the service layer
|
||||
*/
|
||||
public getQueryRows(uri: string, rowStart: number, numberOfRows: number, batchId: number, resultId: number): Promise<azdata.ResultSetSubset | undefined> {
|
||||
public getQueryRows(uri: string, rowStart: number, numberOfRows: number, batchId: number, resultId: number): Promise<ResultSetSubset | undefined> {
|
||||
if (this._queryInfoMap.has(uri)) {
|
||||
return this._getQueryInfo(uri)!.queryRunner!.getQueryRows(rowStart, numberOfRows, batchId, resultId).then(results => {
|
||||
return results.resultSubset;
|
||||
return results;
|
||||
});
|
||||
} else {
|
||||
return Promise.resolve(undefined);
|
||||
@@ -170,15 +173,15 @@ export class QueryModelService implements IQueryModelService {
|
||||
/**
|
||||
* Run a query for the given URI with the given text selection
|
||||
*/
|
||||
public async runQuery(uri: string, selection: azdata.ISelectionData, runOptions?: azdata.ExecutionPlanOptions): Promise<void> {
|
||||
return this.doRunQuery(uri, selection, false, runOptions);
|
||||
public async runQuery(uri: string, range: IRange, runOptions?: azdata.ExecutionPlanOptions): Promise<void> {
|
||||
return this.doRunQuery(uri, range, false, runOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the current SQL statement for the given URI
|
||||
*/
|
||||
public async runQueryStatement(uri: string, selection: azdata.ISelectionData): Promise<void> {
|
||||
return this.doRunQuery(uri, selection, true);
|
||||
public async runQueryStatement(uri: string, range: IRange): Promise<void> {
|
||||
return this.doRunQuery(uri, range, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,7 +194,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
/**
|
||||
* Run Query implementation
|
||||
*/
|
||||
private async doRunQuery(uri: string, selection: azdata.ISelectionData | string,
|
||||
private async doRunQuery(uri: string, range: IRange | string,
|
||||
runCurrentStatement: boolean, runOptions?: azdata.ExecutionPlanOptions): Promise<void> {
|
||||
// Reuse existing query runner if it exists
|
||||
let queryRunner: QueryRunner | undefined;
|
||||
@@ -208,7 +211,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
|
||||
// If the query is not in progress, we can reuse the query runner
|
||||
queryRunner = existingRunner!;
|
||||
info.selection = [];
|
||||
info.range = [];
|
||||
info.selectionSnippet = undefined;
|
||||
} else {
|
||||
// We do not have a query runner for this editor, so create a new one
|
||||
@@ -217,23 +220,23 @@ export class QueryModelService implements IQueryModelService {
|
||||
queryRunner = info.queryRunner!;
|
||||
}
|
||||
|
||||
if (types.isString(selection)) {
|
||||
if (types.isString(range)) {
|
||||
// Run the query string in this case
|
||||
if (selection.length < selectionSnippetMaxLen) {
|
||||
info.selectionSnippet = selection;
|
||||
if (range.length < selectionSnippetMaxLen) {
|
||||
info.selectionSnippet = range;
|
||||
} else {
|
||||
info.selectionSnippet = selection.substring(0, selectionSnippetMaxLen - 3) + '...';
|
||||
info.selectionSnippet = range.substring(0, selectionSnippetMaxLen - 3) + '...';
|
||||
}
|
||||
return queryRunner.runQuery(selection, runOptions);
|
||||
return queryRunner.runQuery(range, runOptions);
|
||||
} else if (runCurrentStatement) {
|
||||
return queryRunner.runQueryStatement(selection);
|
||||
return queryRunner.runQueryStatement(range);
|
||||
} else {
|
||||
return queryRunner.runQuery(selection, runOptions);
|
||||
return queryRunner.runQuery(range, runOptions);
|
||||
}
|
||||
}
|
||||
|
||||
private initQueryRunner(uri: string): QueryInfo {
|
||||
let queryRunner = this._instantiationService.createInstance(QueryRunner, uri);
|
||||
let queryRunner = this._instantiationService.createInstance(EditQueryRunner, uri);
|
||||
let info = new QueryInfo();
|
||||
queryRunner.onResultSet(e => {
|
||||
this._fireQueryEvent(uri, 'resultSet', e);
|
||||
@@ -241,14 +244,14 @@ export class QueryModelService implements IQueryModelService {
|
||||
queryRunner.onBatchStart(b => {
|
||||
let link = undefined;
|
||||
let messageText = nls.localize('runQueryBatchStartMessage', "Started executing query at ");
|
||||
if (b.selection) {
|
||||
if (b.range) {
|
||||
if (info.selectionSnippet) {
|
||||
// This indicates it's a query string. Do not include line information since it'll be inaccurate, but show some of the
|
||||
// executed query text
|
||||
messageText = nls.localize('runQueryStringBatchStartMessage', "Started executing query \"{0}\"", info.selectionSnippet);
|
||||
} else {
|
||||
link = {
|
||||
text: strings.format(nls.localize('runQueryBatchStartLine', "Line {0}"), b.selection.startLine + 1)
|
||||
text: strings.format(nls.localize('runQueryBatchStartLine', "Line {0}"), b.range.startLineNumber)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -260,7 +263,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
link: link
|
||||
};
|
||||
this._fireQueryEvent(uri, 'message', message);
|
||||
info.selection!.push(this._validateSelection(b.selection));
|
||||
info.range!.push(b.range);
|
||||
});
|
||||
queryRunner.onMessage(m => {
|
||||
this._fireQueryEvent(uri, 'message', m);
|
||||
@@ -274,7 +277,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
uri: uri,
|
||||
queryInfo:
|
||||
{
|
||||
selection: info.selection!,
|
||||
range: info.range!,
|
||||
messages: info.queryRunner!.messages
|
||||
}
|
||||
};
|
||||
@@ -292,7 +295,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
uri: uri,
|
||||
queryInfo:
|
||||
{
|
||||
selection: info.selection!,
|
||||
range: info.range!,
|
||||
messages: info.queryRunner!.messages
|
||||
}
|
||||
};
|
||||
@@ -308,7 +311,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
uri: uri,
|
||||
queryInfo:
|
||||
{
|
||||
selection: info.selection!,
|
||||
range: info.range!,
|
||||
messages: info.queryRunner!.messages
|
||||
}
|
||||
};
|
||||
@@ -324,7 +327,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
uri: planInfo.fileUri,
|
||||
queryInfo:
|
||||
{
|
||||
selection: info.selection!,
|
||||
range: info.range!,
|
||||
messages: info.queryRunner!.messages
|
||||
},
|
||||
params: planInfo
|
||||
@@ -338,7 +341,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
uri: uri,
|
||||
queryInfo:
|
||||
{
|
||||
selection: info.selection!,
|
||||
range: info.range!,
|
||||
messages: info.queryRunner!.messages
|
||||
},
|
||||
params: resultSetInfo
|
||||
@@ -399,12 +402,12 @@ export class QueryModelService implements IQueryModelService {
|
||||
// EDIT DATA METHODS /////////////////////////////////////////////////////
|
||||
async initializeEdit(ownerUri: string, schemaName: string, objectName: string, objectType: string, rowLimit: number, queryString: string): Promise<void> {
|
||||
// Reuse existing query runner if it exists
|
||||
let queryRunner: QueryRunner;
|
||||
let queryRunner: EditQueryRunner;
|
||||
let info: QueryInfo;
|
||||
|
||||
if (this._queryInfoMap.has(ownerUri)) {
|
||||
info = this._getQueryInfo(ownerUri)!;
|
||||
let existingRunner: QueryRunner = info.queryRunner!;
|
||||
let existingRunner = info.queryRunner!;
|
||||
|
||||
// If the initialization is already in progress
|
||||
if (existingRunner.isExecuting) {
|
||||
@@ -417,7 +420,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
|
||||
// We do not have a query runner for this editor, so create a new one
|
||||
// and map it to the results uri
|
||||
queryRunner = this._instantiationService.createInstance(QueryRunner, ownerUri);
|
||||
queryRunner = this._instantiationService.createInstance(EditQueryRunner, ownerUri);
|
||||
const resultSetEventType = 'resultSet';
|
||||
queryRunner.onResultSet(resultSet => {
|
||||
this._fireQueryEvent(ownerUri, resultSetEventType, resultSet);
|
||||
@@ -428,14 +431,14 @@ export class QueryModelService implements IQueryModelService {
|
||||
queryRunner.onBatchStart(batch => {
|
||||
let link = undefined;
|
||||
let messageText = nls.localize('runQueryBatchStartMessage', "Started executing query at ");
|
||||
if (batch.selection) {
|
||||
if (batch.range) {
|
||||
if (info.selectionSnippet) {
|
||||
// This indicates it's a query string. Do not include line information since it'll be inaccurate, but show some of the
|
||||
// executed query text
|
||||
messageText = nls.localize('runQueryStringBatchStartMessage', "Started executing query \"{0}\"", info.selectionSnippet);
|
||||
} else {
|
||||
link = {
|
||||
text: strings.format(nls.localize('runQueryBatchStartLine', "Line {0}"), batch.selection.startLine + 1)
|
||||
text: strings.format(nls.localize('runQueryBatchStartLine', "Line {0}"), batch.range.startLineNumber)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -459,7 +462,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
uri: ownerUri,
|
||||
queryInfo:
|
||||
{
|
||||
selection: info.selection!,
|
||||
range: info.range!,
|
||||
messages: info.queryRunner!.messages
|
||||
},
|
||||
};
|
||||
@@ -476,7 +479,7 @@ export class QueryModelService implements IQueryModelService {
|
||||
uri: ownerUri,
|
||||
queryInfo:
|
||||
{
|
||||
selection: info.selection!,
|
||||
range: info.range!,
|
||||
messages: info.queryRunner!.messages
|
||||
},
|
||||
};
|
||||
@@ -596,8 +599,8 @@ export class QueryModelService implements IQueryModelService {
|
||||
|
||||
// PRIVATE METHODS //////////////////////////////////////////////////////
|
||||
|
||||
private internalGetQueryRunner(ownerUri: string): QueryRunner | undefined {
|
||||
let queryRunner: QueryRunner | undefined;
|
||||
private internalGetQueryRunner(ownerUri: string): EditQueryRunner | undefined {
|
||||
let queryRunner: EditQueryRunner | undefined;
|
||||
if (this._queryInfoMap.has(ownerUri)) {
|
||||
let existingRunner = this._getQueryInfo(ownerUri)!.queryRunner!;
|
||||
// If the query is not already executing then set it up
|
||||
@@ -648,17 +651,4 @@ export class QueryModelService implements IQueryModelService {
|
||||
public _getQueryInfo(uri: string): QueryInfo | undefined {
|
||||
return this._queryInfoMap.get(uri);
|
||||
}
|
||||
|
||||
// TODO remove this funciton and its usages when #821 in vscode-mssql is fixed and
|
||||
// the SqlToolsService version is updated in this repo - coquagli 4/19/2017
|
||||
private _validateSelection(selection: azdata.ISelectionData): azdata.ISelectionData {
|
||||
if (!selection) {
|
||||
selection = <azdata.ISelectionData>{};
|
||||
}
|
||||
selection.endColumn = selection ? Math.max(0, selection.endColumn) : 0;
|
||||
selection.endLine = selection ? Math.max(0, selection.endLine) : 0;
|
||||
selection.startColumn = selection ? Math.max(0, selection.startColumn) : 0;
|
||||
selection.startLine = selection ? Math.max(0, selection.startLine) : 0;
|
||||
return selection;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user