/// <summary> /// Reads from the reader until there are no more results to read /// </summary> /// <param name="cancellationToken">Cancellation token for cancelling the query</param> public async Task ReadResultToEnd(CancellationToken cancellationToken) { // Mark that result has been read hasBeenRead = true; // Open a writer for the file using (IFileStreamWriter fileWriter = fileStreamFactory.GetWriter(outputFileName, MaxCharsToStore, MaxXmlCharsToStore)) { // If we can initialize the columns using the column schema, use that if (!DataReader.DbDataReader.CanGetColumnSchema()) { throw new InvalidOperationException(SR.QueryServiceResultSetNoColumnSchema); } Columns = DataReader.Columns; long currentFileOffset = 0; while (await DataReader.ReadAsync(cancellationToken)) { RowCount++; FileOffsets.Add(currentFileOffset); currentFileOffset += fileWriter.WriteRow(DataReader); } } // Check if resultset is 'for xml/json'. If it is, set isJson/isXml value in column metadata SingleColumnXmlJsonResultSet(); }
/// <summary> /// Adds a single row to the end of the buffer file. INTENDED FOR SINGLE ROW INSERTION ONLY. /// </summary> /// <param name="dbDataReader">An UNREAD db data reader</param> /// <returns>The offset into the file where the row was inserted</returns> private async Task <long> AppendRowToBuffer(DbDataReader dbDataReader) { Validate.IsNotNull(nameof(dbDataReader), dbDataReader); // Sanity check to make sure that results read has started if (!hasStartedRead) { throw new InvalidOperationException(SR.QueryServiceResultSetNotRead); } // NOTE: We are no longer checking to see if the data reader has rows before reading // b/c of a quirk in SqlClient. In some scenarios, a SqlException isn't thrown until we // read. In order to get appropriate errors back to the user, we'll read first. // Returning false from .ReadAsync means there aren't any rows. // Create a storage data reader, read it, make sure there were results StorageDataReader dataReader = new StorageDataReader(dbDataReader); if (!await dataReader.ReadAsync(CancellationToken.None)) { throw new InvalidOperationException(SR.QueryServiceResultSetAddNoRows); } using (IFileStreamWriter writer = fileStreamFactory.GetWriter(outputFileName)) { // Write the row to the end of the file long currentFileOffset = totalBytesWritten; writer.Seek(currentFileOffset); totalBytesWritten += writer.WriteRow(dataReader); return(currentFileOffset); } }
/// <summary> /// Adds a single row to the end of the buffer file. INTENDED FOR SINGLE ROW INSERTION ONLY. /// </summary> /// <param name="dbDataReader">An UNREAD db data reader</param> /// <returns>The offset into the file where the row was inserted</returns> private async Task <long> AppendRowToBuffer(DbDataReader dbDataReader) { Validate.IsNotNull(nameof(dbDataReader), dbDataReader); if (!hasBeenRead) { throw new InvalidOperationException(SR.QueryServiceResultSetNotRead); } if (!dbDataReader.HasRows) { throw new InvalidOperationException(SR.QueryServiceResultSetAddNoRows); } StorageDataReader dataReader = new StorageDataReader(dbDataReader); using (IFileStreamWriter writer = fileStreamFactory.GetWriter(outputFileName)) { // Write the row to the end of the file long currentFileOffset = totalBytesWritten; writer.Seek(currentFileOffset); await dataReader.ReadAsync(CancellationToken.None); totalBytesWritten += writer.WriteRow(dataReader); return(currentFileOffset); } }
public void CloseStreams() { if (this.writer != null) { this.writer.Dispose(); this.writer = null; } }
private static IFileStreamFactory GetMockFactory(IFileStreamWriter writer, Func <string, IFileStreamReader> readerGenerator) { var mockFactory = new Mock <IFileStreamFactory>(); mockFactory.Setup(f => f.GetWriter(It.IsAny <string>())) .Returns(writer); mockFactory.Setup(f => f.GetReader(It.IsAny <string>())) .Returns(readerGenerator); return(mockFactory.Object); }
public ApiConnectionConfigurationWritter( IConfigurationFileGetter getEnvPath = null, IConfigurationFileReader getFileInformation = null, IFileStreamWriter writer = null, IFileWrapper fileWrapper = null, EnvironmentVariableReader envVariablesReader = null) { FileWriter = writer ?? new FileStreamWriter(); EnvironmentPathGetter = getEnvPath ?? new ConfigurationFileGetter(); FileInformationGetter = getFileInformation ?? new ConfigurationFileReader(); FileWrap = fileWrapper ?? new FileWrapper(); GetEnvironmentVariable = envVariablesReader ?? new EnvironmentVariableReader(); }
private void EnsureWriterCreated() { if (this.writer == null) { IFileStreamFactory factory; switch (this.requestParams.SaveFormat.ToLowerInvariant()) { case "json": factory = new SaveAsJsonFileStreamFactory() { SaveRequestParams = CreateJsonRequestParams() }; break; case "csv": factory = new SaveAsCsvFileStreamFactory() { SaveRequestParams = CreateCsvRequestParams() }; break; case "xml": factory = new SaveAsXmlFileStreamFactory() { SaveRequestParams = CreateXmlRequestParams() }; break; case "excel": factory = new SaveAsExcelFileStreamFactory() { SaveRequestParams = CreateExcelRequestParams() }; break; default: throw new Exception(SR.SerializationServiceUnsupportedFormat(this.requestParams.SaveFormat)); } this.writer = factory.GetWriter(requestParams.FilePath); } }
/// <summary> /// Constructor /// </summary> /// <param name="fileStreamWriter">Retrieves the stream needed to write to the file</param> public DataFileWriter(IFileStreamWriter fileStreamWriter) { _fileStreamWriter = fileStreamWriter; }