Skip to content

Commit b0dab96

Browse files
authored
Merge pull request #1498 from alexet/alexet/prepare-new-qs
QueryServer: Abstract over the query running parts.
2 parents e4a3161 + fd43bed commit b0dab96

30 files changed

Lines changed: 1727 additions & 1464 deletions

extensions/ql-vscode/src/cli.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { DistributionProvider, FindDistributionResultKind } from './distribution
1616
import { assertNever, getErrorMessage, getErrorStack } from './pure/helpers-pure';
1717
import { QueryMetadata, SortDirection } from './pure/interface-types';
1818
import { Logger, ProgressReporter } from './logging';
19-
import { CompilationMessage } from './pure/messages';
19+
import { CompilationMessage } from './pure/legacy-messages';
2020
import { sarifParser } from './sarif-parser';
2121
import { dbSchemeToLanguage, walkDirectory } from './helpers';
2222

@@ -1248,6 +1248,9 @@ export class CliVersionConstraint {
12481248
*/
12491249
public static CLI_VERSION_WITH_LANGUAGE = new SemVer('2.4.1');
12501250

1251+
1252+
public static CLI_VERSION_WITH_NONDESTURCTIVE_UPGRADES = new SemVer('2.4.2');
1253+
12511254
/**
12521255
* CLI version where `codeql resolve upgrades` supports
12531256
* the `--allow-downgrades` flag
@@ -1264,14 +1267,6 @@ export class CliVersionConstraint {
12641267
*/
12651268
public static CLI_VERSION_WITH_DB_REGISTRATION = new SemVer('2.4.1');
12661269

1267-
/**
1268-
* CLI version where non destructive upgrades were introduced.
1269-
*
1270-
* This was landed in multiple parts so this is the version where all necessary feature were supported.
1271-
*/
1272-
public static CLI_VERSION_WITH_NON_DESTRUCTIVE_UPGRADES = new SemVer('2.4.2');
1273-
1274-
12751270
/**
12761271
* CLI version where the `--allow-library-packs` option to `codeql resolve queries` was
12771272
* introduced.
@@ -1351,6 +1346,10 @@ export class CliVersionConstraint {
13511346
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_WITH_LANGUAGE);
13521347
}
13531348

1349+
public async supportsNonDestructiveUpgrades() {
1350+
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_WITH_NONDESTURCTIVE_UPGRADES);
1351+
}
1352+
13541353
public async supportsDowngrades() {
13551354
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_WITH_DOWNGRADES);
13561355
}
@@ -1367,10 +1366,6 @@ export class CliVersionConstraint {
13671366
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_WITH_DB_REGISTRATION);
13681367
}
13691368

1370-
async supportsNonDestructiveUpgrades(): Promise<boolean> {
1371-
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_WITH_NON_DESTRUCTIVE_UPGRADES);
1372-
}
1373-
13741369
async supportsDatabaseUnbundle() {
13751370
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_WITH_DATABASE_UNBUNDLE);
13761371
}

extensions/ql-vscode/src/contextual/astBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { QueryWithResults } from '../run-queries';
21
import { CodeQLCliServer } from '../cli';
32
import { DecodedBqrsChunk, BqrsId, EntityValue } from '../pure/bqrs-cli-types';
43
import { DatabaseItem } from '../databases';
54
import { ChildAstItem, AstItem } from '../astViewer';
65
import fileRangeFromURI from './fileRangeFromURI';
76
import { Uri } from 'vscode';
7+
import { QueryWithResults } from '../run-queries-shared';
88

99
/**
1010
* A class that wraps a tree of QL results from a query that

extensions/ql-vscode/src/contextual/locationFinder.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import { ColumnKindCode, EntityValue, getResultSetSchema, ResultSetSchema } from
33
import { CodeQLCliServer } from '../cli';
44
import { DatabaseManager, DatabaseItem } from '../databases';
55
import fileRangeFromURI from './fileRangeFromURI';
6-
import * as messages from '../pure/messages';
7-
import { QueryServerClient } from '../queryserver-client';
8-
import { QueryWithResults, compileAndRunQueryAgainstDatabase, createInitialQueryInfo } from '../run-queries';
96
import { ProgressCallback } from '../commandRunner';
107
import { KeyType } from './keyType';
118
import { qlpackOfDatabase, resolveQueries } from './queryResolver';
129
import { CancellationToken, LocationLink, Uri } from 'vscode';
10+
import { createInitialQueryInfo, QueryWithResults } from '../run-queries-shared';
11+
import { QueryRunner } from '../queryRunner';
1312

1413
export const SELECT_QUERY_NAME = '#select';
1514
export const TEMPLATE_NAME = 'selectedSourceFile';
@@ -35,7 +34,7 @@ export interface FullLocationLink extends LocationLink {
3534
*/
3635
export async function getLocationsForUriString(
3736
cli: CodeQLCliServer,
38-
qs: QueryServerClient,
37+
qs: QueryRunner,
3938
dbm: DatabaseManager,
4039
uriString: string,
4140
keyType: KeyType,
@@ -65,19 +64,8 @@ export async function getLocationsForUriString(
6564
},
6665
false
6766
);
68-
69-
const results = await compileAndRunQueryAgainstDatabase(
70-
cli,
71-
qs,
72-
db,
73-
initialInfo,
74-
queryStorageDir,
75-
progress,
76-
token,
77-
templates
78-
);
79-
80-
if (results.result.resultType == messages.QueryResultType.SUCCESS) {
67+
const results = await qs.compileAndRunQueryAgainstDatabase(db, initialInfo, queryStorageDir, progress, token, templates);
68+
if (results.sucessful) {
8169
links.push(...await getLinksFromResults(results, cli, db, filter));
8270
}
8371
}
@@ -114,15 +102,9 @@ async function getLinksFromResults(
114102
return localLinks;
115103
}
116104

117-
function createTemplates(path: string): messages.TemplateDefinitions {
105+
function createTemplates(path: string): Record<string, string> {
118106
return {
119-
[TEMPLATE_NAME]: {
120-
values: {
121-
tuples: [[{
122-
stringValue: path
123-
}]]
124-
}
125-
}
107+
[TEMPLATE_NAME]: path
126108
};
127109
}
128110

extensions/ql-vscode/src/contextual/templateProvider.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ import { CodeQLCliServer } from '../cli';
1616
import { DatabaseManager } from '../databases';
1717
import { CachedOperation } from '../helpers';
1818
import { ProgressCallback, withProgress } from '../commandRunner';
19-
import * as messages from '../pure/messages';
20-
import { QueryServerClient } from '../queryserver-client';
21-
import { compileAndRunQueryAgainstDatabase, createInitialQueryInfo, QueryWithResults } from '../run-queries';
2219
import AstBuilder from './astBuilder';
2320
import {
2421
KeyType,
2522
} from './keyType';
2623
import { FullLocationLink, getLocationsForUriString, TEMPLATE_NAME } from './locationFinder';
2724
import { qlpackOfDatabase, resolveQueries } from './queryResolver';
2825
import { isCanary, NO_CACHE_AST_VIEWER } from '../config';
26+
import { createInitialQueryInfo, QueryWithResults } from '../run-queries-shared';
27+
import { QueryRunner } from '../queryRunner';
2928

3029
/**
3130
* Run templated CodeQL queries to find definitions and references in
@@ -39,7 +38,7 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
3938

4039
constructor(
4140
private cli: CodeQLCliServer,
42-
private qs: QueryServerClient,
41+
private qs: QueryRunner,
4342
private dbm: DatabaseManager,
4443
private queryStorageDir: string,
4544
) {
@@ -83,7 +82,7 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
8382

8483
constructor(
8584
private cli: CodeQLCliServer,
86-
private qs: QueryServerClient,
85+
private qs: QueryRunner,
8786
private dbm: DatabaseManager,
8887
private queryStorageDir: string,
8988
) {
@@ -137,7 +136,7 @@ export class TemplatePrintAstProvider {
137136

138137
constructor(
139138
private cli: CodeQLCliServer,
140-
private qs: QueryServerClient,
139+
private qs: QueryRunner,
141140
private dbm: DatabaseManager,
142141
private queryStorageDir: string,
143142
) {
@@ -195,14 +194,9 @@ export class TemplatePrintAstProvider {
195194
}
196195

197196
const query = queries[0];
198-
const templates: messages.TemplateDefinitions = {
199-
[TEMPLATE_NAME]: {
200-
values: {
201-
tuples: [[{
202-
stringValue: zippedArchive.pathWithinSourceArchive
203-
}]]
204-
}
205-
}
197+
const templates: Record<string, string> = {
198+
[TEMPLATE_NAME]:
199+
zippedArchive.pathWithinSourceArchive
206200
};
207201

208202
const initialInfo = await createInitialQueryInfo(
@@ -215,9 +209,7 @@ export class TemplatePrintAstProvider {
215209
);
216210

217211
return {
218-
query: await compileAndRunQueryAgainstDatabase(
219-
this.cli,
220-
this.qs,
212+
query: await this.qs.compileAndRunQueryAgainstDatabase(
221213
db,
222214
initialInfo,
223215
this.queryStorageDir,
@@ -231,23 +223,23 @@ export class TemplatePrintAstProvider {
231223
}
232224

233225
export class TemplatePrintCfgProvider {
234-
private cache: CachedOperation<[Uri, messages.TemplateDefinitions] | undefined>;
226+
private cache: CachedOperation<[Uri, Record<string, string>] | undefined>;
235227

236228
constructor(
237229
private cli: CodeQLCliServer,
238230
private dbm: DatabaseManager,
239231
) {
240-
this.cache = new CachedOperation<[Uri, messages.TemplateDefinitions] | undefined>(this.getCfgUri.bind(this));
232+
this.cache = new CachedOperation<[Uri, Record<string, string>] | undefined>(this.getCfgUri.bind(this));
241233
}
242234

243-
async provideCfgUri(document?: TextDocument): Promise<[Uri, messages.TemplateDefinitions] | undefined> {
235+
async provideCfgUri(document?: TextDocument): Promise<[Uri, Record<string, string>] | undefined> {
244236
if (!document) {
245237
return;
246238
}
247239
return await this.cache.get(document.uri.toString());
248240
}
249241

250-
private async getCfgUri(uriString: string): Promise<[Uri, messages.TemplateDefinitions]> {
242+
private async getCfgUri(uriString: string): Promise<[Uri, Record<string, string>]> {
251243
const uri = Uri.parse(uriString, true);
252244
if (uri.scheme !== zipArchiveScheme) {
253245
throw new Error('CFG Viewing is only available for databases with zipped source archives.');
@@ -275,14 +267,8 @@ export class TemplatePrintCfgProvider {
275267

276268
const queryUri = Uri.file(queries[0]);
277269

278-
const templates: messages.TemplateDefinitions = {
279-
[TEMPLATE_NAME]: {
280-
values: {
281-
tuples: [[{
282-
stringValue: zippedArchive.pathWithinSourceArchive
283-
}]]
284-
}
285-
}
270+
const templates: Record<string, string> = {
271+
[TEMPLATE_NAME]: zippedArchive.pathWithinSourceArchive
286272
};
287273

288274
return [queryUri, templates];

extensions/ql-vscode/src/databases-ui.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ import {
2828
showAndLogErrorMessage
2929
} from './helpers';
3030
import { logger } from './logging';
31-
import { clearCacheInDatabase } from './run-queries';
32-
import * as qsClient from './queryserver-client';
33-
import { upgradeDatabaseExplicit } from './upgrades';
3431
import {
3532
importArchiveDatabase,
3633
promptImportGithubDatabase,
@@ -40,6 +37,7 @@ import {
4037
import { CancellationToken } from 'vscode';
4138
import { asyncFilter, getErrorMessage } from './pure/helpers-pure';
4239
import { Credentials } from './authentication';
40+
import { QueryRunner } from './queryRunner';
4341
import { isCanary } from './config';
4442

4543
type ThemableIconPath = { light: string; dark: string } | string;
@@ -220,7 +218,7 @@ export class DatabaseUI extends DisposableObject {
220218

221219
public constructor(
222220
private databaseManager: DatabaseManager,
223-
private readonly queryServer: qsClient.QueryServerClient | undefined,
221+
private readonly queryServer: QueryRunner | undefined,
224222
private readonly storagePath: string,
225223
readonly extensionPath: string,
226224
private readonly getCredentials: () => Promise<Credentials>
@@ -390,12 +388,11 @@ export class DatabaseUI extends DisposableObject {
390388
handleChooseDatabaseFolder = async (
391389
progress: ProgressCallback,
392390
token: CancellationToken
393-
): Promise<DatabaseItem | undefined> => {
391+
): Promise<void> => {
394392
try {
395-
return await this.chooseAndSetDatabase(true, progress, token);
393+
await this.chooseAndSetDatabase(true, progress, token);
396394
} catch (e) {
397395
void showAndLogErrorMessage(getErrorMessage(e));
398-
return undefined;
399396
}
400397
};
401398

@@ -458,12 +455,11 @@ export class DatabaseUI extends DisposableObject {
458455
handleChooseDatabaseArchive = async (
459456
progress: ProgressCallback,
460457
token: CancellationToken
461-
): Promise<DatabaseItem | undefined> => {
458+
): Promise<void> => {
462459
try {
463-
return await this.chooseAndSetDatabase(false, progress, token);
460+
await this.chooseAndSetDatabase(false, progress, token);
464461
} catch (e) {
465462
void showAndLogErrorMessage(getErrorMessage(e));
466-
return undefined;
467463
}
468464
};
469465

@@ -576,8 +572,7 @@ export class DatabaseUI extends DisposableObject {
576572

577573
// Search for upgrade scripts in any workspace folders available
578574

579-
await upgradeDatabaseExplicit(
580-
this.queryServer,
575+
await this.queryServer.upgradeDatabaseExplicit(
581576
databaseItem,
582577
progress,
583578
token
@@ -592,8 +587,7 @@ export class DatabaseUI extends DisposableObject {
592587
this.queryServer !== undefined &&
593588
this.databaseManager.currentDatabaseItem !== undefined
594589
) {
595-
await clearCacheInDatabase(
596-
this.queryServer,
590+
await this.queryServer.clearCacheInDatabase(
597591
this.databaseManager.currentDatabaseItem,
598592
progress,
599593
token

extensions/ql-vscode/src/databases.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import {
1717
import { zipArchiveScheme, encodeArchiveBasePath, decodeSourceArchiveUri, encodeSourceArchiveUri } from './archive-filesystem-provider';
1818
import { DisposableObject } from './pure/disposable-object';
1919
import { Logger, logger } from './logging';
20-
import { registerDatabases, Dataset, deregisterDatabases } from './pure/messages';
21-
import { QueryServerClient } from './queryserver-client';
2220
import { getErrorMessage } from './pure/helpers-pure';
21+
import { QueryRunner } from './queryRunner';
2322

2423
/**
2524
* databases.ts
@@ -555,13 +554,13 @@ export class DatabaseManager extends DisposableObject {
555554

556555
constructor(
557556
private readonly ctx: ExtensionContext,
558-
private readonly qs: QueryServerClient,
557+
private readonly qs: QueryRunner,
559558
private readonly cli: cli.CodeQLCliServer,
560559
public logger: Logger
561560
) {
562561
super();
563562

564-
qs.onDidStartQueryServer(this.reregisterDatabases.bind(this));
563+
qs.onStart(this.reregisterDatabases.bind(this));
565564

566565
// Let this run async.
567566
void this.loadPersistedState();
@@ -860,27 +859,14 @@ export class DatabaseManager extends DisposableObject {
860859
token: vscode.CancellationToken,
861860
dbItem: DatabaseItem,
862861
) {
863-
if (dbItem.contents && (await this.cli.cliConstraints.supportsDatabaseRegistration())) {
864-
const databases: Dataset[] = [{
865-
dbDir: dbItem.contents.datasetUri.fsPath,
866-
workingSet: 'default'
867-
}];
868-
await this.qs.sendRequest(deregisterDatabases, { databases }, token, progress);
869-
}
862+
await this.qs.deregisterDatabase(progress, token, dbItem);
870863
}
871-
872864
private async registerDatabase(
873865
progress: ProgressCallback,
874866
token: vscode.CancellationToken,
875867
dbItem: DatabaseItem,
876868
) {
877-
if (dbItem.contents && (await this.cli.cliConstraints.supportsDatabaseRegistration())) {
878-
const databases: Dataset[] = [{
879-
dbDir: dbItem.contents.datasetUri.fsPath,
880-
workingSet: 'default'
881-
}];
882-
await this.qs.sendRequest(registerDatabases, { databases }, token, progress);
883-
}
869+
await this.qs.registerDatabase(progress, token, dbItem);
884870
}
885871

886872
private updatePersistedCurrentDatabaseItem(): void {

0 commit comments

Comments
 (0)