WIP adding unit tests for batch processing

This commit is contained in:
benrr101
2016-08-19 15:22:10 -07:00
parent 7202a7ed65
commit f72ae9ac07
9 changed files with 264 additions and 191 deletions

View File

@@ -72,6 +72,12 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
public async Task Execute(DbConnection conn, CancellationToken cancellationToken)
{
// Sanity check to make sure we haven't already run this batch
if (HasExecuted)
{
throw new InvalidOperationException("Batch has already executed.");
}
try
{
// Register the message listener to *this instance* of the batch
@@ -99,7 +105,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
// Create a message with the number of affected rows -- IF the query affects rows
ResultMessages.Add(reader.RecordsAffected >= 0
? string.Format(RowsAffectedFormat, reader.RecordsAffected)
: "Commad Executed Successfully");
: "Command(s) completed successfully.");
continue;
}
@@ -146,6 +152,26 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
}
}
/// <summary>
/// Generates a subset of the rows from a result set of the batch
/// </summary>
/// <param name="resultSetIndex">The index for selecting the result set</param>
/// <param name="startRow">The starting row of the results</param>
/// <param name="rowCount">How many rows to retrieve</param>
/// <returns>A subset of results</returns>
public ResultSetSubset GetSubset(int resultSetIndex, int startRow, int rowCount)
{
// Sanity check to make sure we have valid numbers
if (resultSetIndex < 0 || resultSetIndex >= ResultSets.Count)
{
throw new ArgumentOutOfRangeException(nameof(resultSetIndex), "Result set index cannot be less than 0" +
"or greater than the number of result sets");
}
// Retrieve the result set
return ResultSets[resultSetIndex].GetSubset(startRow, rowCount);
}
#region Private Helpers
/// <summary>

View File

@@ -17,6 +17,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts
/// </summary>
public string OwnerUri { get; set; }
/// <summary>
/// Index of the batch to get the results from
/// </summary>
public int BatchIndex { get; set; }
/// <summary>
/// Index of the result set to get the results from
/// </summary>

View File

@@ -12,6 +12,7 @@ using System.Threading.Tasks;
using Microsoft.SqlServer.Management.SqlParser.Parser;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
{
@@ -25,7 +26,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
/// <summary>
/// The batches underneath this query
/// </summary>
private IEnumerable<Batch> Batches { get; set; }
private Batch[] Batches { get; set; }
/// <summary>
/// The summaries of the batches underneath this query
@@ -72,7 +73,8 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
/// </summary>
/// <param name="queryText">The text of the query to execute</param>
/// <param name="connection">The information of the connection to use to execute the query</param>
public Query(string queryText, ConnectionInfo connection)
/// <param name="settings">Settings for how to execute the query, from the user</param>
public Query(string queryText, ConnectionInfo connection, QueryExecutionSettings settings)
{
// Sanity check for input
if (String.IsNullOrEmpty(queryText))
@@ -90,8 +92,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
cancellationSource = new CancellationTokenSource();
// Process the query into batches
ParseResult parseResult = Parser.Parse(queryText);
Batches = parseResult.Script.Batches.Select(b => new Batch(b.Sql));
ParseResult parseResult = Parser.Parse(queryText, new ParseOptions
{
BatchSeparator = settings.BatchSeparator
});
Batches = parseResult.Script.Batches.Select(b => new Batch(b.Sql)).ToArray();
}
/// <summary>
@@ -120,11 +125,12 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
/// <summary>
/// Retrieves a subset of the result sets
/// </summary>
/// <param name="batchIndex">The index for selecting the batch item</param>
/// <param name="resultSetIndex">The index for selecting the result set</param>
/// <param name="startRow">The starting row of the results</param>
/// <param name="rowCount">How many rows to retrieve</param>
/// <returns>A subset of results</returns>
public ResultSetSubset GetSubset(int resultSetIndex, int startRow, int rowCount)
public ResultSetSubset GetSubset(int batchIndex, int resultSetIndex, int startRow, int rowCount)
{
// Sanity check that the results are available
if (!HasExecuted)
@@ -132,30 +138,14 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
throw new InvalidOperationException("The query has not completed, yet.");
}
// Sanity check to make sure we have valid numbers
if (resultSetIndex < 0 || resultSetIndex >= ResultSets.Count)
// Sanity check to make sure that the batch is within bounds
if (batchIndex < 0 || batchIndex >= Batches.Length)
{
throw new ArgumentOutOfRangeException(nameof(resultSetIndex), "Result set index cannot be less than 0" +
throw new ArgumentOutOfRangeException(nameof(batchIndex), "Result set index cannot be less than 0" +
"or greater than the number of result sets");
}
ResultSet targetResultSet = ResultSets[resultSetIndex];
if (startRow < 0 || startRow >= targetResultSet.Rows.Count)
{
throw new ArgumentOutOfRangeException(nameof(startRow), "Start row cannot be less than 0 " +
"or greater than the number of rows in the resultset");
}
if (rowCount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(rowCount), "Row count must be a positive integer");
}
// Retrieve the subset of the results as per the request
object[][] rows = targetResultSet.Rows.Skip(startRow).Take(rowCount).ToArray();
return new ResultSetSubset
{
Rows = rows,
RowCount = rows.Length
};
return Batches[batchIndex].GetSubset(resultSetIndex, startRow, rowCount);
}
/// <summary>

View File

@@ -134,7 +134,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
var result = new QueryExecuteSubsetResult
{
Message = null,
ResultSubset = query.GetSubset(
ResultSubset = query.GetSubset(subsetParams.BatchIndex,
subsetParams.ResultSetIndex, subsetParams.RowsStartIndex, subsetParams.RowsCount)
};
await requestContext.SendResult(result);
@@ -262,8 +262,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
ActiveQueries.TryRemove(executeParams.OwnerUri, out oldQuery);
}
// Retrieve the current settings for executing the query with
QueryExecutionSettings settings = WorkspaceService<SqlToolsSettings>.Instance.CurrentSettings.QueryExecutionSettings;
// If we can't add the query now, it's assumed the query is in progress
Query newQuery = new Query(executeParams.QueryText, connectionInfo);
Query newQuery = new Query(executeParams.QueryText, connectionInfo, settings);
if (!ActiveQueries.TryAdd(executeParams.OwnerUri, newQuery))
{
await requestContext.SendResult(new QueryExecuteResult
@@ -292,11 +295,8 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
return;
}
// Retrieve the current settings for executing the query with
QueryExecutionSettings settings = WorkspaceService<SqlToolsSettings>.Instance.CurrentSettings.QueryExecutionSettings;
// Launch the query and respond with successfully launching it
Task executeTask = query.Execute(/*settings*/);
Task executeTask = query.Execute();
await requestContext.SendResult(new QueryExecuteResult
{
Messages = null
@@ -306,10 +306,8 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
await Task.WhenAll(executeTask);
QueryExecuteCompleteParams eventParams = new QueryExecuteCompleteParams
{
HasError = query.HasError,
Messages = query.ResultMessages.ToArray(),
OwnerUri = executeParams.OwnerUri,
ResultSetSummaries = query.ResultSummary
BatchSummaries = query.BatchSummaries
};
await requestContext.SendEvent(QueryExecuteCompleteEvent.Type, eventParams);
}

View File

@@ -3,8 +3,11 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
{
@@ -33,5 +36,33 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
}
Rows.Add(row.ToArray());
}
/// <summary>
/// Generates a subset of the rows from the result set
/// </summary>
/// <param name="startRow">The starting row of the results</param>
/// <param name="rowCount">How many rows to retrieve</param>
/// <returns>A subset of results</returns>
public ResultSetSubset GetSubset(int startRow, int rowCount)
{
// Sanity check to make sure that the row and the row count are within bounds
if (startRow < 0 || startRow >= Rows.Count)
{
throw new ArgumentOutOfRangeException(nameof(startRow), "Start row cannot be less than 0 " +
"or greater than the number of rows in the resultset");
}
if (rowCount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(rowCount), "Row count must be a positive integer");
}
// Retrieve the subset of the results as per the request
object[][] rows = Rows.Skip(startRow).Take(rowCount).ToArray();
return new ResultSetSubset
{
Rows = rows,
RowCount = rows.Length
};
}
}
}