// // 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; using Microsoft.SqlTools.ServiceLayer.SqlContext; using Microsoft.SqlTools.ServiceLayer.Utility; namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage { public class SaveAsXmlFileStreamFactory : IFileStreamFactory { #region Properties /// /// Settings for query execution /// public QueryExecutionSettings QueryExecutionSettings { get; set; } /// /// Parameters for the save as XML request /// public SaveResultsAsXmlRequestParams SaveRequestParams { get; set; } #endregion /// /// File names are not meant to be created with this factory. /// /// Thrown all times [Obsolete] public string CreateFile() { throw new InvalidOperationException(); } /// /// Returns a new service buffer reader for reading results back in from the temporary buffer files, file share is ReadWrite to allow concurrent reads/writes to the file. /// /// Path to the temp buffer file /// Stream reader public IFileStreamReader GetReader(string fileName) { return new ServiceBufferFileStreamReader( new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), QueryExecutionSettings ); } /// /// Returns a new XML writer for writing results to a XML file, file share is ReadWrite to allow concurrent reads/writes to the file. /// /// Path to the XML output file /// /// The entire list of columns for the result set. They will be filtered down as per the /// request params. /// /// Stream writer public IFileStreamWriter GetWriter(string fileName, IReadOnlyList columns) { return new SaveAsXmlFileStreamWriter( new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite), SaveRequestParams, columns ); } /// /// Safely deletes the file /// /// Path to the file to delete public void DisposeFile(string fileName) { FileUtilities.SafeFileDelete(fileName); } } }