Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add leaveOpen constructor parameter to the CsvDataReader class #2227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/CsvHelper/CsvDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ namespace CsvHelper
/// <seealso cref="System.Data.IDataReader" />
public class CsvDataReader : IDataReader
{
private readonly bool leaveOpen;
private readonly CsvReader csv;
private readonly DataTable schemaTable;
private bool skipNextRead;
private bool disposed;

/// <inheritdoc />
public object this[int i]
Expand All @@ -36,6 +38,9 @@ public object this[string name]
}
}

/// <inheritdoc />
public bool IsClosed => disposed;

/// <inheritdoc />
public int Depth
{
Expand All @@ -45,9 +50,6 @@ public int Depth
}
}

/// <inheritdoc />
public bool IsClosed { get; private set; }

/// <inheritdoc />
public int RecordsAffected
{
Expand All @@ -71,9 +73,11 @@ public int FieldCount
/// </summary>
/// <param name="csv">The CSV.</param>
/// <param name="schemaTable">The DataTable representing the file schema.</param>
public CsvDataReader(CsvReader csv, DataTable schemaTable = null)
/// <param name="leaveOpen"><c>true</c> to leave the <see cref="CsvReader"/> open after the <see cref="CsvDataReader"/> object is disposed, otherwise <c>false</c>.</param>
public CsvDataReader(CsvReader csv, DataTable schemaTable = null, bool leaveOpen = false)
{
this.csv = csv;
this.leaveOpen = leaveOpen;

csv.Read();

Expand All @@ -95,11 +99,33 @@ public void Close()
Dispose();
}

/// <inheritdoc />
/// <inheritdoc/>
public void Dispose()
{
csv.Dispose();
IsClosed = true;
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Disposes the object.
/// </summary>
/// <param name="disposing">Indicates if the object is being disposed.</param>
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}

if (disposing)
{
if (!leaveOpen)
{
csv?.Dispose();
}
}

disposed = true;
}

/// <inheritdoc />
Expand Down
48 changes: 48 additions & 0 deletions tests/CsvHelper.Tests/CsvDataReaderDisposalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Globalization;
using System.IO;
using System.Text;
using Xunit;

namespace CsvHelper.Tests
{
public class CsvDataReaderDisposalTests
{
[Fact]
public void ShouldNotDisposeCsvReaderWhenLeaveOpenParameterIsTrue()
{
var s = new StringBuilder();
s.AppendLine("StringColumn");
s.AppendLine("one");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var dataReader = new CsvDataReader(csv, leaveOpen: true);
dataReader.Dispose();

var record = csv.GetRecord<TestRecord>();
Assert.NotNull(record);
}
}

[Fact]
public void DisposeShouldSetIsClosed()
{
var s = new StringBuilder();
s.AppendLine("StringColumn");
s.AppendLine("one");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var dataReader = new CsvDataReader(csv, leaveOpen: true);
dataReader.Dispose();

Assert.True(dataReader.IsClosed);
}
}

private class TestRecord()
{
public string StringColumn { get; set; }
}
}
}