3
3
4
4
using System ;
5
5
using System . Data ;
6
+ using System . IO ;
6
7
using System . Reflection ;
7
8
using Microsoft . Data . SqlClient ;
8
9
using Microsoft . Extensions . CommandLineUtils ;
@@ -15,6 +16,9 @@ public class Program
15
16
private string _connectionString = null ;
16
17
private string _schemaName = null ;
17
18
private string _tableName = null ;
19
+ private string _outputPath = null ;
20
+ private bool _idempotent ;
21
+
18
22
private readonly IConsole _console ;
19
23
20
24
public Program ( IConsole console )
@@ -49,7 +53,7 @@ public int Run(string[] args)
49
53
50
54
app . Command ( "create" , command =>
51
55
{
52
- command . Description = app . Description ;
56
+ command . Description = "Adds table and indexes to the database." ;
53
57
54
58
var connectionStringArg = command . Argument (
55
59
"[connectionString]" , "The connection string to connect to the database." ) ;
@@ -70,7 +74,7 @@ public int Run(string[] args)
70
74
|| string . IsNullOrEmpty ( tableNameArg . Value ) )
71
75
{
72
76
reporter . Error ( "Invalid input" ) ;
73
- app . ShowHelp ( ) ;
77
+ command . ShowHelp ( ) ;
74
78
return 2 ;
75
79
}
76
80
@@ -82,6 +86,51 @@ public int Run(string[] args)
82
86
} ) ;
83
87
} ) ;
84
88
89
+ app . Command ( "script" , command =>
90
+ {
91
+ command . Description = "Generates a SQL script for the table and indexes." ;
92
+
93
+ var schemaNameArg = command . Argument (
94
+ "[schemaName]" , "Name of the table schema." ) ;
95
+
96
+ var tableNameArg = command . Argument (
97
+ "[tableName]" , "Name of the table to be created." ) ;
98
+
99
+ var outputOption = command . Option (
100
+ "-o|--output" ,
101
+ "The file to write the result to." ,
102
+ CommandOptionType . SingleValue ) ;
103
+
104
+ var idempotentOption = command . Option (
105
+ "-i|--idempotent" ,
106
+ "Generates a script that can be used on a database that already has the table." ,
107
+ CommandOptionType . NoValue ) ;
108
+
109
+ command . HelpOption ( ) ;
110
+
111
+ command . OnExecute ( ( ) =>
112
+ {
113
+ var reporter = CreateReporter ( verbose . HasValue ( ) ) ;
114
+ if ( string . IsNullOrEmpty ( schemaNameArg . Value )
115
+ || string . IsNullOrEmpty ( tableNameArg . Value ) )
116
+ {
117
+ reporter . Error ( "Invalid input" ) ;
118
+ command . ShowHelp ( ) ;
119
+ return 2 ;
120
+ }
121
+
122
+ _schemaName = schemaNameArg . Value ;
123
+ _tableName = tableNameArg . Value ;
124
+ _idempotent = idempotentOption . HasValue ( ) ;
125
+ if ( outputOption . HasValue ( ) )
126
+ {
127
+ _outputPath = outputOption . Value ( ) ;
128
+ }
129
+
130
+ return ScriptTableAndIndexes ( reporter ) ;
131
+ } ) ;
132
+ } ) ;
133
+
85
134
// Show help information if no subcommand/option was specified.
86
135
app . OnExecute ( ( ) =>
87
136
{
@@ -100,6 +149,57 @@ public int Run(string[] args)
100
149
101
150
private IReporter CreateReporter ( bool verbose )
102
151
=> new ConsoleReporter ( _console , verbose , quiet : false ) ;
152
+
153
+ private SqlQueries CreateSqlQueries ( )
154
+ => new SqlQueries ( _schemaName , _tableName ) ;
155
+
156
+ private int ScriptTableAndIndexes ( IReporter reporter )
157
+ {
158
+ Action < string > writer = reporter . Output ;
159
+ StreamWriter streamWriter = default ;
160
+
161
+ try
162
+ {
163
+ if ( _outputPath is not null )
164
+ {
165
+ streamWriter = new StreamWriter ( _outputPath ) ;
166
+ writer = streamWriter . WriteLine ;
167
+ }
168
+
169
+ var sqlQueries = CreateSqlQueries ( ) ;
170
+
171
+ if ( _idempotent )
172
+ {
173
+ writer ( "IF NOT EXISTS (" ) ;
174
+ writer ( "\t " + sqlQueries . TableInfo ) ;
175
+ writer ( ")" ) ;
176
+ writer ( "BEGIN" ) ;
177
+ }
178
+
179
+ var prefix = _idempotent ? "\t " : "" ;
180
+ writer ( prefix + sqlQueries . CreateTable ) ;
181
+ writer ( prefix + sqlQueries . CreateNonClusteredIndexOnExpirationTime ) ;
182
+
183
+ if ( _idempotent )
184
+ {
185
+ writer ( "END" ) ;
186
+ }
187
+
188
+ return 0 ;
189
+ }
190
+ catch ( Exception ex )
191
+ {
192
+ reporter . Error (
193
+ $ "An error occurred while trying to script the table and index. { ex . Message } ") ;
194
+
195
+ return 1 ;
196
+ }
197
+ finally
198
+ {
199
+ streamWriter ? . Dispose ( ) ;
200
+ }
201
+ }
202
+
103
203
private int CreateTableAndIndexes ( IReporter reporter )
104
204
{
105
205
ValidateConnectionString ( ) ;
@@ -108,7 +208,7 @@ private int CreateTableAndIndexes(IReporter reporter)
108
208
{
109
209
connection . Open ( ) ;
110
210
111
- var sqlQueries = new SqlQueries ( _schemaName , _tableName ) ;
211
+ var sqlQueries = CreateSqlQueries ( ) ;
112
212
var command = new SqlCommand ( sqlQueries . TableInfo , connection ) ;
113
213
114
214
using ( var reader = command . ExecuteReader ( CommandBehavior . SingleRow ) )
0 commit comments