Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1efa6764d1 | ||
|
|
edc4d5c087 | ||
|
|
507c466b5d | ||
|
|
7fb11364c4 |
@@ -43,8 +43,9 @@ namespace SQLLinter.CLI
|
|||||||
["UpdateWhere"] = Common.RuleViolationSeverity.Critical,
|
["UpdateWhere"] = Common.RuleViolationSeverity.Critical,
|
||||||
["UpperLower"] = Common.RuleViolationSeverity.Critical,
|
["UpperLower"] = Common.RuleViolationSeverity.Critical,
|
||||||
["SetVariable"] = Common.RuleViolationSeverity.Critical,
|
["SetVariable"] = Common.RuleViolationSeverity.Critical,
|
||||||
|
["CreateProcedureInDbo"] = Common.RuleViolationSeverity.Warning,
|
||||||
},
|
},
|
||||||
GenerateDetails = true,
|
GenerateDetails = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
//var linter = new Linter(con, rep);
|
//var linter = new Linter(con, rep);
|
||||||
@@ -62,7 +63,7 @@ namespace SQLLinter.CLI
|
|||||||
|
|
||||||
Dictionary<string, Stream> files = new();
|
Dictionary<string, Stream> files = new();
|
||||||
|
|
||||||
for (int i = 0; i < 15; i++)
|
for (int i = 0; i < 5; i++)
|
||||||
{
|
{
|
||||||
files[name + i + ".sql"] = reader.BaseStream;
|
files[name + i + ".sql"] = reader.BaseStream;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,8 +83,12 @@ public abstract class BaseRuleVisitor : TSqlFragmentVisitor, IRule
|
|||||||
return string.Format(this.Text, param);
|
return string.Format(this.Text, param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private TSqlFragment? FindContextBlock(TSqlFragment node)
|
private TSqlFragment? FindContextBlock(TSqlFragment node)
|
||||||
{
|
{
|
||||||
|
if (_parents == null) return null;
|
||||||
|
return node;
|
||||||
|
|
||||||
var current = node;
|
var current = node;
|
||||||
|
|
||||||
while (current != null)
|
while (current != null)
|
||||||
@@ -139,7 +143,6 @@ public abstract class BaseRuleVisitor : TSqlFragmentVisitor, IRule
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Вспомогательные классы
|
// Вспомогательные классы
|
||||||
public class ExtractedBlock
|
public class ExtractedBlock
|
||||||
{
|
{
|
||||||
@@ -165,13 +168,13 @@ public abstract class BaseRuleVisitor : TSqlFragmentVisitor, IRule
|
|||||||
|
|
||||||
protected ExtractedBlock? ExtractBlock(TSqlFragment? node, TSqlFragment errorNode)
|
protected ExtractedBlock? ExtractBlock(TSqlFragment? node, TSqlFragment errorNode)
|
||||||
{
|
{
|
||||||
if (node == null || node.ScriptTokenStream == null)
|
if (node == null || errorNode.ScriptTokenStream == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var endLine = node.ScriptTokenStream.Where(t => t.Offset < node.StartOffset + node.FragmentLength).Max(t => t.Line) + 2;
|
var endLine = errorNode.ScriptTokenStream.Where(t => t.Offset < node.StartOffset + node.FragmentLength).Max(t => t.Line) + 2;
|
||||||
|
|
||||||
// 1. Получаем токены для блока
|
// 1. Получаем токены для блока
|
||||||
var tokens = node.ScriptTokenStream
|
var tokens = errorNode.ScriptTokenStream
|
||||||
.Where(t => t.Line >= node.StartLine - 2 &&
|
.Where(t => t.Line >= node.StartLine - 2 &&
|
||||||
t.Line <= endLine)
|
t.Line <= endLine)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|||||||
@@ -13,5 +13,6 @@ public interface IRule
|
|||||||
int DynamicSqlStartLine { get; set; }
|
int DynamicSqlStartLine { get; set; }
|
||||||
|
|
||||||
IEnumerable<Violation> Analyze(TSqlFragment fragment);
|
IEnumerable<Violation> Analyze(TSqlFragment fragment);
|
||||||
|
|
||||||
void SetParents(Dictionary<TSqlFragment, TSqlFragment?>? parents);
|
void SetParents(Dictionary<TSqlFragment, TSqlFragment?>? parents);
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,6 @@ using SQLLinter.Common;
|
|||||||
using SQLLinter.Core.Interfaces;
|
using SQLLinter.Core.Interfaces;
|
||||||
using SQLLinter.Infrastructure.Configuration.Overrides;
|
using SQLLinter.Infrastructure.Configuration.Overrides;
|
||||||
using SQLLinter.Infrastructure.Interfaces;
|
using SQLLinter.Infrastructure.Interfaces;
|
||||||
using SQLLinter.Infrastructure.Rules;
|
|
||||||
using SQLLinter.Infrastructure.Rules.RuleExceptions;
|
using SQLLinter.Infrastructure.Rules.RuleExceptions;
|
||||||
using SQLLinter.Infrastructure.Rules.RuleViolations;
|
using SQLLinter.Infrastructure.Rules.RuleViolations;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
@@ -39,7 +38,8 @@ public class SqlRuleVisitor : IRuleVisitor
|
|||||||
|
|
||||||
if (sqlFragment == null) return;
|
if (sqlFragment == null) return;
|
||||||
|
|
||||||
Dictionary<TSqlFragment, TSqlFragment?>? parentMap = generateDetails ? ParentMapBuilder.Build(sqlFragment) : null;
|
//Dictionary<TSqlFragment, TSqlFragment?>? parentMap = generateDetails ? ParentMapBuilder.Build(sqlFragment) : null;
|
||||||
|
Dictionary<TSqlFragment, TSqlFragment?>? parentMap = generateDetails ? new Dictionary<TSqlFragment, TSqlFragment?>() : null;
|
||||||
|
|
||||||
var ruleExceptions = ignoredRules as IRuleException[] ?? ignoredRules.ToArray();
|
var ruleExceptions = ignoredRules as IRuleException[] ?? ignoredRules.ToArray();
|
||||||
if (errors.Any())
|
if (errors.Any())
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ h3 {
|
|||||||
font-size: var(--font-size-sm);
|
font-size: var(--font-size-sm);
|
||||||
padding: var(--spacing-xs) var(--spacing-sm);
|
padding: var(--spacing-xs) var(--spacing-sm);
|
||||||
min-width: 24px;
|
min-width: 24px;
|
||||||
background-color: var(--color-background-neutral);
|
/*background-color: var(--color-background-neutral);*/
|
||||||
color: var(--color-text-secondary);
|
color: var(--color-text-secondary);
|
||||||
border-color: var(--color-border);
|
border-color: var(--color-border);
|
||||||
}
|
}
|
||||||
@@ -224,17 +224,17 @@ h3 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.critical:not(.empty) {
|
.critical:not(.empty) {
|
||||||
background-color: var(--color-critical-bg);
|
/*background-color: var(--color-critical-bg);*/
|
||||||
color: var(--color-critical);
|
color: var(--color-critical);
|
||||||
}
|
}
|
||||||
|
|
||||||
.warning:not(.empty) {
|
.warning:not(.empty) {
|
||||||
background-color: var(--color-warning-bg);
|
/*background-color: var(--color-warning-bg);*/
|
||||||
color: var(--color-warning);
|
color: var(--color-warning);
|
||||||
}
|
}
|
||||||
|
|
||||||
.info:not(.empty) {
|
.info:not(.empty) {
|
||||||
background-color: var(--color-info-bg);
|
/*background-color: var(--color-info-bg);*/
|
||||||
color: var(--color-info);
|
color: var(--color-info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -292,7 +292,7 @@ class ReportRenderer {
|
|||||||
<div class="severity-header">
|
<div class="severity-header">
|
||||||
<div class="severity-title">
|
<div class="severity-title">
|
||||||
<h2>${titles[severity]}</h2>
|
<h2>${titles[severity]}</h2>
|
||||||
<span class="severity-count">${violations.length}</span>
|
<span class="severity-count ${severitySections[severity]}">${violations.length}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="table-container">
|
<div class="table-container">
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class AlterProcedureInDboRule : BaseRuleVisitor
|
|||||||
{
|
{
|
||||||
if (node.ProcedureReference.Name.SchemaIdentifier?.Value.Equals("dbo", StringComparison.OrdinalIgnoreCase) == true)
|
if (node.ProcedureReference.Name.SchemaIdentifier?.Value.Equals("dbo", StringComparison.OrdinalIgnoreCase) == true)
|
||||||
{
|
{
|
||||||
AddViolation(node, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
AddViolation(node.ProcedureReference.Name.SchemaIdentifier, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,14 +17,20 @@ public class ConditionalBeginEndRule : BaseRuleVisitor, IRule
|
|||||||
|
|
||||||
public override void Visit(IfStatement node)
|
public override void Visit(IfStatement node)
|
||||||
{
|
{
|
||||||
if (node.ThenStatement is not BeginEndBlockStatement)
|
if (node.ThenStatement != null && node.ThenStatement is not BeginEndBlockStatement && node.ThenStatement is not TryCatchStatement)
|
||||||
{
|
{
|
||||||
AddViolation(node);
|
if (node.ThenStatement.StartLine != node.StartLine || node.ScriptTokenStream.Where(t => t.Offset <= node.ThenStatement.StartOffset + node.ThenStatement.FragmentLength).Max(t => t.Line) != node.StartLine)
|
||||||
|
{
|
||||||
|
AddViolation(node.ThenStatement);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.ElseStatement != null && node.ElseStatement is not BeginEndBlockStatement && node.ElseStatement is not IfStatement)
|
if (node.ElseStatement != null && node.ElseStatement is not BeginEndBlockStatement && node.ElseStatement is not TryCatchStatement && node.ElseStatement is not IfStatement)
|
||||||
{
|
{
|
||||||
AddViolation(Name, Text, GetLineNumber(node.ElseStatement), GetColumnNumber(node.ElseStatement));
|
if (node.ElseStatement.StartLine != node.StartLine || node.ScriptTokenStream.Where(t => t.Offset <= node.ElseStatement.StartOffset + node.ElseStatement.FragmentLength).Max(t => t.Line) != node.StartLine)
|
||||||
|
{
|
||||||
|
AddViolation(node.ElseStatement);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,14 +12,14 @@ public class CreateProcedureInDboRule : BaseRuleVisitor
|
|||||||
{
|
{
|
||||||
if (node.ProcedureReference.Name.SchemaIdentifier?.Value.Equals("dbo", StringComparison.OrdinalIgnoreCase) == true)
|
if (node.ProcedureReference.Name.SchemaIdentifier?.Value.Equals("dbo", StringComparison.OrdinalIgnoreCase) == true)
|
||||||
{
|
{
|
||||||
AddViolation(node, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
AddViolation(node.ProcedureReference.Name.SchemaIdentifier, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public override void Visit(CreateOrAlterProcedureStatement node)
|
public override void Visit(CreateOrAlterProcedureStatement node)
|
||||||
{
|
{
|
||||||
if (node.ProcedureReference.Name.SchemaIdentifier?.Value.Equals("dbo", StringComparison.OrdinalIgnoreCase) == true)
|
if (node.ProcedureReference.Name.SchemaIdentifier?.Value.Equals("dbo", StringComparison.OrdinalIgnoreCase) == true)
|
||||||
{
|
{
|
||||||
AddViolation(node, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
AddViolation(node.ProcedureReference.Name.SchemaIdentifier, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,6 @@ public class DeleteWhereRule : BaseRuleVisitor, IRule
|
|||||||
.Select(t => t.Text)
|
.Select(t => t.Text)
|
||||||
);
|
);
|
||||||
|
|
||||||
AddViolation(node, name);
|
AddViolation(node.Target, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,6 @@ public class ExecuteAsOwnerRule : BaseRuleVisitor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AddViolation(node, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
AddViolation(node.ProcedureReference.Name, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ public class HeaderCommentRule : BaseRuleVisitor
|
|||||||
|
|
||||||
public override void Visit(CreateOrAlterTriggerStatement node) => private_visit(node, SQLHelpers.ObjectGetFullName(node.Name));
|
public override void Visit(CreateOrAlterTriggerStatement node) => private_visit(node, SQLHelpers.ObjectGetFullName(node.Name));
|
||||||
|
|
||||||
public override void Visit(CreateViewStatement node) => private_visit(node, "");
|
public override void Visit(CreateViewStatement node) => private_visit(node, SQLHelpers.ObjectGetFullName(node.SchemaObjectName));
|
||||||
|
|
||||||
public override void Visit(CreateOrAlterViewStatement node) => private_visit(node, "");
|
public override void Visit(CreateOrAlterViewStatement node) => private_visit(node, SQLHelpers.ObjectGetFullName(node.SchemaObjectName));
|
||||||
|
|
||||||
private void private_visit(TSqlFragment node, string name)
|
private void private_visit(TSqlFragment node, string name)
|
||||||
{
|
{
|
||||||
@@ -40,7 +40,23 @@ public class HeaderCommentRule : BaseRuleVisitor
|
|||||||
prevToken.TokenType != TSqlTokenType.SingleLineComment && prevToken.TokenType != TSqlTokenType.MultilineComment
|
prevToken.TokenType != TSqlTokenType.SingleLineComment && prevToken.TokenType != TSqlTokenType.MultilineComment
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
AddViolation(node, name);
|
if (node is ProcedureStatementBody proc)
|
||||||
|
{
|
||||||
|
AddViolation(proc.ProcedureReference.Name, name);
|
||||||
|
}
|
||||||
|
else if (node is ViewStatementBody view)
|
||||||
|
{
|
||||||
|
AddViolation(view.SchemaObjectName, name);
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (node is TriggerStatementBody tr)
|
||||||
|
{
|
||||||
|
AddViolation(tr.Name, name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
AddViolation(node, name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public class InsertStarRule : BaseRuleVisitor
|
|||||||
{
|
{
|
||||||
if (node.InsertSpecification.Columns.Count == 0) // INSERT без перечисления колонок
|
if (node.InsertSpecification.Columns.Count == 0) // INSERT без перечисления колонок
|
||||||
{
|
{
|
||||||
AddViolation(node);
|
AddViolation(node.InsertSpecification.Target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@ public class ProcedureLoggingRule : BaseRuleVisitor
|
|||||||
public override void Visit(CreateOrAlterProcedureStatement node) => check(node, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
public override void Visit(CreateOrAlterProcedureStatement node) => check(node, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
||||||
public override void Visit(AlterProcedureStatement node) => check(node, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
public override void Visit(AlterProcedureStatement node) => check(node, SQLHelpers.ObjectGetFullName(node.ProcedureReference.Name));
|
||||||
|
|
||||||
private void check(TSqlStatement node, string name)
|
private void check(ProcedureStatementBody node, string name)
|
||||||
{
|
{
|
||||||
var tokens = node.ScriptTokenStream;
|
var tokens = node.ScriptTokenStream;
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ public class ProcedureLoggingRule : BaseRuleVisitor
|
|||||||
|
|
||||||
if (!hasDebugLog || !hasLabelFinish)
|
if (!hasDebugLog || !hasLabelFinish)
|
||||||
{
|
{
|
||||||
AddViolation(node, name);
|
AddViolation(node.ProcedureReference.Name, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class TempTableModificationRule : BaseRuleVisitor
|
|||||||
{
|
{
|
||||||
if (node.UpdateSpecification.Target is NamedTableReference tbl && tbl.SchemaObject.BaseIdentifier.Value.StartsWith("#"))
|
if (node.UpdateSpecification.Target is NamedTableReference tbl && tbl.SchemaObject.BaseIdentifier.Value.StartsWith("#"))
|
||||||
{
|
{
|
||||||
AddViolation(node, SQLHelpers.ObjectGetFullName(tbl.SchemaObject));
|
AddViolation(node.UpdateSpecification.Target, SQLHelpers.ObjectGetFullName(tbl.SchemaObject));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ public class TempTableModificationRule : BaseRuleVisitor
|
|||||||
{
|
{
|
||||||
if (node.DeleteSpecification.Target is NamedTableReference tbl && tbl.SchemaObject.BaseIdentifier.Value.StartsWith("#"))
|
if (node.DeleteSpecification.Target is NamedTableReference tbl && tbl.SchemaObject.BaseIdentifier.Value.StartsWith("#"))
|
||||||
{
|
{
|
||||||
AddViolation(node, SQLHelpers.ObjectGetFullName(tbl.SchemaObject));
|
AddViolation(node.DeleteSpecification.Target, SQLHelpers.ObjectGetFullName(tbl.SchemaObject));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ public class TempTableModificationRule : BaseRuleVisitor
|
|||||||
{
|
{
|
||||||
if (node.SchemaObjectName.BaseIdentifier.Value.StartsWith("#"))
|
if (node.SchemaObjectName.BaseIdentifier.Value.StartsWith("#"))
|
||||||
{
|
{
|
||||||
AddViolation(node, SQLHelpers.ObjectGetFullName(node.SchemaObjectName));
|
AddViolation(node.SchemaObjectName, SQLHelpers.ObjectGetFullName(node.SchemaObjectName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,6 @@ public class UpdateWhereRule : BaseRuleVisitor, IRule
|
|||||||
.Select(t => t.Text)
|
.Select(t => t.Text)
|
||||||
);
|
);
|
||||||
|
|
||||||
AddViolation(node, name);
|
AddViolation(node.Target, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user