|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Linq.Expressions; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Fclp; |
| 10 | +using MySql.Data.MySqlClient; |
| 11 | + |
| 12 | +namespace MySQL_To_CSharp |
| 13 | +{ |
| 14 | + |
| 15 | + public class ApplicationArguments |
| 16 | + { |
| 17 | + public string IP { get; set; } |
| 18 | + public int Port { get; set; } |
| 19 | + public string User { get; set; } |
| 20 | + public string Password { get; set; } |
| 21 | + public string Database { get; set; } |
| 22 | + public string Table { get; set; } |
| 23 | + } |
| 24 | + |
| 25 | + public class Column |
| 26 | + { |
| 27 | + public string Name { get; set; } |
| 28 | + public Type Type { get; set; } |
| 29 | + |
| 30 | + public Column(MySqlDataReader reader) |
| 31 | + { |
| 32 | + this.Name = reader.GetString(1); |
| 33 | + } |
| 34 | + |
| 35 | + public override string ToString() |
| 36 | + { |
| 37 | + return $"public {this.Type.Name} {this.Name} {{ get; set; }}"; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + class Program |
| 42 | + { |
| 43 | + private static void DbToClasses(string dbName, Dictionary<string, List<Column>> db) |
| 44 | + { |
| 45 | + if (!Directory.Exists(dbName)) |
| 46 | + Directory.CreateDirectory(dbName); |
| 47 | + |
| 48 | + var sb = new StringBuilder(); |
| 49 | + foreach (var table in db) |
| 50 | + { |
| 51 | + sb.AppendLine($"public class {table.Key}"); |
| 52 | + sb.AppendLine("{"); |
| 53 | + foreach (var column in table.Value) |
| 54 | + { |
| 55 | + sb.AppendLine(column.ToString()); |
| 56 | + } |
| 57 | + sb.AppendLine("}"); |
| 58 | + |
| 59 | + var sw = new StreamWriter($"{dbName}/{table.Key}.cs", false); |
| 60 | + sw.Write(sb.ToString()); |
| 61 | + sw.Close(); |
| 62 | + sb.Clear(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static void Main(string[] args) |
| 67 | + { |
| 68 | + var parser = new FluentCommandLineParser<ApplicationArguments>(); |
| 69 | + parser.Setup(arg => arg.IP).As('i', "ip").SetDefault("127.0.0.1").WithDescription("(optional) IP address of the MySQL server, will use 127.0.0.1 if not specified"); |
| 70 | + parser.Setup(arg => arg.Port).As('n', "port").SetDefault(3306).WithDescription("(optional) Port number of the MySQL server, will use 3306 if not specified"); |
| 71 | + parser.Setup(arg => arg.User).As('u', "user").SetDefault("root").WithDescription("(optional) Username, will use root if not specified"); |
| 72 | + parser.Setup(arg => arg.Password).As('p', "password").SetDefault(String.Empty).WithDescription("(optional) Password, will use empty password if not specified"); |
| 73 | + parser.Setup(arg => arg.Database).As('d', "database").Required().WithDescription("Database name"); |
| 74 | + parser.Setup(arg => arg.Table).As('t', "table").SetDefault(String.Empty).WithDescription("(optional) Table name, will generate entire database if not specified"); |
| 75 | + parser.SetupHelp("?", "help").Callback(text => Console.WriteLine(text)); |
| 76 | + |
| 77 | + #if DEBUG |
| 78 | + args = new [] { "-p", "123", "-d", "az_world"}; |
| 79 | + #endif |
| 80 | + |
| 81 | + var result = parser.Parse(args); |
| 82 | + if (!result.HasErrors) |
| 83 | + { |
| 84 | + var conf = parser.Object as ApplicationArguments; |
| 85 | + if (conf.Database is null) |
| 86 | + { |
| 87 | + Console.WriteLine("You didn't specify a database"); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + var confString = |
| 92 | + $"Server={conf.IP};Port={conf.Port};Uid={conf.User};Pwd={conf.Password};Database={conf.Database}"; |
| 93 | + Console.WriteLine(confString); |
| 94 | + |
| 95 | + var database = new Dictionary<string, List<Column>>(); |
| 96 | + |
| 97 | + using (var con = new MySqlConnection(confString)) |
| 98 | + { |
| 99 | + con.Open(); |
| 100 | + |
| 101 | + using (var cmd = con.CreateCommand()) |
| 102 | + { |
| 103 | + cmd.CommandText = |
| 104 | + $"SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{conf.Database}'"; |
| 105 | + if (!conf.Table.Equals(string.Empty)) |
| 106 | + cmd.CommandText += $" AND TABLE_NAME = '{conf.Table}'"; |
| 107 | + |
| 108 | + var reader = cmd.ExecuteReader(); |
| 109 | + if (!reader.HasRows) |
| 110 | + return; |
| 111 | + |
| 112 | + while (reader.Read()) |
| 113 | + if (database.ContainsKey(reader.GetString(0))) |
| 114 | + database[reader.GetString(0)].Add(new Column(reader)); |
| 115 | + else |
| 116 | + database.Add(reader.GetString(0), new List<Column>() { new Column(reader) }); |
| 117 | + } |
| 118 | + |
| 119 | + foreach (var table in database) |
| 120 | + { |
| 121 | + using (var cmd = con.CreateCommand()) |
| 122 | + { |
| 123 | + // lul - is there a way to do this without this senseless statement? |
| 124 | + cmd.CommandText = $"SELECT * FROM {table.Key} LIMIT 0"; |
| 125 | + var reader = cmd.ExecuteReader(); |
| 126 | + var schema = reader.GetSchemaTable(); |
| 127 | + foreach (var column in table.Value) |
| 128 | + column.Type = schema.Select($"ColumnName = '{column.Name}'")[0]["DataType"] as Type; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + con.Close(); |
| 133 | + } |
| 134 | + |
| 135 | + DbToClasses(conf.Database, database); |
| 136 | + } |
| 137 | + Console.WriteLine("Successfully generated C# classes!"); |
| 138 | + Console.ReadLine(); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments