Skip to content

Commit 26dfa9d

Browse files
committed
It's now possible to generate a constructor which will read data out of a MySqlDataReader
1 parent 1c36550 commit 26dfa9d

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

MySQL-To-CSharp/Program.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class ApplicationArguments
2020
public string Password { get; set; }
2121
public string Database { get; set; }
2222
public string Table { get; set; }
23+
public bool GenerateConstructorAndOutput { get; set; }
2324
}
2425

2526
public class Column
@@ -40,7 +41,7 @@ public override string ToString()
4041

4142
class Program
4243
{
43-
private static void DbToClasses(string dbName, Dictionary<string, List<Column>> db)
44+
private static void DbToClasses(string dbName, Dictionary<string, List<Column>> db, bool generateConstructorAndOutput)
4445
{
4546
if (!Directory.Exists(dbName))
4647
Directory.CreateDirectory(dbName);
@@ -50,10 +51,26 @@ private static void DbToClasses(string dbName, Dictionary<string, List<Column>>
5051
{
5152
sb.AppendLine($"public class {table.Key}");
5253
sb.AppendLine("{");
54+
55+
// properties
5356
foreach (var column in table.Value)
54-
{
5557
sb.AppendLine(column.ToString());
58+
59+
// constructor
60+
sb.AppendLine($"{Environment.NewLine}public {table.Key}(MySqlDataReader reader)");
61+
sb.AppendLine("{");
62+
foreach (var column in table.Value)
63+
{
64+
// check which type and use correct get method instead of casting
65+
if (column.Type != typeof(string))
66+
sb.AppendLine($"{column.Name} = Convert.To{column.Type.Name}(reader[\"{column.Name}\"].ToString());");
67+
else
68+
sb.AppendLine($"{column.Name} = reader[\"{column.Name}\"].ToString();");
5669
}
70+
71+
sb.AppendLine("}");
72+
73+
// class closing
5774
sb.AppendLine("}");
5875

5976
var sw = new StreamWriter($"{dbName}/{table.Key}.cs", false);
@@ -72,10 +89,12 @@ static void Main(string[] args)
7289
parser.Setup(arg => arg.Password).As('p', "password").SetDefault(String.Empty).WithDescription("(optional) Password, will use empty password if not specified");
7390
parser.Setup(arg => arg.Database).As('d', "database").Required().WithDescription("Database name");
7491
parser.Setup(arg => arg.Table).As('t', "table").SetDefault(String.Empty).WithDescription("(optional) Table name, will generate entire database if not specified");
92+
parser.Setup(arg => arg.GenerateConstructorAndOutput).As('g', "generateconstructorandoutput")
93+
.SetDefault(false).WithDescription("(optional) Generate a reading constructor and SQL statement output - Activate with -g true");
7594
parser.SetupHelp("?", "help").Callback(text => Console.WriteLine(text));
7695

7796
#if DEBUG
78-
args = new [] { "-p", "123", "-d", "az_world"};
97+
args = new [] { "-p", "123", "-d", "az_world", "-g", "true"};
7998
#endif
8099

81100
var result = parser.Parse(args);
@@ -132,9 +151,9 @@ static void Main(string[] args)
132151
con.Close();
133152
}
134153

135-
DbToClasses(conf.Database, database);
154+
DbToClasses(conf.Database, database, conf.GenerateConstructorAndOutput);
155+
Console.WriteLine("Successfully generated C# classes!");
136156
}
137-
Console.WriteLine("Successfully generated C# classes!");
138157
Console.ReadLine();
139158
}
140159
}

0 commit comments

Comments
 (0)