1- import { CancellationToken } from "vscode" ;
1+ import vscode , { CancellationToken } from "vscode" ;
22import { CodeQLCliServer } from "../codeql-cli/cli" ;
3- import { ProgressCallback } from "../common/vscode/progress" ;
3+ import {
4+ ProgressCallback ,
5+ UserCancellationException ,
6+ } from "../common/vscode/progress" ;
47import { DatabaseItem } from "../databases/local-databases" ;
58import { QueryOutputDir } from "../run-queries-shared" ;
6- import { Position , QueryResultType } from "./new-messages" ;
9+ import {
10+ clearCache ,
11+ ClearCacheParams ,
12+ clearPackCache ,
13+ deregisterDatabases ,
14+ Position ,
15+ QueryResultType ,
16+ registerDatabases ,
17+ trimCache ,
18+ TrimCacheParams ,
19+ upgradeDatabase ,
20+ } from "./new-messages" ;
721import { BaseLogger , Logger } from "../common/logging" ;
822import { basename , join } from "path" ;
923import { nanoid } from "nanoid" ;
24+ import { QueryServerClient } from "./query-server-client" ;
25+ import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders" ;
26+ import { compileAndRunQueryAgainstDatabaseCore } from "./run-queries" ;
1027
1128export interface CoreQueryTarget {
1229 /** The full path to the query. */
@@ -45,37 +62,69 @@ export interface CoreQueryRun {
4562export type CoreCompletedQuery = CoreQueryResults &
4663 Omit < CoreQueryRun , "evaluate" > ;
4764
48- export abstract class QueryRunner {
49- abstract restartQueryServer (
65+ export class QueryRunner {
66+ constructor ( public readonly qs : QueryServerClient ) { }
67+
68+ get cliServer ( ) : CodeQLCliServer {
69+ return this . qs . cliServer ;
70+ }
71+
72+ get customLogDirectory ( ) : string | undefined {
73+ return this . qs . config . customLogDirectory ;
74+ }
75+
76+ get logger ( ) : Logger {
77+ return this . qs . logger ;
78+ }
79+
80+ async restartQueryServer (
5081 progress : ProgressCallback ,
5182 token : CancellationToken ,
52- ) : Promise < void > ;
53-
54- abstract cliServer : CodeQLCliServer ;
55- abstract customLogDirectory : string | undefined ;
56- abstract logger : Logger ;
83+ ) : Promise < void > {
84+ await this . qs . restartQueryServer ( progress , token ) ;
85+ }
5786
58- abstract onStart (
59- arg0 : (
87+ onStart (
88+ callBack : (
6089 progress : ProgressCallback ,
6190 token : CancellationToken ,
6291 ) => Promise < void > ,
63- ) : void ;
92+ ) {
93+ this . qs . onDidStartQueryServer ( callBack ) ;
94+ }
6495
65- abstract clearCacheInDatabase (
96+ async clearCacheInDatabase (
6697 dbItem : DatabaseItem ,
6798 token : CancellationToken ,
68- ) : Promise < void > ;
99+ ) : Promise < void > {
100+ if ( dbItem . contents === undefined ) {
101+ throw new Error ( "Can't clear the cache in an invalid database." ) ;
102+ }
69103
70- abstract trimCacheInDatabase (
104+ const db = dbItem . databaseUri . fsPath ;
105+ const params : ClearCacheParams = {
106+ dryRun : false ,
107+ db,
108+ } ;
109+ await this . qs . sendRequest ( clearCache , params , token ) ;
110+ }
111+
112+ async trimCacheInDatabase (
71113 dbItem : DatabaseItem ,
72114 token : CancellationToken ,
73- ) : Promise < void > ;
115+ ) : Promise < void > {
116+ if ( dbItem . contents === undefined ) {
117+ throw new Error ( "Can't trim the cache in an invalid database." ) ;
118+ }
74119
75- /**
76- * Overridden in subclasses to evaluate the query via the query server and return the results.
77- */
78- public abstract compileAndRunQueryAgainstDatabaseCore (
120+ const db = dbItem . databaseUri . fsPath ;
121+ const params : TrimCacheParams = {
122+ db,
123+ } ;
124+ await this . qs . sendRequest ( trimCache , params , token ) ;
125+ }
126+
127+ public async compileAndRunQueryAgainstDatabaseCore (
79128 dbPath : string ,
80129 query : CoreQueryTarget ,
81130 additionalPacks : string [ ] ,
@@ -87,19 +136,72 @@ export abstract class QueryRunner {
87136 token : CancellationToken ,
88137 templates : Record < string , string > | undefined ,
89138 logger : BaseLogger ,
90- ) : Promise < CoreQueryResults > ;
139+ ) : Promise < CoreQueryResults > {
140+ return await compileAndRunQueryAgainstDatabaseCore (
141+ this . qs ,
142+ dbPath ,
143+ query ,
144+ generateEvalLog ,
145+ additionalPacks ,
146+ extensionPacks ,
147+ additionalRunQueryArgs ,
148+ outputDir ,
149+ progress ,
150+ token ,
151+ templates ,
152+ logger ,
153+ ) ;
154+ }
91155
92- abstract deregisterDatabase ( dbItem : DatabaseItem ) : Promise < void > ;
156+ async deregisterDatabase ( dbItem : DatabaseItem ) : Promise < void > {
157+ if ( dbItem . contents ) {
158+ const databases : string [ ] = [ dbItem . databaseUri . fsPath ] ;
159+ await this . qs . sendRequest ( deregisterDatabases , { databases } ) ;
160+ }
161+ }
162+ async registerDatabase ( dbItem : DatabaseItem ) : Promise < void > {
163+ if ( dbItem . contents ) {
164+ const databases : string [ ] = [ dbItem . databaseUri . fsPath ] ;
165+ await this . qs . sendRequest ( registerDatabases , { databases } ) ;
166+ }
167+ }
93168
94- abstract registerDatabase ( dbItem : DatabaseItem ) : Promise < void > ;
169+ async clearPackCache ( ) : Promise < void > {
170+ await this . qs . sendRequest ( clearPackCache , { } ) ;
171+ }
95172
96- abstract upgradeDatabaseExplicit (
173+ async upgradeDatabaseExplicit (
97174 dbItem : DatabaseItem ,
98175 progress : ProgressCallback ,
99176 token : CancellationToken ,
100- ) : Promise < void > ;
177+ ) : Promise < void > {
178+ const yesItem = { title : "Yes" , isCloseAffordance : false } ;
179+ const noItem = { title : "No" , isCloseAffordance : true } ;
180+ const dialogOptions : vscode . MessageItem [ ] = [ yesItem , noItem ] ;
101181
102- abstract clearPackCache ( ) : Promise < void > ;
182+ const message = `Should the database ${ dbItem . databaseUri . fsPath } be destructively upgraded?\n\nThis should not be necessary to run queries
183+ as we will non-destructively update it anyway.` ;
184+ const chosenItem = await vscode . window . showInformationMessage (
185+ message ,
186+ { modal : true } ,
187+ ...dialogOptions ,
188+ ) ;
189+
190+ if ( chosenItem !== yesItem ) {
191+ throw new UserCancellationException (
192+ "User cancelled the database upgrade." ,
193+ ) ;
194+ }
195+ await this . qs . sendRequest (
196+ upgradeDatabase ,
197+ {
198+ db : dbItem . databaseUri . fsPath ,
199+ additionalPacks : getOnDiskWorkspaceFolders ( ) ,
200+ } ,
201+ token ,
202+ progress ,
203+ ) ;
204+ }
103205
104206 /**
105207 * Create a `CoreQueryRun` object. This creates an object whose `evaluate()` function can be
0 commit comments