Skip to content

Commit 4db1de2

Browse files
chrischuAGiorgetti
authored andcommitted
Add checks for getting the byte array
1 parent b28db8d commit 4db1de2

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

src/NEventStore.Persistence.Sql/CommitExtensions.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public static ICommit GetCommit(this IDataRecord record, ISerialize serializer,
3232
var commitSequence = record[CommitSequenceIndex].ToInt();
3333
var commitStamp = sqlDialect.ToDateTime(record[CommitStampIndex]);
3434
var checkpointToken = record.CheckpointNumber();
35-
3635
var headers = serializer.Deserialize<Dictionary<string, object>>(record, HeadersIndex);
36+
var payloadBytes = record.GetByteArray(PayloadIndex);
3737

38-
var events = eventSerializer.DeserializeEventMessages((byte[])record[PayloadIndex], bucketId, streamId,
39-
streamRevision, commitId, commitSequence, commitStamp, checkpointToken, headers);
38+
var events = eventSerializer.DeserializeEventMessages(payloadBytes, bucketId, streamId,
39+
streamRevision, commitId, commitSequence, commitStamp, checkpointToken, headers);
4040

4141
return new Commit(bucketId,
4242
streamId,
@@ -65,20 +65,24 @@ public static long CheckpointNumber(this IDataRecord record)
6565
}
6666

6767
public static T Deserialize<T>(this ISerialize serializer, IDataRecord record, int index)
68+
{
69+
var bytes = record.GetByteArray(index);
70+
return bytes.Length == 0 ? default(T) : serializer.Deserialize<T>(bytes);
71+
}
72+
73+
internal static byte[] GetByteArray(this IDataRecord record, int index)
6874
{
6975
if (index >= record.FieldCount)
7076
{
71-
return default(T);
77+
return default;
7278
}
79+
var value = record[index];
7380

74-
object value = record[index];
7581
if (value == null || value == DBNull.Value)
7682
{
77-
return default(T);
83+
return default;
7884
}
79-
80-
var bytes = (byte[]) value;
81-
return bytes.Length == 0 ? default(T) : serializer.Deserialize<T>(bytes);
85+
return (byte[])value;
8286
}
8387
}
8488
}

0 commit comments

Comments
 (0)