Skip to content
Merged
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
82 changes: 81 additions & 1 deletion source/Sylvan.Data.Csv/CsvDataReader+Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,87 @@ static CsvDataReader CreateInternal(TextReader reader, char[]? buffer, CsvDataRe

bool InitializeReader()
{
return this.InitializeReaderAsync().GetAwaiter().GetResult();
FillBuffer();

bool skip = resultSetMode == ResultSetMode.MultiResult;
while (skip)
{
skip = false;
if (bufferEnd == 0) break;
for (int i = idx; i < bufferEnd; i++)
{
var c = buffer[i];
if (IsEndOfLine(c))
{
if (ShouldSkip(buffer, idx, i - idx))
{
skip = true;
idx = i + 1;
}
break;
}
}
}

if (autoDetectDelimiter)
{
var c = DetectDelimiter();
this.delimiter = c;
}
if (this.newLineMode == NewLineMode.Unknown)
{
this.newLineMode = this.DetectNewLine();
}

this.minSafe = delimiter < '\r' ? '\r' : delimiter;
this.minSafe = minSafe > quote ? minSafe : quote;
this.minSafeQuote = minSafe > escape ? minSafe : escape;

#if INTRINSICS
InitIntrinsics();
#endif

// if the user specified that there are headers
// read them, and use them to determine fieldCount.
if (hasHeaders)
{
if (carryRow || state == State.Open || NextRecord())
{
ThrowPendingException();

carryRow = false;
Initialize();
this.state = State.Initialized;
this.rowNumber++;
var hasNext = NextRecord();
if (resultSetMode == ResultSetMode.SingleResult)
{
this.hasRows = hasNext;
}
else
{
this.hasRows = hasNext && this.fieldCount == this.curFieldCount;
this.state = State.Initialized;
this.carryRow = this.hasRows == false && this.fieldCount > 0;
}
}
else
{
return false;
}
}
else
{
// read the first row of data to determine fieldCount (if there were no headers)
// and support calling HasRows before Read is first called.
this.hasRows = carryRow || NextRecord();
this.carryRow = false;
Initialize();
return fieldCount != 0;
}
this.rowNumber = 0;

return true;
}

bool NextRecord()
Expand Down
19 changes: 18 additions & 1 deletion source/Sylvan.Data/SchemaAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,24 @@ public SchemaAnalyzer(SchemaAnalyzerOptions? options = null)
/// </summary>
public AnalysisResult Analyze(DbDataReader dataReader)
{
return AnalyzeAsync(dataReader).GetAwaiter().GetResult();
var colInfos = new ColumnInfo[dataReader.FieldCount];
for (int i = 0; i < colInfos.Length; i++)
{
colInfos[i] = new ColumnInfo(dataReader, i);
}

var sw = Stopwatch.StartNew();
int c = 0;
while (dataReader.Read()&& c++ < rowCount)
{
for (int i = 0; i < dataReader.FieldCount; i++)
{
colInfos[i].Analyze(dataReader, i);
}
}
sw.Stop();

return new AnalysisResult(detectSeries, colInfos);
}

/// <summary>
Expand Down
Loading