Remove extra connection in app role and db role (#2037)

This commit is contained in:
Hai Cao
2023-05-01 18:04:07 -07:00
committed by GitHub
parent 5dbed25c40
commit 8d48ff735b
2 changed files with 18 additions and 66 deletions

View File

@@ -12,7 +12,6 @@ using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.Management;
using Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts;
@@ -46,27 +45,20 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
throw new ArgumentException("Invalid connection URI '{0}'", parameters.ConnectionUri);
}
string originalDatabaseName = originalConnInfo.ConnectionDetails.DatabaseName;
originalConnInfo.ConnectionDetails.DatabaseName = parameters.Database;
// create a default app role data context and database object
CDataContainer dataContainer;
try
{
originalConnInfo.ConnectionDetails.DatabaseName = parameters.Database;
ConnectParams connectParams = new ConnectParams
{
OwnerUri = parameters.ContextId,
Connection = originalConnInfo.ConnectionDetails,
Type = Connection.ConnectionType.Default
};
await this.ConnectionService.Connect(connectParams);
ServerConnection serverConnection = ConnectionService.OpenServerConnection(originalConnInfo, "DataContainer");
dataContainer = CreateAppRoleDataContainer(serverConnection, null, ConfigAction.Create, parameters.Database);
}
finally
{
originalConnInfo.ConnectionDetails.DatabaseName = originalDatabaseName;
}
ConnectionInfo connInfo;
this.ConnectionService.TryFindConnection(parameters.ContextId, out connInfo);
CDataContainer dataContainer = CreateAppRoleDataContainer(connInfo, null, ConfigAction.Create, parameters.Database);
AppRolePrototype prototype = parameters.IsNewObject
? new AppRolePrototype(dataContainer, parameters.Database)
: new AppRolePrototype(dataContainer, parameters.Database, dataContainer.Server.GetSmoObject(parameters.ObjectUrn) as ApplicationRole);
@@ -149,14 +141,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
private string DoHandleUpdateAppRoleRequest(AppRoleViewContext context, AppRoleInfo appRoleInfo, RunType runType)
{
ConnectionInfo connInfo;
this.ConnectionService.TryFindConnection(context.Parameters.ConnectionUri, out connInfo);
if (connInfo == null)
{
throw new ArgumentException("Invalid ConnectionUri");
}
CDataContainer dataContainer = CreateAppRoleDataContainer(connInfo, null, ConfigAction.Create, context.Parameters.Database);
CDataContainer dataContainer = CreateAppRoleDataContainer(context.Connection, null, ConfigAction.Create, context.Parameters.Database);
AppRolePrototype prototype = new AppRolePrototype(dataContainer, context.Parameters.Database, dataContainer.Server.Databases[context.Parameters.Database].ApplicationRoles[appRoleInfo.Name]);
prototype.ApplyInfoToPrototype(appRoleInfo);
return ConfigureAppRole(dataContainer, ConfigAction.Update, runType, prototype);
@@ -164,23 +149,14 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
private string DoHandleCreateAppRoleRequest(AppRoleViewContext context, AppRoleInfo appRoleInfo, RunType runType)
{
ConnectionInfo connInfo;
this.ConnectionService.TryFindConnection(context.Parameters.ConnectionUri, out connInfo);
if (connInfo == null)
{
throw new ArgumentException("Invalid ConnectionUri");
}
CDataContainer dataContainer = CreateAppRoleDataContainer(connInfo, null, ConfigAction.Create, context.Parameters.Database);
CDataContainer dataContainer = CreateAppRoleDataContainer(context.Connection, null, ConfigAction.Create, context.Parameters.Database);
AppRolePrototype prototype = new AppRolePrototype(dataContainer, context.Parameters.Database, appRoleInfo);
return ConfigureAppRole(dataContainer, ConfigAction.Create, runType, prototype);
}
internal CDataContainer CreateAppRoleDataContainer(ConnectionInfo connInfo, AppRoleInfo role, ConfigAction configAction, string databaseName)
internal CDataContainer CreateAppRoleDataContainer(ServerConnection serverConnection, AppRoleInfo role, ConfigAction configAction, string databaseName)
{
var serverConnection = ConnectionService.OpenServerConnection(connInfo, "DataContainer");
var connectionInfoWithConnection = new SqlConnectionInfoWithConnection();
connectionInfoWithConnection.ServerConnection = serverConnection;

View File

@@ -12,7 +12,6 @@ using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.Management;
using Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts;
@@ -46,27 +45,20 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
throw new ArgumentException("Invalid connection URI '{0}'", parameters.ConnectionUri);
}
string originalDatabaseName = originalConnInfo.ConnectionDetails.DatabaseName;
originalConnInfo.ConnectionDetails.DatabaseName = parameters.Database;
// create a default database role data context and database object
CDataContainer dataContainer;
try
{
originalConnInfo.ConnectionDetails.DatabaseName = parameters.Database;
ConnectParams connectParams = new ConnectParams
{
OwnerUri = parameters.ContextId,
Connection = originalConnInfo.ConnectionDetails,
Type = Connection.ConnectionType.Default
};
await this.ConnectionService.Connect(connectParams);
ServerConnection serverConnection = ConnectionService.OpenServerConnection(originalConnInfo, "DataContainer");
dataContainer = CreateDatabaseRoleDataContainer(serverConnection, null, ConfigAction.Create, parameters.Database);
}
finally
{
originalConnInfo.ConnectionDetails.DatabaseName = originalDatabaseName;
}
ConnectionInfo connInfo;
this.ConnectionService.TryFindConnection(parameters.ContextId, out connInfo);
CDataContainer dataContainer = CreateDatabaseRoleDataContainer(connInfo, null, ConfigAction.Create, parameters.Database);
DatabaseRolePrototype prototype = parameters.IsNewObject
? new DatabaseRolePrototype(dataContainer, parameters.Database)
: new DatabaseRolePrototype(dataContainer, parameters.Database, dataContainer.Server.GetSmoObject(parameters.ObjectUrn) as DatabaseRole);
@@ -148,14 +140,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
private string DoHandleUpdateDatabaseRoleRequest(DatabaseRoleViewContext context, DatabaseRoleInfo DatabaseRoleInfo, RunType runType)
{
ConnectionInfo connInfo;
this.ConnectionService.TryFindConnection(context.Parameters.ConnectionUri, out connInfo);
if (connInfo == null)
{
throw new ArgumentException("Invalid ConnectionUri");
}
CDataContainer dataContainer = CreateDatabaseRoleDataContainer(connInfo, null, ConfigAction.Create, context.Parameters.Database);
CDataContainer dataContainer = CreateDatabaseRoleDataContainer(context.Connection, null, ConfigAction.Create, context.Parameters.Database);
DatabaseRolePrototype prototype = new DatabaseRolePrototype(dataContainer, context.Parameters.Database, dataContainer.Server.Databases[context.Parameters.Database].Roles[DatabaseRoleInfo.Name]);
prototype.ApplyInfoToPrototype(DatabaseRoleInfo);
return ConfigureDatabaseRole(dataContainer, ConfigAction.Update, runType, prototype);
@@ -163,23 +148,14 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
private string DoHandleCreateDatabaseRoleRequest(DatabaseRoleViewContext context, DatabaseRoleInfo DatabaseRoleInfo, RunType runType)
{
ConnectionInfo connInfo;
this.ConnectionService.TryFindConnection(context.Parameters.ConnectionUri, out connInfo);
if (connInfo == null)
{
throw new ArgumentException("Invalid ConnectionUri");
}
CDataContainer dataContainer = CreateDatabaseRoleDataContainer(connInfo, null, ConfigAction.Create, context.Parameters.Database);
CDataContainer dataContainer = CreateDatabaseRoleDataContainer(context.Connection, null, ConfigAction.Create, context.Parameters.Database);
DatabaseRolePrototype prototype = new DatabaseRolePrototype(dataContainer, context.Parameters.Database, DatabaseRoleInfo);
return ConfigureDatabaseRole(dataContainer, ConfigAction.Create, runType, prototype);
}
internal CDataContainer CreateDatabaseRoleDataContainer(ConnectionInfo connInfo, DatabaseRoleInfo role, ConfigAction configAction, string databaseName)
internal CDataContainer CreateDatabaseRoleDataContainer(ServerConnection serverConnection, DatabaseRoleInfo role, ConfigAction configAction, string databaseName)
{
var serverConnection = ConnectionService.OpenServerConnection(connInfo, "DataContainer");
var connectionInfoWithConnection = new SqlConnectionInfoWithConnection();
connectionInfoWithConnection.ServerConnection = serverConnection;