Beispiel #1
0
        public void SortKeys(List <IForeignKeyConstraint> fKeys, List <IResultObject> parentTables)
        {
            var sorted = false;

            var parentTable = parentTables[0].ObjectName;

            IForeignKeyConstraint[] fkArray = fKeys.ToArray();

            // sort collection so that keys that reference the parent table come before other keys
            for (int i = 0; i < fkArray.Length; i++)
            {
                if (parentTable == fkArray[i].PKTable.ObjectName)
                {
                    IForeignKeyConstraint fkey = fkArray[i];

                    for (int j = i; j > 0; j--)
                    {
                        fkArray[j] = fkArray[j - 1];
                    }
                    fkArray[0] = fkey;
                    sorted     = true;
                }
            }

            if (sorted)
            {
                fKeys.Clear();
                fKeys.AddRange(fkArray);
            }

            // sort collection so that Primary Keys that reference the parent table come before other non-primary keys
            sorted = false;
            for (int i = 0; i < fkArray.Length; i++)
            {
                if (parentTable == fkArray[i].PKTable.ObjectName && fkArray[i].Columns[0].PKColumn.IsPrimaryKey)
                {
                    IForeignKeyConstraint fkey = fkArray[i];

                    for (int j = i; j > 0; j--)
                    {
                        fkArray[j] = fkArray[j - 1];
                    }
                    fkArray[0] = fkey;
                    sorted     = true;
                }
            }

            if (sorted)
            {
                fKeys.Clear();
                fKeys.AddRange(fkArray);
            }
        }
        /// <summary>
        ///     <para>
        ///         Creates a human-readable representation of the given metadata.
        ///     </para>
        ///     <para>
        ///         Warning: Do not rely on the format of the returned string.
        ///         It is designed for debugging only and may change arbitrarily between releases.
        ///     </para>
        /// </summary>
        /// <param name="foreignKey"> The metadata item. </param>
        /// <param name="options"> Options for generating the string. </param>
        /// <param name="indent"> The number of indent spaces to use before each new line. </param>
        /// <returns> A human-readable representation. </returns>
        public static string ToDebugString(
            [NotNull] this IForeignKeyConstraint foreignKey,
            MetadataDebugStringOptions options,
            int indent = 0)
        {
            var builder      = new StringBuilder();
            var indentString = new string(' ', indent);

            builder.Append(indentString);
            var singleLine = (options & MetadataDebugStringOptions.SingleLine) != 0;

            if (singleLine)
            {
                builder.Append("ForeignKey: ");
            }

            builder
            .Append(foreignKey.Name)
            .Append(" ")
            .Append(foreignKey.Table.Name)
            .Append(" ")
            .Append(Column.Format(foreignKey.Columns))
            .Append(" -> ")
            .Append(foreignKey.PrincipalTable.Name)
            .Append(" ")
            .Append(Column.Format(foreignKey.PrincipalColumns));

            if (foreignKey.OnDeleteAction != ReferentialAction.NoAction)
            {
                builder
                .Append(" ")
                .Append(foreignKey.OnDeleteAction);
            }

            if (!singleLine &&
                (options & MetadataDebugStringOptions.IncludeAnnotations) != 0)
            {
                builder.Append(foreignKey.AnnotationsToDebugString(indent + 2));
            }

            return(builder.ToString());
        }
    /// <summary>
    ///     Creates a new <see cref="AddForeignKeyOperation" /> from the specified foreign key.
    /// </summary>
    /// <param name="foreignKey">The foreign key.</param>
    /// <returns>The operation.</returns>
    public static AddForeignKeyOperation CreateFrom(IForeignKeyConstraint foreignKey)
    {
        Check.NotNull(foreignKey, nameof(foreignKey));

        var operation = new AddForeignKeyOperation
        {
            Schema           = foreignKey.Table.Schema,
            Table            = foreignKey.Table.Name,
            Name             = foreignKey.Name,
            Columns          = foreignKey.Columns.Select(c => c.Name).ToArray(),
            PrincipalSchema  = foreignKey.PrincipalTable.Schema,
            PrincipalTable   = foreignKey.PrincipalTable.Name,
            PrincipalColumns = foreignKey.PrincipalColumns.Select(c => c.Name).ToArray(),
            OnDelete         = foreignKey.OnDeleteAction
        };

        operation.AddAnnotations(foreignKey.GetAnnotations());

        return(operation);
    }
 /// <inheritdoc />
 public virtual IEnumerable <IAnnotation> ForRemove(IForeignKeyConstraint foreignKey)
 => Enumerable.Empty <IAnnotation>();
 /// <inheritdoc />
 public virtual IEnumerable <IAnnotation> For(IForeignKeyConstraint foreignKey, bool designTime)
 => Enumerable.Empty <IAnnotation>();
 public IEnumerable <IAnnotation> ForRemove(IForeignKeyConstraint foreignKey) => _providers.SelectMany(p => p.ForRemove(foreignKey));