@@ -20,6 +20,7 @@ public class ApplicationArguments
20
20
public string Password { get ; set ; }
21
21
public string Database { get ; set ; }
22
22
public string Table { get ; set ; }
23
+ public bool GenerateConstructorAndOutput { get ; set ; }
23
24
}
24
25
25
26
public class Column
@@ -40,7 +41,7 @@ public override string ToString()
40
41
41
42
class Program
42
43
{
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 )
44
45
{
45
46
if ( ! Directory . Exists ( dbName ) )
46
47
Directory . CreateDirectory ( dbName ) ;
@@ -50,10 +51,26 @@ private static void DbToClasses(string dbName, Dictionary<string, List<Column>>
50
51
{
51
52
sb . AppendLine ( $ "public class { table . Key } ") ;
52
53
sb . AppendLine ( "{" ) ;
54
+
55
+ // properties
53
56
foreach ( var column in table . Value )
54
- {
55
57
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();") ;
56
69
}
70
+
71
+ sb . AppendLine ( "}" ) ;
72
+
73
+ // class closing
57
74
sb . AppendLine ( "}" ) ;
58
75
59
76
var sw = new StreamWriter ( $ "{ dbName } /{ table . Key } .cs", false ) ;
@@ -72,10 +89,12 @@ static void Main(string[] args)
72
89
parser . Setup ( arg => arg . Password ) . As ( 'p' , "password" ) . SetDefault ( String . Empty ) . WithDescription ( "(optional) Password, will use empty password if not specified" ) ;
73
90
parser . Setup ( arg => arg . Database ) . As ( 'd' , "database" ) . Required ( ) . WithDescription ( "Database name" ) ;
74
91
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" ) ;
75
94
parser . SetupHelp ( "?" , "help" ) . Callback ( text => Console . WriteLine ( text ) ) ;
76
95
77
96
#if DEBUG
78
- args = new [ ] { "-p" , "123" , "-d" , "az_world" } ;
97
+ args = new [ ] { "-p" , "123" , "-d" , "az_world" , "-g" , "true" } ;
79
98
#endif
80
99
81
100
var result = parser . Parse ( args ) ;
@@ -132,9 +151,9 @@ static void Main(string[] args)
132
151
con . Close ( ) ;
133
152
}
134
153
135
- DbToClasses ( conf . Database , database ) ;
154
+ DbToClasses ( conf . Database , database , conf . GenerateConstructorAndOutput ) ;
155
+ Console . WriteLine ( "Successfully generated C# classes!" ) ;
136
156
}
137
- Console . WriteLine ( "Successfully generated C# classes!" ) ;
138
157
Console . ReadLine ( ) ;
139
158
}
140
159
}
0 commit comments