Skip to content

Commit 2995b01

Browse files
committed
Provide a way to search for database items by name and language
We'll use this to check whether a database for our ql pack already exists. While there are other methods that search for a database item by URI, we only have a language chosen by the user and an nwo ("github/codeql"). So let's introduce a way to search for the db based on the information we have.
1 parent 053a180 commit 2995b01

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,20 @@ export class DatabaseManager extends DisposableObject {
897897
}
898898
}
899899

900+
public async digForDatabaseItem(
901+
language: string,
902+
databaseNwo: string,
903+
): Promise<DatabaseItem | undefined> {
904+
const dbItems = this.databaseItems || [];
905+
const dbs = dbItems.filter(
906+
(db) => db.language === language && db.name === databaseNwo,
907+
);
908+
if (dbs.length === 0) {
909+
return undefined;
910+
}
911+
return dbs[0];
912+
}
913+
900914
/**
901915
* Returns the index of the workspace folder that corresponds to the source archive of `item`
902916
* if there is one, and -1 otherwise.

extensions/ql-vscode/test/vscode-tests/minimal-workspace/local-databases.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,39 @@ describe("local databases", () => {
785785
});
786786
});
787787

788+
describe("digForDatabaseItem", () => {
789+
describe("when the item exists", () => {
790+
it("should return the database item", async () => {
791+
const mockDbItem = createMockDB();
792+
793+
await (databaseManager as any).addDatabaseItem(
794+
{} as ProgressCallback,
795+
{} as CancellationToken,
796+
mockDbItem,
797+
);
798+
799+
const databaseItem = await databaseManager.digForDatabaseItem(
800+
mockDbItem.language,
801+
mockDbItem.name,
802+
);
803+
804+
expect(databaseItem!.language).toEqual(mockDbItem.language);
805+
expect(databaseItem!.name).toEqual(mockDbItem.name);
806+
});
807+
});
808+
809+
describe("when the item doesn't exist", () => {
810+
it("should return nothing", async () => {
811+
const databaseItem = await databaseManager.digForDatabaseItem(
812+
"ruby",
813+
"mock-database-name",
814+
);
815+
816+
expect(databaseItem).toBeUndefined();
817+
});
818+
});
819+
});
820+
788821
function createMockDB(
789822
mockDbOptions = MOCK_DB_OPTIONS,
790823
// source archive location must be a real(-ish) location since

0 commit comments

Comments
 (0)