Пример #1
0
        /// <summary>
        /// This will return the target column name.  this is important because we do not want to return the column name of the current table, but rather that
        /// column it is pointing to.  this DOES NOT check and see if the item already exists.
        /// </summary>
        /// <param name="This">The Context Relationship</param>
        /// <param name="ContextSchemaObjectName">The parent with the column name you don't want</param>
        /// <returns></returns>
        private static string AsObjectPropertyNameAttempt(this RelationshipSummary relGroupSummary)
        {
            var PROC_NAME         = string.Format("RelationshipExtentions.AsObjectPropertyNameAttempt('FKName={0}')", relGroupSummary.Name);
            var ToObjectFieldName = "";

            try
            {
                var    entity = relGroupSummary.Entity;
                var    RelationshipObjectNameList = new List <string>();
                string ToTableName = entity.Parent.Entities[relGroupSummary.ToTableName].Alias;
                ToObjectFieldName = ToTableName.ToCsObjectName();
                //string ToObjectFieldName = relGroupSummary.ToUniqueColumnName().ToCsObjectName();
                var CountOfThisEntityInRelationships = relGroupSummary.Entity.Relationships.CountItems(RelationSearchField.ToTableName, relGroupSummary.ToTableName);
                if (CountOfThisEntityInRelationships > 1)
                {
                    //ToObjectFieldName = ToTableName + string.Join(",", relGroupSummary.ToColumnName).ToCsObjectName();
                    ToObjectFieldName = ((relGroupSummary.MultiplicityType.EndsAsMany() ?
                                          relGroupSummary.ToUniqueColumnName().ToPlural() :
                                          string.Join(",", relGroupSummary.ToColumnName) + Internal.AppSettings.Instance.Configuration.Database.InverseFKTargetNameCollisionSuffix)
                                         ).ToCsObjectName();
                }
                else
                {
                    ToObjectFieldName = ((relGroupSummary.MultiplicityType.EndsAsMany() ?
                                          relGroupSummary.ToUniqueColumnName().ToPlural() :
                                          ToObjectFieldName + Internal.AppSettings.Instance.Configuration.Database.InverseFKTargetNameCollisionSuffix)
                                         ).ToCsObjectName();
                }
                return(ToObjectFieldName);
            }
            catch (Exception ex)
            {
                return(string.Format("/* ERROR: {0} */", string.Format("{0}: {1}", PROC_NAME, ex.Message)));
            }
        }
Пример #2
0
        /// <summary>
        /// This will return the target column name.  this is important because we do not want to return the column name of the current table, but rather that
        /// column it is pointing to
        /// </summary>
        /// <param name="This">The Context Relationship</param>
        /// <param name="ContextSchemaObjectName">The parent with the column name you don't want</param>
        /// <returns></returns>
        public static string AsObjectPropertyName(this RelationshipSummary relGroupSummary, bool CheckForObjectNameExistance)
        {
            var PROC_NAME         = string.Format("RelationshipExtentions.AsObjectPropertyName('FKName={0}')", relGroupSummary.Name);
            var ToObjectFieldName = "";

            try
            {
                ToObjectFieldName = relGroupSummary.AsObjectPropertyNameAttempt();

                var objectNameList = new Dictionary <string, int>();
                if (CheckForObjectNameExistance)
                {
                    objectNameList = relGroupSummary.Entity.ObjectNameCounts();
                }
                if ((objectNameList.ContainsKey(ToObjectFieldName)) && (objectNameList[ToObjectFieldName] > 1))
                {
                    //If we already have this object name, then lets try to see if we can make a unique name with the Column names
                    ToObjectFieldName = ToObjectFieldName + string.Join(",", relGroupSummary.ToColumnName).ToCsObjectName();
                }

                if ((objectNameList.ContainsKey(ToObjectFieldName)) && (objectNameList[ToObjectFieldName] > 1))
                {
                    throw new Exception(string.Format("Object name {0} already exists in the object list :(", ToObjectFieldName));
                }
                return(ToObjectFieldName);
            }
            catch (Exception ex)
            {
                return(string.Format("/* ERROR: {0} */", string.Format("{0}: {1}", PROC_NAME, ex.Message)));
            }
        }
Пример #3
0
        /// <summary>
        /// Groups a list of relationships by Name
        /// </summary>
        /// <param name="relationshipList">The relationship list.</param>
        /// <returns></returns>
        public static RelationshipSummary AsSummary(this IRelationshipList relationshipList)
        {
            var ret = new RelationshipSummary();
            var i   = 0;

            if (relationshipList != null)
            {
                foreach (var relationship in relationshipList)
                {
                    try
                    {
                        i++;
                        if (i == 1)
                        {
                            ret.Entity           = relationship.Parent;
                            ret.FromTableName    = relationship.FromTableName;
                            ret.Name             = relationship.Name;
                            ret.ToTableName      = relationship.ToTableName;
                            ret.PrimaryTableName = relationship.PrimaryTableName;
                            ret.MultiplicityType = relationship.MultiplicityType;
                            ret.Type             = relationship.Type;
                        }
                        else
                        {
                            var isValidMultiplicty = (
                                (
                                    ((ret.MultiplicityType == RelationshipMultiplicityType.ManyToZeroOrOne) || (ret.MultiplicityType == RelationshipMultiplicityType.ManyToOne)) &&
                                    ((relationship.MultiplicityType == RelationshipMultiplicityType.ManyToOne) || (relationship.MultiplicityType == RelationshipMultiplicityType.ManyToZeroOrOne))
                                )
                                ||
                                (
                                    ((ret.MultiplicityType == RelationshipMultiplicityType.ZeroOrOneToMany) || (ret.MultiplicityType == RelationshipMultiplicityType.OneToMany)) &&
                                    ((relationship.MultiplicityType == RelationshipMultiplicityType.OneToMany) || (relationship.MultiplicityType == RelationshipMultiplicityType.ZeroOrOneToMany) ||
                                     (relationship.MultiplicityType == RelationshipMultiplicityType.ZeroOrOneToOne))
                                )
                                );
                            if (ret.MultiplicityType != relationship.MultiplicityType)
                            {
                                Console.WriteLine(string.Format(@"Multiplicity for FK {0} mismatched but are valid warning ({1} vs {2}): 
 FromTableName:{3}, ToTableName:{4}, PrimaryTableName:{5}",
                                                                relationship.Name, ret.MultiplicityType.ToString(), relationship.MultiplicityType.ToString(), relationship.FromTableName,
                                                                relationship.ToTableName, relationship.PrimaryTableName));
                                ret.MultiplicityTypeWarning = true;
                            }
                            if (!(
                                    (ret.FromTableName == relationship.FromTableName) &&
                                    (ret.Name == relationship.Name) &&
                                    (ret.ToTableName == relationship.ToTableName) &&
                                    (isValidMultiplicty) &&
                                    (ret.PrimaryTableName == relationship.PrimaryTableName)))
                            {
                                throw new Exception(string.Format(@"Relationship List is not grouped! 
 FromTableName:{0}={1},  Name:{2}={3},  
 ToTableName:{4}={5}, MultiplicityType{6}={7},
 PrimaryTableName:{8}={9}", ret.FromTableName, relationship.FromTableName, ret.Name, relationship.Name,
                                                                  ret.ToTableName, relationship.ToTableName, ret.MultiplicityType.ToString(), relationship.MultiplicityType.ToString(), ret.PrimaryTableName, relationship.PrimaryTableName));
                            }
                        }
                        ret.ToColumnName.Add(relationship.ToColumnName);
                        ret.ToFieldName.Add(relationship.ToFieldName);
                        var ToProperty = relationship.Parent.Parent[relationship.ToTableName].Properties[relationship.ToFieldName];
                        ret.ToColumnProperties.Add(ToProperty);
                        ret.ToObjectPropertyName.Add(ToProperty.AsObjectPropertyName());
                        ret.FromColumnName.Add(relationship.FromColumnName);
                        ret.FromFieldName.Add(relationship.FromFieldName);
                        var FromProperty = relationship.Parent.Parent[relationship.FromTableName].Properties[relationship.FromFieldName];
                        ret.FromColumnProperties.Add(FromProperty);
                        ret.FromObjectPropertyName.Add(FromProperty.AsObjectPropertyName());

                        ret.MultiplicityTypes.Add(relationship.MultiplicityType);
                        ret.Types.Add(relationship.Type);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("Relationship {0} error!", relationship.Name), ex);
                    }
                }
            }
            return(ret);
        }
Пример #4
0
        /// <summary>
        /// Used to figure out what the target object name for the end of this particular relationship
        /// </summary>
        /// <param name="thisRelationship">The this relationship.</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string EndAsObjectPropertyName(this RelationshipSummary thisRelationship)
        {
            var PROC_NAME = string.Format("RelationshipExtentions.ToObjectPropertyName('{0}')", thisRelationship.Name);

            return(EndAsObjectPropertyName(thisRelationship.Name, thisRelationship.Entity));
        }
Пример #5
0
 /// <summary>
 /// This will return the target column name.  this is important because we do not want to return the column name of the current table, but rather that
 /// column it is pointing to
 /// </summary>
 /// <param name="This">The Context Relationship</param>
 /// <param name="ContextSchemaObjectName">The parent with the column name you don't want</param>
 /// <returns></returns>
 public static string AsObjectPropertyName(this RelationshipSummary relGroupSummary)
 {
     return(relGroupSummary.AsObjectPropertyName(true));
 }
Пример #6
0
        /// <summary>
        /// This will return the target column name.  this is important because we do not want to return the column name of the current table, but rather that
        /// column it is pointing to
        /// </summary>
        /// <param name="This"></param>
        /// <param name="UseFromAsDefault">Use From Table name as an alias, otherwise use the ToTableAlias</param>
        /// <returns></returns>
        public static string ToUniqueColumnName(this RelationshipSummary This, bool?UseFromAsDefault = null)
        {
            var targetColumnNameCount = 0;
            var fromColumnNameCount   = 0;

            if ((This.FromColumnName.Count == 2) && (This.ToColumnName.Count == 2) && (This.FromTableName.Equals(This.ToTableName)))
            {
                //This relationship is self referencing,  I am sure this will cause an issue with compound self referencing keys,  lets cross the bridge when we get there
                return(This.ToColumnName[0]);
            }
            //Try to see if a combination of names will match a column name that already exists
            foreach (var rel in This.Entity.Relationships)
            {
                if (rel.ToColumnName == string.Join("", This.ToColumnName))
                {
                    targetColumnNameCount++;
                }
            }
            foreach (var rel in This.Entity.Relationships)
            {
                if (rel.FromColumnName == string.Join("", This.FromColumnName))
                {
                    fromColumnNameCount++;
                }
            }
            if (targetColumnNameCount == 1)
            {
                return(string.Join("", This.ToColumnName).Trim());
            }
            if (fromColumnNameCount == 1)
            {
                return(string.Join("", This.FromColumnName).Trim());
            }
            //If we didn't find one,  then use the target object name
            fromColumnNameCount   = 0;
            targetColumnNameCount = 0;
            var FromTableAlias = This.Entity.Parent[This.FromTableName].Alias;
            var ToTableAlias   = This.Entity.Parent[This.ToTableName].Alias;

            foreach (var rel in This.Entity.Relationships)
            {
                if (rel.ToColumnName == ToTableAlias)
                {
                    targetColumnNameCount++;
                }
            }
            foreach (var rel in This.Entity.Relationships)
            {
                if (rel.FromColumnName == FromTableAlias)
                {
                    fromColumnNameCount++;
                }
            }
            if (targetColumnNameCount == 0)
            {
                return(ToTableAlias.Trim());
            }
            if (fromColumnNameCount == 0)
            {
                return(FromTableAlias.Trim());
            }
            if (!UseFromAsDefault.HasValue)
            {
                throw new Exception(string.Format("EzDbSchemaRelationshipExtentions.ToUniqueColumnName: Could not find any unique column names to write to :( {0} or {1} for {2}", This.ToColumnName, This.FromColumnName, This.Name));
            }
            if (UseFromAsDefault.Value)
            {
                return(FromTableAlias.Trim());
            }
            return(ToTableAlias.Trim());
        }