using the new smo method to initialize the smo collections (#367)

* using the new smo method to initialize the smo collections
This commit is contained in:
Leila Lali
2017-06-02 16:07:21 -07:00
committed by GitHub
parent 5c7909d741
commit 950b44137b
11 changed files with 189 additions and 861 deletions

Binary file not shown.

View File

@@ -77,90 +77,9 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
{
return true;
}
protected HashSet<string> GetUrns(SmoQueryContext context, SqlSmoObject smoObject, string filter, string objectName)
{
HashSet<string> urns = null;
string urn = string.Empty;
try
{
string parentUrn = smoObject.Urn != null ? smoObject.Urn.Value : string.Empty;
urn = parentUrn != null ? $"{parentUrn.ToString()}/{objectName}" + filter : string.Empty;
if (!string.IsNullOrEmpty(urn))
{
Enumerator en = new Enumerator();
Request request = new Request(new Urn(urn));
ServerConnection serverConnection = new ServerConnection(context.Server.ConnectionContext.SqlConnectionObject);
if (!serverConnection.IsOpen)
{
serverConnection.Connect();
}
EnumResult result = en.Process(serverConnection, request);
urns = GetUrns(result);
}
}
catch (Exception ex)
{
string error = string.Format(CultureInfo.InvariantCulture, "Failed getting urns. error:{0} inner:{1} stacktrace:{2}",
ex.Message, ex.InnerException != null ? ex.InnerException.Message : "", ex.StackTrace);
Logger.Write(LogLevel.Error, error);
throw ex;
}
return urns;
}
/// <summary>
/// Gets the urn from the enumResult
/// </summary>
protected HashSet<string> GetUrns(EnumResult enumResult)
{
try
{
HashSet<string> urns = null;
if (enumResult != null && enumResult.Data != null)
{
urns = new HashSet<string>();
using (IDataReader reader = GetDataReader(enumResult.Data))
{
if (reader != null)
{
while (reader.Read())
{
urns.Add(reader.GetString(0));
}
}
}
}
return urns;
}
catch(Exception ex)
{
string error = string.Format(CultureInfo.InvariantCulture, "Failed getting urns. error:{0} inner:{1} stacktrace:{2}",
ex.Message, ex.InnerException != null ? ex.InnerException.Message : "", ex.StackTrace);
Logger.Write(LogLevel.Error, error);
}
return null;
}
protected IEnumerable<T> GetSmoCollectionResult<T>(HashSet<string> urns, SmoCollectionBase retValue, SqlSmoObject parent) where T : SqlSmoObject
{
// the below code is filtering out tables on helsinki system
return new SmoCollectionWrapper<T>(retValue);
// if (urns != null)
// {
// return new SmoCollectionWrapper<T>(retValue).Where(c => PassesFinalFilters(parent, c) && urns.Contains(c.Urn));
// }
// else
// {
// return new SmoCollectionWrapper<T>(retValue).Where(c => PassesFinalFilters(parent, c));
// }
}
}
}

View File

@@ -33,6 +33,7 @@ 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";
@@ -62,15 +63,9 @@ 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("if (refresh)");
WriteLine("{");
PushIndent(indent);
WriteLine(string.Format("{0}.{1}.Refresh({2});", parentVar, navigationPath, IsCollection(nodeElement) ? "true" : ""));
PopIndent();
WriteLine("}");
WriteLine(string.Format("var retValue = {0}.{1};", parentVar, navigationPath));
WriteLine("if (retValue != null)");
@@ -80,27 +75,17 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
if (IsCollection(nodeElement))
{
string fieldForUrn = GetNavPathFieldForUrn(xmlFile, nodeName, parentType);
if (!string.IsNullOrEmpty(fieldForUrn))
{
fieldForUrn = string.Format("{0}.{1}", parentVar, fieldForUrn);
}
else
{
fieldForUrn = parentVar;
}
WriteLine("HashSet<string> urns = null;");
WriteLine("if (hasFilter)");
WriteLine("{");
PushIndent(indent);
WriteLine(string.Format("urns = GetUrns(context, {0}, filter, \"{1}\");", fieldForUrn, nodeType));
PopIndent();
WriteLine("}");
WriteLine(string.Format("return GetSmoCollectionResult<{0}>(urns, retValue, {1});", nodeType, parentVar));
WriteLine(string.Format("retValue.InitializeCollection(filter, new string[] {{ {0} }});", properties));
WriteLine(string.Format("return new SmoCollectionWrapper<{0}>(retValue).Where(c => PassesFinalFilters({1}, c));", nodeType, parentVar));
}
else
{
WriteLine("if (refresh)");
WriteLine("{");
PushIndent(indent);
WriteLine(string.Format("{0}.{1}.Refresh();", parentVar, navigationPath));
PopIndent();
WriteLine("}");
WriteLine("return new SqlSmoObject[] { retValue };");
}
@@ -228,6 +213,22 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
// default to assuming a type is under Database
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)
{

View File

@@ -35,7 +35,7 @@
<Node Name="SqlErrorMessage" Type="UserDefinedMessage" Parent="Server" />
<Node Name="SqlTable" Parent="Database" />
<Node Name="SqlTable" Parent="Database" Properties="IsSystemVersioned" />
<Node Name="SqlHistoryTable" Type="Table" Parent="Table" >
<NavigationPath Parent="Table" Type="Table" Field="Parent.Tables" FieldForUrn="Parent" />
</Node>

View File

@@ -14,13 +14,11 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
{
public override string GetNodeCustomName(object smoObject, SmoQueryContext smoContext)
{
// TODO: this code makes expanding the tables slow because of loading the IsSystemVersioned property for each table.
// Have to uncomment this after optimizing the way properties are loaded for SMO objects
//Table table = smoObject as Table;
//if (table != null && table.IsSystemVersioned)
//{
// 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})";
}
return string.Empty;
}

View File

@@ -21,7 +21,7 @@
"Newtonsoft.Json": "9.0.1",
"System.Data.Common": "4.1.0",
"System.Data.SqlClient": "4.4.0-sqltools-24613-04",
"Microsoft.SqlServer.Smo": "140.17053.0",
"Microsoft.SqlServer.Smo": "140.17054.0",
"Microsoft.SqlServer.Management.SqlScriptPublishModel": "140.17049.0",
"System.Security.SecureString": "4.0.0",
"System.Collections.Specialized": "4.0.1",

View File

@@ -17,7 +17,7 @@
"System.Runtime.Serialization.Primitives": "4.1.1",
"System.Data.Common": "4.1.0",
"System.Data.SqlClient": "4.4.0-sqltools-24613-04",
"Microsoft.SqlServer.Smo": "140.17053.0",
"Microsoft.SqlServer.Smo": "140.17054.0",
"System.Security.SecureString": "4.0.0",
"System.Collections.Specialized": "4.0.1",
"System.ComponentModel.TypeConverter": "4.1.0",

View File

@@ -29,7 +29,7 @@ NodeType: Constraint Label: CK_Employee_VacationHours SubType: Status:
NodeType: Index Label: NonClusteredIndex-Login (Non-Unique, Non-Clustered) SubType: Status:
NodeType: Statistic Label: NonClusteredIndex-Login SubType: Status:
NodeType: Statistic Label: PK_Employee_BusinessEntityID SubType: Status:
NodeType: Table Label: HumanResources.Employee_Temporal SubType: Status:
NodeType: Table Label: HumanResources.Employee_Temporal (System-Versioned) SubType: Status:
NodeType: Column Label: BusinessEntityID (PK, int, not null) SubType: Status:
NodeType: Column Label: NationalIDNumber (nvarchar(15), not null) SubType: Status:
NodeType: Column Label: LoginID (nvarchar(256), not null) SubType: Status:

View File

@@ -18,7 +18,7 @@
"System.Runtime.Serialization.Primitives": "4.1.1",
"System.Data.Common": "4.1.0",
"System.Data.SqlClient": "4.4.0-sqltools-24613-04",
"Microsoft.SqlServer.Smo": "140.17053.0",
"Microsoft.SqlServer.Smo": "140.17054.0",
"System.Security.SecureString": "4.0.0",
"System.Collections.Specialized": "4.0.1",
"System.ComponentModel.TypeConverter": "4.1.0",

View File

@@ -19,7 +19,7 @@
"System.Runtime.Serialization.Primitives": "4.1.1",
"System.Data.Common": "4.1.0",
"System.Data.SqlClient": "4.4.0-sqltools-24613-04",
"Microsoft.SqlServer.Smo": "140.17053.0",
"Microsoft.SqlServer.Smo": "140.17054.0",
"System.Security.SecureString": "4.0.0",
"System.Collections.Specialized": "4.0.1",
"System.ComponentModel.TypeConverter": "4.1.0",