Skip to content

Commit c40c740

Browse files
authored
fixed the bug with loading default constraints in oe (#582)
* fixed the bug with loading default constraints in oe
1 parent fc4282b commit c40c740

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
lines changed

docs/guide/object_explorer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
### How to add a new SQL object type
4646
To add a new object type,
4747
* Add the type to TreeNodeDefinition.xml and SmoQueryModelDefinition.xml.
48-
* Regenerate the classes by running Build.cmd/build.sh -target=SRGen
48+
* Regenerate the classes by running Build.cmd/build.sh -target=CodeGen
4949

5050

5151

src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,42 @@ internal partial class SqlDefaultConstraintQuerier: SmoQuerier
487487

488488
public override IEnumerable<SqlSmoObject> Query(SmoQueryContext context, string filter, bool refresh, IEnumerable<string> extraProperties)
489489
{
490-
Column parentColumn = context.Parent as Column;
491-
if (parentColumn != null)
490+
Table parentTable = context.Parent as Table;
491+
if (parentTable != null)
492492
{
493-
var retValue = parentColumn.DefaultConstraint;
493+
var retValue = parentTable.Columns;
494494
if (retValue != null)
495495
{
496-
if (refresh)
496+
retValue.ClearAndInitialize(filter, extraProperties);
497+
List<DefaultConstraint> subFieldResult = new List<DefaultConstraint>();
498+
foreach(Column field in retValue)
497499
{
498-
parentColumn.DefaultConstraint.Refresh();
500+
DefaultConstraint subField = field.DefaultConstraint;
501+
if (subField != null)
502+
{
503+
subFieldResult.Add(subField);
504+
}
499505
}
500-
return new SqlSmoObject[] { retValue };
506+
return subFieldResult.Where(c => PassesFinalFilters(parentTable, c));
507+
}
508+
}
509+
UserDefinedTableType parentUserDefinedTableType = context.Parent as UserDefinedTableType;
510+
if (parentUserDefinedTableType != null)
511+
{
512+
var retValue = parentUserDefinedTableType.Columns;
513+
if (retValue != null)
514+
{
515+
retValue.ClearAndInitialize(filter, extraProperties);
516+
List<DefaultConstraint> subFieldResult = new List<DefaultConstraint>();
517+
foreach(Column field in retValue)
518+
{
519+
DefaultConstraint subField = field.DefaultConstraint;
520+
if (subField != null)
521+
{
522+
subFieldResult.Add(subField);
523+
}
524+
}
525+
return subFieldResult.Where(c => PassesFinalFilters(parentUserDefinedTableType, c));
501526
}
502527
}
503528
return Enumerable.Empty<SqlSmoObject>();

src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModel.tt

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
7171
WriteLine("{");
7272
PushIndent(indent);
7373

74-
string navigationPath = GetNavigationPath(nodeElement, xmlFile, nodeName, parentType);
74+
XmlElement navPathElement = GetNavPathElement(xmlFile, nodeName, parentType);
75+
string navigationPath = GetNavigationPath(nodeElement, nodeName, navPathElement);
76+
string subField = GetNavPathAttribute(navPathElement, "SubField");
77+
string fieldType = GetNavPathAttribute(navPathElement, "FieldType");
7578

7679

7780
WriteLine(string.Format("var retValue = {0}.{1};", parentVar, navigationPath));
@@ -83,7 +86,27 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
8386
if (IsCollection(nodeElement))
8487
{
8588
WriteLine(string.Format("retValue.ClearAndInitialize(filter, extraProperties);"));
86-
WriteLine(string.Format("return new SmoCollectionWrapper<{0}>(retValue).Where(c => PassesFinalFilters({1}, c));", nodeType, parentVar));
89+
if (string.IsNullOrEmpty(subField) )
90+
{
91+
WriteLine(string.Format("return new SmoCollectionWrapper<{0}>(retValue).Where(c => PassesFinalFilters({1}, c));", nodeType, parentVar));
92+
}
93+
else
94+
{
95+
WriteLine(string.Format("List<{0}> subFieldResult = new List<{0}>();", nodeType));
96+
WriteLine(string.Format("foreach({0} field in retValue)", fieldType));
97+
WriteLine("{");
98+
PushIndent(indent);
99+
WriteLine(string.Format("{0} subField = field.{1};", nodeType, subField));
100+
WriteLine(string.Format("if (subField != null)"));
101+
WriteLine("{");
102+
PushIndent(indent);
103+
WriteLine(string.Format("subFieldResult.Add(subField);"));
104+
PopIndent();
105+
WriteLine("}");
106+
PopIndent();
107+
WriteLine("}");
108+
WriteLine(string.Format("return subFieldResult.Where(c => PassesFinalFilters({1}, c));", nodeType, parentVar));
109+
}
87110
}
88111
else
89112
{
@@ -142,27 +165,23 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel
142165
return (XmlElement)doc.SelectSingleNode(string.Format("/SmoQueryModel/Node[@Name='{0}']", nodeName));
143166
}
144167

145-
public static string GetNavPathField(string xmlFile, string nodeName, string parent)
168+
public static XmlElement GetNavPathElement(string xmlFile, string nodeName, string parent)
146169
{
147170
XmlDocument doc = new XmlDocument();
148171
doc.Load(xmlFile);
149172
XmlElement navPathElement = (XmlElement)doc.SelectSingleNode(string.Format("/SmoQueryModel/Node[@Name='{0}']/NavigationPath[@Parent='{1}']", nodeName, parent));
150173

151-
return navPathElement == null ? null : navPathElement.GetAttribute("Field");
174+
return navPathElement;
152175
}
153176

154-
public static string GetNavPathFieldForUrn(string xmlFile, string nodeName, string parent)
177+
public static string GetNavPathAttribute(XmlElement navPathElement, string attributeName)
155178
{
156-
XmlDocument doc = new XmlDocument();
157-
doc.Load(xmlFile);
158-
XmlElement navPathElement = (XmlElement)doc.SelectSingleNode(string.Format("/SmoQueryModel/Node[@Name='{0}']/NavigationPath[@Parent='{1}']", nodeName, parent));
159-
160-
return navPathElement == null ? null : navPathElement.GetAttribute("FieldForUrn");
179+
return navPathElement == null ? null : navPathElement.GetAttribute(attributeName);
161180
}
162181

163-
public static string GetNavigationPath(XmlElement nodeElement, string xmlFile, string nodeName, string parentName)
182+
public static string GetNavigationPath(XmlElement nodeElement, string nodeName, XmlElement navPathElement)
164183
{
165-
string navPathField = GetNavPathField(xmlFile, nodeName, parentName);
184+
string navPathField = GetNavPathAttribute(navPathElement, "Field");
166185
if (!string.IsNullOrEmpty(navPathField))
167186
{
168187
return navPathField;

src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryModelDefinition.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
<Node Name="SqlTable" Parent="Database" />
3939
<Node Name="SqlHistoryTable" Type="Table" Parent="Table" >
40-
<NavigationPath Parent="Table" Type="Table" Field="Parent.Tables" FieldForUrn="Parent" />
40+
<NavigationPath Parent="Table" Type="Table" Field="Parent.Tables" />
4141
</Node>
4242

4343
<Node Name="SqlView" Parent="Database" />
@@ -51,7 +51,12 @@
5151

5252
<Node Name="SqlCheck" Parent="Table"/>
5353
<Node Name="SqlForeignKeyConstraint" Type="ForeignKey" Parent="Table" />
54-
<Node Name="SqlDefaultConstraint" Parent="Column" Collection="False" />
54+
<Node Name="SqlDefaultConstraint" Collection="True" >
55+
<Parent>Table</Parent>
56+
<Parent>UserDefinedTableType</Parent>
57+
<NavigationPath Parent="Table" Field="Columns" SubField="DefaultConstraint" FieldType="Column" />
58+
<NavigationPath Parent="UserDefinedTableType" Field="Columns" SubField="DefaultConstraint" FieldType="Column" />
59+
</Node>
5560
<Node Name="SqlDmlTrigger" Type="Trigger" Parent="Table" ValidFor="NotSqlDw" />
5661
<Node Name="SqlFullTextIndex" Parent="Table" Collection="False" ValidFor="NotSqlDw" />
5762
<Node Name="SqlStatistic" Parent="TableViewBase"/>

test/Microsoft.SqlTools.ServiceLayer.Test.Common/TestData/ObjectExplorer/Baselines/AllSqlObjects.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ NodeType: Constraint Label: CK_Employee_HireDate SubType: Status:
4141
NodeType: Constraint Label: CK_Employee_MaritalStatus SubType: Status:
4242
NodeType: Constraint Label: CK_Employee_SickLeaveHours SubType: Status:
4343
NodeType: Constraint Label: CK_Employee_VacationHours SubType: Status:
44+
NodeType: Constraint Label: DF_Employee_SalariedFlag SubType: Status:
45+
NodeType: Constraint Label: DF_Employee_VacationHours SubType: Status:
46+
NodeType: Constraint Label: DF_Employee_SickLeaveHours SubType: Status:
47+
NodeType: Constraint Label: DF_Employee_CurrentFlag SubType: Status:
48+
NodeType: Constraint Label: DF_Employee_rowguid SubType: Status:
49+
NodeType: Constraint Label: DF_Employee_ModifiedDate SubType: Status:
4450
NodeType: Index Label: NonClusteredIndex-Login (Non-Unique, Non-Clustered) SubType: Status:
4551
NodeType: Index Label: PK_Employee_BusinessEntityID (Unique, Clustered) SubType:PrimaryKey Status:
4652
NodeType: Statistic Label: NonClusteredIndex-Login SubType: Status:
@@ -94,6 +100,10 @@ NodeType: Column Label: ModifiedDate (datetime, not null) SubType: Status:
94100
NodeType: Key Label: PK_Person_BusinessEntityID SubType:PrimaryKey Status:
95101
NodeType: Constraint Label: CK_Person_EmailPromotion SubType: Status:
96102
NodeType: Constraint Label: CK_Person_PersonType SubType: Status:
103+
NodeType: Constraint Label: DF_Person_NameStyle SubType: Status:
104+
NodeType: Constraint Label: DF_Person_EmailPromotion SubType: Status:
105+
NodeType: Constraint Label: DF_Person_rowguid SubType: Status:
106+
NodeType: Constraint Label: DF_Person_ModifiedDate SubType: Status:
97107
NodeType: Trigger Label: TableTrigger SubType: Status:
98108
NodeType: Index Label: PK_Person_BusinessEntityID (Unique, Clustered) SubType:PrimaryKey Status:
99109
NodeType: Statistic Label: PK_Person_BusinessEntityID SubType: Status:

0 commit comments

Comments
 (0)