Skip to content

Commit

Permalink
Improve InlineFunctionSqlCodeAnalysisRule
Browse files Browse the repository at this point in the history
  • Loading branch information
C0nquistadore committed Apr 21, 2022
1 parent 6bb1059 commit b7432b8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public override void Visit(CreateFunctionStatement node)

public override void Visit(CheckConstraintDefinition node)
{
// CONSTRAINT [CK_dbx_codeanalysis_error_037_table_x] CHECK ([dbo].[dbx_codeanalysis_error_037_scalar]() = 0)
ScalarFunctionCallVisitor visitor = new ScalarFunctionCallVisitor(this.IsScalarFunctionCall);
node.CheckCondition.Accept(visitor);
foreach (FunctionCall call in visitor.Locations)
Expand All @@ -51,39 +52,62 @@ public override void Visit(CheckConstraintDefinition node)

public override void Visit(DeclareVariableElement node)
{
// DECLARE @y INT = [dbo].[dbx_codeanalysis_error_037_scalar]()
if (node.Value is FunctionCall call)
{
VisitFunctionCall(call);
return;
}

// DECLARE @x INT = (SELECT [dbo].[dbx_codeanalysis_error_037_scalar]())
if (node.Value is ScalarSubquery scalarSubquery
&& scalarSubquery.QueryExpression is QuerySpecification querySpecification)
this.VisitQuerySpecification(querySpecification);
}

public override void Visit(SelectStatement node)
{
// SELECT @x = [dbo].[dbx_codeanalysis_error_037_scalar]()
// and
// SELECT [dbo].[dbx_codeanalysis_error_037_scalar]()
if (node.QueryExpression is QuerySpecification querySpecification)
this.VisitQuerySpecification(querySpecification);
}

public override void Visit(SetVariableStatement node)
{
TSqlFragment target = node?.Expression;
if (target == null)
return;

if (this._scalarFunctionCalls.ContainsKey(target.StartOffset))
this._scalarFunctionCalls.Remove(target.StartOffset);
// SET @x = [dbo].[dbx_codeanalysis_error_037_scalar]()
if (node?.Expression is FunctionCall call)
VisitFunctionCall(call);
}

private void VisitQuerySpecification(QuerySpecification node)
{
if (node.SelectElements.Count == 1
&& node.FromClause == null)
if (node.SelectElements.Count != 1 || node.FromClause != null)
return;

TSqlFragment target = node.SelectElements[0];
switch (target)
{
TSqlFragment target = node.SelectElements[0];
if (target is SelectSetVariable selectSetVariable)
// SELECT @x = [dbo].[dbx_codeanalysis_error_037_scalar]()
case SelectSetVariable selectSetVariable:
target = selectSetVariable.Expression;
break;

if (this._scalarFunctionCalls.ContainsKey(target.StartOffset))
this._scalarFunctionCalls.Remove(target.StartOffset);
// DECLARE @x INT = (SELECT [dbo].[dbx_codeanalysis_error_037_scalar]())
case SelectScalarExpression selectScalarExpression:
target = selectScalarExpression.Expression;
break;
}

if (target is FunctionCall call)
this.VisitFunctionCall(call);
}

private void VisitFunctionCall(FunctionCall call)
{
if (this._scalarFunctionCalls.ContainsKey(call.StartOffset) && IsScalarFunctionCall(call))
this._scalarFunctionCalls.Remove(call.StartOffset);
}

private bool IsScalarFunctionCall(FunctionCall call) => base.Model.IsScalarFunction(call);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ AS
BEGIN

DECLARE @x INT = (SELECT [dbo].[dbx_codeanalysis_error_037_scalar]()) -- OK
DECLARE @y INT = [dbo].[dbx_codeanalysis_error_037_scalar]() -- OK
SELECT @x = [dbo].[dbx_codeanalysis_error_037_scalar]() -- OK
SET @x = [dbo].[dbx_codeanalysis_error_037_scalar]() -- OK
SELECT [dbo].[dbx_codeanalysis_error_037_scalar]() -- OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<error
ruleid="37"
message="Scalar functions should only be used for assignments or check constraints. Otherwise replace it with an inline table-valued function: dbx_codeanalysis_error_037_scalar"
line="40"
line="41"
column="16" />
<error
ruleid="37"
message="Scalar functions should only be used for assignments or check constraints. Otherwise replace it with an inline table-valued function: dbx_codeanalysis_error_037_scalar"
line="42"
line="43"
column="18" />
</errors>

0 comments on commit b7432b8

Please sign in to comment.