From 55b786d967b191c447c0aae4f837373ab0f998c4 Mon Sep 17 00:00:00 2001 From: joncloud Date: Tue, 23 Feb 2021 08:45:46 -0800 Subject: [PATCH 1/2] Creates script command for sql-cache-tool * Includes idempotency, output to stdout, or specific file * Fixes help suggestion to be command-specific --- src/Tools/dotnet-sql-cache/src/Program.cs | 106 +++++++++++++++++++++- 1 file changed, 103 insertions(+), 3 deletions(-) diff --git a/src/Tools/dotnet-sql-cache/src/Program.cs b/src/Tools/dotnet-sql-cache/src/Program.cs index 2e9d0331bc6e..624123f6258a 100644 --- a/src/Tools/dotnet-sql-cache/src/Program.cs +++ b/src/Tools/dotnet-sql-cache/src/Program.cs @@ -3,6 +3,7 @@ using System; using System.Data; +using System.IO; using System.Reflection; using Microsoft.Data.SqlClient; using Microsoft.Extensions.CommandLineUtils; @@ -15,6 +16,9 @@ public class Program private string _connectionString = null; private string _schemaName = null; private string _tableName = null; + private string _outputPath = null; + private bool _idempotent; + private readonly IConsole _console; public Program(IConsole console) @@ -49,7 +53,7 @@ public int Run(string[] args) app.Command("create", command => { - command.Description = app.Description; + command.Description = "Adds table and indexes to the database."; var connectionStringArg = command.Argument( "[connectionString]", "The connection string to connect to the database."); @@ -70,7 +74,7 @@ public int Run(string[] args) || string.IsNullOrEmpty(tableNameArg.Value)) { reporter.Error("Invalid input"); - app.ShowHelp(); + command.ShowHelp(); return 2; } @@ -82,6 +86,51 @@ public int Run(string[] args) }); }); + app.Command("script", command => + { + command.Description = "Generates a SQL script for the table and indexes."; + + var schemaNameArg = command.Argument( + "[schemaName]", "Name of the table schema."); + + var tableNameArg = command.Argument( + "[tableName]", "Name of the table to be created."); + + var outputOption = command.Option( + "-o|--output", + "The file to write the result to.", + CommandOptionType.SingleValue); + + var idempontentOption = command.Option( + "-i|--idempotent", + "Generates a script that can be used on a database that already has the table.", + CommandOptionType.NoValue); + + command.HelpOption(); + + command.OnExecute(() => + { + var reporter = CreateReporter(verbose.HasValue()); + if (string.IsNullOrEmpty(schemaNameArg.Value) + || string.IsNullOrEmpty(tableNameArg.Value)) + { + reporter.Error("Invalid input"); + command.ShowHelp(); + return 2; + } + + _schemaName = schemaNameArg.Value; + _tableName = tableNameArg.Value; + _idempotent = idempontentOption.HasValue(); + if (outputOption.HasValue()) + { + _outputPath = outputOption.Value(); + } + + return ScriptTableAndIndexes(reporter); + }); + }); + // Show help information if no subcommand/option was specified. app.OnExecute(() => { @@ -100,6 +149,57 @@ public int Run(string[] args) private IReporter CreateReporter(bool verbose) => new ConsoleReporter(_console, verbose, quiet: false); + + private SqlQueries CreateSqlQueries() + => new SqlQueries(_schemaName, _tableName); + + private int ScriptTableAndIndexes(IReporter reporter) + { + Action writer = reporter.Output; + StreamWriter streamWriter = default; + + try + { + if (_outputPath is not null) + { + streamWriter = new StreamWriter(_outputPath); + writer = streamWriter.WriteLine; + } + + var sqlQueries = CreateSqlQueries(); + + if (_idempotent) + { + writer("IF NOT EXISTS ("); + writer("\t" + sqlQueries.TableInfo); + writer(")"); + writer("BEGIN"); + } + + var prefix = _idempotent ? "\t" : ""; + writer(prefix + sqlQueries.CreateTable); + writer(prefix + sqlQueries.CreateNonClusteredIndexOnExpirationTime); + + if (_idempotent) + { + writer("END"); + } + + return 0; + } + catch (Exception ex) + { + reporter.Error( + $"An error occurred while trying to script the table and index. {ex.Message}"); + + return 1; + } + finally + { + streamWriter?.Dispose(); + } + } + private int CreateTableAndIndexes(IReporter reporter) { ValidateConnectionString(); @@ -108,7 +208,7 @@ private int CreateTableAndIndexes(IReporter reporter) { connection.Open(); - var sqlQueries = new SqlQueries(_schemaName, _tableName); + var sqlQueries = CreateSqlQueries(); var command = new SqlCommand(sqlQueries.TableInfo, connection); using (var reader = command.ExecuteReader(CommandBehavior.SingleRow)) From 70a59fbad45823b24b266acb304ca9dc2ae58543 Mon Sep 17 00:00:00 2001 From: joncloud Date: Wed, 24 Mar 2021 12:36:22 -0700 Subject: [PATCH 2/2] Corrects typo --- src/Tools/dotnet-sql-cache/src/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tools/dotnet-sql-cache/src/Program.cs b/src/Tools/dotnet-sql-cache/src/Program.cs index 624123f6258a..fd75607f5bff 100644 --- a/src/Tools/dotnet-sql-cache/src/Program.cs +++ b/src/Tools/dotnet-sql-cache/src/Program.cs @@ -101,7 +101,7 @@ public int Run(string[] args) "The file to write the result to.", CommandOptionType.SingleValue); - var idempontentOption = command.Option( + var idempotentOption = command.Option( "-i|--idempotent", "Generates a script that can be used on a database that already has the table.", CommandOptionType.NoValue); @@ -121,7 +121,7 @@ public int Run(string[] args) _schemaName = schemaNameArg.Value; _tableName = tableNameArg.Value; - _idempotent = idempontentOption.HasValue(); + _idempotent = idempotentOption.HasValue(); if (outputOption.HasValue()) { _outputPath = outputOption.Value();