Skip to content

Commit 1c2b44f

Browse files
authored
Mark database OE nodes as leaf nodes if they are unavailable (#569)
1 parent eacc011 commit 1c2b44f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,52 @@ public override string GetNodeStatus(object smoObject, SmoQueryContext smoContex
1818
{
1919
return DatabasesCustomNodeHelper.GetStatus(smoObject, smoContext, CachedSmoProperties);
2020
}
21+
22+
protected override void InitializeChild(TreeNode parent, TreeNode child, object context)
23+
{
24+
base.InitializeChild(parent, child, context);
25+
var smoTreeNode = child as SmoTreeNode;
26+
if (smoTreeNode != null && smoTreeNode.SmoObject != null
27+
&& DatabasesCustomNodeHelper.GetDatabaseIsUnavailable(smoTreeNode.SmoObject, parent.GetContextAs<SmoQueryContext>(), CachedSmoProperties))
28+
{
29+
child.IsAlwaysLeaf = true;
30+
}
31+
}
2132
}
2233

2334
internal static class DatabasesCustomNodeHelper
2435
{
36+
private static readonly DatabaseStatus[] UnavailableDatabaseStatuses = { DatabaseStatus.Inaccessible, DatabaseStatus.Offline, DatabaseStatus.Recovering,
37+
DatabaseStatus.RecoveryPending, DatabaseStatus.Restoring, DatabaseStatus.Suspect, DatabaseStatus.Shutdown };
38+
39+
internal static bool GetDatabaseIsUnavailable(object smoObject, SmoQueryContext smoContext, IEnumerable<NodeSmoProperty> supportedProperties)
40+
{
41+
Database db = smoObject as Database;
42+
if (db != null && SmoChildFactoryBase.IsPropertySupported("Status", smoContext, db, supportedProperties))
43+
{
44+
DatabaseStatus status;
45+
try
46+
{
47+
status = db.Status;
48+
}
49+
catch (SqlServer.Management.Common.ConnectionFailureException)
50+
{
51+
// We get into this situation with DW Nodes which are paused.
52+
return true;
53+
}
54+
55+
foreach (DatabaseStatus unavailableStatus in DatabasesCustomNodeHelper.UnavailableDatabaseStatuses)
56+
{
57+
if (status.HasFlag(unavailableStatus))
58+
{
59+
return true;
60+
}
61+
}
62+
}
63+
64+
return false;
65+
}
66+
2567
internal static string GetStatus(object smoObject, SmoQueryContext smoContext, IEnumerable<NodeSmoProperty> supportedProperties)
2668
{
2769
Database db = smoObject as Database;

0 commit comments

Comments
 (0)