48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using Microsoft.SqlServer.TransactSql.ScriptDom;
|
|
using SQLLinter.Common;
|
|
using SQLLinter.Common.Helpers;
|
|
|
|
namespace SQLLinter.Infrastructure.Rules;
|
|
|
|
public class NamedConstraintRule : BaseRuleVisitor, IRule
|
|
{
|
|
|
|
public override string Text => "Именованные ограничения во временных таблицах могут вызывать коллизии при параллельном запуске: {0}";
|
|
|
|
public override void Visit(CreateTableStatement node)
|
|
{
|
|
// применять правило только к временным таблицам
|
|
if (!node.SchemaObjectName.BaseIdentifier.Value.Contains("#"))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var constraintVisitor = new ConstraintVisitor();
|
|
node.AcceptChildren(constraintVisitor);
|
|
|
|
if (constraintVisitor.NamedConstraintExists)
|
|
{
|
|
AddViolation(node, SQLHelpers.ObjectGetFullName(node.SchemaObjectName));
|
|
}
|
|
}
|
|
|
|
private class ConstraintVisitor : TSqlFragmentVisitor
|
|
{
|
|
public bool NamedConstraintExists
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
public override void Visit(ConstraintDefinition node)
|
|
{
|
|
if (NamedConstraintExists)
|
|
{
|
|
return;
|
|
}
|
|
|
|
NamedConstraintExists = node.ConstraintIdentifier != null;
|
|
}
|
|
}
|
|
}
|