Skip to content

Commit

Permalink
Add IsValid(int ordinal) and RowNumber to DataValidationContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkPflug committed Jun 13, 2024
1 parent 466678e commit 2ce5980
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions source/Sylvan.Data/ValidatingDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public IEnumerable<int> GetErrors()
return accessor.GetErrors();
}

/// <summary>
/// Indicates that the field at the given ordinal didn't have a schema validation error.
/// </summary>
/// <param name="ordinal">A column ordinal</param>
/// <returns>True if the column was valid otherwise false.</returns>
public bool IsValid(int ordinal)
{
return accessor.IsValid(ordinal);
}

/// <summary>
/// Gets the exception that was thrown when processing the column.
/// </summary>
Expand Down Expand Up @@ -78,6 +88,18 @@ public object GetValue(int ordinal)
{
return this.accessor.GetValue(ordinal);
}

/// <summary>
/// Gets the row number of the underlying DbDataReader.
/// This counts the number of times `Read()` has been called.
/// </summary>
public int RowNumber
{
get
{
return this.accessor.RowNumber;
}
}
}

sealed class ValidatingDataReader : DataReaderAdapter
Expand All @@ -99,15 +121,20 @@ public IEnumerable<int> GetErrors()
var markers = reader.errorMarker;
for (int i = 0; i < markers.Length; i++)
{
if (markers[i] == reader.counter)
if (markers[i] == reader.rowNumber)
yield return i;
}
}

public bool IsValid(int ordinal)
{
return reader.errorMarker[ordinal] != reader.rowNumber;
}

public Exception? GetException(int ordinal)
{
return
reader.errorMarker[ordinal] == reader.counter
reader.errorMarker[ordinal] == reader.rowNumber
? reader.exceptions[ordinal]
: null;
}
Expand All @@ -128,6 +155,8 @@ public object GetValue(int ordinal)
return r.GetValue();
}

public int RowNumber => this.reader.rowNumber;

public void SetValue<T>(int ordinal, T? value)
{
var r = this.reader.cache[ordinal];
Expand Down Expand Up @@ -393,7 +422,7 @@ public override void ProcessValue(DbDataReader reader, int ordinal, ValueRef<T>
// stores the index of last row that encountered an error in that column.
int[] errorMarker;
Exception[] exceptions;
int counter;
int rowNumber;

readonly DbDataReader inner;
readonly DataValidationContext validationContext;
Expand All @@ -415,7 +444,7 @@ internal ValidatingDataReader(DbDataReader inner, DataValidationHandler? errorHa
this.errorMarker = Array.Empty<int>();
this.exceptions = Array.Empty<Exception>();
this.validationContext = new DataValidationContext(new Accessor(this));
this.counter = -1;
this.rowNumber = -1;
this.validateAllRows = validateAllRows;
InitializeSchema();
}
Expand Down Expand Up @@ -507,7 +536,7 @@ static ValueAccessor GetAccessor(Type? type, bool allowNull)

bool ProcessRow()
{
counter++;
rowNumber++;
var errorCount = 0;
for (int i = 0; i < inner.FieldCount; i++)
{
Expand All @@ -520,7 +549,7 @@ bool ProcessRow()
{
errorCount++;
this.exceptions[i] = ex;
this.errorMarker[i] = counter;
this.errorMarker[i] = rowNumber;
}
}
if (validateAllRows || errorCount > 0)
Expand Down Expand Up @@ -706,6 +735,7 @@ public override bool Read()
}
// otherwise move to the next row
}
this.rowNumber = -1;
return false;
}

Expand Down

0 comments on commit 2ce5980

Please sign in to comment.