コード例 #1
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());
        }