Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions extensions/ql-vscode/src/databases/config/db-config-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pathExists, writeJSON, readJSON, readJSONSync } from "fs-extra";
import { join } from "path";
import { cloneDbConfig, DbConfig } from "./db-config";
import { cloneDbConfig, DbConfig, SelectedDbItem } from "./db-config";
import * as chokidar from "chokidar";
import { DisposableObject, DisposeHandler } from "../../pure/disposable-object";
import { DbConfigValidator } from "./db-config-validator";
Expand Down Expand Up @@ -56,11 +56,31 @@ export class DbConfigStore extends DisposableObject {
return this.configPath;
}

public async setSelectedDbItem(dbItem: SelectedDbItem): Promise<void> {
if (!this.config) {
// If the app is trying to set the selected item without a config
// being set it means that there is a bug in our code, so we throw
// an error instead of just returning an error result.
throw Error("Cannot select database item if config is not loaded");
}

const config: DbConfig = {
...this.config,
selected: dbItem,
};

await this.writeConfig(config);
}

private async writeConfig(config: DbConfig): Promise<void> {
await writeJSON(this.configPath, config, {
spaces: 2,
});
}

private async loadConfig(): Promise<void> {
if (!(await pathExists(this.configPath))) {
await writeJSON(this.configPath, this.createEmptyConfig(), {
spaces: 2,
});
await this.writeConfig(this.createEmptyConfig());
}

await this.readConfig();
Expand Down
50 changes: 50 additions & 0 deletions extensions/ql-vscode/src/databases/db-item-selection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DbItem, DbItemKind, LocalDbItem, RemoteDbItem } from "./db-item";
import { SelectedDbItem, SelectedDbItemKind } from "./config/db-config";

export function getSelectedDbItem(dbItems: DbItem[]): DbItem | undefined {
for (const dbItem of dbItems) {
Expand Down Expand Up @@ -42,3 +43,52 @@ function extractSelected(
}
return undefined;
}

export function mapDbItemToSelectedDbItem(
dbItem: DbItem,
): SelectedDbItem | undefined {
switch (dbItem.kind) {
case DbItemKind.RootLocal:
case DbItemKind.RootRemote:
// Root items are not selectable.
return undefined;

case DbItemKind.LocalList:
return {
kind: SelectedDbItemKind.LocalUserDefinedList,
listName: dbItem.listName,
};

case DbItemKind.RemoteUserDefinedList:
return {
kind: SelectedDbItemKind.RemoteUserDefinedList,
listName: dbItem.listName,
};

case DbItemKind.RemoteSystemDefinedList:
return {
kind: SelectedDbItemKind.RemoteSystemDefinedList,
listName: dbItem.listName,
};

case DbItemKind.RemoteOwner:
return {
kind: SelectedDbItemKind.RemoteOwner,
ownerName: dbItem.ownerName,
};

case DbItemKind.LocalDatabase:
return {
kind: SelectedDbItemKind.LocalDatabase,
listName: dbItem?.parentListName,
databaseName: dbItem.databaseName,
};

case DbItemKind.RemoteRepo:
return {
kind: SelectedDbItemKind.RemoteRepository,
listName: dbItem?.parentListName,
repositoryName: dbItem.repoFullName,
};
Comment thread
charisk marked this conversation as resolved.
}
}
12 changes: 11 additions & 1 deletion extensions/ql-vscode/src/databases/db-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { AppEvent, AppEventEmitter } from "../common/events";
import { ValueResult } from "../common/value-result";
import { DbConfigStore } from "./config/db-config-store";
import { DbItem } from "./db-item";
import { getSelectedDbItem } from "./db-item-selection";
import {
getSelectedDbItem,
mapDbItemToSelectedDbItem,
} from "./db-item-selection";
import { createLocalTree, createRemoteTree } from "./db-tree-creator";

export class DbManager {
Expand Down Expand Up @@ -44,4 +47,11 @@ export class DbManager {
public getConfigPath(): string {
return this.dbConfigStore.getConfigPath();
}

public async setSelectedDbItem(dbItem: DbItem): Promise<void> {
const selectedDbItem = mapDbItemToSelectedDbItem(dbItem);
if (selectedDbItem) {
await this.dbConfigStore.setSelectedDbItem(selectedDbItem);
}
}
}