( context: vscode.ExtensionContext, connectionsSidepanel: SQLNotebookConnections )
| 28 | } |
| 29 | |
| 30 | export 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); |
no test coverage detected