Example #1
0
        public override IList<DataRuleProblem> Analyze( DataRuleSetting ruleSetting, DataRuleExecutionContext context )
        {
            if ( context.ModelElement.Name == null )
            {
                return null;
            }

            var problems = new List<DataRuleProblem> ();

            var sqlTable = ( ISqlTable ) context.ModelElement;

            if ( sqlTable.Name.Parts[ 1 ] != "HiValue" )
            {
                ISqlConstraint primaryKey = sqlTable.GetPrimaryKey();

                if ( primaryKey == null )
                {
                    string ruleProblemDescription = string.Format ( CultureInfo.CurrentCulture,
                                                                    "Table [{0}].[{1}] does not have a Primary Key.",
                                                                    context.ModelElement.Name.Parts[ 0 ],
                                                                    context.ModelElement.Name.Parts[ 1 ] );

                    var problem = new DataRuleProblem ( this, ruleProblemDescription, sqlTable )
                                      {
                                          FileName = sqlTable.PrimarySource.SourceName,
                                          StartLine = sqlTable.PrimarySource.StartLine,
                                          StartColumn = sqlTable.PrimarySource.StartColumn
                                      };

                    problems.Add ( problem );
                }
            }

            return problems;
        }
        public override IList<DataRuleProblem> Analyze( DataRuleSetting ruleSetting, DataRuleExecutionContext context )
        {
            if ( context.ModelElement.Name == null )
            {
                return null;
            }

            var sqlTable = ( ISqlTable ) context.ModelElement;

            IEnumerable<ISqlPrimaryKeyConstraint> primaryKeys = sqlTable.Constraints.OfType<ISqlPrimaryKeyConstraint> ();

            var problems = new List<DataRuleProblem> ();

            if ( primaryKeys.Count () > 1 )
            {
                string ruleProblemDescription = string.Format ( CultureInfo.CurrentCulture,
                                                                "Table [{0}].[{1}] has more than 1 PrimaryKey.",
                                                                context.ModelElement.Name.Parts[ 0 ],
                                                                context.ModelElement.Name.Parts[ 1 ] );

                var problem = new DataRuleProblem ( this, ruleProblemDescription, sqlTable )
                                  {
                                      FileName = sqlTable.PrimarySource.SourceName,
                                      StartLine = sqlTable.PrimarySource.StartLine,
                                      StartColumn = sqlTable.PrimarySource.StartColumn
                                  };

                problems.Add ( problem );
            }

            return problems;
        }
Example #3
0
        static readonly Dictionary<string, List<string>> SqlTypeColumnNameSuffixMapping = GetSqlTypeColumnNameSuffixMappingFromExcelFile(); // GetSqlTypeColumnNameSuffixMapping();

        #endregion Fields

        #region Methods

        public override IList<DataRuleProblem> Analyze(DataRuleSetting ruleSetting, DataRuleExecutionContext context)
        {
            var problems = new List<DataRuleProblem>();

            var column = (ISqlColumn) context.ModelElement;

            var schemaName = column.Name.Parts[0];
            var tableName = column.Name.Parts[1];
            var columnName = column.Name.Parts[2];
            var columnTypeName = string.Format(CultureInfo.CurrentCulture, "{0}", column.TypeSpecifier.Type);

            string ruleProblemDescription = null;

            if (columnName != "Version")
            {
                if (!SqlTypeColumnNameSuffixMapping.ContainsKey(columnTypeName))
                {
                    ruleProblemDescription = string.Format(CultureInfo.CurrentCulture,
                                                           "Defining Table [{0}].[{1}]: For column [{2}] of type [{3}], there is no column name suffix mapping defined for [{4}] sql data type.",
                                                           schemaName,
                                                           tableName,
                                                           columnName,
                                                           columnTypeName,
                                                           columnTypeName);
                }
                else
                {
                    var expectedSuffixList = SqlTypeColumnNameSuffixMapping[columnTypeName];
                    var result = expectedSuffixList.Any(columnName.EndsWith);

                    if (!result)
                    {
                        ruleProblemDescription = string.Format(CultureInfo.CurrentCulture,
                                                               "Defining Table [{0}].[{1}]: Column [{2}] of type [{3}] does not end with suffix defined in the column name suffix mapping for [{4}] sql data type.",
                                                               schemaName,
                                                               tableName,
                                                               columnName,
                                                               columnTypeName,
                                                               columnTypeName);
                    }
                }
            }

            if (!String.IsNullOrEmpty(ruleProblemDescription))
            {
                var problem = new DataRuleProblem(this, Environment.NewLine + ruleProblemDescription + Environment.NewLine, column)
                                  {
                                      FileName = column.PrimarySource.SourceName,
                                      StartLine = column.PrimarySource.StartLine,
                                      StartColumn = column.PrimarySource.StartColumn
                                  };

                problems.Add(problem);
            }

            return problems;
        }
        public override IList<DataRuleProblem> Analyze( DataRuleSetting ruleSetting, DataRuleExecutionContext context )
        {
            var table = context.ModelElement as ISqlTable;

            if ( table == null || table.Name.Parts[ 1 ] == "HiValue" )
            {
                return null;
            }

            var problems = new List<DataRuleProblem> ();

            string columnName;

            foreach ( ISqlColumn column in table.Columns )
            {
                columnName = column.Name.Parts[ 2 ];

                if ( columnName.EndsWith ( "Key" ) )
                {
                    if ( columnName == "CreatedAccountKey" ||
                         columnName == "UpdatedAccountKey" ||
                         columnName == "RevisedAccountKey" ||
                         column.IsPrimaryKey(table))
                    {
                        continue;
                    }

                    ISqlForeignKeyConstraint foreignKey = table.GetForeignKeyForColumn(column);

                    if ( foreignKey == null )
                    {
                        string ruleProblemDescription = string.Format ( CultureInfo.CurrentCulture,
                                                                        "Table [{0}].[{1}] does not have a Foreign Key for Column {2}.",
                                                                        table.Name.Parts[ 0 ],
                                                                        table.Name.Parts[ 1 ],
                                                                        columnName );

                        var problem = new DataRuleProblem ( this, ruleProblemDescription, table )
                                          {
                                              FileName = table.PrimarySource.SourceName,
                                              StartLine = table.PrimarySource.StartLine,
                                              StartColumn = table.PrimarySource.StartColumn
                                          };

                        problems.Add ( problem );
                    }
                }
            }

            return problems;
        }
        public override IList<DataRuleProblem> Analyze( DataRuleSetting ruleSetting, DataRuleExecutionContext context )
        {
            var foreignKeyConstraint = context.ModelElement as ISqlForeignKeyConstraint;
            if (foreignKeyConstraint == null)
            {
                return null;
            }

            if (foreignKeyConstraint.IsDefinedOnSameColumnsAsPrimaryKey())
            {
                return null;
            }

            string foreignKeyName = foreignKeyConstraint.Name.Parts[ 1 ];
            string expectedIndexName = foreignKeyName + "_IDX";

            var definingTable = foreignKeyConstraint.DefiningTable as ISql100Table;

            if ( definingTable == null )
            {
                return null;
            }

            IEnumerable<ISqlIndex> index = definingTable.Indexes.Where ( p => p.Name.Parts[ 2 ] == expectedIndexName );

            var problems = new List<DataRuleProblem> ();

            if ( index.Count () == 0 )
            {
                string ruleProblemDescription = string.Format ( CultureInfo.CurrentCulture,
                                                                "Defining Table [{0}].[{1}]: ForeignKey [{3}] does not have an Index named [{2}].",
                                                                definingTable.Name.Parts[ 0 ],
                                                                definingTable.Name.Parts[ 1 ],
                                                                expectedIndexName,
                                                                foreignKeyName );

                var problem = new DataRuleProblem ( this, ruleProblemDescription, foreignKeyConstraint )
                                  {
                                      FileName = foreignKeyConstraint.PrimarySource.SourceName,
                                      StartLine = foreignKeyConstraint.PrimarySource.StartLine,
                                      StartColumn = foreignKeyConstraint.PrimarySource.StartColumn
                                  };

                problems.Add ( problem );
            }

            return problems;
        }
Example #6
0
        public override IList<DataRuleProblem> Analyze( DataRuleSetting ruleSetting, DataRuleExecutionContext context )
        {
            var foreignKeyConstraint = context.ModelElement as ISqlForeignKeyConstraint;

            if ( foreignKeyConstraint == null )
            {
                return null;
            }

            ISqlSpecifiesTable definingTable = foreignKeyConstraint.DefiningTable;
            ISqlTable foreignTable = foreignKeyConstraint.ForeignTable;

            IList<DataRuleProblem> problems = new List<DataRuleProblem> ();

            string prefix = definingTable.Name.Parts[ 1 ] + "_" + foreignTable.Name.Parts[ 1 ];
            const string suffix = "_FK";

            string foreignKeyPattern = @"^" + prefix + @"[a-zA-Z0-9_]*" + suffix;

            if ( !Regex.IsMatch ( foreignKeyConstraint.Name.Parts[ 1 ], foreignKeyPattern ) )
            {
                string ruleProblemDescription = string.Format ( CultureInfo.CurrentCulture,
                                                                "Defining Table [{0}].[{1}]: ForeignKey is named improperly. Expected to start with {2} and end with {3}. Found {4}",
                                                                definingTable.Name.Parts[ 0 ],
                                                                definingTable.Name.Parts[ 1 ],
                                                                prefix,
                                                                suffix,
                                                                foreignKeyConstraint.Name.Parts[ 1 ] );

                var problem = new DataRuleProblem ( this, ruleProblemDescription, foreignKeyConstraint )
                                  {
                                      FileName = foreignKeyConstraint.PrimarySource.SourceName,
                                      StartLine = foreignKeyConstraint.PrimarySource.StartLine,
                                      StartColumn = foreignKeyConstraint.PrimarySource.StartColumn
                                  };

                problems.Add ( problem );
            }

            return problems;
        }
        public override IList<DataRuleProblem> Analyze( DataRuleSetting ruleSetting, DataRuleExecutionContext context )
        {
            if ( context.ModelElement.Name == null )
            {
                return null;
            }

            var primaryKey = ( ISqlPrimaryKeyConstraint ) context.ModelElement;
            string columnName = primaryKey.ColumnSpecifications[ 0 ].Column.Name.Parts[ 2 ];
            string tableName = primaryKey.DefiningTable.Name.Parts[ 1 ];

            var problems = new List<DataRuleProblem> ();

            string properyPrimaryKeyColumnName = tableName + "Key";
            if ( columnName != properyPrimaryKeyColumnName )
            {
                string errorMessage =
                    string.Format (
                        "Table [{0}].[{1}] does not have a properly named PrimaryKey Column. Expected {2}.  Found {3}.",
                        context.ModelElement.Name.Parts[ 0 ],
                        tableName,
                        properyPrimaryKeyColumnName,
                        columnName );

                string ruleProblemDescription = string.Format ( CultureInfo.CurrentCulture, errorMessage );

                var problem = new DataRuleProblem ( this, ruleProblemDescription, primaryKey.DefiningTable )
                                  {
                                      FileName = primaryKey.DefiningTable.PrimarySource.SourceName,
                                      StartLine = primaryKey.DefiningTable.PrimarySource.StartLine,
                                      StartColumn = primaryKey.DefiningTable.PrimarySource.StartColumn
                                  };

                problems.Add ( problem );
            }

            return problems;
        }
        public override IList<DataRuleProblem> Analyze( DataRuleSetting ruleSetting, DataRuleExecutionContext context )
        {
            var foreignKeyConstraint = context.ModelElement as ISqlForeignKeyConstraint;

            if ( foreignKeyConstraint == null )
            {
                return null;
            }

            ISqlSpecifiesTable definingTable = foreignKeyConstraint.DefiningTable;
            ISqlTable referentialTable = foreignKeyConstraint.ForeignTable;

            string foreignKeyColumnName = foreignKeyConstraint.Columns.First().Name.Parts[2];

            IList<DataRuleProblem> problems = new List<DataRuleProblem>();

            if (referentialTable.HasFoeignKeyAlsoDefinedOnPrimaryKeyColumns())
            {
                bool result = foreignKeyColumnName == referentialTable.Name.Parts[1] + "Key";

                if (!result)
                {
                    string ruleProblemDescription = string.Format(CultureInfo.CurrentCulture,
                                                                    "Defining Table [{0}].[{1}]: Foreign key [{2}] column is not named after the table name of the referential table [{3}].[{4}].  Expected {5}. Found {6}",
                                                                    definingTable.Name.Parts[0],
                                                                    definingTable.Name.Parts[1],
                                                                    foreignKeyConstraint.Name.Parts[1],
                                                                    referentialTable.Name.Parts[0],
                                                                    referentialTable.Name.Parts[1],
                                                                    referentialTable.Name.Parts[1] + "Key",
                                                                    foreignKeyColumnName);

                    var problem = CreateDateRuleProblem(foreignKeyConstraint, ruleProblemDescription);
                    problems.Add(problem);
                }
            }
            else
            {
                ISqlPrimaryKeyConstraint referentialTablePrimaryKey =
                referentialTable.Constraints.OfType<ISqlPrimaryKeyConstraint>().SingleOrDefault();

                string referentialTablePrimaryKeyColumnName =
                    referentialTablePrimaryKey.ColumnSpecifications[0].Column.Name.Parts[2];

                bool columnsNameMatch = foreignKeyColumnName == referentialTablePrimaryKeyColumnName;

                bool result = columnsNameMatch || foreignKeyColumnName.EndsWith(referentialTablePrimaryKeyColumnName);

                if (!result)
                {

                    string ruleProblemDescription = string.Format(CultureInfo.CurrentCulture,
                                                                    "Defining Table [{0}].[{1}]: Foreign key [{2}] column is not named after (matches or ends with) the primary key column name of the referential table [{3}].[{4}].  Expected {5}. Found {6}",
                                                                    definingTable.Name.Parts[0],
                                                                    definingTable.Name.Parts[1],
                                                                    foreignKeyConstraint.Name.Parts[1],
                                                                    referentialTable.Name.Parts[0],
                                                                    referentialTable.Name.Parts[1],
                                                                    referentialTablePrimaryKeyColumnName,
                                                                    foreignKeyColumnName);

                    var problem = CreateDateRuleProblem ( foreignKeyConstraint, ruleProblemDescription );
                    problems.Add(problem);
                }
            }

            return problems;
        }