mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 17:23:27 -05:00
handling offline and inassisable db in oe (#369)
* handling offline and inassisable db in oe
This commit is contained in:
@@ -4,7 +4,10 @@
|
||||
//
|
||||
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
{
|
||||
@@ -34,5 +37,38 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void PopulateChildren(bool refresh, string name = null)
|
||||
{
|
||||
SmoQueryContext context = this.GetContextAs<SmoQueryContext>();
|
||||
if (IsAccessible(context))
|
||||
{
|
||||
base.PopulateChildren(refresh, name);
|
||||
}
|
||||
else
|
||||
{
|
||||
ErrorMessage = string.Format(CultureInfo.InvariantCulture, SR.DatabaseNotAccessible, context.Database.Name);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsAccessible(SmoQueryContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (context == null || context.Database == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return context.Database.IsAccessible;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return true;
|
||||
string error = string.Format(CultureInfo.InvariantCulture, "Failed to get IsAccessible. error:{0} inner:{1} stacktrace:{2}",
|
||||
ex.Message, ex.InnerException != null ? ex.InnerException.Message : "", ex.StackTrace);
|
||||
Logger.Write(LogLevel.Error, error);
|
||||
ErrorMessage = ex.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,6 +84,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
|
||||
IEnumerable<SmoQuerier> queriers = context.ServiceProvider.GetServices<SmoQuerier>(q => IsCompatibleQuerier(q));
|
||||
var filters = this.Filters.ToList();
|
||||
var smoProperties = this.SmoProperties.Where(p => (p.ValidFor == 0 || p.ValidFor.HasFlag(validForFlag))).Select(x => x.Name);
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
filters.Add(new NodeFilter
|
||||
@@ -98,7 +99,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
string propertyFilter = GetProperyFilter(filters, querier.GetType(), validForFlag);
|
||||
try
|
||||
{
|
||||
var smoObjectList = querier.Query(context, propertyFilter, refresh).ToList();
|
||||
var smoObjectList = querier.Query(context, propertyFilter, refresh, smoProperties).ToList();
|
||||
foreach (var smoObject in smoObjectList)
|
||||
{
|
||||
if (smoObject == null)
|
||||
@@ -239,6 +240,14 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<NodeSmoProperty> SmoProperties
|
||||
{
|
||||
get
|
||||
{
|
||||
return Enumerable.Empty<NodeSmoProperty>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any final validation of the object to be added passes, and false
|
||||
/// if validation fails. This provides a chance to filter specific items out of a list
|
||||
|
||||
@@ -6,14 +6,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.Data.Tools.DataSets;
|
||||
using Microsoft.SqlServer.Management.Common;
|
||||
using Microsoft.SqlServer.Management.Sdk.Sfc;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlTools.Extensibility;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
{
|
||||
@@ -33,7 +28,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public abstract IEnumerable<SqlSmoObject> Query(SmoQueryContext context, string filter, bool refresh);
|
||||
public abstract IEnumerable<SqlSmoObject> Query(SmoQueryContext context, string filter, bool refresh, IEnumerable<string> extraProperties);
|
||||
|
||||
internal IMultiServiceProvider ServiceProvider
|
||||
{
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -33,7 +33,6 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
{
|
||||
XmlElement nodeElement = GetNodeElement(xmlFile, nodeName);
|
||||
IList<string> parents = GetParents(nodeElement, xmlFile, nodeName);
|
||||
string properties = GetProperties(nodeElement, xmlFile, nodeName);
|
||||
string nodeType = GetNodeType(nodeElement, nodeName);
|
||||
|
||||
string queryBaseClass = "SmoQuerier";
|
||||
@@ -51,7 +50,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
WriteLine("");
|
||||
|
||||
// Query impl
|
||||
WriteLine("public override IEnumerable<SqlSmoObject> Query(SmoQueryContext context, string filter, bool refresh)");
|
||||
WriteLine("public override IEnumerable<SqlSmoObject> Query(SmoQueryContext context, string filter, bool refresh, IEnumerable<string> extraProperties)");
|
||||
WriteLine("{");
|
||||
PushIndent(indent);
|
||||
|
||||
@@ -75,7 +74,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
|
||||
if (IsCollection(nodeElement))
|
||||
{
|
||||
WriteLine(string.Format("retValue.InitializeCollection(filter, new string[] {{ {0} }});", properties));
|
||||
WriteLine(string.Format("retValue.ClearAndInitialize(filter, extraProperties);"));
|
||||
WriteLine(string.Format("return new SmoCollectionWrapper<{0}>(retValue).Where(c => PassesFinalFilters({1}, c));", nodeType, parentVar));
|
||||
}
|
||||
else
|
||||
@@ -214,22 +213,6 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
return new string[] { "Database" };
|
||||
}
|
||||
|
||||
public static string GetProperties(XmlElement nodeElement, string xmlFile, string parentName)
|
||||
{
|
||||
var propertiesAttr = nodeElement.GetAttribute("Properties");
|
||||
string result = string.Empty;
|
||||
if (!string.IsNullOrEmpty(propertiesAttr))
|
||||
{
|
||||
var properties = propertiesAttr.Split(';');
|
||||
foreach (var item in properties)
|
||||
{
|
||||
result = result + (string.IsNullOrEmpty(result) ? "" : ",") + "\"" + item + "\"";
|
||||
}
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<XmlElement> GetChildren(string xmlFile, string parentName, string childNode)
|
||||
{
|
||||
XmlElement nodeElement = GetNodeElement(xmlFile, parentName);
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
|
||||
<Node Name="SqlErrorMessage" Type="UserDefinedMessage" Parent="Server" />
|
||||
|
||||
<Node Name="SqlTable" Parent="Database" Properties="IsSystemVersioned" />
|
||||
<Node Name="SqlTable" Parent="Database" />
|
||||
<Node Name="SqlHistoryTable" Type="Table" Parent="Table" >
|
||||
<NavigationPath Parent="Table" Type="Table" Field="Parent.Tables" FieldForUrn="Parent" />
|
||||
</Node>
|
||||
|
||||
@@ -14,10 +14,17 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
{
|
||||
public override string GetNodeCustomName(object smoObject, SmoQueryContext smoContext)
|
||||
{
|
||||
Table table = smoObject as Table;
|
||||
if (table != null && table.IsSystemVersioned)
|
||||
try
|
||||
{
|
||||
return $"{table.Schema}.{table.Name} ({SR.SystemVersioned_LabelPart})";
|
||||
Table table = smoObject as Table;
|
||||
if (table != null && table.IsSystemVersioned)
|
||||
{
|
||||
return $"{table.Schema}.{table.Name} ({SR.SystemVersioned_LabelPart})";
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
//Ignore the exception and just not change create custom name
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
|
||||
@@ -75,6 +75,9 @@
|
||||
<Value>TableTemporalType.SystemVersioned</Value>
|
||||
</Filter>
|
||||
</Filters>
|
||||
<Properties>
|
||||
<Property Name="IsSystemVersioned" ValidFor="Sql2016|Sql2017|AzureV12"/>
|
||||
</Properties>
|
||||
<Child Name="SystemTables" IsSystemObject="1"/>
|
||||
<!--
|
||||
<Child Name="FileTables"/>
|
||||
|
||||
@@ -764,6 +764,20 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<NodeSmoProperty> SmoProperties
|
||||
{
|
||||
get
|
||||
{
|
||||
var properties = new List<NodeSmoProperty>();
|
||||
properties.Add(new NodeSmoProperty
|
||||
{
|
||||
Name = "IsSystemVersioned",
|
||||
ValidFor = ValidForFlag.Sql2016|ValidForFlag.AzureV12
|
||||
});
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnExpandPopulateFolders(IList<TreeNode> currentChildren, TreeNode parent)
|
||||
{
|
||||
currentChildren.Add(new FolderNode {
|
||||
|
||||
@@ -107,9 +107,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
|
||||
List<XmlElement> children = GetChildren(xmlFile, type);
|
||||
List<XmlElement> filters = GetNodeFilters(xmlFile, type);
|
||||
|
||||
|
||||
|
||||
List<XmlElement> smoProperties = GetNodeSmoProperties(xmlFile, type);
|
||||
|
||||
if (filters.Count > 0)
|
||||
{
|
||||
@@ -180,6 +178,43 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (smoProperties.Count > 0)
|
||||
{
|
||||
WriteLine("");
|
||||
WriteLine(" public override IEnumerable<NodeSmoProperty> SmoProperties");
|
||||
WriteLine(" {");
|
||||
WriteLine(" get");
|
||||
WriteLine(" {");
|
||||
|
||||
WriteLine(" var properties = new List<NodeSmoProperty>();");
|
||||
foreach (var smoPropertiy in smoProperties)
|
||||
{
|
||||
var propertyName = smoPropertiy.GetAttribute("Name");
|
||||
var validFor = smoPropertiy.GetAttribute("ValidFor");
|
||||
|
||||
|
||||
|
||||
|
||||
WriteLine(" properties.Add(new NodeSmoProperty");
|
||||
WriteLine(" {");
|
||||
WriteLine(string.Format(" Name = \"{0}\",", propertyName));
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(validFor))
|
||||
{
|
||||
WriteLine(string.Format(" ValidFor = {0}", GetValidForFlags(validFor)));
|
||||
}
|
||||
WriteLine(" });");
|
||||
|
||||
|
||||
}
|
||||
|
||||
WriteLine(" return properties;");
|
||||
WriteLine(" }");
|
||||
WriteLine(" }");
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (children.Count > 0)
|
||||
{
|
||||
@@ -483,6 +518,26 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
|
||||
}
|
||||
return retElements;
|
||||
}
|
||||
|
||||
|
||||
public static List<XmlElement> GetNodeSmoProperties(string xmlFile, string parentName)
|
||||
{
|
||||
XmlElement nodeElement = GetNodeElement(xmlFile, parentName);
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.Load(xmlFile);
|
||||
|
||||
List<XmlElement> retElements = new List<XmlElement>();
|
||||
XmlNodeList nodeList = doc.SelectNodes(string.Format("/ServerExplorerTree/Node[@Name='{0}']/Properties/Property", parentName));
|
||||
foreach (var item in nodeList)
|
||||
{
|
||||
XmlElement itemAsElement = item as XmlElement;
|
||||
if (itemAsElement != null)
|
||||
{
|
||||
retElements.Add(itemAsElement);
|
||||
}
|
||||
}
|
||||
return retElements;
|
||||
}
|
||||
|
||||
public static List<XmlElement> GetNodeFilterValues(string xmlFile, string parentName, string filterProperty)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user