From 76f84311fe375f033a2777edcc07b5221c0efff7 Mon Sep 17 00:00:00 2001 From: Alistair Mackay <34012094+fireflycons@users.noreply.github.com> Date: Fri, 28 Sep 2018 14:27:35 +0100 Subject: [PATCH] Fix final statistics message not getting to output file --- .../CommandExecuter.cs | 108 +++++++++--------- Firefly.SqlCmdParser.Client/SqlExecuteImpl.cs | 19 --- Firefly.SqlCmdParser/ICommandExecuter.cs | 12 ++ Firefly.SqlCmdParser/Parser.cs | 50 +++++--- .../Commands/FileParameterCommand.cs | 7 +- 5 files changed, 107 insertions(+), 89 deletions(-) diff --git a/Firefly.SqlCmdParser.Client/CommandExecuter.cs b/Firefly.SqlCmdParser.Client/CommandExecuter.cs index 24ce863..3863586 100644 --- a/Firefly.SqlCmdParser.Client/CommandExecuter.cs +++ b/Firefly.SqlCmdParser.Client/CommandExecuter.cs @@ -611,6 +611,60 @@ public virtual void ServerList() { } + /// + /// Writes a message to current stderr destination + /// + /// The message. + public void WriteStderrMessage(string message) + { + if (this.StderrDestination == OutputDestination.File) + { + if (!message.EndsWith(Environment.NewLine)) + { + message += Environment.NewLine; + } + + var bytes = Encoding.UTF8.GetBytes(message); + this.stderrFile.Write(bytes, 0, bytes.Length); + + // in case someone is tailing the file, flush to keep it up to date. + this.stderrFile.Flush(); + } + else + { + this.Message?.Invoke( + this, + new OutputMessageEventArgs(this.NodeNumber, message, this.StderrDestination)); + } + } + + /// + /// Writes a message to current stdout destination + /// /// + /// The message. + public void WriteStdoutMessage(string message) + { + if (this.StdoutDestination == OutputDestination.File) + { + if (!message.EndsWith(Environment.NewLine)) + { + message += Environment.NewLine; + } + + var bytes = Encoding.UTF8.GetBytes(message); + this.stdoutFile.Write(bytes, 0, bytes.Length); + + // in case someone is tailing the file, flush to keep it up to date. + this.stdoutFile.Flush(); + } + else + { + this.Message?.Invoke( + this, + new OutputMessageEventArgs(this.NodeNumber, message, this.StdoutDestination)); + } + } + /// /// /// :XML directive. @@ -980,60 +1034,6 @@ private void OutputWithResults(SqlCommand sqlCommand) } } - /// - /// Writes a message to current stderr destination - /// - /// The message. - private void WriteStderrMessage(string message) - { - if (this.StderrDestination == OutputDestination.File) - { - if (!message.EndsWith(Environment.NewLine)) - { - message += Environment.NewLine; - } - - var bytes = Encoding.UTF8.GetBytes(message); - this.stderrFile.Write(bytes, 0, bytes.Length); - - // in case someone is tailing the file, flush to keep it up to date. - this.stderrFile.Flush(); - } - else - { - this.Message?.Invoke( - this, - new OutputMessageEventArgs(this.NodeNumber, message, this.StderrDestination)); - } - } - - /// - /// Writes a message to current stdout destination - /// /// - /// The message. - private void WriteStdoutMessage(string message) - { - if (this.StdoutDestination == OutputDestination.File) - { - if (!message.EndsWith(Environment.NewLine)) - { - message += Environment.NewLine; - } - - var bytes = Encoding.UTF8.GetBytes(message); - this.stdoutFile.Write(bytes, 0, bytes.Length); - - // in case someone is tailing the file, flush to keep it up to date. - this.stdoutFile.Flush(); - } - else - { - this.Message?.Invoke( - this, - new OutputMessageEventArgs(this.NodeNumber, message, this.StdoutDestination)); - } - } - /// /// Buffer object for output of /// diff --git a/Firefly.SqlCmdParser.Client/SqlExecuteImpl.cs b/Firefly.SqlCmdParser.Client/SqlExecuteImpl.cs index d4f3da2..912d319 100644 --- a/Firefly.SqlCmdParser.Client/SqlExecuteImpl.cs +++ b/Firefly.SqlCmdParser.Client/SqlExecuteImpl.cs @@ -260,33 +260,14 @@ private void InvokeParser(int nodeNumber, RunConfiguration runConfiguration) this.arguments.DisableVariablesSet, this.arguments.CurrentDirectoryResolver); - var sw = new Stopwatch(); - sw.Start(); - try { parser.Parse(); } finally { - sw.Stop(); - this.BatchCount += parser.BatchCount; this.ErrorCount += runConfiguration.CommandExecuter.ErrorCount; - var batch = parser.BatchCount == 1 ? "batch" : "batches"; - var error = runConfiguration.CommandExecuter.ErrorCount == 1 ? "error" : "errors"; - this.arguments.OutputMessage?.Invoke( - this, - new OutputMessageEventArgs( - nodeNumber, - $"{parser.BatchCount} {batch} processed in {sw.Elapsed.Minutes} min, {sw.Elapsed.Seconds}.{sw.Elapsed.Milliseconds:D3} sec.", - runConfiguration.CommandExecuter.StdoutDestination)); - this.arguments.OutputMessage?.Invoke( - this, - new OutputMessageEventArgs( - nodeNumber, - $"{runConfiguration.CommandExecuter.ErrorCount} SQL {error} in execution.", - runConfiguration.CommandExecuter.StdoutDestination)); } } } diff --git a/Firefly.SqlCmdParser/ICommandExecuter.cs b/Firefly.SqlCmdParser/ICommandExecuter.cs index 1464f0c..c7cf556 100644 --- a/Firefly.SqlCmdParser/ICommandExecuter.cs +++ b/Firefly.SqlCmdParser/ICommandExecuter.cs @@ -212,5 +212,17 @@ public interface ICommandExecuter : IDisposable /// The sender. /// The instance containing the event data. void OnInputSourceChanged(object sender, InputSourceChangedEventArgs args); + + /// + /// Writes a message to current stderr destination + /// /// + /// The message. + void WriteStderrMessage(string message); + + /// + /// Writes a message to current stdout destination + /// /// + /// The message. + void WriteStdoutMessage(string message); } } \ No newline at end of file diff --git a/Firefly.SqlCmdParser/Parser.cs b/Firefly.SqlCmdParser/Parser.cs index 7eb9f53..fe80da8 100644 --- a/Firefly.SqlCmdParser/Parser.cs +++ b/Firefly.SqlCmdParser/Parser.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Data.SqlClient; + using System.Diagnostics; using System.IO; using System.Linq; using System.Text.RegularExpressions; @@ -161,20 +162,22 @@ public Parser( /// or public void Parse() { - if (this.runConfiguration.OutputFile != null) + var sw = new Stopwatch(); + + try { - this.CommandExecuter.Out(OutputDestination.File, this.runConfiguration.OutputFile); - } + if (this.runConfiguration.OutputFile != null) + { + this.CommandExecuter.Out(OutputDestination.File, this.runConfiguration.OutputFile); + } - this.InputSourceChanged += this.CommandExecuter.OnInputSourceChanged; + this.InputSourceChanged += this.CommandExecuter.OnInputSourceChanged; - this.CommandExecuter.ConnectWithConnectionString(this.runConfiguration.ConnectionString); + this.CommandExecuter.ConnectWithConnectionString(this.runConfiguration.ConnectionString); - this.SetInputSource(this.runConfiguration.InitialBatchSource); - var tokenizer = new Tokenizer(); + this.SetInputSource(this.runConfiguration.InitialBatchSource); + var tokenizer = new Tokenizer(); - try - { while (this.sourceStack.Count > 0) { this.Source = this.sourceStack.Peek(); @@ -232,13 +235,14 @@ public void Parse() try { + // Increment first, so a failed batch is counted + ++this.BatchCount; this.CommandExecuter.ProcessBatch(this.currentBatch, go.ExecutionCount); } finally { this.previousBatch = this.currentBatch; this.currentBatch = new SqlBatch(); - ++this.BatchCount; } break; @@ -345,12 +349,16 @@ public void Parse() case ErrorCommand error: - this.CommandExecuter.Error(error.OutputDestination, FileParameterCommand.GetNodeFilepath(this.nodeNumber, error.Filename)); + this.CommandExecuter.Error( + error.OutputDestination, + FileParameterCommand.GetNodeFilepath(this.nodeNumber, error.Filename)); break; case OutCommand @out: - this.CommandExecuter.Out(@out.OutputDestination, FileParameterCommand.GetNodeFilepath(this.nodeNumber, @out.Filename)); + this.CommandExecuter.Out( + @out.OutputDestination, + FileParameterCommand.GetNodeFilepath(this.nodeNumber, @out.Filename)); break; // ReSharper disable once UnusedVariable @@ -457,8 +465,9 @@ public void Parse() // If we have anything left, it's the last batch if (!string.IsNullOrWhiteSpace(this.currentBatch.Sql)) { - this.CommandExecuter.ProcessBatch(this.currentBatch, 1); + // Increment first, so a failed batch is counted ++this.BatchCount; + this.CommandExecuter.ProcessBatch(this.currentBatch, 1); } } catch (ParserException) @@ -473,6 +482,21 @@ public void Parse() { throw new ParserException(ex.Message, ex, this.Source); } + finally + { + // Output statistics + sw.Stop(); + + var batches = this.BatchCount == 1 ? "batch" : "batches"; + var errors = this.CommandExecuter.ErrorCount == 1 ? "error" : "errors"; + + this.CommandExecuter.WriteStdoutMessage( + string.Join( + Environment.NewLine, + Environment.NewLine, + $"{this.BatchCount} {batches} processed in {sw.Elapsed.Minutes} min, {sw.Elapsed.Seconds}.{sw.Elapsed.Milliseconds:D3} sec.", + $"{this.CommandExecuter.ErrorCount} SQL {errors} in execution.")); + } } /// diff --git a/Firefly.SqlCmdParser/SimpleParser/Commands/FileParameterCommand.cs b/Firefly.SqlCmdParser/SimpleParser/Commands/FileParameterCommand.cs index 3a25920..8ec0004 100644 --- a/Firefly.SqlCmdParser/SimpleParser/Commands/FileParameterCommand.cs +++ b/Firefly.SqlCmdParser/SimpleParser/Commands/FileParameterCommand.cs @@ -21,7 +21,7 @@ public abstract class FileParameterCommand : ICommandMatcher public abstract CommandType CommandType { get; } /// - /// Gets the command text (out, perftrace etc.) + /// Gets the command text (out, perf trace etc.) /// /// /// The command text. @@ -72,10 +72,11 @@ public static string GetNodeFilepath(int nodeNumber, string filepath) return filepath; } - var fn = Path.GetFileName(filepath); + var fn = Path.GetFileNameWithoutExtension(filepath); + var ext = Path.GetExtension(filepath); var dir = Path.GetDirectoryName(filepath); - fn = $"{nodeNumber:D2}-{fn}"; + fn = $"{fn}.{nodeNumber:D2}{ext}"; if (!string.IsNullOrEmpty(dir)) {