mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-18 01:25:41 -05:00
added handler for oe refresh and close session requests (#332)
* added handler for oe refresh and close session requests
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Contracts
|
||||
{
|
||||
/// <summary>
|
||||
/// Information returned from a <see cref="CloseSessionRequest"/>.
|
||||
/// Contains success information, a <see cref="SessionId"/> to be used when
|
||||
/// requesting closing an existing session.
|
||||
/// </summary>
|
||||
public class CloseSessionResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// Boolean indicating if the session was closed successfully
|
||||
/// </summary>
|
||||
public bool Success { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Unique ID to use when sending any requests for objects in the
|
||||
/// tree under the node
|
||||
/// </summary>
|
||||
public string SessionId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parameters to the <see cref="CloseSessionRequest"/>.
|
||||
/// </summary>
|
||||
public class CloseSessionParams
|
||||
{
|
||||
/// <summary>
|
||||
/// The Id returned from a <see cref="CreateSessionRequest"/>. This
|
||||
/// is used to disambiguate between different trees.
|
||||
/// </summary>
|
||||
public string SessionId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Establishes an Object Explorer tree session for a specific connection.
|
||||
/// This will create a connection to a specific server or database, register
|
||||
/// it for use in the
|
||||
/// </summary>
|
||||
public class CloseSessionRequest
|
||||
{
|
||||
public static readonly
|
||||
RequestType<CloseSessionParams, CloseSessionResponse> Type =
|
||||
RequestType<CloseSessionParams, CloseSessionResponse>.Create("objectexplorer/closesession");
|
||||
}
|
||||
}
|
||||
@@ -1,39 +1,56 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Contracts
|
||||
{
|
||||
/// <summary>
|
||||
/// Parameters to the <see cref="ExpandRequest"/>.
|
||||
/// </summary>
|
||||
public class RefreshParams
|
||||
{
|
||||
/// <summary>
|
||||
/// The Id returned from a <see cref="CreateSessionRequest"/>. This
|
||||
/// is used to disambiguate between different trees.
|
||||
/// </summary>
|
||||
public string SessionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Path identifying the node to expand. See <see cref="NodeInfo.NodePath"/> for details
|
||||
/// </summary>
|
||||
public string[] NodePath { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A request to expand a
|
||||
/// </summary>
|
||||
public class RefreshRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns children of a given node as a <see cref="NodeInfo"/> array.
|
||||
/// </summary>
|
||||
public static readonly
|
||||
RequestType<ExpandParams, NodeInfo[]> Type =
|
||||
RequestType<ExpandParams, NodeInfo[]>.Create("objectexplorer/refresh");
|
||||
}
|
||||
}
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Contracts
|
||||
{
|
||||
/// <summary>
|
||||
/// Information returned from a <see cref="RefreshRequest"/>.
|
||||
/// </summary>
|
||||
public class RefreshResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// Unique ID to use when sending any requests for objects in the
|
||||
/// tree under the node
|
||||
/// </summary>
|
||||
public string SessionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Information describing the expanded nodes in the tree
|
||||
/// </summary>
|
||||
public NodeInfo[] Nodes { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parameters to the <see cref="ExpandRequest"/>.
|
||||
/// </summary>
|
||||
public class RefreshParams
|
||||
{
|
||||
/// <summary>
|
||||
/// The Id returned from a <see cref="CreateSessionRequest"/>. This
|
||||
/// is used to disambiguate between different trees.
|
||||
/// </summary>
|
||||
public string SessionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Path identifying the node to refresh. See <see cref="NodeInfo.NodePath"/> for details
|
||||
/// </summary>
|
||||
public string NodePath { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A request to expand a
|
||||
/// </summary>
|
||||
public class RefreshRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns children of a given node as a <see cref="NodeInfo"/> array.
|
||||
/// </summary>
|
||||
public static readonly
|
||||
RequestType<RefreshParams, RefreshResponse> Type =
|
||||
RequestType<RefreshParams, RefreshResponse>.Create("objectexplorer/refresh");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes
|
||||
{
|
||||
proeprtyValue = (int)Convert.ChangeType(value, Type);
|
||||
}
|
||||
string orPrefix = i == 0 ? "" : "or";
|
||||
string orPrefix = i == 0 ? string.Empty : "or";
|
||||
filter = $"{filter} {orPrefix} @{Property} = {proeprtyValue}";
|
||||
}
|
||||
}
|
||||
@@ -91,8 +91,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes
|
||||
{
|
||||
var value = list[i];
|
||||
|
||||
string orPrefix = i == 0 ? "" : "and";
|
||||
filter = $"{filter} {orPrefix} {value.ToPropertyFilterString()}";
|
||||
string andPrefix = i == 0 ? string.Empty : "and";
|
||||
filter = $"{filter} {andPrefix} {value.ToPropertyFilterString()}";
|
||||
}
|
||||
filter = $"[{filter}]";
|
||||
|
||||
|
||||
@@ -195,6 +195,17 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes
|
||||
return children;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refresh this node and returns its children
|
||||
/// </summary>
|
||||
/// <returns>Children as an IList. This is the raw children collection, not a copy</returns>
|
||||
public IList<TreeNode> Refresh()
|
||||
{
|
||||
// TODO consider why solution explorer has separate Children and Items options
|
||||
PopulateChildren();
|
||||
return children;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a readonly view of the currently defined children for this node.
|
||||
/// This does not expand the node at all
|
||||
@@ -242,7 +253,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes
|
||||
{
|
||||
return Parent as T;
|
||||
}
|
||||
|
||||
|
||||
protected void PopulateChildren()
|
||||
{
|
||||
Debug.Assert(IsAlwaysLeaf == false);
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Composition;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
@@ -33,7 +35,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
|
||||
// Instance of the connection service, used to get the connection info for a given owner URI
|
||||
private ConnectionService connectionService;
|
||||
private IProtocolEndpoint serviceHost;
|
||||
private Dictionary<string, ObjectExplorerSession> sessionMap;
|
||||
private ConcurrentDictionary<string, ObjectExplorerSession> sessionMap;
|
||||
private readonly Lazy<Dictionary<string, HashSet<ChildFactory>>> applicableNodeChildFactories;
|
||||
private IMultiServiceProvider serviceProvider;
|
||||
|
||||
@@ -42,7 +44,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
|
||||
/// </summary>
|
||||
public ObjectExplorerService()
|
||||
{
|
||||
sessionMap = new Dictionary<string, ObjectExplorerSession>();
|
||||
sessionMap = new ConcurrentDictionary<string, ObjectExplorerSession>();
|
||||
applicableNodeChildFactories = new Lazy<Dictionary<string, HashSet<ChildFactory>>>(() => PopulateFactories());
|
||||
}
|
||||
|
||||
@@ -62,6 +64,17 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
|
||||
return applicableNodeChildFactories.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the session ids
|
||||
/// </summary>
|
||||
internal IReadOnlyCollection<string> SessionIds
|
||||
{
|
||||
get
|
||||
{
|
||||
return new ReadOnlyCollection<string>(sessionMap.Keys.ToList());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// As an <see cref="IComposableService"/>, this will be set whenever the service is initialized
|
||||
@@ -86,6 +99,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
|
||||
// Register handlers for requests
|
||||
serviceHost.SetRequestHandler(CreateSessionRequest.Type, HandleCreateSessionRequest);
|
||||
serviceHost.SetRequestHandler(ExpandRequest.Type, HandleExpandRequest);
|
||||
serviceHost.SetRequestHandler(RefreshRequest.Type, HandleRefreshRequest);
|
||||
serviceHost.SetRequestHandler(CloseSessionRequest.Type, HandleCloseSessionRequest);
|
||||
}
|
||||
|
||||
public void CloseSession(string uri)
|
||||
@@ -94,7 +109,11 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
|
||||
if (sessionMap.TryGetValue(uri, out session))
|
||||
{
|
||||
// Establish a connection to the specified server/database
|
||||
sessionMap.Remove(session.Uri);
|
||||
sessionMap.TryRemove(session.Uri, out session);
|
||||
connectionService.Disconnect(new DisconnectParams()
|
||||
{
|
||||
OwnerUri = uri
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +155,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
|
||||
await HandleRequestAsync(doCreateSession, context, "HandleCreateSessionRequest");
|
||||
}
|
||||
|
||||
internal async Task<NodeInfo[]> ExpandNode(ObjectExplorerSession session, string nodePath)
|
||||
internal async Task<NodeInfo[]> ExpandNode(ObjectExplorerSession session, string nodePath, bool forceRefresh = false)
|
||||
{
|
||||
return await Task.Factory.StartNew(() =>
|
||||
{
|
||||
@@ -144,7 +163,14 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
|
||||
TreeNode node = session.Root.FindNodeByPath(nodePath);
|
||||
if(node != null)
|
||||
{
|
||||
nodes = node.Expand().Select(x => x.ToNodeInfo()).ToArray();
|
||||
if (forceRefresh)
|
||||
{
|
||||
nodes = node.Refresh().Select(x => x.ToNodeInfo()).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
nodes = node.Expand().Select(x => x.ToNodeInfo()).ToArray();
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
});
|
||||
@@ -167,7 +193,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
|
||||
}
|
||||
|
||||
session = ObjectExplorerSession.CreateSession(connectionResult, serviceProvider);
|
||||
sessionMap[uri] = session;
|
||||
sessionMap.AddOrUpdate(uri, session, (key, oldSession) => session);
|
||||
return session;
|
||||
}
|
||||
|
||||
@@ -212,30 +238,87 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
|
||||
string uri = expandParams.SessionId;
|
||||
ObjectExplorerSession session = null;
|
||||
NodeInfo[] nodes = null;
|
||||
if (sessionMap.ContainsKey(uri))
|
||||
ExpandResponse response;
|
||||
if (!sessionMap.TryGetValue(uri, out session))
|
||||
{
|
||||
session = sessionMap[uri];
|
||||
}
|
||||
else
|
||||
{
|
||||
//TODO: error
|
||||
Logger.Write(LogLevel.Verbose, $"Cannot expand object explorer node. Couldn't find session for uri. {uri} ");
|
||||
}
|
||||
|
||||
|
||||
if (session != null)
|
||||
{
|
||||
// Establish a connection to the specified server/database
|
||||
// expand the nodes for given node path
|
||||
nodes = await ExpandNode(session, expandParams.NodePath);
|
||||
}
|
||||
|
||||
ExpandResponse response;
|
||||
response = new ExpandResponse() { Nodes = nodes, SessionId = uri };
|
||||
return response;
|
||||
};
|
||||
|
||||
await HandleRequestAsync(expandNode, context, "HandleExpandRequest");
|
||||
}
|
||||
|
||||
|
||||
internal async Task HandleRefreshRequest(RefreshParams refreshParams, RequestContext<RefreshResponse> context)
|
||||
{
|
||||
Logger.Write(LogLevel.Verbose, "HandleRefreshRequest");
|
||||
Func<Task<RefreshResponse>> refreshNode = async () =>
|
||||
{
|
||||
Validate.IsNotNull(nameof(refreshParams), refreshParams);
|
||||
Validate.IsNotNull(nameof(context), context);
|
||||
|
||||
string uri = refreshParams.SessionId;
|
||||
ObjectExplorerSession session = null;
|
||||
NodeInfo[] nodes = null;
|
||||
RefreshResponse response;
|
||||
if (!sessionMap.TryGetValue(uri, out session))
|
||||
{
|
||||
Logger.Write(LogLevel.Verbose, $"Cannot refresh object explorer node. Couldn't find session for uri. {uri} ");
|
||||
}
|
||||
|
||||
if (session != null)
|
||||
{
|
||||
// refresh the nodes for given node path
|
||||
nodes = await ExpandNode(session, refreshParams.NodePath, true);
|
||||
}
|
||||
|
||||
response = new RefreshResponse() { Nodes = nodes, SessionId = uri };
|
||||
return response;
|
||||
};
|
||||
|
||||
await HandleRequestAsync(refreshNode, context, "HandleRefreshRequest");
|
||||
}
|
||||
|
||||
internal async Task HandleCloseSessionRequest(CloseSessionParams closeSessionParams, RequestContext<CloseSessionResponse> context)
|
||||
{
|
||||
Validate.IsNotNull(nameof(closeSessionParams), closeSessionParams);
|
||||
Validate.IsNotNull(nameof(context), context);
|
||||
Logger.Write(LogLevel.Verbose, "HandleCloseSessionRequest");
|
||||
Func<Task<CloseSessionResponse>> closeSession = () =>
|
||||
{
|
||||
return Task.Factory.StartNew(() =>
|
||||
{
|
||||
string uri = closeSessionParams.SessionId;
|
||||
ObjectExplorerSession session = null;
|
||||
bool success = false;
|
||||
if (!sessionMap.TryGetValue(uri, out session))
|
||||
{
|
||||
Logger.Write(LogLevel.Verbose, $"Cannot close object explorer session. Couldn't find session for uri. {uri} ");
|
||||
}
|
||||
|
||||
if (session != null)
|
||||
{
|
||||
// refresh the nodes for given node path
|
||||
CloseSession(uri);
|
||||
success = true;
|
||||
}
|
||||
|
||||
var response = new CloseSessionResponse() {Success = success, SessionId = uri};
|
||||
return response;
|
||||
});
|
||||
};
|
||||
|
||||
await HandleRequestAsync(closeSession, context, "HandleCloseSessionRequest");
|
||||
}
|
||||
|
||||
private async Task HandleRequestAsync<T>(Func<Task<T>> handler, RequestContext<T> requestContext, string requestType)
|
||||
{
|
||||
Logger.Write(LogLevel.Verbose, requestType);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -64,28 +64,31 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
WriteLine(string.Format("if ({0} != null)", parentVar));
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
WriteLine("bool hasFilter = !string.IsNullOrEmpty(filter);");
|
||||
|
||||
string navigationPath = GetNavigationPath(nodeElement, xmlFile, nodeName, parentType);
|
||||
WriteLine(string.Format("var retValue = {0}.{1};", parentVar, navigationPath));
|
||||
WriteLine("bool hasFilter = !string.IsNullOrEmpty(filter);");
|
||||
WriteLine("HashSet<string> urns = null;");
|
||||
|
||||
WriteLine("if (hasFilter)");
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
WriteLine(string.Format("string urn = $\"{{{0}.Urn.ToString()}}/{1}\" + filter;", parentVar, nodeType));
|
||||
WriteLine("Enumerator en = new Enumerator();");
|
||||
WriteLine("Request request = new Request(new Urn(urn));");
|
||||
WriteLine("ServerConnection serverConnection = new ServerConnection(context.Server.ConnectionContext.SqlConnectionObject);");
|
||||
WriteLine("EnumResult result = en.Process(serverConnection, request);");
|
||||
WriteLine("urns = GetUrns(result);");
|
||||
PopIndent();
|
||||
WriteLine("}");
|
||||
WriteLine("if (retValue != null)");
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
|
||||
|
||||
if (IsCollection(nodeElement))
|
||||
{
|
||||
|
||||
WriteLine("HashSet<string> urns = null;");
|
||||
|
||||
WriteLine("if (hasFilter)");
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
WriteLine(string.Format("string urn = $\"{{{0}.Urn.ToString()}}/{1}\" + filter;", parentVar, nodeType));
|
||||
WriteLine("Enumerator en = new Enumerator();");
|
||||
WriteLine("Request request = new Request(new Urn(urn));");
|
||||
WriteLine("ServerConnection serverConnection = new ServerConnection(context.Server.ConnectionContext.SqlConnectionObject);");
|
||||
WriteLine("EnumResult result = en.Process(serverConnection, request);");
|
||||
WriteLine("urns = GetUrns(result);");
|
||||
PopIndent();
|
||||
WriteLine("}");
|
||||
WriteLine("if (hasFilter && urns != null)");
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
|
||||
Reference in New Issue
Block a user