Files
sqltoolsservice/src/Microsoft.Kusto.ServiceLayer/Metadata/MetadataService.cs
Justin M c932ef8613 3278 Kusto Unit Tests - Part 2 (#1063)
* 3278 Added unit tests in MetadataFactoryTests and Microsoft.Kusto.ServiceLayer.UnitTests project

* 3278 Removed todo and changed unit test to validate megabytes

* 3278 Added file and unit tests in AutoCompleteHelperTests.cs

* 3278 Removed unused functions from Kusto > ScriptAsScriptingOperation

* 3278 Added unit tests for DataSourceFactory

* 3278 Refactored AdminService to pass in ConnectionService rather than through instance variable. Added unit test for AdminServiceTests

* 3278 Refactored DataSourceFactory to not have static functions for future unit tests

* 3278 Re-added properties that were flagged as unused but are being used by ADS in ReliableDataSourceConnection.cs

* 3278 Re-added properties that were flagged as unused but are being used by ADS in ReliableDataSourceConnection.cs

* adding pipeline to execute tests (#1062)

* 3278 Converted GetDefaultAutoComplete and GetDefaultSemanticMarkers to static functions in DataSourceFactory. Removed unused constructor in ScriptFile. Added positive unit tests for both functions

* undoing release version bump

* adding additional configs

* 3278 Minor refactors in ConnectionInfo, BindingQueue, DiagnosticsHelper, MetadataService, and HostLoader. Changed AssemblyInfo to only allow Kusto Unit Tests for internal access. Added lots of unit tests.

* 3278 Commented out bindingContext.IsConnected in AddConnectionContext_Sets_BindingContext

* 3278 Reversed order of unit tests in ConnectedBindingQueueTests and added throw to Catch block.

* 3278 Reverted change to ConnectedBindingQueue. Removed unit test from AddConnectionContext for NeedsMetaData True

Co-authored-by: Jorge Berumen <52225468+joberume@users.noreply.github.com>
Co-authored-by: joberume <jberumen3@miners.utep.edu>
2020-09-03 16:17:39 -07:00

91 lines
3.5 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.Kusto.ServiceLayer.Connection;
using Microsoft.Kusto.ServiceLayer.Metadata.Contracts;
using Microsoft.Kusto.ServiceLayer.Utility;
using Microsoft.Kusto.ServiceLayer.DataSource;
using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
namespace Microsoft.Kusto.ServiceLayer.Metadata
{
/// <summary>
/// Main class for Metadata Service functionality
/// </summary>
public sealed class MetadataService
{
private static readonly Lazy<MetadataService> LazyInstance = new Lazy<MetadataService>();
public static MetadataService Instance => LazyInstance.Value;
private static ConnectionService _connectionService;
internal Task MetadataListTask { get; private set; }
/// <summary>
/// Initializes the Metadata Service instance
/// </summary>
/// <param name="serviceHost"></param>
/// <param name="connectionService"></param>
public void InitializeService(IProtocolEndpoint serviceHost, ConnectionService connectionService)
{
_connectionService = connectionService;
serviceHost.SetRequestHandler(MetadataListRequest.Type, HandleMetadataListRequest);
}
/// <summary>
/// Handle a metadata query request
/// </summary>
internal async Task HandleMetadataListRequest(
MetadataQueryParams metadataParams,
RequestContext<MetadataQueryResult> requestContext)
{
try
{
Func<Task> requestHandler = async () =>
{
ConnectionInfo connInfo;
_connectionService.TryFindConnection(metadataParams.OwnerUri, out connInfo);
var metadata = new List<ObjectMetadata>();
if (connInfo != null)
{
ReliableDataSourceConnection connection;
connInfo.TryGetConnection("Default", out connection);
IDataSource dataSource = connection.GetUnderlyingConnection();
DataSourceObjectMetadata objectMetadata = MetadataFactory.CreateClusterMetadata(connInfo.ConnectionDetails.ServerName);
DataSourceObjectMetadata databaseMetadata = MetadataFactory.CreateDatabaseMetadata(objectMetadata, connInfo.ConnectionDetails.DatabaseName);
IEnumerable<DataSourceObjectMetadata> databaseChildMetadataInfo = dataSource.GetChildObjects(databaseMetadata, true);
metadata = MetadataFactory.ConvertToObjectMetadata(databaseChildMetadataInfo);
}
await requestContext.SendResult(new MetadataQueryResult
{
Metadata = metadata.ToArray()
});
};
Task task = Task.Run(async () => await requestHandler()).ContinueWithOnFaulted(async t =>
{
await requestContext.SendError(t.Exception.ToString());
});
MetadataListTask = task;
}
catch (Exception ex)
{
await requestContext.SendError(ex.ToString());
}
}
}
}