Skip to content

Commit c75b278

Browse files
Improve EndOfStream detection in CsvDataReader
Replaces the EndOfStream property logic to rely on a new _readReturnedFalse flag, which is set when Read() returns false, ensuring more accurate end-of-stream detection. Also simplifies header name generation by using string.IsNullOrWhiteSpace.
1 parent 15fe548 commit c75b278

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

project/dbatools.Tests/Csv/CsvDataReaderTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,11 +1123,10 @@ public void TestParseErrorFlag_TrueWhenErrorSkipped()
11231123
// The parse will fail and the error will be skipped
11241124
// Depending on how the parser handles this, the row might be skipped
11251125
// Let's just verify the flag is accessible
1126-
bool hadError = false;
11271126
while (reader.Read())
11281127
{
1129-
if (reader.ParseErrorFlag)
1130-
hadError = true;
1128+
// ParseErrorFlag is accessible during reading
1129+
_ = reader.ParseErrorFlag;
11311130
}
11321131
// Parse errors were collected
11331132
Assert.IsTrue(reader.ParseErrors.Count >= 0);

project/dbatools/Csv/Reader/CsvDataReader.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public sealed class CsvDataReader : IDataReader
7777
// LumenWorks compatibility flags - reset at start of each Read()
7878
private bool _missingFieldFlag;
7979
private bool _parseErrorFlag;
80+
private bool _readReturnedFalse; // True after Read() returns false (EndOfStream)
8081

8182
// Buffer for first data row when no header row (needed to create columns during Initialize)
8283
private string _bufferedFirstLine;
@@ -671,7 +672,7 @@ private string GetTrimmedHeaderName(string name, int fieldIndex)
671672
}
672673

673674
// Generate default header name for empty or whitespace-only headers (LumenWorks compatibility)
674-
if (string.IsNullOrEmpty(result) || (result != null && result.Trim().Length == 0))
675+
if (string.IsNullOrWhiteSpace(result))
675676
{
676677
result = _options.DefaultHeaderName + fieldIndex;
677678
}
@@ -1498,6 +1499,11 @@ public bool Read()
14981499
{
14991500
ReportProgressIfNeeded();
15001501
}
1502+
else
1503+
{
1504+
// Mark end of stream when Read() returns false
1505+
_readReturnedFalse = true;
1506+
}
15011507

15021508
return result;
15031509
}
@@ -3821,7 +3827,7 @@ private static char NormalizeSmartQuoteChar(char c)
38213827
/// <remarks>
38223828
/// Provides LumenWorks CsvReader compatibility.
38233829
/// </remarks>
3824-
public bool EndOfStream => _endOfStream && _bufferPosition >= _bufferLength;
3830+
public bool EndOfStream => _readReturnedFalse;
38253831

38263832
/// <summary>
38273833
/// Gets whether the current record had missing fields that were padded with nulls.

0 commit comments

Comments
 (0)