public static string BuildDdl(Schema schema, ITypeMapper typeMapper) { var ddlBuilder = new StringBuilder(); foreach (var entity in schema.Entities) { ddlBuilder.Append(EntitySqlBuilder.BuildDdl(entity) + ";"); ddlBuilder.AppendLine(); } ddlBuilder.AppendLine(); BuildManyToMany(schema, ddlBuilder, typeMapper); BuildConstraintsAndRelation(schema, ddlBuilder); return(ddlBuilder.ToString()); }
private static void BuildManyToMany(Schema schema, StringBuilder ddlBuilder, ITypeMapper typeMapper) { var manyToMany = schema.Entities .SelectMany(e => e.Columns) .Select(cols => cols.Relation) .Where(relation => relation?.GetType() == typeof(ManyToMany)) .Select(m => m as ManyToMany) .Distinct(new ManyToManyComparer()) .ToList(); manyToMany .Select(m => EntitySqlBuilder.BuildManyToManyDdl(m, typeMapper)) .Aggregate(ddlBuilder, (builder, s) => builder.AppendLine(s + ";")); ddlBuilder.AppendLine(); manyToMany .Select(m => BuildPrimaryKey(m.TableName, new[] { m.ForeignKeyFar, m.ForeignKeyNear })) .Aggregate(ddlBuilder, (builder, s) => builder.AppendLine(s + ";")); }