Skip to content

Commit

Permalink
Community Tools - Object context
Browse files Browse the repository at this point in the history
Allow community tools to be displayed at object level, passing in the object/schema name.
  • Loading branch information
DavidWiseman committed Dec 8, 2024
1 parent 4386daa commit bdd0531
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
14 changes: 12 additions & 2 deletions DBADashGUI/CommunityTools/CommunityTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ internal static class CommunityTools
URL = FirstResponderKitUrl,
Description = "List the most resource-intensive queries from the plan cache",
DatabaseNameParameter = "@DatabaseName",
ObjectParameterName = "@StoredProcName",
Params = new Params
{
ParamList = new List<Param>
Expand Down Expand Up @@ -1561,6 +1562,8 @@ internal static class CommunityTools
CancellationMessageWarning =
"Cancellation of this report will leave an extended event session running that will require cleanup.",
DatabaseNameParameter = "@database_name",
ObjectParameterName = "@object_name",
SchemaParameterName = "@object_schema",
Params = new Params()
{
ParamList = new List<Param>
Expand Down Expand Up @@ -2140,6 +2143,8 @@ internal static class CommunityTools
URL = ErikDarlingUrl,
Description = "Query store analysis",
DatabaseNameParameter = "@database_name",
ObjectParameterName = "@procedure_name",
SchemaParameterName = "@procedure_schema",
Params = new Params()
{
ParamList = new()
Expand Down Expand Up @@ -2313,6 +2318,7 @@ internal static class CommunityTools
URL = ErikDarlingUrl,
Description = "Procedure for parsing sqlserver.blocked_process_report extended event",
DatabaseNameParameter = "@database_name",
ObjectParameterName = "@object_name",
Params = new Params()
{
ParamList = new()
Expand Down Expand Up @@ -2541,6 +2547,7 @@ internal static class CommunityTools
URL = "https://github.com/sqlstudent144/SQL-Server-Scripts/blob/main/sp_DBPermissions.sql",
Description = "Database principals,role membership and permissions",
DatabaseNameParameter = "@DBName",
ObjectParameterName = "@ObjectName",
Params = new Params()
{
ParamList = new()
Expand Down Expand Up @@ -2667,15 +2674,15 @@ internal static class CommunityTools
"RevokeScript",
new TextLinkColumnInfo()
{
TargetColumn = "DropScript",
TargetColumn = "RevokeScript",
TextHandling = CodeEditor.CodeEditorModes.SQL
}
},
{
"GrantScript",
new TextLinkColumnInfo()
{
TargetColumn = "AddScript",
TargetColumn = "GrantScript",
TextHandling = CodeEditor.CodeEditorModes.SQL
}
}
Expand Down Expand Up @@ -2705,6 +2712,9 @@ internal static class CommunityTools
sp_DBPermissions
};

public static List<DirectExecutionReport> ProcedureLevelTools =>
CommunityToolsList.Where(t => !string.IsNullOrEmpty(t.ObjectParameterName)).OrderBy(t => t.ProcedureName).ToList();

public static List<DirectExecutionReport> DatabaseLevelCommunityTools =>
CommunityToolsList.Where(x => x.DatabaseNameParameter != null).ToList();
}
Expand Down
2 changes: 2 additions & 0 deletions DBADashGUI/CommunityTools/DirectExecutionReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
internal class DirectExecutionReport : CustomReports.CustomReport
{
public string DatabaseNameParameter { get; set; }
public string ObjectParameterName { get; set; }
public string SchemaParameterName { get; set; }
}
}
7 changes: 5 additions & 2 deletions DBADashGUI/CustomReports/CustomReportView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,8 +1048,11 @@ private void SetContextParametersForDirectExecutionReport() // Set DatabaseName

if (context.ObjectID > 0)
{
var objectParamName = string.IsNullOrEmpty(dxReport.ObjectParameterName) ? "@ObjectName" : dxReport.ObjectParameterName;
var schemaParamName = string.IsNullOrEmpty(dxReport.ObjectParameterName) ? "@SchemaName" : dxReport.ObjectParameterName;

var pObjectName = customParams.FirstOrDefault(p =>
p.Param.ParameterName.Equals("@ObjectName", StringComparison.InvariantCultureIgnoreCase) &&
p.Param.ParameterName.Equals(objectParamName, StringComparison.InvariantCultureIgnoreCase) &&
p.UseDefaultValue);
if (pObjectName != null && !string.IsNullOrEmpty(context.ObjectName))
{
Expand All @@ -1068,7 +1071,7 @@ private void SetContextParametersForDirectExecutionReport() // Set DatabaseName
}

var pSchemaName = customParams.FirstOrDefault(p =>
p.Param.ParameterName.Equals("@SchemaName", StringComparison.InvariantCultureIgnoreCase) &&
p.Param.ParameterName.Equals(schemaParamName, StringComparison.InvariantCultureIgnoreCase) &&
p.UseDefaultValue);
if (pSchemaName != null && !string.IsNullOrEmpty(context.ObjectName))
{
Expand Down
33 changes: 29 additions & 4 deletions DBADashGUI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -848,19 +849,43 @@ private List<TabPage> GetAllowedTabs()
case SQLTreeItem.TreeType.StoredProcedure or SQLTreeItem.TreeType.CLRProcedure
or SQLTreeItem.TreeType.ScalarFunction or SQLTreeItem.TreeType.CLRScalarFunction
or SQLTreeItem.TreeType.Trigger or SQLTreeItem.TreeType.CLRTrigger:
allowedTabs.AddRange(new[] { tabObjectExecutionSummary, tabPerformance });
allowedTabs.AddRange(new[] { tabObjectExecutionSummary, tabPerformance, tabSchema });

foreach (var tool in CommunityTools.CommunityTools.ProcedureLevelTools)
{
if (!Enum.TryParse<ProcedureExecutionMessage.CommandNames>(tool.ProcedureName,
out var proc)) continue;
if (!n.Context.IsScriptAllowed(ProcedureExecutionMessage.CommandNames.sp_QuickieStore))
continue;

allowedTabs.Add(CommunityToolsTabPages[proc]);
}

break;

case SQLTreeItem.TreeType.Table:
allowedTabs.Add(tabTableSize);
allowedTabs.AddRange(new[] { tabTableSize, tabSchema });

if (n.Context.IsScriptAllowed(ProcedureExecutionMessage.CommandNames.sp_BlitzIndex))
{
allowedTabs.Add(tabBlitzIndex);
}

if (n.Context.IsScriptAllowed(ProcedureExecutionMessage.CommandNames.sp_DBPermissions))
{
allowedTabs.Add(CommunityToolsTabPages[ProcedureExecutionMessage.CommandNames.sp_DBPermissions]);
}

break;
}

allowedTabs.Add(tabSchema);
default:
allowedTabs.Add(tabSchema);
if (n.Context.IsScriptAllowed(ProcedureExecutionMessage.CommandNames.sp_DBPermissions))
{
allowedTabs.Add(CommunityToolsTabPages[ProcedureExecutionMessage.CommandNames.sp_DBPermissions]);
}
break;
}
}

if (allowedTabs.Contains(tabSlowQueries) && n.Context.ProductVersion?.Major < 10)
Expand Down

0 comments on commit bdd0531

Please sign in to comment.