public static void Test3() { IMigrationContextHolderService holder = CreateContextHolder(); MigrationContext context = holder.Context; DataPathTranslationService dataPathTranslator = new DataPathTranslationService(); dataPathTranslator.ContextHolder = holder; dataPathTranslator.RegisterJoin( DataPath.Parse("SECCODE:SECCODEID"), DataPath.Parse("ACCOUNT:SECID")); Debug.Assert(context.Tables.Count == 2); Debug.Assert(context.Tables.ContainsKey("ACCOUNT")); Debug.Assert(context.Tables.ContainsKey("SECCODE")); TableInfo account = context.Tables["ACCOUNT"]; TableInfo seccode = context.Tables["SECCODE"]; Debug.Assert(account.Joins.Count == 0); Debug.Assert(account.Columns.Count == 1); Debug.Assert(account.Columns.Contains("SECID")); Debug.Assert(seccode.Joins.Count == 1); DataPathJoin j = new DataPathJoin("SECCODE", "SECCODEID", "SECID", "ACCOUNT"); Debug.Assert(seccode.Joins.ContainsKey(j)); Debug.Assert(seccode.Joins[j] == account); Debug.Assert(seccode.Columns.Count == 1); Debug.Assert(seccode.Columns.Contains("SECCODEID")); }
public void RegisterJoin(DataPath leftPath, DataPath rightPath) { TableInfo leftTable = InternalRegisterField(leftPath); TableInfo rightTable = InternalRegisterField(rightPath); DataPathJoin join = new DataPathJoin(leftPath.TargetTable, leftPath.TargetField, rightPath.TargetTable, rightPath.TargetField); leftTable.Joins[join] = rightTable; }
public static void Test2() { IMigrationContextHolderService holder = CreateContextHolder(); MigrationContext context = holder.Context; DataPathTranslationService dataPathTranslator = new DataPathTranslationService(); dataPathTranslator.ContextHolder = holder; dataPathTranslator.RegisterField(DataPath.Parse("CONTACT:ACCID>ACCOUNTID.ACCOUNT!ACCOUNT")); dataPathTranslator.RegisterField(DataPath.Parse("CONTACT:ACCID>ACCOUNTID.ACCOUNT!TYPE")); dataPathTranslator.RegisterField(DataPath.Parse("CONTACT:ACCID>ACCOUNTID.ACCOUNT!STATUS")); Debug.Assert(context.Tables.Count == 2); Debug.Assert(context.Tables.ContainsKey("CONTACT")); Debug.Assert(context.Tables.ContainsKey("ACCOUNT")); TableInfo contact = context.Tables["CONTACT"]; TableInfo account = context.Tables["ACCOUNT"]; Debug.Assert(contact.Joins.Count == 1); DataPathJoin j = new DataPathJoin("CONTACT", "ACCID", "ACCOUNTID", "ACCOUNT"); Debug.Assert(contact.Joins.ContainsKey(j)); Debug.Assert(contact.Joins[j] == account); Debug.Assert(contact.Columns.Count == 1); Debug.Assert(contact.Columns.Contains("ACCID")); Debug.Assert(account.Joins.Count == 0); Debug.Assert(account.Columns.Count == 4); Debug.Assert(account.Columns.ContainsAll(new string[] {"ACCOUNTID", "ACCOUNT", "TYPE", "STATUS"})); }
private string InternalTranslate(DataPath dataPath, string targetTable, string targetField, bool isOneToMany) { Guard.ArgumentNotNull(dataPath, "dataPath"); Guard.ArgumentNotNull(targetTable, "targetTable"); Guard.ArgumentNotNull(targetField, "targetField"); int joinCount = dataPath.Joins.Count; List<string> parts = new List<string>(); RelationshipInfo relationship; for (int i = 0; i < joinCount; i++) { DataPathJoin join = dataPath.Joins[i]; if (!_context.Relationships.TryGetValue(join, out relationship)) { throw new MigrationException(string.Format("Unable to find a relationship based on the '{0}' join string", join)); } if (!(i == joinCount - 1 && StringUtils.CaseInsensitiveEquals(join.ToTable, targetTable) && StringUtils.CaseInsensitiveEquals(join.ToField, targetField))) { Debug.Assert(!relationship.IsOneToMany); parts.Add(relationship.PropertyName); } } DataPathJoin targetJoin = new DataPathJoin(dataPath.TargetTable, dataPath.TargetField, targetTable, targetField); if (!_context.Relationships.TryGetValue(targetJoin, out relationship)) { throw new MigrationException(string.Format("Unable to find a relationship based on the '{0}' join string", targetJoin)); } Debug.Assert(relationship.IsOneToMany == isOneToMany); parts.Add(relationship.PropertyName); return string.Join(".", parts.ToArray()); }
private RelationshipInfo CreateRelationship(DataPathJoin join, OrmEntity leftEntity, OrmEntity rightEntity, bool isRightPK) { string targetField = join.FromField; if (targetField.StartsWith("@")) { targetField = targetField.Substring(1); } OrmFieldProperty leftProperty = leftEntity.Properties.GetFieldPropertyByFieldName(targetField); if (leftProperty == null) { LogError(leftEntity, "Property based on field '{0}' not found on entity based on table '{1}'", join.FromField, leftEntity.TableName); return null; } targetField = join.ToField; if (targetField.StartsWith("@")) { targetField = targetField.Substring(1); } OrmFieldProperty rightProperty = rightEntity.Properties.GetFieldPropertyByFieldName(targetField); if (rightProperty == null) { LogError(rightEntity, "Property based on field '{0}' not found on entity based on table '{1}'", join.ToField, rightEntity.TableName); return null; } OrmRelationship ormRelationship = CollectionUtils.Find( _ormModel.Relationships, delegate(OrmRelationship item) { if (item.Columns.Count > 0) { OrmRelationshipColumn column = item.Columns[0]; return ((item.ParentEntity == leftEntity && item.ChildEntity == rightEntity && column.ParentProperty == leftProperty && column.ChildProperty == rightProperty && (item.Cardinality == OrmRelationship.ManyToOne) == isRightPK && item.ParentProperty.Include) || (item.ParentEntity == rightEntity && item.ChildEntity == leftEntity && column.ParentProperty == rightProperty && column.ChildProperty == leftProperty && (item.Cardinality == OrmRelationship.OneToMany) == isRightPK && item.ChildProperty.Include)); } return false; }); bool isInverted; if (ormRelationship == null) { bool isLeftDynamic = leftEntity.Package.GetGenerateAssembly(); if (!isLeftDynamic) { LogError("Cannot create the necessary relationship property for '{0}' join", join); return null; } string cardinality; string parentPropertyName; if (isRightPK) { cardinality = OrmRelationship.ManyToOne; parentPropertyName = GenerateReferencePropertyName(rightEntity, leftEntity, leftProperty); } else { cardinality = OrmRelationship.OneToMany; parentPropertyName = GenerateCollectionPropertyName(leftEntity, rightEntity); } ormRelationship = new OrmRelationship( leftEntity, rightEntity, cardinality, leftProperty, rightProperty, CascadeOption.SaveUpdate); ormRelationship.ParentProperty.PropertyName = parentPropertyName; leftEntity.ChildEntities.Add(ormRelationship); rightEntity.ParentEntities.Add(ormRelationship); ormRelationship.ChildProperty.Include = false; isInverted = false; } else { isInverted = (ormRelationship.ParentEntity == rightEntity); } return new RelationshipInfo(ormRelationship, isInverted); }
private RelationshipInfo CreateExtentedEntity(DataPathJoin join, OrmEntity leftEntity, OrmEntity rightEntity, string leftKeyField, string rightKeyField) { if (leftEntity.ExtendedEntity == rightEntity) { return new RelationshipInfo(rightEntity, leftEntity, true); } else if (rightEntity.ExtendedEntity == leftEntity) { return new RelationshipInfo(leftEntity, rightEntity, false); } else { int leftSimilarity = MeasureSimilarity(join.FromTable, leftKeyField); int rightSimilarity = MeasureSimilarity(join.ToTable, rightKeyField); if (leftSimilarity == rightSimilarity) { LogError("Unable to establish owning table in join '{0}'", join); return null; } RelationshipInfo relationship = (rightSimilarity > leftSimilarity ? CreateExtentedEntity(rightEntity, leftEntity, true) : CreateExtentedEntity(leftEntity, rightEntity, false)); leftEntity.Validate(); leftEntity.Save(); rightEntity.Validate(); rightEntity.Save(); return relationship; } }
public static void Test4() { IMigrationContextHolderService holder = CreateContextHolder(); MigrationContext context = holder.Context; DataPathTranslationService dataPathTranslator = new DataPathTranslationService(); dataPathTranslator.ContextHolder = holder; dataPathTranslator.RegisterJoin( DataPath.Parse("USERINFO:USERID>ACCESSID.SECCODE!SECCODEID"), DataPath.Parse("CONTACT:ACCID>ACCOUNTID.ACCOUNT!SECID")); Debug.Assert(context.Tables.Count == 4); Debug.Assert(context.Tables.ContainsKey("CONTACT")); Debug.Assert(context.Tables.ContainsKey("ACCOUNT")); Debug.Assert(context.Tables.ContainsKey("USERINFO")); Debug.Assert(context.Tables.ContainsKey("SECCODE")); TableInfo contact = context.Tables["CONTACT"]; TableInfo account = context.Tables["ACCOUNT"]; TableInfo userinfo = context.Tables["USERINFO"]; TableInfo seccode = context.Tables["SECCODE"]; Debug.Assert(contact.Joins.Count == 1); DataPathJoin j = new DataPathJoin("CONTACT", "ACCID", "ACCOUNTID", "ACCOUNT"); Debug.Assert(contact.Joins.ContainsKey(j)); Debug.Assert(contact.Joins[j] == account); Debug.Assert(contact.Columns.Count == 1); Debug.Assert(contact.Columns.Contains("ACCID")); Debug.Assert(account.Joins.Count == 0); Debug.Assert(account.Columns.Count == 2); Debug.Assert(account.Columns.ContainsAll(new string[] {"ACCOUNTID", "SECID"})); Debug.Assert(userinfo.Joins.Count == 1); j = new DataPathJoin("USERINFO", "USERID", "ACCESSID", "SECCODE"); Debug.Assert(userinfo.Joins.ContainsKey(j)); Debug.Assert(userinfo.Joins[j] == seccode); Debug.Assert(userinfo.Columns.Count == 1); Debug.Assert(userinfo.Columns.Contains("USERID")); Debug.Assert(seccode.Joins.Count == 1); j = new DataPathJoin("SECCODE", "SECCODEID", "SECID", "ACCOUNT"); Debug.Assert(seccode.Joins.ContainsKey(j)); Debug.Assert(seccode.Joins[j] == account); Debug.Assert(seccode.Columns.Count == 2); Debug.Assert(seccode.Columns.ContainsAll(new string[] {"ACCESSID", "SECCODEID"})); }