Skip to content

Commit 8170c46

Browse files
committed
feat: extract common logic
1 parent a93bf14 commit 8170c46

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

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

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -972,18 +972,14 @@ export class DatabaseUI extends DisposableObject {
972972
}
973973

974974
/**
975-
* Ask the user for a database directory. Returns the chosen database, or `undefined` if the
976-
* operation was canceled.
975+
* Import database from uri. Returns the imported database, or `undefined` if the
976+
* operation was unsuccessful.
977977
*/
978-
private async chooseAndSetDatabase(
978+
private async importDatabase(
979+
uri: Uri,
979980
byFolder: boolean,
980981
progress: ProgressCallback,
981982
): Promise<DatabaseItem | undefined> {
982-
const uri = await chooseDatabaseDir(byFolder);
983-
if (!uri) {
984-
return undefined;
985-
}
986-
987983
if (byFolder && !uri.fsPath.endsWith("testproj")) {
988984
const fixedUri = await this.fixDbUri(uri);
989985
// we are selecting a database folder
@@ -1001,6 +997,22 @@ export class DatabaseUI extends DisposableObject {
1001997
}
1002998
}
1003999

1000+
/**
1001+
* Ask the user for a database directory. Returns the chosen database, or `undefined` if the
1002+
* operation was canceled.
1003+
*/
1004+
private async chooseAndSetDatabase(
1005+
byFolder: boolean,
1006+
progress: ProgressCallback,
1007+
): Promise<DatabaseItem | undefined> {
1008+
const uri = await chooseDatabaseDir(byFolder);
1009+
if (!uri) {
1010+
return undefined;
1011+
}
1012+
1013+
return await this.importDatabase(uri, byFolder, progress);
1014+
}
1015+
10041016
/**
10051017
* Ask the user for a parent directory that contains all databases.
10061018
* Returns all valid databases, or `undefined` if the operation was canceled.
@@ -1024,24 +1036,38 @@ export class DatabaseUI extends DisposableObject {
10241036
message: `Importing ${entry[0]}`,
10251037
});
10261038

1027-
if (entry[1] === FileType.Directory) {
1028-
try {
1029-
const fixedUri = await this.fixDbUri(Uri.joinPath(uri, entry[0]));
1030-
const database = await this.databaseManager.openDatabase(fixedUri, {
1031-
type: "folder",
1032-
});
1039+
const subProgress: ProgressCallback = (p) => {
1040+
progress({
1041+
step: index + 1,
1042+
maxStep: entries.length,
1043+
message: `Importing ${entry[0]} (${p.step}/${p.maxStep}): ${p.message}`,
1044+
});
1045+
};
1046+
1047+
try {
1048+
const fixedUri = await this.fixDbUri(Uri.joinPath(uri, entry[0]));
1049+
const database = await this.importDatabase(
1050+
fixedUri,
1051+
entry[1] === FileType.Directory,
1052+
subProgress,
1053+
);
1054+
if (database) {
10331055
databases.push(database);
1034-
} catch (e) {
1056+
} else {
10351057
failures.push(entry[0]);
10361058
}
1059+
} catch (e) {
1060+
failures.push(entry[0]);
10371061
}
10381062
}
10391063

10401064
if (failures.length) {
10411065
void showAndLogErrorMessage(
10421066
this.app.logger,
10431067
`Failed to import ${failures.length} database(s), successfully imported ${databases.length} database(s).`,
1044-
{ fullMessage: `Failed to import ${failures.length} database(s), successfully imported ${databases.length} database(s). Failed folders to import:\n ${failures.join("\n ")}` },
1068+
{
1069+
fullMessage: `Failed to import ${failures.length} database(s), successfully imported ${databases.length} database(s). Failed folders to import:\n - ${failures.join("\n - ")}`,
1070+
},
10451071
);
10461072
} else if (databases.length === 0) {
10471073
void showAndLogErrorMessage(

0 commit comments

Comments
 (0)