private static void ProcessLines(List <string> lines) { var lastline = ""; string ns = "", folder = ""; for (var i = 0; i < lines.Count; i++) { var line = lines[i]; if (i == 0) { ns = GetPascalName(line, true); folder = CreateFolder(ns); } if (line.StartsWith("CREATE TABLE")) { var j = 0; var cls = new ClassObject(); while (!line.StartsWith(");")) { line = lines[i]; if (line.StartsWith(");")) { break; } if (j == 0) { Console.WriteLine(line); var name = GetPascalName(line.Replace("CREATE TABLE", ""), false); cls.Name = name; cls.Namespace = ns; if (lastline.StartsWith("--")) { cls.Comment = lastline.Replace("--", ""); } } else { ProcessProperty(cls, line); } j++; i++; } WriteClassFile(folder, cls); //Console.ReadKey(); } lastline = line; } }
private static void ProcessProperty(ClassObject cls, string line) { var skipStarts = new[] { "--", "UNIQUE", "PRIMARY KEY", "FOREIGN KEY", "CONSTRAINT" }; if (skipStarts.Any(line.StartsWith)) { return; } var arr = line.Split(' '); var prop = new PropertyObject(); prop.PropertyName = GetPascalName(arr[0], false); try { prop.RawType = arr[1]; } catch { Console.WriteLine(line); throw; } if (line.Contains("-- ")) { prop.Comment = line.Split("--", StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); } if (line.Contains("DEFAULT")) { prop.DefaultValue = line.Split("DEFAULT", StringSplitOptions.RemoveEmptyEntries).LastOrDefault()? .Replace(",", "").Replace("'", "").Trim(); if (prop.DefaultValue == "CURRENT_TIMESTAMP") { prop.DefaultValue = "DateTime.Now"; } if (prop.PropertyType == "string") { prop.DefaultValue = $"\"{prop.DefaultValue}\""; } } if (line.Contains("PRIMARY KEY")) { prop.IsPrimary = true; } if (line.Contains("NOT NULL")) { prop.IsNotNull = true; } cls.Properties.Add(prop); }
/// <summary> /// </summary> /// <param name="folder"></param> /// <param name="cls"></param> private static void WriteClassFile(string folder, ClassObject cls) { var filename = Path.Combine(folder, $"{cls.Name}.cs"); if (File.Exists(filename)) { return; } var sb = new StringBuilder(); sb.Append(@"/*--------------------------------------------------------------------------------------------- * Copyright (c) STB Chain. All rights reserved. * Licensed under the Source EULA. See License in the project root for license information. * Source code : https://github.com/stbchain * Website : http://www.soft2b.com/ *--------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------*/ using System; namespace STB.Core"); if (!string.IsNullOrWhiteSpace(cls.Namespace)) { sb.Append($".{cls.Namespace}"); } sb.AppendLine().AppendLine("{"); //begin ns if (!string.IsNullOrWhiteSpace(cls.Comment)) { sb.AppendLine("\t/// <summary>"); sb.AppendLine($"\t/// {cls.Comment}"); sb.AppendLine("\t/// </summary>"); } sb.AppendLine($"\tpublic class {cls.Name}"); sb.AppendLine("\t{"); //begin class foreach (var prop in cls.Properties) { if (string.IsNullOrWhiteSpace(prop.PropertyName)) { continue; } if (!string.IsNullOrWhiteSpace(prop.Comment)) { sb.AppendLine("\t\t/// <summary>"); sb.AppendLine($"\t\t/// {prop.Comment}"); sb.AppendLine("\t\t/// </summary>"); } sb.Append($"\t\tpublic {prop.PropertyType} {prop.PropertyName} {{get;set;}}"); if (prop.DefaultValue != null) { sb.Append($" = {prop.DefaultValue};"); } sb.AppendLine(); } sb.AppendLine("\t}"); //close class sb.AppendLine("}"); //close namespace Console.WriteLine(sb.ToString()); File.WriteAllText(filename, sb.ToString()); }