Skip to content

Commit b0328b0

Browse files
committed
Allow users more flexibility when opening a DB
Closes #383. See the heuristics in the issue.
1 parent 2d7d6fb commit b0328b0

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { clearCacheInDatabase, UserCancellationException } from './run-queries';
99
import * as qsClient from './queryserver-client';
1010
import { upgradeDatabase } from './upgrades';
1111
import { importArchiveDatabase, promptImportInternetDatabase } from './databaseFetcher';
12+
import * as fs from 'fs-extra';
1213

1314
type ThemableIconPath = { light: string; dark: string } | string;
1415

@@ -361,13 +362,36 @@ export class DatabaseUI extends DisposableObject {
361362
}
362363

363364
if (byFolder) {
365+
const fixedUri = await this.fixDbUri(uri);
364366
// we are selecting a database folder
365-
return await this.setCurrentDatabase(uri);
367+
return await this.setCurrentDatabase(fixedUri);
366368
}
367369
else {
368370
// we are selecting a database archive. Must unzip into a workspace-controlled area
369371
// before importing.
370372
return await importArchiveDatabase(uri.toString(true), this.databaseManager, this.storagePath);
371373
}
372374
}
375+
376+
/**
377+
* Perform some heuristics to ensure a proper database location is chosen.
378+
*
379+
* 1. If the selected URI to add is a file, choose the containing directory
380+
* 2. If the selected URI is a directory matching db-*, choose the containing directory
381+
* 3. choose the current directory
382+
*
383+
* @param uri a URI that is a datbase folder or inside it
384+
*
385+
* @return the actual database folder found by using the heuristics above.
386+
*/
387+
private async fixDbUri(uri: Uri): Promise<Uri> {
388+
let dbPath = uri.fsPath;
389+
if ((await fs.stat(dbPath)).isFile()) {
390+
dbPath = path.dirname(dbPath);
391+
}
392+
if (path.basename(dbPath).startsWith('db-')) {
393+
dbPath = path.dirname(dbPath);
394+
}
395+
return Uri.file(dbPath);
396+
}
373397
}

0 commit comments

Comments
 (0)