Пример #1
0
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            List <SqlRuleProblem> problems = new List <SqlRuleProblem>();
            TSqlObject            sqlObj   = ruleExecutionContext.ModelElement;

            if (sqlObj == null || sqlObj.IsWhiteListed())
            {
                return(problems);
            }
            var fragment  = ruleExecutionContext.ScriptFragment.GetFragment(typeof(CreateTableStatement));
            var tableName = sqlObj.Name.GetName();

            ColumnDefinitionVisitor columnVisitor = new ColumnDefinitionVisitor();

            fragment.Accept(columnVisitor);

            var longChars = columnVisitor.Statements
                            .Where(col => col.DataType != null && col.DataType.Name != null)
                            .Select(col => new
            {
                column = col,
                name   = col.ColumnIdentifier.Value,
                type   = col.DataType.Name.Identifiers.FirstOrDefault()?.Value,
                length = GetDataTypeLength(col)
            })
                            .Where(x => (_comparer.Equals(x.type, "char") || _comparer.Equals(x.type, "nchar")) && x.length > 9);

            problems.AddRange(longChars.Select(col => new SqlRuleProblem(Message, sqlObj, col.column)));

            return(problems);
        }
        /// <summary>
        /// Performs analysis and returns a list of problems detected
        /// </summary>
        /// <param name="ruleExecutionContext">Contains the schema model and model element to analyze</param>
        /// <returns>
        /// The problems detected by the rule in the given element
        /// </returns>
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            var problems = new List <SqlRuleProblem>();
            var sqlModel = ruleExecutionContext.SchemaModel;

            if (sqlModel == null)
            {
                return(problems);
            }

            var tables     = sqlModel.GetObjects(DacQueryScopes.UserDefined, Table.TypeClass).Where(t => !t.IsWhiteListed());
            var columnList = new List <TableColumnInfo>();

            foreach (var table in tables)
            {
                var fragment      = table.GetFragment();
                var columnVisitor = new ColumnDefinitionVisitor();
                fragment.Accept(columnVisitor);
                columnList.AddRange(columnVisitor.NotIgnoredStatements(RuleId)
                                    .Where(col => col.DataType != null)
                                    .Select(col =>
                                            new TableColumnInfo()
                {
                    TableName          = table.Name.GetName(),
                    ColumnName         = col.ColumnIdentifier.Value,
                    DataType           = col.DataType.Name.Identifiers.FirstOrDefault()?.Value,
                    DataTypeParameters = GetDataTypeLengthParameters(col),
                    Column             = col,
                    Table = table
                }
                                            ));
            }

            //find all the columns that match by name but differ by data type or length....
            var offenders = columnList.Where(x =>
                                             columnList.Any(y =>
                                                            !_comparer.Equals(x.TableName, y.TableName) &&
                                                            _comparer.Equals(x.ColumnName, y.ColumnName) &&
                                                            (
                                                                !_comparer.Equals(x.DataType, y.DataType) ||
                                                                !_comparer.Equals(x.DataTypeParameters, y.DataTypeParameters)
                                                            )
                                                            )
                                             );

            problems.AddRange(offenders
                              .Select(col => new SqlRuleProblem(string.Format(Message, col.ToString()), col.Table, col.Column)));

            return(problems);
        }
        /// <summary>
        /// Performs analysis and returns a list of problems detected
        /// </summary>
        /// <param name="ruleExecutionContext">Contains the schema model and model element to analyze</param>
        /// <returns>
        /// The problems detected by the rule in the given element
        /// </returns>
        public override IList <SqlRuleProblem> Analyze(SqlRuleExecutionContext ruleExecutionContext)
        {
            List <SqlRuleProblem> problems = new List <SqlRuleProblem>();
            TSqlObject            sqlObj   = ruleExecutionContext.ModelElement;

            if (sqlObj == null || sqlObj.IsWhiteListed())
            {
                return(problems);
            }
            var fragment = ruleExecutionContext.ScriptFragment.GetFragment(typeof(CreateTableStatement));
            var objName  = sqlObj.Name.GetName();

            var dbCollation = ruleExecutionContext.SchemaModel.CopyModelOptions().Collation;

            ColumnDefinitionVisitor columnVisitor = new ColumnDefinitionVisitor();

            fragment.Accept(columnVisitor);

            var statements = columnVisitor.NotIgnoredStatements(RuleId).ToList();

            var columnOffenders = statements.Where(col =>
                                                   (col.Collation != null && !_comparer.Equals(col.Collation?.Value, dbCollation))
                                                   ).ToList();

            problems.AddRange(columnOffenders.Select(col => new SqlRuleProblem(MessageColumn, sqlObj, col)));

            var defaultOffenders = statements.Where(col =>
            {
                var collation = (col.DefaultConstraint?.Expression as PrimaryExpression)?.Collation;

                return(collation != null && !_comparer.Equals(collation.Value, dbCollation));
            }).ToList();

            problems.AddRange(defaultOffenders.Select(col => new SqlRuleProblem(MessageDefault, sqlObj, col.DefaultConstraint)));

            return(problems);
        }