Skip to content

Commit

Permalink
Adding new folder for SimpleOE for external tables (#2389)
Browse files Browse the repository at this point in the history
* Add Shortcut folder for external tables

* Fix type
  • Loading branch information
mykolian committed Sep 12, 2024
1 parent 4646cd3 commit c2f0d61
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 25 deletions.
11 changes: 11 additions & 0 deletions src/Microsoft.SqlTools.SqlCore/Localization/sr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,14 @@ public static string SchemaHierarchy_ColumnSetLabelWithTypeAndKeyString
}
}

public static string SchemaHierarchy_Shortcuts
{
get
{
return Keys.GetString(Keys.SchemaHierarchy_Shortcuts);
}
}

public static string UniqueIndex_LabelPart
{
get
Expand Down Expand Up @@ -3449,6 +3457,9 @@ public class Keys
public const string SchemaHierarchy_ColumnSetLabelWithTypeAndKeyString = "SchemaHierarchy_ColumnSetLabelWithTypeAndKeyString";


public const string SchemaHierarchy_Shortcuts = "SchemaHierarchy_Shortcuts";


public const string UniqueIndex_LabelPart = "UniqueIndex_LabelPart";


Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.SqlTools.SqlCore/Localization/sr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,10 @@
<value>{0} (Column Set, {1}, {2}, {3})</value>
<comment></comment>
</data>
<data name="SchemaHierarchy_Shortcuts" xml:space="preserve">
<value>Shortcuts</value>
<comment></comment>
</data>
<data name="UniqueIndex_LabelPart" xml:space="preserve">
<value>Unique</value>
<comment></comment>
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.SqlTools.SqlCore/Localization/sr.strings
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ SchemaHierarchy_ColumnSetLabelWithType = {0} (Column Set, {1}{2}, {3})

SchemaHierarchy_ColumnSetLabelWithTypeAndKeyString = {0} (Column Set, {1}, {2}, {3})

SchemaHierarchy_Shortcuts = Shortcuts

UniqueIndex_LabelPart = Unique

NonUniqueIndex_LabelPart = Non-Unique
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.SqlTools.SqlCore/Localization/sr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,11 @@
<note>.
Parameters: 0 - table (string) </note>
</trans-unit>
<trans-unit id="SchemaHierarchy_Shortcuts">
<source>Shortcuts</source>
<target state="new">Shortcuts</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class ObjectExplorer
{
public static async Task<TreeNode> GetObjectExplorerModel(SqlConnection connection, bool enableRetry = true)
{
ObjectMetadata[] metdata = await FetchObjectExplorerMetadataTable(connection, enableRetry);
ObjectMetadata[] metadata = await FetchObjectExplorerMetadataTable(connection, enableRetry);
TreeNode root = new DatabaseNode(null, new ObjectMetadata() { Name = connection.Database, Type = "Database", DisplayName = connection.Database });
// Load all the children
Stack<TreeNode> stack = new Stack<TreeNode>();
Expand All @@ -28,7 +28,7 @@ public static async Task<TreeNode> GetObjectExplorerModel(SqlConnection connecti
{
continue;
}
currentNode.LoadChildren(metdata);
currentNode.LoadChildren(metadata);
if (currentNode.Children != null)
{
foreach (TreeNode child in currentNode.Children)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public override void LoadChildren(ObjectMetadata[] metadata)
Children.Add(new ViewsFolder(this));
Children.Add(new StoredProceduresFolder(this));
Children.Add(new FunctionsFolder(this));
Children.Add(new ShortcutsFolder(this));
}
}
/// <summary>
Expand Down Expand Up @@ -199,6 +200,24 @@ public override void LoadChildren(ObjectMetadata[] metadata)
Children.Add(new ParametersFolder(this));
}
}
/// <summary>
/// Shortcut Node
/// </summary>
public class ShortcutNode : TreeNode
{
public ShortcutNode(TreeNode parent, ObjectMetadata metadata) : base(parent, metadata, false)
{
Type = NodeTypes.Shortcut;
IsLeaf = false;
ScriptingObject.Type = "Shortcut";
}

public override void LoadChildren(ObjectMetadata[] metadata)
{
this.Children = new List<TreeNode>();
Children.Add(new ColumnsFolder(this));
}
}
public class TablesFolder : FolderNode
{
public TablesFolder(TreeNode parent) : base(parent)
Expand Down Expand Up @@ -392,6 +411,28 @@ public override void LoadChildren(ObjectMetadata[] metadata)
}
}
}
public class ShortcutsFolder : FolderNode
{
public ShortcutsFolder(TreeNode parent) : base(parent)
{
Name = "Shortcuts";
Type = NodeTypes.Shortcuts;
IsLeaf = false;
Label = SR.SchemaHierarchy_Shortcuts;
}

public override void LoadChildren(ObjectMetadata[] metadata)
{
this.Children = new List<TreeNode>();
foreach(ObjectMetadata child in metadata)
{
if (child.Type == "Shortcut" && child.Parent == this.Parent.Name && child.Schema == this.SchemaName)
{
Children.Add(new ShortcutNode(this, child));
}
}
}
}

public static class ObjectExplorerModelQueries
{
Expand Down Expand Up @@ -429,17 +470,17 @@ SCHEMA_NAME NOT IN (
{
"Table",
@"
SELECT
TABLE_SCHEMA AS schema_name,
TABLE_NAME AS object_name,
TABLE_SCHEMA AS parent_name,
TABLE_NAME AS display_name,
'Table' AS object_type,
NULL AS object_sub_type
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
SELECT
s.name AS schema_name,
t.name AS object_name,
s.name AS parent_name,
t.name AS display_name,
'Table' AS object_type,
NULL AS object_sub_type
FROM
sys.tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.is_external = 0
"
},
{
Expand Down Expand Up @@ -603,6 +644,22 @@ sys.schemas AS S
JOIN sys.objects AS P ON S.schema_id = P.schema_id
WHERE
P.type = 'IF' OR P.type = 'TF'
"
},
{
"Shortcut",
@"
SELECT
s.name AS schema_name,
t.name AS object_name,
s.name AS parent_name,
t.name AS display_name,
'Shortcut' AS object_type,
NULL AS object_sub_type
FROM
sys.tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.is_external = 1
"
},
};
Expand All @@ -620,6 +677,7 @@ public enum NodeTypes
Param,
ScalarFunction,
TableValuedFunction,
Shortcut,
Tables,
Columns,
Indexes,
Expand All @@ -629,6 +687,7 @@ public enum NodeTypes
Functions,
ScalarFunctions,
TableValuedFunctions,
Shortcuts,
Folder,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<ChildFolder Name="Views" />
<ChildFolder Name="StoredProcedures" />
<ChildFolder Name="Functions"/>
<ChildFolder Name="Shortcuts"/>
</Node>

<!-- Tables folder definition-->
Expand All @@ -88,17 +89,17 @@

<!-- Table Query -->
<Querier NodeType="Table" Query="
SELECT
TABLE_SCHEMA AS schema_name,
TABLE_NAME AS object_name,
TABLE_SCHEMA AS parent_name,
TABLE_NAME AS display_name,
'Table' AS object_type,
NULL AS object_sub_type
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
SELECT
s.name AS schema_name,
t.name AS object_name,
s.name AS parent_name,
t.name AS display_name,
'Table' AS object_type,
NULL AS object_sub_type
FROM
sys.tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.is_external = 0
" />

<!-- Table Node -->
Expand Down Expand Up @@ -336,4 +337,29 @@
<Node Name="TableValuedFunction" Type="TableValuedFunction" IsLeaf="false" ScriptingObjectType="UserDefinedFunction">
<ChildFolder Name="Parameters" />
</Node>

<!-- Shortcuts folder definition-->
<Folder Name="Shortcuts" Label="SR.SchemaHierarchy_Shortcuts" ParentName = "Parent.Name">
<ChildQuerier NodeType="Shortcut" />
</Folder>

<!-- Shortcut Query -->
<Querier NodeType="Shortcut" Query="
SELECT
s.name AS schema_name,
t.name AS object_name,
s.name AS parent_name,
t.name AS display_name,
'Shortcut' AS object_type,
NULL AS object_sub_type
FROM
sys.tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.is_external = 1
" />

<!-- Shortcut Node -->
<Node Name="Shortcut" Type="Shortcut" IsLeaf="false">
<ChildFolder Name="Columns" />
</Node>
</ObjectExplorerModel>
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ await RunTest(databaseName, query, "testdb", async (testdbName, connection) =>
// Expand dbo schema node
nodes = OE.GetNodeChildrenFromPath(oeRoot, "/dbo/");
Assert.AreEqual(4, nodes.Length, "dbo schema node should have 4 folders");
Assert.AreEqual(5, nodes.Length, "dbo schema node should have 5 folders");
Assert.IsNotNull(nodes.Find(node => node.Name == "Shortcuts"), "dbo schema should have Shortcuts folder");
// Expand Tables folder
nodes = OE.GetNodeChildrenFromPath(oeRoot, "/dbo/Tables/");
Expand Down Expand Up @@ -124,6 +125,9 @@ await RunTest(databaseName, query, "testdb", async (testdbName, connection) =>
Assert.IsNotNull(nodes.Find(node => node.Name == "c2"), "Column c2 should exist");
Assert.IsNotNull(nodes.Find(node => node.Label == "c2 (datetime2(7), null)"), "Display Name for a c2 should have datetime 2 and null");
nodes = OE.GetNodeChildrenFromPath(oeRoot, "/dbo/Shortcuts/");
Assert.AreEqual(0, nodes.Length, "Should have no records under existing Shortcut folder yet");
// Expand Views folder
nodes = OE.GetNodeChildrenFromPath(oeRoot, "/dbo/Views/");
Assert.AreEqual(1, nodes.Length, "Views folder should have 1 view");
Expand Down

0 comments on commit c2f0d61

Please sign in to comment.