mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Move Save As to ResultSet (#181)
It's an overhaul of the Save As mechanism to utilize the file reader/writer classes to better align with the patterns laid out by the rest of the query execution. Why make this change? This change makes our code base more uniform and adherent to the patterns/paradigms we've set up. This change also helps with the encapsulation of the classes to "separate the concerns" of each component of the save as function.
* Replumbing the save as execution to pass the call down the query stack as QueryExecutionService->Query->Batch->ResultSet
* Each layer performs it's own parameter checking
* QueryExecutionService checks if the query exists
* Query checks if the batch exists
* Batch checks if the result set exists
* ResultSet checks if the row counts are valid and if the result set has been executed
* Success/Failure delegates are passed down the chain as well
* Determination of whether a save request is a "selection" moved to the SaveResultsRequest class to eliminate duplication of code and creation of utility classes
* Making the IFileStream* classes more generic
* Removing the requirements of max characters to store from the GetWriter method, and moving it into the constructor for the temporary buffer writer - the values have been moved to the settings and given defaults
* Removing the individual type writers from IFileStreamWriter
* Removing the individual type writers from IFIleStreamReader
* Adding a new overload for WriteRow to IFileStreamWriter that will write out data, given a row's worth of data and the list of columns
* Creating a new IFileStreamFactory that creates a reader/writer pair for reading from the temporary files and writing to CSV files
* Creating a new IFileStreamFactory that creates a reader/writer pair for reading from the temporary files and writing to JSON files
* Dramatically simplified the CSV encoding functionality
* Removed duplicated logic for saving in different types and condensed down to a single chain that only differs based on what type of factory is provided
* Removing the logic for managing the list of save as tasks, since the ResultSet now performs the actual saving work, there's no real need to expose the internals of the ResultSet
* Adding new strings to the sr.strings file for save as error messages
* Completely rewriting the unit tests for the save as mechanism. Very fine grained unit tests now that should cover majority of cases (aside from race conditions)
* Refactoring maxchars params into settings and out of file stream factory
* Removing write*/read* methods from file stream readers/writers
* Migrating the CSV save as to the resultset
* Tweaks to unit testing to eliminate writing files to disk
* WIP, moving to a base class for save results writers
* Everything is wired up and compiles
* Adding unit tests for CSV encoding
* Adding unit tests for CSV and Json writers
* Adding tests to the result set for saving
* Refactor to throw exceptions on errors instead of calling failure handler
* Unit tests for batch/query argument in range
* Unit tests
* Adding service integration unit tests
* Final polish, copyright notices, etc
* Adding NULL logic
* Fixing issue of unicode to utf8
* Fixing issues as per @kburtram code review comments
* Adding files that got broken?
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstract class for implementing writers that save results to file. Stores some basic info
|
||||
/// that all save as writer would need.
|
||||
/// </summary>
|
||||
public abstract class SaveAsStreamWriter : IFileStreamWriter
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores the internal state for the writer that will be necessary for any writer.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream that will be written to</param>
|
||||
/// <param name="requestParams">The SaveAs request parameters</param>
|
||||
protected SaveAsStreamWriter(Stream stream, SaveResultsRequestParams requestParams)
|
||||
{
|
||||
FileStream = stream;
|
||||
var saveParams = requestParams;
|
||||
if (requestParams.IsSaveSelection)
|
||||
{
|
||||
// ReSharper disable PossibleInvalidOperationException IsSaveSelection verifies these values exist
|
||||
ColumnStartIndex = saveParams.ColumnStartIndex.Value;
|
||||
ColumnEndIndex = saveParams.ColumnEndIndex.Value;
|
||||
ColumnCount = saveParams.ColumnEndIndex.Value - saveParams.ColumnStartIndex.Value + 1;
|
||||
// ReSharper restore PossibleInvalidOperationException
|
||||
}
|
||||
}
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Index of the first column to write to the output file
|
||||
/// </summary>
|
||||
protected int? ColumnStartIndex { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Number of columns to write to the output file
|
||||
/// </summary>
|
||||
protected int? ColumnCount { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Index of the last column to write to the output file
|
||||
/// </summary>
|
||||
protected int? ColumnEndIndex { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The file stream to use to write the output file
|
||||
/// </summary>
|
||||
protected Stream FileStream { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Not implemented, do not use.
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public int WriteRow(StorageDataReader dataReader)
|
||||
{
|
||||
throw new InvalidOperationException("This type of writer is meant to write values from a list of cell values only.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a row of data to the output file using the format provided by the implementing class.
|
||||
/// </summary>
|
||||
/// <param name="row">The row of data to output</param>
|
||||
/// <param name="columns">The list of columns to output</param>
|
||||
public abstract void WriteRow(IList<DbCellValue> row, IList<DbColumnWrapper> columns);
|
||||
|
||||
/// <summary>
|
||||
/// Flushes the file stream buffer
|
||||
/// </summary>
|
||||
public void FlushBuffer()
|
||||
{
|
||||
FileStream.Flush();
|
||||
}
|
||||
|
||||
#region IDisposable Implementation
|
||||
|
||||
private bool disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the instance by flushing and closing the file stream
|
||||
/// </summary>
|
||||
/// <param name="disposing"></param>
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed || !disposing)
|
||||
{
|
||||
disposed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
FileStream.Flush();
|
||||
FileStream.Dispose();
|
||||
}
|
||||
public virtual void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user