Fix/merge xml rows (#85)

* Concatenate rows for 'for Xml' and 'for Json'

* Modify select

* remove get set for isSingleColumnXmlJsonResultSet

* Remove toList
This commit is contained in:
Sharon Ravindran
2016-10-12 17:24:12 -07:00
committed by GitHub
parent c5f44ccee1
commit 71fabd5c75

View File

@@ -7,6 +7,7 @@ using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
@@ -54,6 +55,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
/// </summary>
private bool hasBeenRead;
/// <summary>
/// Whether resultSet is a 'for xml' or 'for json' result
/// </summary>
private bool isSingleColumnXmlJsonResultSet;
/// <summary>
/// The name of the temporary file we're using to output these results in
/// </summary>
@@ -156,13 +162,29 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
return Task.Factory.StartNew(() =>
{
// Figure out which rows we need to read back
IEnumerable<long> rowOffsets = FileOffsets.Skip(startRow).Take(rowCount);
// Iterate over the rows we need and process them into output
string[][] rows = rowOffsets.Select(rowOffset =>
fileStreamReader.ReadRow(rowOffset, Columns).Select(cell => cell.DisplayValue).ToArray())
.ToArray();
string[][] rows;
// If result set is 'for xml' or 'for json',
// Concatenate all the rows together into one row
if (isSingleColumnXmlJsonResultSet)
{
// Iterate over all the rows and process them into a list of string builders
IEnumerable<StringBuilder> sbRows = FileOffsets.Select(rowOffset => fileStreamReader.ReadRow(rowOffset, Columns)
.Select(cell => cell.DisplayValue).Aggregate(new StringBuilder(), (sb, value) => sb.Append(value)));
rows = new[] { new[] { string.Join(string.Empty, sbRows) } };
}
else
{
// Figure out which rows we need to read back
IEnumerable<long> rowOffsets = FileOffsets.Skip(startRow).Take(rowCount);
// Iterate over the rows we need and process them into output
rows = rowOffsets.Select(rowOffset =>
fileStreamReader.ReadRow(rowOffset, Columns).Select(cell => cell.DisplayValue).ToArray())
.ToArray();
}
// Retrieve the subset of the results as per the request
return new ResultSetSubset
@@ -243,15 +265,19 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
/// </summary>
private void SingleColumnXmlJsonResultSet() {
if (Columns?.Length == 1)
if (Columns?.Length == 1 && RowCount != 0)
{
if (Columns[0].ColumnName.Equals(NameOfForXMLColumn, StringComparison.Ordinal))
{
Columns[0].IsXml = true;
isSingleColumnXmlJsonResultSet = true;
RowCount = 1;
}
else if (Columns[0].ColumnName.Equals(NameOfForJSONColumn, StringComparison.Ordinal))
{
Columns[0].IsJson = true;
isSingleColumnXmlJsonResultSet = true;
RowCount = 1;
}
}
}