MCPcopy Create free account
hub / github.com/cmoog/vscode-sql-notebook / connectToDatabase

Function connectToDatabase

src/commands.ts:30–106  ·  view source on GitHub ↗
(
  context: vscode.ExtensionContext,
  connectionsSidepanel: SQLNotebookConnections
)

Source from the content-addressed store, hash-verified

28}
29
30export function connectToDatabase(
31 context: vscode.ExtensionContext,
32 connectionsSidepanel: SQLNotebookConnections
33) {
34 return async (item?: ConnectionListItem) => {
35 let selectedName: string;
36 if (!item) {
37 const names = context.globalState
38 .get(storageKey, [])
39 .map(({ name }) => name);
40 const namePicked = await vscode.window.showQuickPick(names, {
41 ignoreFocusOut: true,
42 });
43 if (!namePicked) {
44 vscode.window.showErrorMessage(`Invalid database connection name.`);
45 return;
46 }
47 selectedName = namePicked;
48 } else {
49 selectedName = item.config.name;
50 }
51
52 const match = context.globalState
53 .get<ConnData[]>(storageKey, [])
54 .find(({ name }) => name === selectedName);
55 if (!match) {
56 vscode.window.showErrorMessage(
57 `"${selectedName}" not found. Please add the connection config in the sidebar before connecting.`
58 );
59 return;
60 }
61
62 let password: string | undefined;
63 try {
64 if (match.driver === 'sqlite') {
65 globalConnPool.pool = await getPool({
66 driver: 'sqlite',
67 path: match.path,
68 });
69 } else {
70 password = await context.secrets.get(match.passwordKey);
71 if (password === undefined) {
72 // can also mean that the platform doesn't work with `keytar`, see #18
73 vscode.window.showWarningMessage(
74 `Connection password not found in secret store. There may be a problem with the system keychain.`
75 );
76 // continue so that Linux users without a keychain can use empty password configurations
77 }
78
79 globalConnPool.pool = await getPool({
80 ...match,
81 password,
82 queryTimeout: getQueryTimeoutConfiguration(),
83 } as PoolConfig);
84 }
85 const conn = await globalConnPool.pool.getConnection();
86 await conn.query('SELECT 1'); // essentially a ping to see if the connection works
87 connectionsSidepanel.setActive(match.name);

Callers 1

activateFunction · 0.90

Calls 6

getPoolFunction · 0.90
shouldUseLanguageServerFunction · 0.85
startLanguageServerFunction · 0.85
setActiveMethod · 0.80
stopMethod · 0.80

Tested by

no test coverage detected