Skip to content

Commit 8303255

Browse files
authored
moving best-match-from-list function down to ConnectionMatcher (#20491)
1 parent 894e3bf commit 8303255

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

src/controllers/connectionManager.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,14 @@ export default class ConnectionManager {
386386
return fileUri in this._connections && this._connections[fileUri].connecting;
387387
}
388388

389+
/**
390+
* Finds the closest-matching profile from the saved connections for the given partial connection profile
391+
* @param connProfile partial connection profile to match against
392+
* @returns closest-matching connection profile from the saved connections. If none is found, returns {score: MatchScore.NotMatch}
393+
*/
389394
public async findMatchingProfile(
390395
connProfile: IConnectionProfile,
391-
): Promise<{ profile: IConnectionProfile; score: Utils.MatchScore } | undefined> {
396+
): Promise<{ profile: IConnectionProfile; score: Utils.MatchScore }> {
392397
return this.connectionStore.findMatchingProfile(connProfile);
393398
}
394399

src/models/connectionStore.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -727,24 +727,17 @@ export class ConnectionStore {
727727
return connResults;
728728
}
729729

730+
/**
731+
* Finds the closest-matching profile from the saved connections for the given partial connection profile
732+
* @param connProfile partial connection profile to match against
733+
* @returns closest-matching connection profile from the saved connections. If none is found, returns {score: MatchScore.NotMatch}
734+
*/
730735
public async findMatchingProfile(
731736
connProfile: IConnectionProfile,
732-
): Promise<{ profile: IConnectionProfile; score: MatchScore } | undefined> {
737+
): Promise<{ profile: IConnectionProfile; score: MatchScore }> {
733738
const savedConnections = await this.readAllConnections();
734739

735-
let bestMatch: IConnectionProfile | undefined;
736-
let bestMatchScore = MatchScore.NotMatch;
737-
738-
for (const savedConn of savedConnections) {
739-
const matchLevel = ConnectionMatcher.isMatchingConnection(savedConn, connProfile);
740-
741-
if (matchLevel > bestMatchScore) {
742-
bestMatchScore = matchLevel;
743-
bestMatch = savedConn;
744-
}
745-
}
746-
747-
return { profile: bestMatch, score: bestMatchScore };
740+
return ConnectionMatcher.findMatchingProfile(connProfile, savedConnections);
748741
}
749742

750743
/** Gets the groupId for connections */

src/models/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,30 @@ export class ConnectionMatcher {
355355
return MatchScore.NotMatch;
356356
}
357357

358+
/**
359+
* Finds the closest-matching profile from the provided list for the given partial connection profile
360+
* @param connProfile partial connection profile to match against
361+
* @returns closest-matching connection profile from the provided list. If none is found, returns {score: MatchScore.NotMatch}
362+
*/
363+
public static findMatchingProfile(
364+
connProfile: IConnectionProfile,
365+
connectionList: IConnectionProfile[],
366+
): { profile: IConnectionProfile; score: MatchScore } {
367+
let bestMatch: IConnectionProfile | undefined;
368+
let bestMatchScore = MatchScore.NotMatch;
369+
370+
for (const savedConn of connectionList) {
371+
const matchLevel = ConnectionMatcher.isMatchingConnection(savedConn, connProfile);
372+
373+
if (matchLevel > bestMatchScore) {
374+
bestMatchScore = matchLevel;
375+
bestMatch = savedConn;
376+
}
377+
}
378+
379+
return { profile: bestMatch, score: bestMatchScore };
380+
}
381+
358382
/**
359383
* Checks if the server names match. Normalizes "." to "localhost" for comparison purposes. Case-sensitive.
360384
*/

test/unit/utils.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { expect, assert } from "chai";
77
import * as Utils from "../../src/models/utils";
88
import * as Constants from "../../src/constants/constants";
99
import { ConnectionCredentials } from "../../src/models/connectionCredentials";
10-
import { IConnectionProfile } from "../../src/models/interfaces";
10+
import { IConnectionProfile, IConnectionProfileWithSource } from "../../src/models/interfaces";
1111
import * as utilUtils from "../../src/utils/utils";
1212

1313
suite("Utility Tests - parseTimeString", () => {
@@ -367,6 +367,31 @@ suite("ConnectionMatcher", () => {
367367
).to.equal(testCase.expected);
368368
}
369369
});
370+
371+
test("findMatchingProfile", async () => {
372+
const connections = [
373+
sqlAuthConn as IConnectionProfileWithSource,
374+
azureAuthConn as IConnectionProfileWithSource,
375+
connStringConn as IConnectionProfileWithSource,
376+
];
377+
378+
let match = Utils.ConnectionMatcher.findMatchingProfile(azureAuthConn, connections);
379+
expect(match).to.deep.equal({
380+
profile: azureAuthConn,
381+
score: Utils.MatchScore.AllAvailableProps,
382+
});
383+
384+
match = Utils.ConnectionMatcher.findMatchingProfile(
385+
{
386+
server: "noMatch",
387+
} as IConnectionProfile,
388+
connections,
389+
);
390+
expect(match).to.deep.equal({
391+
profile: undefined,
392+
score: Utils.MatchScore.NotMatch,
393+
});
394+
});
370395
});
371396

372397
export const sqlAuthConn = {

0 commit comments

Comments
 (0)