From d451447ebc184553e15ec3593ab1469f5493b49d Mon Sep 17 00:00:00 2001 From: Anthony Dresser Date: Fri, 30 Sep 2016 11:46:32 -0700 Subject: [PATCH] Feature/timestamp messages (#68) * added support for timestamps * fixed tests * Moved message class to own file; added 'z' to end of date strings * added default time constructor * removed unnecessary z * added time string format info in comment * changed from utc time to using local time --- nuget.config | 1 + .../QueryExecution/Batch.cs | 20 ++++----- .../QueryExecution/Contracts/BatchSummary.cs | 2 +- .../QueryExecution/Contracts/ResultMessage.cs | 44 +++++++++++++++++++ .../QueryExecution/ExecuteTests.cs | 6 +-- 5 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/ResultMessage.cs diff --git a/nuget.config b/nuget.config index edd564a3..f5d41658 100644 --- a/nuget.config +++ b/nuget.config @@ -1,6 +1,7 @@ + diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Batch.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Batch.cs index b79af0b2..17065991 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Batch.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Batch.cs @@ -38,7 +38,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution /// /// Internal representation of the messages so we can modify internally /// - private readonly List resultMessages; + private readonly List resultMessages; /// /// Internal representation of the result sets so we can modify internally @@ -58,7 +58,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution Selection = new SelectionData(startLine, startColumn, endLine, endColumn); HasExecuted = false; resultSets = new List(); - resultMessages = new List(); + resultMessages = new List(); this.outputFileFactory = outputFileFactory; } @@ -82,7 +82,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution /// /// Messages that have come back from the server /// - public IEnumerable ResultMessages + public IEnumerable ResultMessages { get { return resultMessages; } } @@ -168,9 +168,9 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution if (!reader.HasRows && reader.FieldCount == 0) { // Create a message with the number of affected rows -- IF the query affects rows - resultMessages.Add(reader.RecordsAffected >= 0 - ? SR.QueryServiceAffectedRows(reader.RecordsAffected) - : SR.QueryServiceCompletedSuccessfully); + resultMessages.Add(new ResultMessage(reader.RecordsAffected >= 0 + ? SR.QueryServiceAffectedRows(reader.RecordsAffected) + : SR.QueryServiceCompletedSuccessfully)); continue; } @@ -183,7 +183,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution resultSets.Add(resultSet); // Add a message for the number of rows the query returned - resultMessages.Add(SR.QueryServiceAffectedRows(resultSet.RowCount)); + resultMessages.Add(new ResultMessage(SR.QueryServiceAffectedRows(resultSet.RowCount))); } while (await reader.NextResultAsync(cancellationToken)); } } @@ -244,7 +244,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution /// Arguments from the event private void StoreDbMessage(object sender, SqlInfoMessageEventArgs args) { - resultMessages.Add(args.Message); + resultMessages.Add(new ResultMessage(args.Message)); } /// @@ -268,13 +268,13 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution string message = string.Format("Msg {0}, Level {1}, State {2}, Line {3}{4}{5}", sqlError.Number, sqlError.Class, sqlError.State, lineNumber, Environment.NewLine, sqlError.Message); - resultMessages.Add(message); + resultMessages.Add(new ResultMessage(message)); } } } else { - resultMessages.Add(dbe.Message); + resultMessages.Add(new ResultMessage(dbe.Message)); } } diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/BatchSummary.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/BatchSummary.cs index 4205fecb..7e1b2837 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/BatchSummary.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/BatchSummary.cs @@ -28,7 +28,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts /// /// Any messages that came back from the server during execution of the batch /// - public string[] Messages { get; set; } + public ResultMessage[] Messages { get; set; } /// /// The summaries of the result sets inside the batch diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/ResultMessage.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/ResultMessage.cs new file mode 100644 index 00000000..27e6713b --- /dev/null +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/ResultMessage.cs @@ -0,0 +1,44 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; + +namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts +{ + /// + /// Result message object with timestamp and actual message + /// + public class ResultMessage + { + /// + /// Timestamp of the message + /// Stored in UTC ISO 8601 format; should be localized before displaying to any user + /// + public string Time { get; set; } + + /// + /// Message contents + /// + public string Message { get; set; } + + /// + /// Full constructor + /// + public ResultMessage(string timeStamp, string message) + { + Time = timeStamp; + Message = message; + } + + /// + /// Constructor with default "Now" time + /// + public ResultMessage(string message) + { + Time = DateTime.Now.ToString("o"); + Message = message; + } + } +} \ No newline at end of file diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/ExecuteTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/ExecuteTests.cs index edfd6bad..c7e8ac0d 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/ExecuteTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/ExecuteTests.cs @@ -74,7 +74,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution // ... There should be a message for how many rows were affected Assert.Equal(1, batch.ResultMessages.Count()); - Assert.Contains("1 ", batch.ResultMessages.First()); + Assert.Contains("1 ", batch.ResultMessages.First().Message); // NOTE: 1 is expected because this test simulates a 'update' statement where 1 row was affected. // The 1 in quotes is to make sure the 1 isn't part of a larger number } @@ -108,7 +108,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution // ... There should be a message for how many rows were affected Assert.Equal(resultSets, batch.ResultMessages.Count()); - Assert.Contains(Common.StandardRows.ToString(), batch.ResultMessages.First()); + Assert.Contains(Common.StandardRows.ToString(), batch.ResultMessages.First().Message); } [Fact] @@ -155,7 +155,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution Assert.Equal(resultSets, batch.ResultMessages.Count()); foreach (var rsm in batch.ResultMessages) { - Assert.Contains(Common.StandardRows.ToString(), rsm); + Assert.Contains(Common.StandardRows.ToString(), rsm.Message); } }