Skip to content

Commit

Permalink
Let drivers implement a resolveConnection function (mtxr#1058)
Browse files Browse the repository at this point in the history
* Let drivers implement a `resolveConnection` function

* Bump version of `@sqltools/types` package we publish

* Fix a typo
  • Loading branch information
gjsjohnmurray authored Dec 8, 2022
1 parent ebee712 commit c2dcf9a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sqltools",
"displayName": "SQLTools",
"description": "Connecting users to many of the most commonly used databases. Welcome to database management done right.",
"version": "0.26.0",
"version": "0.27.0-SNAPSHOT",
"publisher": "mtxr",
"license": "MIT",
"preview": false,
Expand Down
11 changes: 9 additions & 2 deletions packages/plugins/connection-manager/explorer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import sortBy from 'lodash/sortBy';
import { createLogger } from '@sqltools/log/src';
import Context from '@sqltools/vscode/context';
import Config from '@sqltools/util/config-manager';
import { resolveConnection } from '../extension-util';

const log = createLogger('conn-man:explorer');

Expand Down Expand Up @@ -40,9 +41,11 @@ export class ConnectionExplorer implements TreeDataProvider<SidebarTreeItem> {

public async getActive(): Promise<IConnection | null> {
const conns = await this.getConnections();
const active = conns.find(c => c.isActive);
let active = conns.find(c => c.isActive);
if (!active) return null;

active = await resolveConnection(active);

return {
...active,
id: getConnectionId(active),
Expand Down Expand Up @@ -78,7 +81,11 @@ export class ConnectionExplorer implements TreeDataProvider<SidebarTreeItem> {
public async getConnectionById(id: string): Promise<IConnection> {
if (!id) return null;
const items = await this.getConnections();
return items.find(c => getConnectionId(c) === id) || null;
let connection = items.find(c => getConnectionId(c) === id) || null;
if (connection) {
connection = await resolveConnection(connection);
}
return connection;
}

public getSelection() {
Expand Down
18 changes: 13 additions & 5 deletions packages/plugins/connection-manager/extension-util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { extensions } from 'vscode';
import Context from '@sqltools/vscode/context';
import PluginResourcesMap, { buildResourceKey } from '@sqltools/util/plugin-resources';
import { IDriverExtensionApi, IIcons } from '@sqltools/types';
import { IConnection, IDriverExtensionApi, IIcons } from '@sqltools/types';
import fs from 'fs';
import prepareSchema from './webview/lib/prepare-schema';
import { SettingsScreenState } from './webview/ui/screens/Settings/interfaces';
Expand Down Expand Up @@ -78,8 +78,16 @@ export const getExtension = async (id: string): Promise<IDriverExtensionApi | nu
};

export const driverPluginExtension = async (driverName: string) => {
const pluginExtenxionId = PluginResourcesMap.get(buildResourceKey({ type: 'driver', name: driverName, resource: 'extension-id' }));
log.debug(`Driver name %s. Plugin ext: %s`, driverName, pluginExtenxionId);
if (!pluginExtenxionId) return null;
return getExtension(pluginExtenxionId);
const pluginExtensionId = PluginResourcesMap.get(buildResourceKey({ type: 'driver', name: driverName, resource: 'extension-id' }));
log.debug(`Driver name %s. Plugin ext: %s`, driverName, pluginExtensionId);
if (!pluginExtensionId) return null;
return getExtension(pluginExtensionId);
};

export const resolveConnection = async (connInfo: IConnection) => {
const pluginExt = await driverPluginExtension(connInfo.driver);
if (pluginExt && pluginExt.resolveConnection) {
connInfo = await pluginExt.resolveConnection({ connInfo });
}
return connInfo;
}
4 changes: 3 additions & 1 deletion packages/plugins/connection-manager/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { CancellationTokenSource, commands, ConfigurationTarget, env as vscodeEn
import CodeLensPlugin from '../codelens/extension';
import { ConnectRequest, DisconnectRequest, ForceListRefresh, GetChildrenForTreeItemRequest, GetConnectionPasswordRequest, GetConnectionsRequest, GetInsertQueryRequest, ProgressNotificationComplete, ProgressNotificationCompleteParams, ProgressNotificationStart, ProgressNotificationStartParams, ReleaseResultsRequest, RunCommandRequest, GetResultsRequest, SearchConnectionItemsRequest, TestConnectionRequest } from './contracts';
import DependencyManager from './dependency-manager/extension';
import { getExtension } from './extension-util';
import { getExtension, resolveConnection } from './extension-util';
import statusBar from './status-bar';
import { removeAttachedConnection, attachConnection, getAttachedConnection } from './attached-files';

Expand Down Expand Up @@ -51,6 +51,7 @@ export class ConnectionManagerPlugin implements IExtensionPlugin {

private ext_testConnection = async (c: IConnection) => {
let password = null;
c = await resolveConnection(c);

if (c.askForPassword) password = await this._askForPassword(c);
if (c.askForPassword && password === null) return;
Expand Down Expand Up @@ -576,6 +577,7 @@ export class ConnectionManagerPlugin implements IExtensionPlugin {
let password = null;

if (c) {
c = await resolveConnection(c);
c.id = getConnectionId(c);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,14 +729,16 @@ export interface IExtension {

export interface IDriverExtensionApi {
/**
* Prepare connection settings that will be saved to settings file
* Prepare connection settings that will be saved to settings file,
* and/or resolve settings before passing them to the language server (e.g. call an AuthenticationProvider to retrieve credentials)
*
* @param {{ connInfo: IConnection }} arg
* @returns {(Promise<IConnection> | IConnection)}
* @memberof IDriverExtensionApi
*/
parseBeforeSaveConnection?(arg: { connInfo: IConnection }): Promise<IConnection> | IConnection;
parseBeforeEditConnection?(arg: { connInfo: IConnection }): Promise<IConnection> | IConnection;
resolveConnection?(arg: { connInfo: IConnection }): Promise<IConnection> | IConnection;
readonly driverName?: string;
readonly driverAliases: IDriverAlias[];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sqltools/types",
"version": "0.1.6",
"version": "0.1.7",
"description": "SQLTools interfaces and types",
"types": "index.d.ts",
"main": "index.js",
Expand Down

0 comments on commit c2dcf9a

Please sign in to comment.