/// <summary> /// Commit the data back to the file /// </summary> /// <returns>If the write was successful</returns> public override Boolean Commit() { Boolean result = false; // Failed by default // Generate the flat file content based on the definition when connecting String flatFileContent = DelimitedFileHelper.DataTableToString(this.definition, this.Connection, this.memoryData); // Try and write the file to disk try { // Get the processed connection string (with any injected items) String connectionString = this.Connection.ConnectionStringProcessed; // Write the file File.WriteAllText(connectionString, flatFileContent, definition.EncodingFormat); // Does the file now exist (and there were no errors writing) result = File.Exists(connectionString); } catch { } base.MarkLastAction(); // Tell the provider base class that it did something // Return the result return(result); }
/// <summary> /// Connect to a stream of flat file data /// </summary> /// <param name="stream">The stream of data to connect to</param> /// <returns>If the data was valid and is a stream</returns> public override Boolean Connect(DataItemDefinition definition, DataConnection connection, Stream stream) { this.Connection = connection; this.definition = definition; // Assign the definition to use this.memoryData = new DataTable(); // Blank data by default // Do we have a stream and a definition if (stream != null && definition == null) { base.connected = true; return(true); } else if (stream != null && definition != null) { stream.Position = 0; // Reset back to the start again in case someone else has read it base.MarkLastAction(); // Tell the provider base class that it did something // Read the data from the stream provided using (StreamReader textReader = new StreamReader(stream)) { this.memoryData = DelimitedFileHelper.TextToDataTable(definition, connection, textReader.ReadToEnd()); base.connected = true; // Mark the provider as connected return(true); // Connected without any errors } } return(false); // Failed if we get to here }
/// <summary> /// Look at the file and try and represent the file as a dataset without a definition /// </summary> /// <returns>A representation of the data</returns> public override DataItemDefinition Analyse(AnalyseRequest <Object> request) { // Create a blank result data table DataItemDefinition result = new DataItemDefinition() { }; String rawData = ""; // The data which ultimately will be read from // Check to see what can of analysis is being requested if ((request.Connection?.ConnectionString ?? String.Empty) != String.Empty) { // Get the stream of data from the raw file rawData = File.ReadAllText(request.Connection.ConnectionStringProcessed); } else { // No connection was provided so use the raw data provided instead switch (request.Data.GetType().ToShortName()) { case "string": rawData = (String)request.Data; break; default: ((Stream)request.Data).Position = 0; // Reset the stream position // Read the data from the stream StreamReader reader = new StreamReader((Stream)request.Data); rawData = reader.ReadToEnd(); // Reset the position again so that it can be re-used ((Stream)request.Data).Position = 0; break; } } // Pass down to the analyse text core function AnalyseRequest <String> analyseTextRequest = new AnalyseRequest <String>() { Data = rawData, Connection = request.Connection }; result = DelimitedFileHelper.AnalyseText(analyseTextRequest); base.MarkLastAction(); // Tell the provider base class that it did something // Send the analysis data table back return(result); }
public void Analyse_Column_Numbers_From_String() { // Arrange TestHelper testHelper = new TestHelper(); String testData = testHelper.GetResourceString(TestHelper.TestFile_Headers); // Act DataItemDefinition definition = DelimitedFileHelper.AnalyseText( new AnalyseRequest <String>() { Data = testData, Connection = testHelper.TestConnection() } ); // Assert Assert.Equal(definition.ItemProperties.Count, (int)3); }
public void Analyse_Column_DataType_From_String() { // Arrange TestHelper testHelper = new TestHelper(); String testData = testHelper.GetResourceString(TestHelper.TestFile_DataTypes); // Act DataItemDefinition definition = DelimitedFileHelper.AnalyseText( new AnalyseRequest <string>() { Data = testData, Connection = testHelper.TestConnection() }); // Assert Assert.Equal(definition.ItemProperties.Count, (int)5); Assert.Equal(typeof(String), definition.ItemProperties[0].DataType); Assert.Equal(typeof(DateTime), definition.ItemProperties[1].DataType); Assert.Equal(typeof(int), definition.ItemProperties[2].DataType); Assert.Equal(typeof(String), definition.ItemProperties[3].DataType); Assert.Equal(typeof(int), definition.ItemProperties[4].DataType); }