Пример #1
0
        internal static IList <TableMapping.ForeignKey> GetForeignKeys(PropertyInfo[] properties)
        {
            var foreignKeys = new List <TableMapping.ForeignKey>();

            var attributes = from attribute in
                             (from p in properties
                              from att in p.GetAttributes <ForeignKeyAttribute>()
                              select new
            {
                Attribute = att,
                Property = p,
                att.Order
            })
                             orderby attribute.Order
                             select attribute;

            foreach (var att in attributes)
            {
                TableMapping.ForeignKey key = foreignKeys.FirstOrDefault(fk => fk.Name == att.Attribute.Name);
                var childTable = att.Attribute.ChildTable;
                if (key == null)
                {
                    key = new TableMapping.ForeignKey
                    {
                        Name       = att.Attribute.Name,
                        ChildTable = GetTableName(childTable),
                        OnDelete   = att.Attribute.OnDeleteAction,
                        OnUpdate   = att.Attribute.OnUpdateAction,
                        NullMatch  = att.Attribute.NullMatch,
                        Deferred   = att.Attribute.Deferred
                    };
                    foreignKeys.Add(key);
                }

                var childProps = childTable.GetMappableProperties();
                var childProp  = childProps.SingleOrDefault(x => x.Name == att.Attribute.ChildKey);

                if (childProp == null)
                {
                    throw new ArgumentException(
                              string.Format("Property {0} does not exist in type {1}.",
                                            att.Attribute.ChildKey, childTable.FullName));
                }

                key.Keys.Add(GetColumnName(att.Property), GetColumnName(childProp));
            }

            return(foreignKeys);
        }
Пример #2
0
        public static string GetCreateSql(this TableMapping.ForeignKey foreignKey)
        {
            var sb = new StringBuilder();

            if (!foreignKey.Name.IsNullOrWhitespace())
            {
                sb.Append("CONSTRAINT ");
                sb.Append(Quote(foreignKey.Name));
                sb.AppendLine();
            }
            sb.Append("FOREIGN KEY (");
            sb.Append(string.Join(", ", foreignKey.Keys.Keys.Select(Quote).ToArray()));
            sb.Append(")");
            sb.AppendLine();
            sb.Append("REFERENCES ");
            sb.Append(Quote(foreignKey.ChildTable));
            sb.Append(" (");
            sb.Append(string.Join(", ", foreignKey.Keys.Values.Select(Quote).ToArray()));
            sb.Append(")");
            if (foreignKey.OnUpdate != ForeignKeyAction.Default)
            {
                sb.AppendLine();
                sb.Append("ON UPDATE ");
                sb.Append(OrmHelper.GetForeignKeyActionString(foreignKey.OnUpdate));
            }
            if (foreignKey.OnDelete != ForeignKeyAction.Default)
            {
                sb.AppendLine();
                sb.Append("ON DELETE ");
                sb.Append(OrmHelper.GetForeignKeyActionString(foreignKey.OnDelete));
            }
            if (foreignKey.NullMatch != NullMatch.Default)
            {
                sb.AppendLine();
                sb.Append("MATCH ");
                sb.Append(foreignKey.NullMatch);
            }
            if (foreignKey.Deferred != Deferred.Default)
            {
                sb.AppendLine();
                sb.Append("DEFERRABLE INITIALLY ");
                sb.Append(foreignKey.Deferred);
            }
            return(sb.ToString());
        }