Skip to content

Commit e50e354

Browse files
authored
Creates script command for sql-cache-tool (#30398)
* Creates script command for sql-cache-tool * Includes idempotency, output to stdout, or specific file * Fixes help suggestion to be command-specific * Corrects typo
1 parent b2c22cf commit e50e354

File tree

1 file changed

+103
-3
lines changed

1 file changed

+103
-3
lines changed

src/Tools/dotnet-sql-cache/src/Program.cs

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Data;
6+
using System.IO;
67
using System.Reflection;
78
using Microsoft.Data.SqlClient;
89
using Microsoft.Extensions.CommandLineUtils;
@@ -15,6 +16,9 @@ public class Program
1516
private string _connectionString = null;
1617
private string _schemaName = null;
1718
private string _tableName = null;
19+
private string _outputPath = null;
20+
private bool _idempotent;
21+
1822
private readonly IConsole _console;
1923

2024
public Program(IConsole console)
@@ -49,7 +53,7 @@ public int Run(string[] args)
4953

5054
app.Command("create", command =>
5155
{
52-
command.Description = app.Description;
56+
command.Description = "Adds table and indexes to the database.";
5357

5458
var connectionStringArg = command.Argument(
5559
"[connectionString]", "The connection string to connect to the database.");
@@ -70,7 +74,7 @@ public int Run(string[] args)
7074
|| string.IsNullOrEmpty(tableNameArg.Value))
7175
{
7276
reporter.Error("Invalid input");
73-
app.ShowHelp();
77+
command.ShowHelp();
7478
return 2;
7579
}
7680

@@ -82,6 +86,51 @@ public int Run(string[] args)
8286
});
8387
});
8488

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+
85134
// Show help information if no subcommand/option was specified.
86135
app.OnExecute(() =>
87136
{
@@ -100,6 +149,57 @@ public int Run(string[] args)
100149

101150
private IReporter CreateReporter(bool verbose)
102151
=> 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+
103203
private int CreateTableAndIndexes(IReporter reporter)
104204
{
105205
ValidateConnectionString();
@@ -108,7 +208,7 @@ private int CreateTableAndIndexes(IReporter reporter)
108208
{
109209
connection.Open();
110210

111-
var sqlQueries = new SqlQueries(_schemaName, _tableName);
211+
var sqlQueries = CreateSqlQueries();
112212
var command = new SqlCommand(sqlQueries.TableInfo, connection);
113213

114214
using (var reader = command.ExecuteReader(CommandBehavior.SingleRow))

0 commit comments

Comments
 (0)