Add rest of sql-bindings/azure funcs logic to sql bindings ext (#18733)

* refactor the rest of azure function and sql binding

* remove vscode-mssql typings that are moved to our sql-bindings ext
This commit is contained in:
Vasu Bhog
2022-03-15 15:10:42 -07:00
committed by GitHub
parent 01509de495
commit d585e75706
9 changed files with 236 additions and 74 deletions

View File

@@ -0,0 +1,21 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AddSqlBindingParams, GetAzureFunctionsParams, GetAzureFunctionsResult, ResultStatus } from 'sql-bindings';
import { RequestType } from 'vscode-languageclient';
/**
* Adds a SQL Binding to a specified Azure function in a file
*/
export namespace AddSqlBindingRequest {
export const type = new RequestType<AddSqlBindingParams, ResultStatus, void, void>('azureFunctions/sqlBinding');
}
/**
* Gets the names of the Azure functions in a file
*/
export namespace GetAzureFunctionsRequest {
export const type = new RequestType<GetAzureFunctionsParams, GetAzureFunctionsResult, void, void>('azureFunctions/getAzureFunctions');
}

View File

@@ -5,11 +5,12 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { BindingType, ConnectionDetails, IConnectionInfo } from 'vscode-mssql';
import { ConnectionDetails, IConnectionInfo } from 'vscode-mssql';
import * as constants from '../common/constants';
import * as utils from '../common/utils';
import * as azureFunctionsUtils from '../common/azureFunctionsUtils';
import { TelemetryActions, TelemetryReporter, TelemetryViews } from '../common/telemetry';
import { BindingType } from 'sql-bindings';
export async function launchAddSqlBindingQuickpick(uri: vscode.Uri | undefined): Promise<void> {
TelemetryReporter.sendActionEvent(TelemetryViews.SqlBindingsQuickPick, TelemetryActions.startAddSqlBinding);

View File

@@ -4,11 +4,12 @@
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { ITreeNodeInfo } from 'vscode-mssql';
import { IExtension, BindingType } from 'sql-bindings';
import { getAzdataApi, getVscodeMssqlApi } from './common/utils';
import { launchAddSqlBindingQuickpick } from './dialogs/addSqlBindingQuickpick';
import { createAzureFunction } from './services/azureFunctionsService';
import { addSqlBinding, createAzureFunction, getAzureFunctions } from './services/azureFunctionsService';
export async function activate(context: vscode.ExtensionContext): Promise<void> {
export async function activate(context: vscode.ExtensionContext): Promise<IExtension> {
const vscodeMssqlApi = await getVscodeMssqlApi();
void vscode.commands.executeCommand('setContext', 'azdataAvailable', !!getAzdataApi());
@@ -32,6 +33,14 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
const connectionString = await vscodeMssqlApi.getConnectionString(connectionDetails, false, false);
await createAzureFunction(connectionString, node.metadata.schema, node.metadata.name);
}));
return {
addSqlBinding: async (bindingType: BindingType, filePath: string, functionName: string, objectName: string, connectionStringSetting: string) => {
return addSqlBinding(bindingType, filePath, functionName, objectName, connectionStringSetting);
},
getAzureFunctions: async (filePath: string) => {
return getAzureFunctions(filePath);
}
};
}
export function deactivate(): void {

View File

@@ -4,11 +4,12 @@
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as mssql from 'vscode-mssql';
import * as path from 'path';
import * as utils from '../common/utils';
import * as azureFunctionUtils from '../common/azureFunctionsUtils';
import * as constants from '../common/constants';
import * as azureFunctionsContracts from '../contracts/azureFunctions/azureFunctionsContracts';
import { AddSqlBindingParams, BindingType, GetAzureFunctionsParams, GetAzureFunctionsResult, ResultStatus } from 'sql-bindings';
export const hostFileName: string = 'host.json';
@@ -86,14 +87,14 @@ export async function createAzureFunction(connectionString: string, schema: stri
}
// select input or output binding
const inputOutputItems: (vscode.QuickPickItem & { type: mssql.BindingType })[] = [
const inputOutputItems: (vscode.QuickPickItem & { type: BindingType })[] = [
{
label: constants.input,
type: mssql.BindingType.input
type: BindingType.input
},
{
label: constants.output,
type: mssql.BindingType.output
type: BindingType.output
}
];
@@ -123,3 +124,47 @@ export async function createAzureFunction(connectionString: string, schema: stri
azureFunctionUtils.overwriteAzureFunctionMethodBody(functionFile);
}
}
/**
* Adds a SQL Binding to a specified Azure function in a file
* @param bindingType Type of SQL Binding
* @param filePath Path of the file where the Azure Functions are
* @param functionName Name of the function where the SQL Binding is to be added
* @param objectName Name of Object for the SQL Query
* @param connectionStringSetting Setting for the connection string
* @returns Azure Function SQL binding
*/
export async function addSqlBinding(
bindingType: BindingType,
filePath: string,
functionName: string,
objectName: string,
connectionStringSetting: string
): Promise<ResultStatus> {
const params: AddSqlBindingParams = {
bindingType: bindingType,
filePath: filePath,
functionName: functionName,
objectName: objectName,
connectionStringSetting: connectionStringSetting
};
const vscodeMssqlApi = await utils.getVscodeMssqlApi();
return vscodeMssqlApi.sendRequest(azureFunctionsContracts.AddSqlBindingRequest.type, params);
}
/**
* Gets the names of the Azure functions in the file
* @param filePath Path of the file to get the Azure functions
* @returns array of names of Azure functions in the file
*/
export async function getAzureFunctions(filePath: string): Promise<GetAzureFunctionsResult> {
const params: GetAzureFunctionsParams = {
filePath: filePath
};
const vscodeMssqlApi = await utils.getVscodeMssqlApi();
return vscodeMssqlApi.sendRequest(azureFunctionsContracts.GetAzureFunctionsRequest.type, params);
}

View File

@@ -0,0 +1,102 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
declare module 'sql-bindings' {
export const enum extension {
name = 'Microsoft.sql-bindings',
vsCodeName = 'ms-mssql.sql-bindings-vscode'
}
/**
* sql bindings extension
*/
export interface IExtension {
/**
* Adds a SQL Binding to a specified Azure function in a file
* @param bindingType Type of SQL Binding
* @param filePath Path of the file where the Azure Functions are
* @param functionName Name of the function where the SQL Binding is to be added
* @param objectName Name of Object for the SQL Query
* @param connectionStringSetting Setting for the connection string
*/
addSqlBinding(bindingType: BindingType, filePath: string, functionName: string, objectName: string, connectionStringSetting: string): Promise<ResultStatus>;
/**
* Gets the names of the Azure Functions in the file
* @param filePath Path of the file to get the Azure Functions
* @returns array of names of Azure Functions in the file
*/
getAzureFunctions(filePath: string): Promise<GetAzureFunctionsResult>;
}
/**
* Parameters for adding a SQL binding to an Azure function
*/
export interface AddSqlBindingParams {
/**
* Absolute file path of file to add SQL binding
*/
filePath: string;
/**
* Name of function to add SQL binding
*/
functionName: string;
/**
* Name of object to use in SQL binding
*/
objectName: string;
/**
* Type of Azure function binding
*/
bindingType: BindingType;
/**
* Name of SQL connection string setting specified in local.settings.json
*/
connectionStringSetting: string;
}
/**
* Azure Functions binding type
*/
export const enum BindingType {
input,
output
}
/**
* Base result object from a request to the SQL Tools Service
*/
export interface ResultStatus {
success: boolean;
errorMessage: string;
}
/**
* Parameters for getting the names of the Azure Functions in a file
*/
export interface GetAzureFunctionsParams {
/**
* Absolute file path of file to get Azure Functions
*/
filePath: string;
}
/**
* Result from a get Azure Functions request
*/
export interface GetAzureFunctionsResult extends ResultStatus {
/**
* Array of names of Azure Functions in the file
*/
azureFunctions: string[];
}
}

View File

@@ -9,6 +9,8 @@ import * as path from 'path';
import * as TypeMoq from 'typemoq';
import * as mssql from '../../../mssql/src/mssql';
import * as vscodeMssql from 'vscode-mssql';
import { RequestType } from 'vscode-languageclient';
import { BindingType, GetAzureFunctionsResult } from 'sql-bindings';
export interface TestUtils {
context: vscode.ExtensionContext;
@@ -136,8 +138,8 @@ export const mockGetAzureFunctionsResult = {
};
export class MockAzureFunctionService implements vscodeMssql.IAzureFunctionsService {
addSqlBinding(_: vscodeMssql.BindingType, __: string, ___: string, ____: string, _____: string): Thenable<vscodeMssql.ResultStatus> { return Promise.resolve(mockResultStatus); }
getAzureFunctions(_: string): Thenable<vscodeMssql.GetAzureFunctionsResult> { return Promise.resolve(mockGetAzureFunctionsResult); }
addSqlBinding(_: BindingType, __: string, ___: string, ____: string, _____: string): Thenable<vscodeMssql.ResultStatus> { return Promise.resolve(mockResultStatus); }
getAzureFunctions(_: string): Thenable<GetAzureFunctionsResult> { return Promise.resolve(mockGetAzureFunctionsResult); }
}
export const mockDacFxMssqlOptionResult: vscodeMssql.DacFxOptionsResult = {
@@ -254,6 +256,9 @@ export class MockVscodeMssqlIExtension implements vscodeMssql.IExtension {
this.schemaCompare = new MockSchemaCompareService;
this.azureFunctions = new MockAzureFunctionService;
}
sendRequest<P, R, E, R0>(_: RequestType<P, R, E, R0>, __?: P): Promise<R> {
throw new Error('Method not implemented.');
}
promptForConnection(_?: boolean): Promise<vscodeMssql.IConnectionInfo | undefined> {
throw new Error('Method not implemented.');
}

View File

@@ -6,6 +6,8 @@
declare module 'vscode-mssql' {
import * as vscode from 'vscode';
import { RequestType } from 'vscode-languageclient';
import { BindingType, GetAzureFunctionsResult } from 'sql-bindings';
/**
* Covers defining what the vscode-mssql extension exports to other extensions
@@ -86,12 +88,20 @@ declare module 'vscode-mssql' {
getConnectionString(connectionUriOrDetails: string | ConnectionDetails, includePassword?: boolean, includeApplicationName?: boolean): Promise<string>;
/**
* Set connection details for the provided connection info
* Able to use this for getConnectionString requests to STS that require ConnectionDetails type
* @param connectionInfo connection info of the connection
* @returns connection details credentials for the connection
*/
* Set connection details for the provided connection info
* Able to use this for getConnectionString requests to STS that require ConnectionDetails type
* @param connectionInfo connection info of the connection
* @returns connection details credentials for the connection
*/
createConnectionDetails(connectionInfo: IConnectionInfo): ConnectionDetails;
/**
* Send a request to the SQL Tools Server client
* @param requestType The type of the request
* @param params The params to pass with the request
* @returns A promise object for when the request receives a response
*/
sendRequest<P, R, E, R0>(requestType: RequestType<P, R, E, R0>, params?: P): Promise<R>;
}
/**
@@ -589,64 +599,6 @@ declare module 'vscode-mssql' {
parentTypeName?: string;
}
/**
* Azure functions binding type
*/
export const enum BindingType {
input,
output
}
/**
* Parameters for adding a SQL binding to an Azure function
*/
export interface AddSqlBindingParams {
/**
* Aboslute file path of file to add SQL binding
*/
filePath: string;
/**
* Name of function to add SQL binding
*/
functionName: string;
/**
* Name of object to use in SQL binding
*/
objectName: string;
/**
* Type of Azure function binding
*/
bindingType: BindingType;
/**
* Name of SQL connection string setting specified in local.settings.json
*/
connectionStringSetting: string;
}
/**
* Parameters for getting the names of the Azure functions in a file
*/
export interface GetAzureFunctionsParams {
/**
* Absolute file path of file to get Azure functions
*/
filePath: string;
}
/**
* Result from a get Azure functions request
*/
export interface GetAzureFunctionsResult extends ResultStatus {
/**
* Array of names of Azure functions in the file
*/
azureFunctions: string[];
}
/**
* Parameters to initialize a connection to a database
*/