public string Generate(string @namespace, string className, MySqlTableDefinition table, ITypeConverter typeConverter) { var builder = new StringBuilder(); // namespace builder.AppendLine($"// ------------------------------------------------------------------------------"); builder.AppendLine($"// <auto-generated>"); builder.AppendLine($"// Code Generated by {nameof(MySQLToCsharp)}"); builder.AppendLine($"// </auto-generated>"); builder.AppendLine($"// ------------------------------------------------------------------------------"); builder.AppendLine(); builder.AppendLine($"using System;"); builder.AppendLine($"using System.ComponentModel.DataAnnotations;"); builder.AppendLine($"using System.ComponentModel.DataAnnotations.Schema;"); builder.AppendLine(); builder.AppendLine($"namespace { @namespace}"); builder.AppendLine("{"); // class if (!string.IsNullOrWhiteSpace(table.Comment)) { builder.AppendLineIndent4($"/// <summary>"); builder.AppendLineIndent4($"/// {table.Comment}"); builder.AppendLineIndent4($"/// </summary>"); } builder.AppendLineIndent4($@"public partial class {className}"); builder.AppendLineIndent4("{"); // property foreach (var column in table.Columns) { if (!string.IsNullOrWhiteSpace(column.Comment)) { builder.AppendLineIndent8($"/// <summary>"); builder.AppendLineIndent8($"/// {column.Comment}"); builder.AppendLineIndent8($"/// </summary>"); } var(clrType, attributes) = typeConverter.Convert(column.Data); if (column.PrimaryKeyReference != null) { builder.AppendLineIndent8($"[Key]"); builder.AppendLineIndent8($"[Column(Order = {column.Order})]"); } foreach (var attribute in attributes) { builder.AppendLineIndent8($"[{attribute}]"); } builder.AppendLineIndent8($"public {clrType} {column.Name} {{ get; set; }}"); } builder.AppendLineIndent4(@"}"); builder.AppendLine("}"); return(InternalUtils.NormalizeNewLines(builder.ToString())); }
private string Generate(string @namespace, string classname, MySqlTableDefinition table, ITypeConverter typeConverter) { var builder = new StringBuilder(); // TODO: change signature to "Auto Generated by MySQLToCsharp" // namespace builder.Append($@"// auto generated by SqlToCsharp using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace {@namespace} {{ "); // class if (!string.IsNullOrWhiteSpace(table.Comment)) { builder.AppendLine($" /// <summary>"); builder.AppendLine($" /// {table.Comment}"); builder.AppendLine($" /// </summary>"); } builder.Append($@" public partial class {classname} {{ "); // property foreach (var column in table.Columns) { if (!string.IsNullOrWhiteSpace(column.Comment)) { builder.AppendLine($" /// <summary>"); builder.AppendLine($" /// {column.Comment}"); builder.AppendLine($" /// </summary>"); } var(clrType, attributes) = typeConverter.Convert(column.Data); if (column.PrimaryKeyReference != null) { builder.AppendLine($" [Key]"); builder.AppendLine($" [Column(Order = {column.Order})]"); } foreach (var attribute in attributes) { builder.AppendLine($" [{attribute}]"); } builder.AppendLine($" public {clrType} {column.Name} {{ get; set; }}"); } builder.Append(@" } } "); return(builder.ToString()); }
public void Save(string @namespace, MySqlTableDefinition table, string outputFolderPath) { var @class = GetClassName(table.Name); var fileName = @class + extension; var outputFile = Path.Combine(outputFolderPath, fileName); var generated = Generate(@namespace, @class, table, TypeConverter); if (!Directory.Exists(outputFolderPath)) { Directory.CreateDirectory(outputFolderPath); } if (File.Exists(outputFile)) { var current = File.ReadAllText(outputFile, encoding); if (generated == current) { Console.WriteLine($"[-] skipped: {fileName} (no change)"); return; } } Console.WriteLine($"[o] generate: {fileName}"); File.WriteAllText(outputFile, generated, encoding); }