Beispiel #1
0
 private static MatchLevel Compare(Type thisSideType, Association thisSide, Type otherSideType,
                                   Association otherSide)
 {
     MatchLevel noMatch = MatchLevel.NoMatch;
     if (((thisSide.IsForeignKey == true) && (otherSide.IsForeignKey != true)) ||
         ((thisSide.IsForeignKey != true) && (otherSide.IsForeignKey == true)))
     {
         noMatch = noMatch;
     }
     if (thisSideType.Name == otherSide.Type)
     {
         noMatch |= MatchLevel.ThisTypeAgrees;
     }
     if (otherSideType.Name == thisSide.Type)
     {
         noMatch |= MatchLevel.OtherTypeAgrees;
     }
     if (BuildKeyField(thisSide.GetOtherKey()) == BuildKeyField(otherSide.GetThisKey()))
     {
         noMatch |= MatchLevel.ThisOtherKeyMatchesOtherThisKey;
     }
     if (BuildKeyField(thisSide.GetThisKey()) == BuildKeyField(otherSide.GetOtherKey()))
     {
         noMatch |= MatchLevel.ThisThisKeyMatchisOtherOtherKey;
     }
     return noMatch;
 }
            public override Association VisitAssociation(Association association)
            {
                if (association == null)
                    return null;

                writer.WriteStartElement("Association");

                if (association.Name != null)
                    writer.WriteAttributeString("Name", association.Name);

                if (association.Member != null)
                    writer.WriteAttributeString("Member", association.Member);

                if (association.Storage != null)
                    writer.WriteAttributeString("Storage", association.Storage);

                if (association.AccessModifier.HasValue)
                    writer.WriteAttributeString("AccessModifier", association.AccessModifier.ToString());

                if (association.Modifier.HasValue)
                    writer.WriteAttributeString("Modifier", association.Modifier.ToString());

                string thisKey = BuildKeyField(association.GetThisKey());
                if (!string.IsNullOrEmpty(thisKey))
                    writer.WriteAttributeString("ThisKey", thisKey);

                string otherKey = BuildKeyField(association.GetOtherKey());
                if (!string.IsNullOrEmpty(otherKey))
                    writer.WriteAttributeString("OtherKey", otherKey);

                if (association.Type != null)
                    writer.WriteAttributeString("Type", association.Type);

                if (association.IsForeignKey.HasValue)
                    writer.WriteAttributeString("IsForeignKey", ToXmlBooleanString(association.IsForeignKey));

                if (association.Cardinality.HasValue)
                    writer.WriteAttributeString("Cardinality", association.Cardinality.ToString());

                if (association.DeleteRule != null)
                    writer.WriteAttributeString("DeleteRule", association.DeleteRule);

                if (association.DeleteOnNull.HasValue)
                    writer.WriteAttributeString("DeleteOnNull", ToXmlBooleanString(association.DeleteOnNull));

                writer.WriteEndElement();
                return association;
            }
            public override Association VisitAssociation(Association association)
            {
                if (association == null)
                    return null;

                var a = new Association(association.Name)
                                       {
                                           Member = association.Member,
                                           Storage = association.Storage,
                                           AccessModifier = association.AccessModifier,
                                           Modifier = association.Modifier
                                       };
                a.SetThisKey(association.GetThisKey());
                a.SetOtherKey(association.GetOtherKey());
                a.IsForeignKey = association.IsForeignKey;
                a.Cardinality = association.Cardinality;
                a.DeleteOnNull = association.DeleteOnNull;
                a.DeleteRule = association.DeleteRule;
                a.Type = association.Type;
                return a;
            }
            public override Association VisitAssociation(Association association)
            {
                if (association == null)
                    return null;

                if (association.AccessModifier.HasValue && association.AccessModifier.Value == AccessModifier.Public)
                    association.AccessModifier = null;
                
                if (association.IsForeignKey == false)
                    association.IsForeignKey = null;

                if (association.Cardinality.HasValue && association.Cardinality.Value == Cardinality.One && association.IsForeignKey == true)
                    association.Cardinality = null;
                
                if (association.Cardinality.HasValue && association.Cardinality.Value == Cardinality.Many && association.IsForeignKey != true)
                    association.Cardinality = null;
                
                if ((association.Member != null) && (association.Storage == ("_" + association.Member)))
                    association.Storage = null;
                
                if (association.IsForeignKey != true)
                {
                    Association otherAssociation = associationPartners[association];
                    if (otherAssociation != null)
                    {
                        Type type = TypeFromTypeName(otherAssociation.Type);
                        if ((type != null) && IsPrimaryKeyOfType(type, association.GetThisKey()))
                            association.SetThisKey(null);
                    }
                }

                if (association.IsForeignKey == true)
                {
                    Type type = TypeFromTypeName(association.Type);
                    if ((type != null) && IsPrimaryKeyOfType(type, association.GetOtherKey()))
                        association.SetOtherKey(null);
                }

                return association;
            }
            public override Association VisitAssociation(Association association)
            {
                if (association == null)
                    return null;

                if (!association.AccessModifier.HasValue)
                    association.AccessModifier = 0;

                if (!association.IsForeignKey.HasValue)
                    association.IsForeignKey = false;

                if (!association.Cardinality.HasValue)
                    association.Cardinality = association.IsForeignKey == true ? 0 : (Cardinality) 1;

                if (!association.DeleteOnNull.HasValue)
                    association.DeleteOnNull = false;

                if (association.Storage == null)
                    association.Storage = "_" + association.Member;

                if ((association.GetThisKey().Length == 0) && (association.IsForeignKey != true))
                {
                    Association other = associationPartners[association];
                    if (other != null)
                    {
                        Type type = TypeFromTypeName(other.Type);
                        if (type != null)
                            association.SetThisKey(GetPrimaryKeys(type));
                    }
                }

                if ((association.GetOtherKey().Length == 0) && (association.IsForeignKey == true))
                {
                    Type type2 = TypeFromTypeName(association.Type);
                    if (type2 != null)
                        association.SetOtherKey(GetPrimaryKeys(type2));
                }
                return association;
            }
        private void CreateAssociation(Table foreignTable, TableKeySchema tableKeySchema)
        {
            if (Settings.IsIgnored(tableKeySchema.PrimaryKeyTable.FullName))
            {
                Debug.WriteLine("Skipping Association because one of the tables is skipped: " + tableKeySchema.Name);
                Trace.WriteLine("Skipping Association because one of the tables is skipped: " + tableKeySchema.Name);
                return;
            }

            // skip unsupported type
            if (tableKeySchema.ForeignKeyMemberColumns.Any(c => Settings.IsUnsupportedDbType(c))
                || tableKeySchema.PrimaryKeyMemberColumns.Any(c => Settings.IsUnsupportedDbType(c)))
            {
                Debug.WriteLine("Skipping Association because one of the associated columns is an unsupported db type: " + tableKeySchema.Name);
                Trace.WriteLine("Skipping Association because one of the associated columns is an unsupported db type: " + tableKeySchema.Name);
                return;
            }

            Table primaryTable = GetTable(tableKeySchema.PrimaryKeyTable, false);

            string primaryClass = primaryTable.Type.Name;
            string foreignClass = foreignTable.Type.Name;

            string name = string.Format("{0}_{1}", primaryClass, foreignClass);
            name = MakeUnique(AssociationNames, name);

            string foreignMembers = GetKeyMembers(foreignTable,
                tableKeySchema.ForeignKeyMemberColumns, tableKeySchema.Name);

            string primaryMembers = GetKeyMembers(primaryTable,
                tableKeySchema.PrimaryKeyMemberColumns, tableKeySchema.Name);

            AssociationKey key = AssociationKey.CreateForeignKey(name);
            bool isNew = !foreignTable.Type.Associations.Contains(key);

            Association foreignAssociation;

            if (isNew)
                foreignAssociation = new Association(name);
            else
                foreignAssociation = foreignTable.Type.Associations[key];

            foreignAssociation.IsForeignKey = true;
            foreignAssociation.ThisKey = foreignMembers;
            foreignAssociation.OtherKey = primaryMembers;
            foreignAssociation.Type = primaryClass;

            string prefix = GetMemberPrefix(foreignAssociation, primaryClass, foreignClass);

            if (isNew)
            {
                foreignAssociation.Member = ToPropertyName(foreignTable.Type.Name, prefix + primaryClass);
                foreignAssociation.Storage = CommonUtility.GetFieldName(foreignAssociation.Member);
                foreignTable.Type.Associations.Add(foreignAssociation);
            }
            else
            {
                PropertyNames[foreignTable.Type.Name].Add(foreignAssociation.Member);
            }

            // add reverse association
            key = AssociationKey.CreatePrimaryKey(name);
            isNew = !primaryTable.Type.Associations.Contains(key);

            Association primaryAssociation;
            if (isNew)
                primaryAssociation = new Association(name);
            else
                primaryAssociation = primaryTable.Type.Associations[key];

            primaryAssociation.IsForeignKey = false;
            primaryAssociation.ThisKey = foreignAssociation.OtherKey;
            primaryAssociation.OtherKey = foreignAssociation.ThisKey;
            primaryAssociation.Type = foreignClass;

            bool isOneToOne = IsOneToOne(tableKeySchema, foreignAssociation);

            if (primaryAssociation.Cardinality == null && isOneToOne)
                primaryAssociation.Cardinality = Cardinality.One;

            if (isNew)
            {
                string propertyName = prefix + foreignClass;
                if (!isOneToOne)
                {
                    if (settings.AssociationNaming == AssociationNamingEnum.ListSuffix)
                        propertyName += "List";
                    else if (settings.AssociationNaming == AssociationNamingEnum.Plural)
                        propertyName = StringUtil.ToPlural(propertyName);
                }

                primaryAssociation.Member = ToPropertyName(primaryTable.Type.Name, propertyName);
                primaryAssociation.Storage = CommonUtility.GetFieldName(primaryAssociation.Member);
                primaryTable.Type.Associations.Add(primaryAssociation);
            }
            else
            {
                PropertyNames[primaryTable.Type.Name].Add(primaryAssociation.Member);
            }

            if (IsTableKeySchemaCascadeDelete(tableKeySchema))
            {
                foreignAssociation.DeleteRule = "CASCADE";
            }

            if (settings.IncludeDeleteOnNull || foreignTable.Type.IsManyToMany())
            {
                if (!foreignAssociation.DeleteOnNull.HasValue && IsTableDeleteOnNull(tableKeySchema))
                    foreignAssociation.DeleteOnNull = true;
            }
            else
                foreignAssociation.DeleteOnNull = null;

            foreignAssociation.IsProcessed = true;
            primaryAssociation.IsProcessed = true;
        }
        private static bool IsOneToOne(TableKeySchema tableKeySchema, Association foreignAssociation)
        {
            bool isFkeyPkey = tableKeySchema.ForeignKeyTable.HasPrimaryKey
                              && tableKeySchema.ForeignKeyTable.PrimaryKey != null
                              && tableKeySchema.ForeignKeyTable.PrimaryKey.MemberColumns.Count == 1
                              && tableKeySchema.ForeignKeyTable.PrimaryKey.MemberColumns.Contains(foreignAssociation.ThisKey);

            if (isFkeyPkey)
                return true;

            // if f.key is unique
            foreach (var column in tableKeySchema.ForeignKeyMemberColumns)
                if (!column.IsUnique)
                    return false;

            return true;
        }
        private static string GetMemberPrefix(Association association, string primaryClass, string foreignClass)
        {
            bool isSameName = association.ThisKey.Equals(association.OtherKey,
                StringComparison.OrdinalIgnoreCase);
            isSameName = (isSameName || association.ThisKey.Equals(
                primaryClass + association.OtherKey,
                StringComparison.OrdinalIgnoreCase));

            string prefix = string.Empty;
            if (isSameName)
                return prefix;

            prefix = association.ThisKey.Replace(association.OtherKey, "");
            prefix = prefix.Replace(primaryClass, "");
            prefix = prefix.Replace(foreignClass, "");
            prefix = CleanIdRegex.Replace(prefix, "");

            // FIX ME
            Regex regex = new Regex(@"^\d");
            if (regex.IsMatch(prefix))
                prefix = String.Format("My{0}", prefix);

            return prefix;
        }
Beispiel #9
0
 public override Association VisitAssociation(Association association)
 {
     if (!associations.ContainsKey(association.Name))
     {
         associations[association.Name] = new List<Association>();
     }
     associations[association.Name].Add(association);
     associationTypes[association] = currentType;
     return base.VisitAssociation(association);
 }
Beispiel #10
0
            private static Association ReadAssociation(XmlTextReader reader)
            {
                ValidateAttributes(reader,
                                   new[]
                                       {
                                           "Name", "Type", "Member", "Storage", "AccessModifier", "Modifier",
                                           "IsForeignKey", "Cardinality", "DeleteOnNull", "ThisKey", "OtherKey",
                                           "DeleteRule"
                                       });
                var association = new Association("");
                string attribute = reader.GetAttribute("Name");
                if (attribute == null)
                {
                    throw Error.RequiredAttributeMissingViolation("Name", reader.LineNumber);
                }
                if (reader.GetAttribute("Type") == null)
                {
                    throw Error.RequiredAttributeMissingViolation("Type", reader.LineNumber);
                }
                association.Name = attribute;
                association.Member = reader.GetAttribute("Member");
                if (reader.GetAttribute("Member") == null)
                {
                    throw Error.RequiredAttributeMissingViolation("Member", reader.LineNumber);
                }
                association.Storage = reader.GetAttribute("Storage");
                attribute = reader.GetAttribute("AccessModifier");
                try
                {
                    if (attribute == null)
                        association.AccessModifier = null;
                    else
                        association.AccessModifier =
                            (AccessModifier) Enum.Parse(typeof (AccessModifier), attribute, true);

                    attribute = reader.GetAttribute("Modifier");
                    if (attribute == null)
                        association.Modifier = null;
                    else
                        association.Modifier = (MemberModifier) Enum.Parse(typeof (MemberModifier), attribute, true);

                    attribute = reader.GetAttribute("IsForeignKey");
                    association.IsForeignKey = (attribute == null) ? null : new bool?(bool.Parse(attribute));

                    attribute = reader.GetAttribute("Cardinality");
                    if (attribute == null)
                        association.Cardinality = null;
                    else
                        association.Cardinality = (Cardinality) Enum.Parse(typeof (Cardinality), attribute, true);

                    attribute = reader.GetAttribute("DeleteOnNull");
                    association.DeleteOnNull = (attribute == null) ? null : new bool?(bool.Parse(attribute));
                }
                catch (FormatException)
                {
                    throw Error.InvalidBooleanAttributeValueViolation(attribute, reader.LineNumber);
                }
                catch (ArgumentException)
                {
                    throw Error.InvalidEnumAttributeValueViolation(attribute, reader.LineNumber);
                }
                association.SetThisKey(ParseKeyField(reader.GetAttribute("ThisKey")));
                association.Type = reader.GetAttribute("Type");
                association.SetOtherKey(ParseKeyField(reader.GetAttribute("OtherKey")));
                association.DeleteRule = reader.GetAttribute("DeleteRule");
                AssertEmptyElement(reader);
                return association;
            }