diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/ResultSet.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/ResultSet.cs index ad392cf7..39aabd49 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/ResultSet.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/ResultSet.cs @@ -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 /// private bool hasBeenRead; + /// + /// Whether resultSet is a 'for xml' or 'for json' result + /// + private bool isSingleColumnXmlJsonResultSet; + /// /// The name of the temporary file we're using to output these results in /// @@ -156,13 +162,29 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution return Task.Factory.StartNew(() => { - // Figure out which rows we need to read back - IEnumerable 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 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 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 /// 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; } } }