//
// 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;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlTools.Extensibility;
using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes;
namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
{
///
/// Context object containing key properties needed to query for SMO objects
///
public class SmoQueryContext
{
///
/// Creates a context object with a server to use as the basis for any queries
///
///
public SmoQueryContext(Server server, IMultiServiceProvider serviceProvider)
{
Server = server;
ServiceProvider = serviceProvider;
}
///
/// The server type
///
public SqlServerType SqlServerType { get; set; }
///
/// The server SMO will query against
///
public Server Server { get; private set; }
///
/// Optional Database context object to query against
///
public Database Database { get; set; }
///
/// Parent of a give node to use for queries
///
public SmoObjectBase Parent { get; set; }
///
/// A query loader that can be used to find objects
/// for specific SMO types
///
public IMultiServiceProvider ServiceProvider { get; private set; }
///
/// Helper method to cast a parent to a specific type
///
///
///
public T ParentAs()
where T : TreeNode
{
return Parent as T;
}
///
/// Gets the if available, by looking it up
/// from the
///
///
///
/// Thrown if the is not set or the
/// isn't available from that provider
///
public ObjectExplorerService GetObjectExplorerService()
{
if (ServiceProvider == null)
{
throw new InvalidOperationException(SqlTools.Hosting.SR.ServiceProviderNotSet);
}
ObjectExplorerService service = ServiceProvider.GetService();
if (service == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
SqlTools.Hosting.SR.ServiceNotFound, nameof(ObjectExplorerService)));
}
return service;
}
///
/// Copies the context for use by another node
///
/// New Parent to set
/// new with all fields except the same
public SmoQueryContext CopyWithParent(SmoObjectBase parent)
{
SmoQueryContext context = new SmoQueryContext(this.Server, this.ServiceProvider)
{
Database = this.Database,
Parent = parent,
SqlServerType = this.SqlServerType
};
return context;
}
}
}