Skip to content

Commit

Permalink
Fix IsNull issues with ValidatingDataReader
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkPflug committed Jun 10, 2024
1 parent 64e11a5 commit a7c66d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
3 changes: 0 additions & 3 deletions source/Sylvan.Data/DataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public static DbDataReader Select(this DbDataReader reader, params string[] colu
return new TransformDataReader(reader, ordinals);
}


/// <summary>
/// Selects a subset of columns for a DbDataReader.
/// </summary>
Expand Down Expand Up @@ -229,8 +228,6 @@ public static DbDataReader Where(this DbDataReader reader, Func<DbDataReader, bo
{
if (reader == null) throw new ArgumentNullException(nameof(reader));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
// TODO: TransformDataReader needs to merge into a new object rather than
// nest. nesting will lead to excessive method call overhead.
return new TransformDataReader(reader, null, predicate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Sylvan.Data;

/// <summary>
/// Defines a method for handling schema violations.
/// Defines a method for handling data validation.
/// </summary>
public delegate bool DataValidationHandler(DataValidationContext context);

Expand Down Expand Up @@ -80,7 +80,7 @@ public object GetValue(int ordinal)
}
}

class ValidatingDataReader : DataReaderAdapter
sealed class ValidatingDataReader : DataReaderAdapter
{
// limited visibility to internals
internal struct Accessor
Expand Down Expand Up @@ -150,24 +150,24 @@ public void SetValue(int ordinal, object? value)

abstract class Ref
{
public bool IsNull { get; set; }
public abstract bool IsNull { get; }

public abstract object GetValue();
public abstract void SetValue(object? value);

public virtual void Reset()
{
this.IsNull = true;
}
public abstract void Reset();
}

// A reusable strongly-typed box to store row values.
// This is used to avoid boxing every single value.
sealed class ValueRef<T> : Ref
{
public override bool IsNull => Value is null;

public T? Value
{
get; set;
get;
set;
}

public override object GetValue()
Expand All @@ -178,7 +178,6 @@ public override object GetValue()
public override void SetValue(object? value)
{
var isNull = value == null || value == DBNull.Value;
this.IsNull = isNull;
if (isNull)
{
this.Value = default;
Expand All @@ -191,8 +190,7 @@ public override void SetValue(object? value)

public override void Reset()
{
this.Value = default;
base.Reset();
Value = default;
}
}

Expand Down Expand Up @@ -297,7 +295,6 @@ public override void ProcessValue(DbDataReader reader, int ordinal, ValueRef<byt
reader.GetBytes(ordinal, 0, buffer, 0, 1);
value.Value = buffer;
}
value.IsNull = isNull;
}
}

Expand All @@ -324,7 +321,6 @@ public override void ProcessValue(DbDataReader reader, int ordinal, ValueRef<cha
reader.GetChars(ordinal, 0, buffer, 0, 1);
value.Value = buffer;
}
value.IsNull = isNull;
}
}

Expand Down Expand Up @@ -357,7 +353,6 @@ public override void ProcessValue(DbDataReader reader, int ordinal, ValueRef<T>
var isNull = reader.IsDBNull(ordinal);

value.Value = isNull ? default : accessor(reader, ordinal);
value.IsNull = isNull;
}
}

Expand All @@ -374,7 +369,7 @@ public override void ProcessValue(DbDataReader reader, int ordinal, ValueRef<T>
readonly DataValidationHandler validationHandler;
readonly bool validateAllRows;

static bool Fail(DataValidationContext context)
static bool Skip(DataValidationContext context)
{
return false;
}
Expand All @@ -383,7 +378,7 @@ internal ValidatingDataReader(DbDataReader inner, DataValidationHandler? errorHa
: base(inner)
{
this.inner = inner;
this.validationHandler = errorHandler ?? Fail;
this.validationHandler = errorHandler ?? Skip;
this.cache = Array.Empty<Ref>();
this.accessors = Array.Empty<ValueAccessor>();
this.errorMarker = Array.Empty<int>();
Expand Down

0 comments on commit a7c66d9

Please sign in to comment.