Добавьте файлы проекта.
This commit is contained in:
80
SQLLinter/Infrastructure/Rules/ColumnNullabilityRule.cs
Normal file
80
SQLLinter/Infrastructure/Rules/ColumnNullabilityRule.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using Microsoft.SqlServer.TransactSql.ScriptDom;
|
||||
using SQLLinter.Common;
|
||||
using SQLLinter.Common.Helpers;
|
||||
|
||||
namespace SQLLinter.Infrastructure.Rules;
|
||||
|
||||
public class ColumnNullabilityRule : BaseRuleVisitor
|
||||
{
|
||||
public override string Text => "При объявлении таблицы необходимо указывать для столбцов NULL/NOT NULL: таблица {0} - столбец [{1}]";
|
||||
|
||||
private string? _currentTable;
|
||||
|
||||
public override void Visit(CreateTableStatement node)
|
||||
{
|
||||
_currentTable = SQLHelpers.ObjectGetFullName(node.SchemaObjectName);
|
||||
|
||||
|
||||
foreach (var element in node.Definition.ColumnDefinitions)
|
||||
{
|
||||
CheckColumn(element);
|
||||
}
|
||||
|
||||
_currentTable = null;
|
||||
}
|
||||
|
||||
public override void Visit(AlterTableAddTableElementStatement node)
|
||||
{
|
||||
_currentTable = SQLHelpers.ObjectGetFullName(node.SchemaObjectName);
|
||||
|
||||
foreach (var element in node.Definition.ColumnDefinitions)
|
||||
{
|
||||
CheckColumn(element);
|
||||
}
|
||||
|
||||
_currentTable = null;
|
||||
}
|
||||
|
||||
public override void Visit(AlterTableAlterColumnStatement node)
|
||||
{
|
||||
_currentTable = SQLHelpers.ObjectGetFullName(node.SchemaObjectName);
|
||||
|
||||
bool hasNullabilityConstraint = node.Options
|
||||
.OfType<NullableConstraintDefinition>()
|
||||
.Any();
|
||||
|
||||
if (!hasNullabilityConstraint)
|
||||
{
|
||||
AddViolation(node,
|
||||
_currentTable ?? "<unknown>",
|
||||
node.ColumnIdentifier?.Value ?? "<unknown>");
|
||||
}
|
||||
|
||||
_currentTable = null;
|
||||
|
||||
}
|
||||
|
||||
public override void Visit(DeclareTableVariableStatement node)
|
||||
{
|
||||
_currentTable = node.Body.VariableName.Value;
|
||||
|
||||
foreach (var column in node.Body.Definition.ColumnDefinitions)
|
||||
{
|
||||
CheckColumn(column);
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckColumn(ColumnDefinition node)
|
||||
{
|
||||
bool hasNullabilityConstraint = node.Constraints
|
||||
.OfType<NullableConstraintDefinition>()
|
||||
.Any();
|
||||
|
||||
if (!hasNullabilityConstraint)
|
||||
{
|
||||
AddViolation(node,
|
||||
_currentTable ?? "<unknown>",
|
||||
node.ColumnIdentifier?.Value ?? "<unknown>");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user