Exemple #1
0
 private static void AsExpected(ForeignKeyDescription foreignKeyDescription)
 {
     Assert.NotNull(foreignKeyDescription);
     DfAssert.NotEmpty(foreignKeyDescription.Name);
     DfAssert.Past(foreignKeyDescription.Created);
     DfAssert.Past(foreignKeyDescription.Modified);
     Assert.NotNull(foreignKeyDescription.Parent);
     Assert.NotNull(foreignKeyDescription.Referenced);
     DfAssert.GreaterThanOrEqual(foreignKeyDescription.Modified, foreignKeyDescription.Created);
     AsExpected(foreignKeyDescription.ColumnRelationshipDescriptions);
 }
            private static void UpdateForeignKeys(ISql sql, ForeignKeyDescription foreignKeyDescription)
            {
                var parentTableName                = foreignKeyDescription.Parent.TableName();
                var referencedTableName            = foreignKeyDescription.Referenced.TableName();
                var columnRelationshipDescriptions = foreignKeyDescription.ColumnRelationshipDescriptions;
                var referencedColumnNames          = columnRelationshipDescriptions.Select(_ => _.Referenced.Name);
                var parentColumnNames              = columnRelationshipDescriptions.Select(_ => _.Parent.Name);
                var updatePairs = parentColumnNames.Zip(referencedColumnNames, (p, r) => $"T1.{p} = T2.{r}");
                var sb          = new StringBuilder("UPDATE T1 SET ")
                                  .AppendJoin(", ", updatePairs)
                                  .AppendFormatInvariant(" FROM {0} T1, {1} T2 WHERE T2.[@DF1] = T1.[@DF2];", parentTableName, referencedTableName)
                                  .ToString();

                sql.NonQuery(sb);
            }
Exemple #3
0
        private static IReadOnlyList <ForeignKeyDescription> ReadForeignKeysForParent(MetaDbContext context, IReadOnlyList <TableDescription> tables, TableDescription parent)
        {
            var foreignKeyGroups = from fkc in context.ForeignKeyColumns
                                   where fkc.ParentObjectId == parent.ObjectId
                                   group new
            {
                fkc.ConstraintColumnId,
                fkc.ParentColumnId,
                fkc.ReferencedColumnId,
            }
            by fkc.ConstraintObjectId;

            var foreignKeys = new List <ForeignKeyDescription>();

            foreach (var foreignKeyColumnGroup in foreignKeyGroups)
            {
                var foreignKey          = context.ForeignKeys.Single(_ => _.ObjectId == foreignKeyColumnGroup.Key);
                var referenced          = tables.Single(_ => _.ObjectId == foreignKey.ReferencedObjectId);
                var columnRelationships = new List <ColumnRelationshipDescription>();
                foreach (var foreignKeyColumn in foreignKeyColumnGroup)
                {
                    var parentColumn       = parent.ColumnDescriptions.Single(_ => _.Order == foreignKeyColumn.ParentColumnId);
                    var referencedColumn   = referenced.ColumnDescriptions.Single(c => c.Order == foreignKeyColumn.ReferencedColumnId);
                    var columnRelationship = new ColumnRelationshipDescription(foreignKeyColumn.ConstraintColumnId)
                    {
                        Parent     = parentColumn,
                        Referenced = referencedColumn,
                    };
                    columnRelationships.Add(columnRelationship);
                }

                var foreignKeyDescription = new ForeignKeyDescription(foreignKey.Name, foreignKey.CreateDate, foreignKey.ModifyDate)
                {
                    ColumnRelationshipDescriptions = columnRelationships,
                };

                foreignKeys.Add(foreignKeyDescription);
            }

            return(foreignKeys);
        }