-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds code analysis rule sample template #515
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Microsoft.Build.Sql.Templates/sqlcodeanalysis/.template.config/template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/template", | ||
"author": "Microsoft", | ||
"classifications": [ | ||
"Database", | ||
"SqlServer" | ||
], | ||
"identity": "Microsoft.Build.Sql.CodeAnalysis", | ||
"name": "SQL Server Database Code Analysis", | ||
"description": "A a .NET library project that contains the scaffolding for SQL Code Analysis", | ||
"shortName": "sqlcodeanalysis", | ||
"tags": { | ||
"language": "SQL", | ||
"type": "project" | ||
}, | ||
"sourceName": "SqlCodeAnalysis1", | ||
"preferNameDirectory": true, | ||
"sources": [ | ||
{ | ||
"source": "./", | ||
"target": "./", | ||
"include": ["SqlCodeAnalysis1.csproj", "SqlCodeAnalysis1.cs", "README.md"] | ||
} | ||
] | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Microsoft.Build.Sql.Templates/sqlcodeanalysis/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# New SQL code analysis rule for SQL projects | ||
|
||
## Build | ||
|
||
To build the code analysis project, run the following command: | ||
|
||
```bash | ||
dotnet build | ||
``` | ||
|
||
To package the code analysis project as a NuGet package for referencing in a SQL project, run the following command: | ||
|
||
```bash | ||
dotnet pack | ||
``` | ||
|
||
🎉 Congrats! You have successfully built the project and now have a NuGet package to reference in your SQL project. | ||
|
||
## Use the code analysis rule in SQL projects | ||
|
||
To reference the code analysis project in a SQL project, we need to complete 2 steps: | ||
1. Publish the code analysis project as a NuGet package. | ||
1. Reference the code analysis project in the SQL project. | ||
|
||
### Publish the code analysis project | ||
|
||
We packaged the code analysis project as a NuGet package in the previous step and will publish it to a remote feed or a [local source](https://learn.microsoft.com/dotnet/core/tools/dotnet-nuget-add-source) (folder). Add a folder as a local feed by running the following command: | ||
|
||
```bash | ||
dotnet nuget add source c:\packages | ||
``` | ||
|
||
Copy the NuGet package from `bin/Release` to the local source folder. | ||
|
||
### Reference the code analysis project in the SQL project | ||
|
||
The following example demonstrates how to reference the code analysis project in a SQL project: | ||
|
||
```xml | ||
<ItemGroup> | ||
<PackageReference Include="Sample.WaitForDelay" Version="1.0.0" /> | ||
</ItemGroup> | ||
``` | ||
|
||
Set either the SQL project property `<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>` or run `dotnet build /p:RunSqlCodeAnalysis=True` to generate code analysis output in the build log. |
150 changes: 150 additions & 0 deletions
150
src/Microsoft.Build.Sql.Templates/sqlcodeanalysis/SqlCodeAnalysis1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Microsoft.SqlServer.Dac.CodeAnalysis; | ||
using Microsoft.SqlServer.Dac.Model; | ||
using Microsoft.SqlServer.TransactSql.ScriptDom; | ||
|
||
namespace Sample.SqlCodeAnalysis1 { | ||
/// <summary> | ||
/// This is a rule that returns a warning message | ||
/// whenever there is a WAITFOR DELAY statement appears inside a subroutine body. | ||
/// This rule only applies to stored procedures, functions and triggers. | ||
/// </summary> | ||
[ExportCodeAnalysisRule( | ||
id: RuleId, | ||
displayName: RuleName, | ||
Description = ProblemDescription, | ||
Category = RuleCategory, | ||
RuleScope = SqlRuleScope.Element)] | ||
public sealed class AvoidWaitForDelayRule : SqlCodeAnalysisRule | ||
{ | ||
/// <summary> | ||
/// The Rule ID should resemble a fully-qualified class name. In the Visual Studio UI | ||
/// rules are grouped by "Namespace + Category", and each rule is shown using "Short ID: DisplayName". | ||
/// For this rule, that means the grouping will be "Public.Dac.Samples.Performance", with the rule | ||
/// shown as "SR1004: Avoid using WaitFor Delay statements in stored procedures, functions and triggers." | ||
/// </summary> | ||
public const string RuleId = "Sample.SqlCodeAnalysis1.SSCA1004"; | ||
public const string RuleName = "Avoid using WaitFor Delay statements in stored procedures, functions and triggers."; | ||
public const string ProblemDescription = "Avoid using WAITFOR DELAY in {0}"; | ||
public const string RuleCategory = "Performance"; | ||
|
||
public AvoidWaitForDelayRule() | ||
{ | ||
// This rule supports Procedures, Functions and Triggers. Only those objects will be passed to the Analyze method | ||
SupportedElementTypes = new[] | ||
{ | ||
// Note: can use the ModelSchema definitions, or access the TypeClass for any of these types | ||
ModelSchema.ExtendedProcedure, | ||
ModelSchema.Procedure, | ||
ModelSchema.TableValuedFunction, | ||
ModelSchema.ScalarFunction, | ||
ModelSchema.DatabaseDdlTrigger, | ||
ModelSchema.DmlTrigger, | ||
ModelSchema.ServerDdlTrigger, | ||
}; | ||
} | ||
|
||
/// <summary> | ||
/// For element-scoped rules the Analyze method is executed once for every matching | ||
/// object in the model. | ||
/// </summary> | ||
/// <param name="ruleExecutionContext">The context object contains the TSqlObject being | ||
/// analyzed, a TSqlFragment | ||
/// that's the AST representation of the object, the current rule's descriptor, and a | ||
/// reference to the model being | ||
/// analyzed. | ||
/// </param> | ||
/// <returns>A list of problems should be returned. These will be displayed in the Visual | ||
/// Studio error list</returns> | ||
public override IList<SqlRuleProblem> Analyze( | ||
SqlRuleExecutionContext ruleExecutionContext) | ||
{ | ||
var problems = new List<SqlRuleProblem>(); | ||
var modelElement = ruleExecutionContext.ModelElement; | ||
|
||
// this rule does not apply to inline table-valued function | ||
// we simply do not return any problem in that case. | ||
if (IsInlineTableValuedFunction(modelElement)) | ||
{ | ||
return problems; | ||
} | ||
|
||
var elementName = GetElementName(ruleExecutionContext, modelElement); | ||
|
||
// The rule execution context has all the objects we'll need, including the | ||
// fragment representing the object, | ||
// and a descriptor that lets us access rule metadata | ||
var fragment = ruleExecutionContext.ScriptFragment; | ||
var ruleDescriptor = ruleExecutionContext.RuleDescriptor; | ||
|
||
// To process the fragment and identify WAITFOR DELAY statements we will use a | ||
// visitor | ||
var visitor = new WaitForDelayVisitor(); | ||
fragment.Accept(visitor); | ||
var waitforDelayStatements = visitor.WaitForDelayStatements; | ||
|
||
// Create problems for each WAITFOR DELAY statement found | ||
// When creating a rule problem, always include the TSqlObject being analyzed. This | ||
// is used to determine | ||
// the name of the source this problem was found in and a best guess as to the | ||
// line/column the problem was found at. | ||
// | ||
// In addition if you have a specific TSqlFragment that is related to the problem | ||
// also include this | ||
// since the most accurate source position information (start line and column) will | ||
// be read from the fragment | ||
foreach (WaitForStatement waitForStatement in waitforDelayStatements) | ||
{ | ||
var problem = new SqlRuleProblem( | ||
string.Format(CultureInfo.InvariantCulture, ruleDescriptor.DisplayDescription, elementName), | ||
modelElement, | ||
waitForStatement); | ||
problems.Add(problem); | ||
} | ||
|
||
return problems; | ||
} | ||
|
||
private static string GetElementName( | ||
SqlRuleExecutionContext ruleExecutionContext, | ||
TSqlObject modelElement) | ||
{ | ||
// Get the element name using the built in DisplayServices. This provides a number of | ||
// useful formatting options to | ||
// make a name user-readable | ||
var displayServices = ruleExecutionContext.SchemaModel.DisplayServices; | ||
var elementName = displayServices.GetElementName( | ||
modelElement, ElementNameStyle.EscapedFullyQualifiedName); | ||
return elementName; | ||
} | ||
|
||
private static bool IsInlineTableValuedFunction(TSqlObject modelElement) | ||
{ | ||
return TableValuedFunction.TypeClass.Equals(modelElement.ObjectType) | ||
&& modelElement.GetMetadata<FunctionType>(TableValuedFunction.FunctionType) | ||
== FunctionType.InlineTableValuedFunction; | ||
} | ||
} | ||
|
||
internal class WaitForDelayVisitor : TSqlConcreteFragmentVisitor | ||
{ | ||
public IList<WaitForStatement> WaitForDelayStatements { get; private set; } | ||
|
||
// Define the class constructor | ||
public WaitForDelayVisitor() | ||
{ | ||
WaitForDelayStatements = new List<WaitForStatement>(); | ||
} | ||
|
||
public override void ExplicitVisit(WaitForStatement node) | ||
{ | ||
// We are only interested in WAITFOR DELAY occurrences | ||
if (node.WaitForOption == WaitForOption.Delay) | ||
{ | ||
WaitForDelayStatements.Add(node); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Microsoft.Build.Sql.Templates/sqlcodeanalysis/SqlCodeAnalysis1.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>Sample.SqlCodeAnalysis1</PackageId> | ||
<PackageVersion>1.0.0</PackageVersion> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="###DACFX_ASSEMBLY_VERSION###" PrivateAssets="All"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="bin\$(Configuration)\$(TargetFramework)\SqlCodeAnalysis1.dll" | ||
Pack="true" | ||
PackagePath="analyzers\dotnet\cs" | ||
Visible="false" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this could be simplified to $(TargetPath) ?