-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathquery-resolver.ts
More file actions
110 lines (102 loc) · 3.42 KB
/
query-resolver.ts
File metadata and controls
110 lines (102 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { getOnDiskWorkspaceFolders } from "../../common/vscode/workspace-folders";
import type { QlPacksForLanguage } from "../../databases/qlpack";
import type { KeyType } from "./key-type";
import { kindOfKeyType, nameOfKeyType, tagOfKeyType } from "./key-type";
import type { CodeQLCliServer } from "../../codeql-cli/cli";
import type { DatabaseItem } from "../../databases/local-databases";
import {
qlpackOfDatabase,
resolveQueriesByLanguagePack as resolveLocalQueriesByLanguagePack,
} from "../../local-queries/query-resolver";
import { extLogger } from "../../common/logging/vscode";
import { TeeLogger } from "../../common/logging";
import type { CancellationToken } from "vscode";
import type { ProgressCallback } from "../../common/vscode/progress";
import type { CoreCompletedQuery, QueryRunner } from "../../query-server";
import { createLockFileForStandardQuery } from "../../local-queries/standard-queries";
import { basename } from "path";
/**
* This wil try to determine the qlpacks for a given database. If it can't find a matching
* dbscheme with downloaded packs, it will download the default packs instead.
*
* @param cli The CLI server to use
* @param databaseItem The database item to find the qlpacks for
*/
export async function resolveContextualQlPacksForDatabase(
cli: CodeQLCliServer,
databaseItem: DatabaseItem,
): Promise<QlPacksForLanguage> {
try {
return await qlpackOfDatabase(cli, databaseItem);
} catch {
// If we can't find the qlpacks for the database, use the defaults instead
}
const dbInfo = await cli.resolveDatabase(databaseItem.databaseUri.fsPath);
const primaryLanguage = dbInfo.languages?.[0];
if (!primaryLanguage) {
throw new Error("Unable to determine primary language of database");
}
const libraryPack = `codeql/${primaryLanguage}-all`;
const queryPack = `codeql/${primaryLanguage}-queries`;
await cli.packDownload([libraryPack, queryPack]);
// Return the default packs. If these weren't valid packs, the download would have failed.
return {
dbschemePack: libraryPack,
dbschemePackIsLibraryPack: true,
queryPack,
};
}
export async function resolveContextualQueries(
cli: CodeQLCliServer,
qlpacks: QlPacksForLanguage,
keyType: KeyType,
): Promise<string[]> {
return resolveLocalQueriesByLanguagePack(
cli,
qlpacks,
nameOfKeyType(keyType),
{
kind: kindOfKeyType(keyType),
"tags contain": [tagOfKeyType(keyType)],
},
);
}
export async function runContextualQuery(
query: string,
db: DatabaseItem,
queryStorageDir: string,
qs: QueryRunner,
cli: CodeQLCliServer,
progress: ProgressCallback,
token: CancellationToken,
templates: Record<string, string>,
): Promise<CoreCompletedQuery> {
const { cleanup } = await createLockFileForStandardQuery(cli, query);
const queryRun = qs.createQueryRun(
db.databaseUri.fsPath,
[
{
queryPath: query,
outputBaseName: "results",
quickEvalPosition: undefined,
},
],
false,
getOnDiskWorkspaceFolders(),
undefined,
{},
queryStorageDir,
basename(query),
templates,
);
void extLogger.log(
`Running contextual query ${query}; results will be stored in ${queryRun.outputDir.querySaveDir}`,
);
const teeLogger = new TeeLogger(qs.logger, queryRun.outputDir.logPath);
try {
return await queryRun.evaluate(progress, token, teeLogger);
} finally {
await cleanup?.();
teeLogger.dispose();
}
}