From 9c71fdea3d072409ded873bd1f24af0c8c44a697 Mon Sep 17 00:00:00 2001 From: Kevin Bernau Date: Tue, 22 Mar 2022 08:50:25 +0100 Subject: [PATCH] Fixed DataReader not being closed Cleaned up solution Updated nuget packages [Untested] Used Get functions instead of casting the types --- MySQL-To-CSharp/App.config | 57 ++++++- MySQL-To-CSharp/ApplicationArguments.cs | 2 +- MySQL-To-CSharp/Column.cs | 10 +- MySQL-To-CSharp/MySQL-To-CSharp.csproj | 171 ++++++++++++++------- MySQL-To-CSharp/Program.cs | 64 +++++--- MySQL-To-CSharp/Properties/AssemblyInfo.cs | 3 +- MySQL-To-CSharp/StringExtensions.cs | 2 +- MySQL-To-CSharp/packages.config | 11 +- 8 files changed, 220 insertions(+), 100 deletions(-) diff --git a/MySQL-To-CSharp/App.config b/MySQL-To-CSharp/App.config index 3841a61..7722548 100644 --- a/MySQL-To-CSharp/App.config +++ b/MySQL-To-CSharp/App.config @@ -1,11 +1,54 @@  - + - - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MySQL-To-CSharp/ApplicationArguments.cs b/MySQL-To-CSharp/ApplicationArguments.cs index 5f8b152..7a1eca7 100644 --- a/MySQL-To-CSharp/ApplicationArguments.cs +++ b/MySQL-To-CSharp/ApplicationArguments.cs @@ -12,4 +12,4 @@ public class ApplicationArguments public bool GenerateMarkupPages { get; set; } public string MarkupDatabaseNameReplacement { get; set; } } -} +} \ No newline at end of file diff --git a/MySQL-To-CSharp/Column.cs b/MySQL-To-CSharp/Column.cs index 4a5bae9..40d6745 100644 --- a/MySQL-To-CSharp/Column.cs +++ b/MySQL-To-CSharp/Column.cs @@ -5,19 +5,19 @@ namespace MySQL_To_CSharp { public class Column { - public string Name { get; set; } - public Type Type { get; set; } - public string ColumnType { get; set; } - public Column(MySqlDataReader reader) { Name = reader.GetString(1); ColumnType = reader.GetString(2); } + public string Name { get; set; } + public Type Type { get; set; } + public string ColumnType { get; set; } + public override string ToString() { return $"public {Type.Name} {Name.FirstCharUpper()} {{ get; set; }}"; } } -} +} \ No newline at end of file diff --git a/MySQL-To-CSharp/MySQL-To-CSharp.csproj b/MySQL-To-CSharp/MySQL-To-CSharp.csproj index a1c49c8..f97e544 100644 --- a/MySQL-To-CSharp/MySQL-To-CSharp.csproj +++ b/MySQL-To-CSharp/MySQL-To-CSharp.csproj @@ -1,62 +1,115 @@  - - - Debug - AnyCPU - {8673C77C-EACF-4140-8357-1A2BEA96ED8E} - Exe - MySQL_To_CSharp - MySQL-To-CSharp - v4.6.1 - 512 - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\FluentCommandLineParser.1.4.3\lib\net35\FluentCommandLineParser.dll - - - ..\packages\MySql.Data.6.10.4\lib\net452\MySql.Data.dll - - - - - - - - - - - - - - - - - - - - - - + + + Debug + AnyCPU + {8673C77C-EACF-4140-8357-1A2BEA96ED8E} + Exe + MySQL_To_CSharp + MySQL-To-CSharp + v4.6.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\BouncyCastle.1.8.9\lib\BouncyCastle.Crypto.dll + True + + + ..\packages\FluentCommandLineParser.1.4.3\lib\net35\FluentCommandLineParser.dll + + + ..\packages\Google.Protobuf.3.20.0-rc2\lib\net45\Google.Protobuf.dll + True + + + ..\packages\K4os.Compression.LZ4.1.2.16\lib\net46\K4os.Compression.LZ4.dll + True + + + ..\packages\K4os.Compression.LZ4.Streams.1.2.16\lib\net46\K4os.Compression.LZ4.Streams.dll + True + + + ..\packages\K4os.Hash.xxHash.1.0.7\lib\net46\K4os.Hash.xxHash.dll + True + + + + ..\packages\MySql.Data.8.0.28\lib\net452\MySql.Data.dll + True + + + + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + True + + + + + + + + + ..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll + True + + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + True + + + ..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + True + + + + + + + + + + ..\packages\MySql.Data.8.0.28\lib\net452\Ubiety.Dns.Core.dll + True + + + ..\packages\MySql.Data.8.0.28\lib\net452\ZstdNet.dll + True + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MySQL-To-CSharp/Program.cs b/MySQL-To-CSharp/Program.cs index ca71618..3c89086 100644 --- a/MySQL-To-CSharp/Program.cs +++ b/MySQL-To-CSharp/Program.cs @@ -7,30 +7,39 @@ namespace MySQL_To_CSharp { - class Program + internal class Program { - static void Main(string[] args) + private static void Main(string[] args) { var parser = new FluentCommandLineParser(); - 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"); - parser.Setup(arg => arg.Port).As('n', "port").SetDefault(3306).WithDescription("(optional) Port number of the MySQL server, will use 3306 if not specified"); - parser.Setup(arg => arg.User).As('u', "user").SetDefault("root").WithDescription("(optional) Username, will use root if not specified"); - parser.Setup(arg => arg.Password).As('p', "password").SetDefault(String.Empty).WithDescription("(optional) Password, will use empty password if not specified"); + 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"); + parser.Setup(arg => arg.Port).As('n', "port").SetDefault(3306) + .WithDescription("(optional) Port number of the MySQL server, will use 3306 if not specified"); + parser.Setup(arg => arg.User).As('u', "user").SetDefault("root") + .WithDescription("(optional) Username, will use root if not specified"); + parser.Setup(arg => arg.Password).As('p', "password").SetDefault(string.Empty) + .WithDescription("(optional) Password, will use empty password if not specified"); parser.Setup(arg => arg.Database).As('d', "database").Required().WithDescription("Database name"); - parser.Setup(arg => arg.Table).As('t', "table").SetDefault(String.Empty).WithDescription("(optional) Table name, will generate entire database if not specified"); + parser.Setup(arg => arg.Table).As('t', "table").SetDefault(string.Empty) + .WithDescription("(optional) Table name, will generate entire database if not specified"); parser.Setup(arg => arg.GenerateConstructorAndOutput).As('g', "generateconstructorandoutput") - .SetDefault(false).WithDescription("(optional) Generate a reading constructor and SQL statement output - Activate with -g true"); + .SetDefault(false) + .WithDescription( + "(optional) Generate a reading constructor and SQL statement output - Activate with -g true"); parser.Setup(arg => arg.GenerateMarkupPages).As('m', "generatemarkuppages") .SetDefault(false) - .WithDescription("(optional) Generate markup pages for database and tables which can be used in wikis - Activate with -m true"); + .WithDescription( + "(optional) Generate markup pages for database and tables which can be used in wikis - Activate with -m true"); parser.Setup(arg => arg.MarkupDatabaseNameReplacement).As('r', "markupdatabasenamereplacement") - .SetDefault("").WithDescription("(optional) Will use this instead of database name for wiki breadcrump generation"); + .SetDefault("") + .WithDescription("(optional) Will use this instead of database name for wiki breadcrump generation"); parser.SetupHelp("?", "help").Callback(text => Console.WriteLine(text)); var result = parser.Parse(args); if (!result.HasErrors) { - var conf = parser.Object as ApplicationArguments; + var conf = parser.Object; if (conf.Database is null) { Console.WriteLine("You didn't specify a database"); @@ -62,11 +71,12 @@ static void Main(string[] args) if (database.ContainsKey(reader.GetString(0))) database[reader.GetString(0)].Add(new Column(reader)); else - database.Add(reader.GetString(0), new List() { new Column(reader) }); + database.Add(reader.GetString(0), new List { new Column(reader) }); + + reader.Close(); } foreach (var table in database) - { using (var cmd = con.CreateCommand()) { // TODO: Is there a way to do this without this senseless statement? @@ -75,21 +85,27 @@ static void Main(string[] args) var schema = reader.GetSchemaTable(); foreach (var column in table.Value) column.Type = schema.Select($"ColumnName = '{column.Name}'")[0]["DataType"] as Type; + + reader.Close(); } - } con.Close(); } DbToClasses(conf.Database, database, conf.GenerateConstructorAndOutput); if (conf.GenerateMarkupPages) - DbToMarkupPage(string.IsNullOrEmpty(conf.MarkupDatabaseNameReplacement) ? conf.Database : conf.MarkupDatabaseNameReplacement, database); + DbToMarkupPage( + string.IsNullOrEmpty(conf.MarkupDatabaseNameReplacement) + ? conf.Database + : conf.MarkupDatabaseNameReplacement, database); Console.WriteLine("Successfully generated C# classes!"); } + Console.ReadLine(); } - private static void DbToClasses(string dbName, Dictionary> db, bool generateConstructorAndOutput) + private static void DbToClasses(string dbName, Dictionary> db, + bool generateConstructorAndOutput) { if (!Directory.Exists(dbName)) Directory.CreateDirectory(dbName); @@ -110,13 +126,11 @@ private static void DbToClasses(string dbName, Dictionary> sb.AppendLine($"{Environment.NewLine}public {table.Key}(MySqlDataReader reader)"); sb.AppendLine("{"); foreach (var column in table.Value) - { // check which type and use correct get method instead of casting if (column.Type != typeof(string)) - sb.AppendLine($"{column.Name.FirstCharUpper()} = Convert.To{column.Type.Name}(reader[\"{column.Name}\"].ToString());"); + sb.AppendLine($"{column.Name.FirstCharUpper()} = reader.Get{column.Type.Name}(\"{column.Name}\");"); else - sb.AppendLine($"{column.Name.FirstCharUpper()} = reader[\"{column.Name}\"].ToString();"); - } + sb.AppendLine($"{column.Name.FirstCharUpper()} = reader.GetString(\"{column.Name}\");"); sb.AppendLine($"}}{Environment.NewLine}"); // update query @@ -141,7 +155,8 @@ private static void DbToClasses(string dbName, Dictionary> // delete query sb.AppendLine("public string DeleteQuery()"); sb.AppendLine("{"); - sb.AppendLine($"return $\"DELETE FROM `{table.Key}` WHERE {table.Value[0].Name} = {{{table.Value[0].Name.FirstCharUpper()}}};\";"); + sb.AppendLine( + $"return $\"DELETE FROM `{table.Key}` WHERE {table.Value[0].Name} = {{{table.Value[0].Name.FirstCharUpper()}}};\";"); sb.AppendLine("}"); } @@ -157,7 +172,7 @@ private static void DbToClasses(string dbName, Dictionary> private static void DbToMarkupPage(string dbName, Dictionary> db) { - var wikiDir = $"wiki"; + var wikiDir = "wiki"; var wikiDbDir = $"{wikiDir}/{dbName}"; var wikiTableDir = $"{wikiDbDir}/tables"; @@ -188,7 +203,8 @@ private static void DbToMarkupPage(string dbName, Dictionary [[{dbName}|{dbName}]] > [[{table.Key}|{table.Key}]]"); + sb.AppendLine( + $"[[Database Structure|Database Structure]] > [[{dbName}|{dbName}]] > [[{table.Key}|{table.Key}]]"); sb.AppendLine(""); sb.AppendLine("Column | Type | Description"); sb.AppendLine("--- | --- | ---"); @@ -202,4 +218,4 @@ private static void DbToMarkupPage(string dbName, Dictionary + + - + + + + + + + + \ No newline at end of file