mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
Added support for specifying delimiter while exporting query results as CSV (#653)
This commit is contained in:
committed by
Matt Irvine
parent
ebaec9c319
commit
77a08b4bdd
@@ -77,6 +77,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts
|
|||||||
/// Include headers of columns in CSV
|
/// Include headers of columns in CSV
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IncludeHeaders { get; set; }
|
public bool IncludeHeaders { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Delimeter for separating data items in CSV
|
||||||
|
/// </summary>
|
||||||
|
public string Delimiter { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -48,13 +48,20 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
|
|||||||
/// </param>
|
/// </param>
|
||||||
public override void WriteRow(IList<DbCellValue> row, IList<DbColumnWrapper> columns)
|
public override void WriteRow(IList<DbCellValue> row, IList<DbColumnWrapper> columns)
|
||||||
{
|
{
|
||||||
|
string delimiter = ",";
|
||||||
|
if(!string.IsNullOrEmpty(saveParams.Delimiter))
|
||||||
|
{
|
||||||
|
delimiter = saveParams.Delimiter;
|
||||||
|
}
|
||||||
|
|
||||||
// Write out the header if we haven't already and the user chose to have it
|
// Write out the header if we haven't already and the user chose to have it
|
||||||
if (saveParams.IncludeHeaders && !headerWritten)
|
if (saveParams.IncludeHeaders && !headerWritten)
|
||||||
{
|
{
|
||||||
// Build the string
|
// Build the string
|
||||||
var selectedColumns = columns.Skip(ColumnStartIndex ?? 0).Take(ColumnCount ?? columns.Count)
|
var selectedColumns = columns.Skip(ColumnStartIndex ?? 0).Take(ColumnCount ?? columns.Count)
|
||||||
.Select(c => EncodeCsvField(c.ColumnName) ?? string.Empty);
|
.Select(c => EncodeCsvField(c.ColumnName) ?? string.Empty);
|
||||||
string headerLine = string.Join(",", selectedColumns);
|
|
||||||
|
string headerLine = string.Join(delimiter, selectedColumns);
|
||||||
|
|
||||||
// Encode it and write it out
|
// Encode it and write it out
|
||||||
byte[] headerBytes = Encoding.UTF8.GetBytes(headerLine + Environment.NewLine);
|
byte[] headerBytes = Encoding.UTF8.GetBytes(headerLine + Environment.NewLine);
|
||||||
@@ -67,7 +74,7 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
|
|||||||
var selectedCells = row.Skip(ColumnStartIndex ?? 0)
|
var selectedCells = row.Skip(ColumnStartIndex ?? 0)
|
||||||
.Take(ColumnCount ?? columns.Count)
|
.Take(ColumnCount ?? columns.Count)
|
||||||
.Select(c => EncodeCsvField(c.DisplayValue));
|
.Select(c => EncodeCsvField(c.DisplayValue));
|
||||||
string rowLine = string.Join(",", selectedCells);
|
string rowLine = string.Join(delimiter, selectedCells);
|
||||||
|
|
||||||
// Encode it and write it out
|
// Encode it and write it out
|
||||||
byte[] rowBytes = Encoding.UTF8.GetBytes(rowLine + Environment.NewLine);
|
byte[] rowBytes = Encoding.UTF8.GetBytes(rowLine + Environment.NewLine);
|
||||||
|
|||||||
@@ -212,5 +212,54 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.DataStorage
|
|||||||
Assert.Equal(data[i].DisplayValue, dataValues[i - 1]);
|
Assert.Equal(data[i].DisplayValue, dataValues[i - 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WriteRowWithCustomDelimeters()
|
||||||
|
{
|
||||||
|
// Setup:
|
||||||
|
// ... Create a request params that has custom delimeter say pipe("|") then this delimeter should be used
|
||||||
|
// ... Create a set of data to write
|
||||||
|
// ... Create a memory location to store the data
|
||||||
|
var requestParams = new SaveResultsAsCsvRequestParams
|
||||||
|
{
|
||||||
|
Delimiter = "|",
|
||||||
|
IncludeHeaders = true
|
||||||
|
};
|
||||||
|
List<DbCellValue> data = new List<DbCellValue>
|
||||||
|
{
|
||||||
|
new DbCellValue { DisplayValue = "item1" },
|
||||||
|
new DbCellValue { DisplayValue = "item2" }
|
||||||
|
};
|
||||||
|
List<DbColumnWrapper> columns = new List<DbColumnWrapper>
|
||||||
|
{
|
||||||
|
new DbColumnWrapper(new TestDbColumn("column1")),
|
||||||
|
new DbColumnWrapper(new TestDbColumn("column2"))
|
||||||
|
};
|
||||||
|
byte[] output = new byte[8192];
|
||||||
|
|
||||||
|
// If: I write a row
|
||||||
|
SaveAsCsvFileStreamWriter writer = new SaveAsCsvFileStreamWriter(new MemoryStream(output), requestParams);
|
||||||
|
using (writer)
|
||||||
|
{
|
||||||
|
writer.WriteRow(data, columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then:
|
||||||
|
// ... It should have written two lines
|
||||||
|
string outputString = Encoding.UTF8.GetString(output).TrimEnd('\0', '\r', '\n');
|
||||||
|
string[] lines = outputString.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
|
||||||
|
Assert.Equal(2, lines.Length);
|
||||||
|
|
||||||
|
// ... It should have written a header line with two, pipe("|") separated names
|
||||||
|
string[] headerValues = lines[0].Split('|');
|
||||||
|
Assert.Equal(2, headerValues.Length);
|
||||||
|
for (int i = 0; i < columns.Count; i++)
|
||||||
|
{
|
||||||
|
Assert.Equal(columns[i].ColumnName, headerValues[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: No need to check values, it is done as part of the previous tests
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user