Skip to content

Commit

Permalink
DapperRow: value is object?
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Aug 20, 2023
1 parent abca409 commit baff3b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
6 changes: 3 additions & 3 deletions Dapper/SqlMapper.DapperRow.Descriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ AttributeCollection ICustomTypeDescriptor.GetAttributes()
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) => EventDescriptorCollection.Empty;

internal static PropertyDescriptorCollection GetProperties(DapperRow row) => GetProperties(row?.table, row);
internal static PropertyDescriptorCollection GetProperties(DapperTable? table, IDictionary<string,object>? row = null)
internal static PropertyDescriptorCollection GetProperties(DapperTable? table, IDictionary<string,object?>? row = null)
{
string[]? names = table?.FieldNames;
if (names is null || names.Length == 0) return PropertyDescriptorCollection.Empty;
Expand Down Expand Up @@ -97,8 +97,8 @@ public RowBoundPropertyDescriptor(Type type, string name, int index) : base(name
public override Type PropertyType => _type;
public override object GetValue(object component)
=> ((DapperRow)component).TryGetValue(_index, out var val) ? (val ?? DBNull.Value): DBNull.Value;
public override void SetValue(object component, object value)
=> ((DapperRow)component).SetValue(_index, value is DBNull ? null! : value);
public override void SetValue(object component, object? value)
=> ((DapperRow)component).SetValue(_index, value is DBNull ? null : value);
}
}
}
Expand Down
72 changes: 36 additions & 36 deletions Dapper/SqlMapper.DapperRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Dapper
public static partial class SqlMapper
{
private sealed partial class DapperRow
: IDictionary<string, object>
, IReadOnlyDictionary<string, object>
: IDictionary<string, object?>
, IReadOnlyDictionary<string, object?>
{
private readonly DapperTable table;
private object[] values;
private object?[] values;

public DapperRow(DapperTable table, object[] values)
public DapperRow(DapperTable table, object?[] values)
{
this.table = table ?? throw new ArgumentNullException(nameof(table));
this.values = values ?? throw new ArgumentNullException(nameof(values));
Expand All @@ -26,7 +26,7 @@ private sealed class DeadValue
private DeadValue() { /* hiding constructor */ }
}

int ICollection<KeyValuePair<string, object>>.Count
int ICollection<KeyValuePair<string, object?>>.Count
{
get
{
Expand All @@ -39,18 +39,18 @@ int ICollection<KeyValuePair<string, object>>.Count
}
}

public bool TryGetValue(string key, out object value)
public bool TryGetValue(string key, out object? value)
=> TryGetValue(table.IndexOfName(key), out value);

internal bool TryGetValue(int index, out object value)
internal bool TryGetValue(int index, out object? value)
{
if (index < 0)
{ // doesn't exist
value = null!;
return false;
}
// exists, **even if** we don't have a value; consider table rows heterogeneous
value = index < values.Length ? values[index] : null!;
value = index < values.Length ? values[index] : null;
if (value is DeadValue)
{ // pretend it isn't here
value = null!;
Expand Down Expand Up @@ -79,15 +79,15 @@ public override string ToString()
return sb.Append('}').ToStringRecycle();
}

public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
{
var names = table.FieldNames;
for (var i = 0; i < names.Length; i++)
{
object value = i < values.Length ? values[i] : null!;
object? value = i < values.Length ? values[i] : null!;
if (!(value is DeadValue))
{
yield return new KeyValuePair<string, object>(names[i], value);
yield return new KeyValuePair<string, object?>(names[i], value);
}
}
}
Expand All @@ -99,55 +99,55 @@ IEnumerator IEnumerable.GetEnumerator()

#region Implementation of ICollection<KeyValuePair<string,object>>

void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item)
{
IDictionary<string, object> dic = this;
IDictionary<string, object?> dic = this;
dic.Add(item.Key, item.Value);
}

void ICollection<KeyValuePair<string, object>>.Clear()
void ICollection<KeyValuePair<string, object?>>.Clear()
{ // removes values for **this row**, but doesn't change the fundamental table
for (int i = 0; i < values.Length; i++)
values[i] = DeadValue.Default;
}

bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item)
{
return TryGetValue(item.Key, out object value) && Equals(value, item.Value);
return TryGetValue(item.Key, out object? value) && Equals(value, item.Value);
}

void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
void ICollection<KeyValuePair<string, object?>>.CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex)
{
foreach (var kv in this)
{
array[arrayIndex++] = kv; // if they didn't leave enough space; not our fault
}
}

bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item)
{
IDictionary<string, object> dic = this;
IDictionary<string, object?> dic = this;
return dic.Remove(item.Key);
}

bool ICollection<KeyValuePair<string, object>>.IsReadOnly => false;
bool ICollection<KeyValuePair<string, object?>>.IsReadOnly => false;
#endregion

#region Implementation of IDictionary<string,object>

bool IDictionary<string, object>.ContainsKey(string key)
bool IDictionary<string, object?>.ContainsKey(string key)
{
int index = table.IndexOfName(key);
if (index < 0 || index >= values.Length || values[index] is DeadValue) return false;
return true;
}

void IDictionary<string, object>.Add(string key, object value)
void IDictionary<string, object?>.Add(string key, object? value)
{
SetValue(key, value, true);
}

bool IDictionary<string, object>.Remove(string key)
bool IDictionary<string, object?>.Remove(string key)
=> Remove(table.IndexOfName(key));

internal bool Remove(int index)
Expand All @@ -157,18 +157,18 @@ internal bool Remove(int index)
return true;
}

object IDictionary<string, object>.this[string key]
object? IDictionary<string, object?>.this[string key]
{
get { TryGetValue(key, out object val); return val; }
get { TryGetValue(key, out object? val); return val; }
set { SetValue(key, value, false); }
}

public object SetValue(string key, object value)
public object? SetValue(string key, object? value)
{
return SetValue(key, value, false);
}

private object SetValue(string key, object value, bool isAdd)
private object? SetValue(string key, object? value, bool isAdd)
{
if (key is null) throw new ArgumentNullException(nameof(key));
int index = table.IndexOfName(key);
Expand All @@ -183,7 +183,7 @@ private object SetValue(string key, object value, bool isAdd)
}
return SetValue(index, value);
}
internal object SetValue(int index, object value)
internal object? SetValue(int index, object? value)
{
int oldLength = values.Length;
if (oldLength <= index)
Expand All @@ -199,12 +199,12 @@ internal object SetValue(int index, object value)
return values[index] = value;
}

ICollection<string> IDictionary<string, object>.Keys
ICollection<string> IDictionary<string, object?>.Keys
{
get { return this.Select(kv => kv.Key).ToArray(); }
}

ICollection<object> IDictionary<string, object>.Values
ICollection<object?> IDictionary<string, object?>.Values
{
get { return this.Select(kv => kv.Value).ToArray(); }
}
Expand All @@ -215,31 +215,31 @@ ICollection<object> IDictionary<string, object>.Values
#region Implementation of IReadOnlyDictionary<string,object>


int IReadOnlyCollection<KeyValuePair<string, object>>.Count
int IReadOnlyCollection<KeyValuePair<string, object?>>.Count
{
get
{
return values.Count(t => !(t is DeadValue));
}
}

bool IReadOnlyDictionary<string, object>.ContainsKey(string key)
bool IReadOnlyDictionary<string, object?>.ContainsKey(string key)
{
int index = table.IndexOfName(key);
return index >= 0 && index < values.Length && !(values[index] is DeadValue);
}

object IReadOnlyDictionary<string, object>.this[string key]
object? IReadOnlyDictionary<string, object?>.this[string key]
{
get { TryGetValue(key, out object val); return val; }
get { TryGetValue(key, out object? val); return val; }
}

IEnumerable<string> IReadOnlyDictionary<string, object>.Keys
IEnumerable<string> IReadOnlyDictionary<string, object?>.Keys
{
get { return this.Select(kv => kv.Key); }
}

IEnumerable<object> IReadOnlyDictionary<string, object>.Values
IEnumerable<object?> IReadOnlyDictionary<string, object?>.Values
{
get { return this.Select(kv => kv.Value); }
}
Expand Down

0 comments on commit baff3b2

Please sign in to comment.