From 8a8104b4cf96e8a7965db98df69c6a5815de358a Mon Sep 17 00:00:00 2001 From: benrr101 Date: Thu, 18 Aug 2016 15:14:38 -0700 Subject: [PATCH] Fixing bug where select returns no messages --- .../QueryExecution/Query.cs | 22 +++++++++++-------- .../QueryExecution/Common.cs | 3 +-- .../QueryExecution/ExecuteTests.cs | 3 +++ .../Utility/TestDbDataReader.cs | 7 +++++- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs index d9a886d4..5ecf82ec 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs @@ -21,6 +21,8 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution /// public class Query : IDisposable { + private const string RowsAffectedFormat = "({0} row(s) affected)"; + #region Properties /// @@ -114,13 +116,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution throw new InvalidOperationException("Query has already executed."); } - DbConnection conn = null; - // Create a connection from the connection details try { string connectionString = ConnectionService.BuildConnectionString(EditorConnection.ConnectionDetails); - using (conn = EditorConnection.Factory.CreateSqlConnection(connectionString)) + using (DbConnection conn = EditorConnection.Factory.CreateSqlConnection(connectionString)) { // If we have the message listener, bind to it SqlConnection sqlConn = conn as SqlConnection; @@ -142,14 +142,14 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution { do { - // Create a message with the number of affected rows - if (reader.RecordsAffected >= 0) - { - ResultMessages.Add(String.Format("({0} row(s) affected)", reader.RecordsAffected)); - } - + // Skip this result set if there aren't any rows 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 + ? string.Format(RowsAffectedFormat, reader.RecordsAffected) + : "Command Executed Successfully"); + continue; } @@ -168,6 +168,10 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution // Add the result set to the results of the query ResultSets.Add(resultSet); + + // Add a message for the number of rows the query returned + ResultMessages.Add(string.Format(RowsAffectedFormat, resultSet.Rows.Count)); + } while (await reader.NextResultAsync(cancellationSource.Token)); } } diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/Common.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/Common.cs index f887b50f..8b3b4286 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/Common.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/Common.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Data; using System.Data.Common; -using System.Data.SqlClient; using System.Threading.Tasks; using Microsoft.SqlTools.ServiceLayer.Connection; using Microsoft.SqlTools.ServiceLayer.Connection.Contracts; @@ -106,7 +105,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution ServerName = "sqltools11" }; - return new ConnectionInfo(CreateMockFactory(data, throwOnRead), "test://test", connDetails); + return new ConnectionInfo(CreateMockFactory(data, throwOnRead), OwnerUri, connDetails); } #endregion diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/ExecuteTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/ExecuteTests.cs index 1cc56e53..9ed19c50 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/ExecuteTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/QueryExecution/ExecuteTests.cs @@ -365,6 +365,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution VerifyQueryExecuteCallCount(requestContext, Times.Once(), Times.Never(), Times.Never()); Assert.NotNull(result.Messages); Assert.NotEmpty(result.Messages); + + // ... There should not be an active query + Assert.Empty(queryService.ActiveQueries); } [Fact] diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestDbDataReader.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestDbDataReader.cs index 69edef72..e2003789 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestDbDataReader.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestDbDataReader.cs @@ -91,6 +91,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Utility public override int FieldCount { get { return Rows?.Current.Count ?? 0; } } + public override int RecordsAffected + { + // Mimics the behavior of SqlDataReader + get { return Rows != null ? -1 : 1; } + } + #region Not Implemented public override bool GetBoolean(int ordinal) @@ -200,7 +206,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Utility public override int Depth { get; } public override bool IsClosed { get; } - public override int RecordsAffected { get; } #endregion }