diff --git a/source/Sylvan.Data/ValidatingDataReader.cs b/source/Sylvan.Data/ValidatingDataReader.cs index 7e8ef85..f548149 100644 --- a/source/Sylvan.Data/ValidatingDataReader.cs +++ b/source/Sylvan.Data/ValidatingDataReader.cs @@ -37,6 +37,16 @@ public IEnumerable GetErrors() return accessor.GetErrors(); } + /// + /// Indicates that the field at the given ordinal didn't have a schema validation error. + /// + /// A column ordinal + /// True if the column was valid otherwise false. + public bool IsValid(int ordinal) + { + return accessor.IsValid(ordinal); + } + /// /// Gets the exception that was thrown when processing the column. /// @@ -78,6 +88,18 @@ public object GetValue(int ordinal) { return this.accessor.GetValue(ordinal); } + + /// + /// Gets the row number of the underlying DbDataReader. + /// This counts the number of times `Read()` has been called. + /// + public int RowNumber + { + get + { + return this.accessor.RowNumber; + } + } } sealed class ValidatingDataReader : DataReaderAdapter @@ -99,15 +121,20 @@ public IEnumerable 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; } @@ -128,6 +155,8 @@ public object GetValue(int ordinal) return r.GetValue(); } + public int RowNumber => this.reader.rowNumber; + public void SetValue(int ordinal, T? value) { var r = this.reader.cache[ordinal]; @@ -393,7 +422,7 @@ public override void ProcessValue(DbDataReader reader, int ordinal, ValueRef // 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; @@ -415,7 +444,7 @@ internal ValidatingDataReader(DbDataReader inner, DataValidationHandler? errorHa this.errorMarker = Array.Empty(); this.exceptions = Array.Empty(); this.validationContext = new DataValidationContext(new Accessor(this)); - this.counter = -1; + this.rowNumber = -1; this.validateAllRows = validateAllRows; InitializeSchema(); } @@ -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++) { @@ -520,7 +549,7 @@ bool ProcessRow() { errorCount++; this.exceptions[i] = ex; - this.errorMarker[i] = counter; + this.errorMarker[i] = rowNumber; } } if (validateAllRows || errorCount > 0) @@ -706,6 +735,7 @@ public override bool Read() } // otherwise move to the next row } + this.rowNumber = -1; return false; }