//
// 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
{
///
/// Abstract class for implementing writers that save results to file. Stores some basic info
/// that all save as writer would need.
///
public abstract class SaveAsStreamWriter : IFileStreamWriter
{
///
/// Stores the internal state for the writer that will be necessary for any writer.
///
/// The stream that will be written to
/// The SaveAs request parameters
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
///
/// Index of the first column to write to the output file
///
protected int? ColumnStartIndex { get; private set; }
///
/// Number of columns to write to the output file
///
protected int? ColumnCount { get; private set; }
///
/// Index of the last column to write to the output file
///
protected int? ColumnEndIndex { get; private set; }
///
/// The file stream to use to write the output file
///
protected Stream FileStream { get; private set; }
#endregion
///
/// Not implemented, do not use.
///
[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.");
}
///
/// Writes a row of data to the output file using the format provided by the implementing class.
///
/// The row of data to output
/// The list of columns to output
public abstract void WriteRow(IList row, IList columns);
///
/// Not implemented, do not use.
///
[Obsolete]
public void Seek(long offset)
{
throw new InvalidOperationException("SaveAs writers are meant to be written once contiguously.");
}
///
/// Flushes the file stream buffer
///
public void FlushBuffer()
{
FileStream.Flush();
}
#region IDisposable Implementation
private bool disposed;
///
/// Disposes the instance by flushing and closing the file stream
///
///
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
FileStream.Dispose();
}
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}