31 lines
1.0 KiB
C#
31 lines
1.0 KiB
C#
using Microsoft.SqlServer.TransactSql.ScriptDom;
|
|
using SQLLinter.Common;
|
|
using System.Linq;
|
|
|
|
namespace SQLLinter.Infrastructure.Rules;
|
|
|
|
public class OrderByWithoutTopOffsetRule : BaseRuleVisitor
|
|
{
|
|
public override string Text => "Обнаружен ORDER BY без TOP или OFFSET: {0}";
|
|
|
|
public override void Visit(QuerySpecification node)
|
|
{
|
|
if (node.OrderByClause != null && node.TopRowFilter == null && node.OffsetClause == null)
|
|
{
|
|
var tokens = node.OrderByClause.ScriptTokenStream
|
|
.Skip(node.OrderByClause.FirstTokenIndex)
|
|
.Take(node.OrderByClause.LastTokenIndex - node.OrderByClause.FirstTokenIndex + 1)
|
|
.Select(t => t.TokenType == TSqlTokenType.WhiteSpace ? " " : t.Text);
|
|
|
|
var orderByString = string.Join("", tokens);
|
|
if (orderByString.Length > 53)
|
|
{
|
|
orderByString = orderByString[..50] + "...";
|
|
}
|
|
|
|
AddViolation(node.OrderByClause, orderByString);
|
|
}
|
|
base.Visit(node);
|
|
}
|
|
}
|