55using MySqlConnector ;
66using System ;
77using System . Data ;
8+ using System . IO ;
89using System . Reflection ;
910using System . Threading ;
1011using System . Threading . Tasks ;
1112
1213namespace Pomelo . Extensions . Caching . MySqlConfig . Tools
1314{
14- public class Program : IDisposable
15+ /// <summary>
16+ /// Based (if not entirely) of off https://github.com/dotnet/aspnetcore/blob/main/src/Tools/dotnet-sql-cache/src/Program.cs
17+ /// </summary>
18+ public class Program
1519 {
1620 private string _connectionString = null ;
1721 private string _databaseName = null ;
1822 private string _tableName = null ;
1923
24+ internal TextWriter Error { get ; set ; } = Console . Error ;
25+ internal TextWriter Out { get ; set ; } = Console . Out ;
26+
2027 public Program ( )
2128 {
2229 }
2330
2431 public static int Main ( string [ ] args )
2532 {
26- using ( var p = new Program ( ) )
27- {
28- return p . Run ( args ) ;
29- }
33+ var p = new Program ( ) ;
34+
35+ return p . Run ( args ) ;
3036 }
3137
3238 public int Run ( string [ ] args )
@@ -36,11 +42,14 @@ public int Run(string[] args)
3642 var description = "Creates table and indexes in MySQL Server database " +
3743 "to be used for distributed caching" ;
3844
39- var app = new CommandLineApplication ( ) ;
40- app . FullName = "MySQL Server Cache Command Line Tool" ;
41- app . Name = "dotnet-mysql-cache" ;
42- app . Description = description ;
43- app . ShortVersionGetter = ( ) =>
45+ var cliApp = new CommandLineApplication ( ) ;
46+ cliApp . Error = Error ;
47+ cliApp . Out = Out ;
48+
49+ cliApp . FullName = "MySQL Server Cache Command Line Tool" ;
50+ cliApp . Name = "dotnet-mysql-cache" ;
51+ cliApp . Description = description ;
52+ cliApp . ShortVersionGetter = ( ) =>
4453 {
4554 var assembly = typeof ( Program ) . GetTypeInfo ( ) . Assembly ;
4655 var infoVersion = assembly
@@ -50,10 +59,13 @@ public int Run(string[] args)
5059 ? assembly ? . GetName ( ) . Version . ToString ( )
5160 : infoVersion ;
5261 } ;
53- app . HelpOption ( "-?|-h|--help" ) ;
62+ cliApp . HelpOption ( "-?|-h|--help" ) ;
5463
55- app . Command ( "create" , command =>
64+ cliApp . Command ( "create" , command =>
5665 {
66+ command . Error = Error ; //internal command Out/Erro are not yet changed, possible shortcomming
67+ command . Out = Out ;
68+
5769 command . Description = description ;
5870 var connectionStringArg = command . Argument (
5971 "[connectionString]" ,
@@ -65,11 +77,11 @@ public int Run(string[] args)
6577 command . OnExecute ( async ( ) =>
6678 {
6779 if ( string . IsNullOrEmpty ( connectionStringArg . Value )
68- || string . IsNullOrEmpty ( databaseNameArg . Value )
69- || string . IsNullOrEmpty ( tableNameArg . Value ) )
80+ || string . IsNullOrEmpty ( databaseNameArg . Value )
81+ || string . IsNullOrEmpty ( tableNameArg . Value ) )
7082 {
71- await Console . Error . WriteLineAsync ( "Invalid input" ) ;
72- app . ShowHelp ( ) ;
83+ await Error . WriteLineAsync ( "Invalid input" ) ;
84+ cliApp . ShowHelp ( command . Name ) ;
7385 return 2 ;
7486 }
7587
@@ -81,18 +93,45 @@ public int Run(string[] args)
8193 } ) ;
8294 } ) ;
8395
96+ cliApp . Command ( "script" , command =>
97+ {
98+ command . Error = Error ; //internal command Out/Erro are not yet changed, possible shortcomming
99+ command . Out = Out ;
100+
101+ command . Description = "Generate creation script" ;
102+ var databaseNameArg = command . Argument ( "[databaseName]" , "Name of the database." ) ;
103+ var tableNameArg = command . Argument ( "[tableName]" , "Name of the table to be created." ) ;
104+ command . HelpOption ( "-?|-h|--help" ) ;
105+
106+ command . OnExecute ( async ( ) =>
107+ {
108+ if ( string . IsNullOrEmpty ( databaseNameArg . Value )
109+ || string . IsNullOrEmpty ( tableNameArg . Value ) )
110+ {
111+ await Error . WriteLineAsync ( "Invalid input" ) ;
112+ cliApp . ShowHelp ( command . Name ) ;
113+ return 2 ;
114+ }
115+
116+ _databaseName = databaseNameArg . Value ;
117+ _tableName = tableNameArg . Value ;
118+
119+ return await GenerateScript ( ) ;
120+ } ) ;
121+ } ) ;
122+
84123 // Show help information if no subcommand/option was specified.
85- app . OnExecute ( ( ) =>
124+ cliApp . OnExecute ( ( ) =>
86125 {
87- app . ShowHelp ( ) ;
126+ cliApp . ShowHelp ( ) ;
88127 return 2 ;
89128 } ) ;
90129
91- return app . Execute ( args ) ;
130+ return cliApp . Execute ( args ) ;
92131 }
93- catch ( Exception exception )
132+ catch ( Exception ex )
94133 {
95- Console . Error . WriteLine ( $ "An error occurred. { exception . Message } ") ;
134+ Error . WriteLine ( $ "An error occurred. { ex . Message } ") ;
96135 return 1 ;
97136 }
98137 }
@@ -112,7 +151,7 @@ public int Run(string[] args)
112151 {
113152 if ( await reader . ReadAsync ( token ) )
114153 {
115- Console . Error . WriteLine (
154+ Error . WriteLine (
116155 $ "Table '{ _tableName } ' from database '{ _databaseName } ' already exists. " +
117156 "Provide a different table name and try again." ) ;
118157 return 1 ;
@@ -138,11 +177,11 @@ public int Run(string[] args)
138177
139178 transaction . Commit ( ) ;
140179
141- await Console . Out . WriteLineAsync ( "Table and index were created successfully." ) ;
180+ await Out . WriteLineAsync ( "Table and index were created successfully." ) ;
142181 }
143182 catch ( Exception ex )
144183 {
145- await Console . Error . WriteLineAsync (
184+ await Error . WriteLineAsync (
146185 $ "An error occurred while trying to create the table and index. { ex . Message } ") ;
147186 transaction . Rollback ( ) ;
148187
@@ -154,52 +193,36 @@ await Console.Error.WriteLineAsync(
154193 return 0 ;
155194 }
156195
157- private void ValidateConnectionString ( )
196+ private async Task < int > GenerateScript ( CancellationToken token = default ( CancellationToken ) )
158197 {
159198 try
160199 {
161- new MySqlConnectionStringBuilder ( _connectionString ) ;
200+ var sqlQueries = new MySqlQueries ( _databaseName , _tableName ) ;
201+ string cmd = sqlQueries . CreateTable ;
202+
203+ await Out . WriteLineAsync ( $ "{ Environment . NewLine } { cmd } { Environment . NewLine } ") ;
162204 }
163205 catch ( Exception ex )
164206 {
165- throw new ArgumentException (
166- $ "Invalid MySql server connection string '{ _connectionString } '. { ex . Message } ", ex ) ;
207+ await Error . WriteLineAsync ( $ "An error occurred while trying to create the table and index. { ex . Message } ") ;
208+
209+ return 1 ;
167210 }
168- }
169211
170- #region IDisposable Support
171- private bool disposedValue = false ; // To detect redundant calls
212+ return 0 ;
213+ }
172214
173- protected virtual void Dispose ( bool disposing )
215+ private void ValidateConnectionString ( )
174216 {
175- if ( ! disposedValue )
217+ try
176218 {
177- if ( disposing )
178- {
179- // TODO: dispose managed state (managed objects).
180- }
181-
182- // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
183- // TODO: set large fields to null.
184-
185- disposedValue = true ;
219+ new MySqlConnectionStringBuilder ( _connectionString ) ;
220+ }
221+ catch ( Exception ex )
222+ {
223+ throw new ArgumentException (
224+ $ "Invalid MySql server connection string '{ _connectionString } '. { ex . Message } ", ex ) ;
186225 }
187226 }
188-
189- // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
190- // ~Program() {
191- // // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
192- // Dispose(false);
193- // }
194-
195- // This code added to correctly implement the disposable pattern.
196- public void Dispose ( )
197- {
198- // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
199- Dispose ( true ) ;
200- // TODO: uncomment the following line if the finalizer is overridden above.
201- // GC.SuppressFinalize(this);
202- }
203- #endregion
204227 }
205228}
0 commit comments