Skip to content

Commit

Permalink
use value-tuples instead of tuples (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored Dec 16, 2021
1 parent d552f05 commit 616bfa8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
13 changes: 6 additions & 7 deletions src/MsSqlCdc/CdcDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using System;

namespace MsSqlCdc;

Expand Down Expand Up @@ -59,7 +58,7 @@ public static async Task<byte[]> GetNextLsn(SqlConnection connection, long lsn)
return nextLsn;
}

public static async Task<List<List<Tuple<string, object>>>> GetAllChanges(
public static async Task<List<List<(string fieldName, object fieldValue)>>> GetAllChanges(
SqlConnection connection,
string captureInstance,
long beginLsn,
Expand All @@ -68,7 +67,7 @@ public static async Task<List<List<Tuple<string, object>>>> GetAllChanges(
return await GetChanges(connection, "cdc.fn_cdc_get_all_changes", captureInstance, beginLsn, endLsn);
}

public static async Task<List<List<Tuple<string, object>>>> GetNetChanges(
public static async Task<List<List<(string fieldName, object fieldValue)>>> GetNetChanges(
SqlConnection connection,
string captureInstance,
long beginLsn,
Expand All @@ -77,7 +76,7 @@ public static async Task<List<List<Tuple<string, object>>>> GetNetChanges(
return await GetChanges(connection, "cdc.fn_cdc_get_net_changes", captureInstance, beginLsn, endLsn);
}

private static async Task<List<List<Tuple<string, object>>>> GetChanges(
private static async Task<List<List<(string fieldName, object fieldValue)>>> GetChanges(
SqlConnection connection,
string cdcFunction,
string captureInstance,
Expand All @@ -94,14 +93,14 @@ private static async Task<List<List<Tuple<string, object>>>> GetChanges(
command.Parameters.AddWithValue("@begin_lsn", beginLsn);
command.Parameters.AddWithValue("@end_lsn", endLsn);

var changes = new List<List<Tuple<string, object>>>();
var changes = new List<List<(string name, object value)>>();
using SqlDataReader reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var column = new List<Tuple<string, object>>();
var column = new List<(string fieldName, object fieldValue)>();
for (var i = 0; i < reader.FieldCount; i++)
{
column.Add(new Tuple<string, object>(reader.GetName(i), reader.GetValue(i)));
column.Add((reader.GetName(i), reader.GetValue(i)));
}

changes.Add(column);
Expand Down
14 changes: 8 additions & 6 deletions src/MsSqlCdc/DataConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ public static class DataConvert
/// <param name="captureInstance">The tablename of the column.</param>
/// <returns>Returns the CDC column as a ChangeData record.</returns>
/// <exception cref="Exception"></exception>
public static ChangeData<dynamic> ConvertCdcColumn(List<Tuple<string, object>> columnFields, string captureInstance)
public static ChangeData<dynamic> ConvertCdcColumn(
List<(string fieldName, object fieldValue)> columnFields,
string captureInstance)
{
if (columnFields.Count < 3)
throw new Exception($"Count of column fields should be 4 or greater, instead got '{columnFields.Count}'.");

var startLsn = ConvertBinaryLsn((byte[])columnFields[0].Item2);
var seqVal = ConvertBinaryLsn((byte[])columnFields[1].Item2);
var operation = ConvertIntOperation((int)columnFields[2].Item2);
var updateMask = Encoding.UTF8.GetString((byte[])columnFields[3].Item2);
var startLsn = ConvertBinaryLsn((byte[])columnFields[0].fieldValue);
var seqVal = ConvertBinaryLsn((byte[])columnFields[1].fieldValue);
var operation = ConvertIntOperation((int)columnFields[2].fieldValue);
var updateMask = Encoding.UTF8.GetString((byte[])columnFields[3].fieldValue);

var body = columnFields.Skip(4)
.Aggregate(new ExpandoObject() as IDictionary<string, object>,
(acc, x) => { acc[x.Item1] = x.Item2; return acc; }) as dynamic;
(acc, x) => { acc[x.fieldName] = x.fieldValue; return acc; }) as dynamic;

return new ChangeData<dynamic>(
startLsn,
Expand Down
18 changes: 9 additions & 9 deletions test/MsSqlCdc.Tests/DataConvertTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public void ConvertCdcColumn_ShouldReturnChangeData_OnValidInput(
double salary)
{
var captureInstance = "dbo_Employee";
var columnFields = new List<Tuple<string, object>>
var columnFields = new List<(string name, object value)>
{
new Tuple<string, object>("__$start_lsn", BitConverter.GetBytes(startLsn).Reverse().ToArray()),
new Tuple<string, object>("__$seqval", BitConverter.GetBytes(seqVal).Reverse().ToArray()),
new Tuple<string, object>("__$operation", (int)operation),
new Tuple<string, object>("__$update_mask", Encoding.ASCII.GetBytes(updateMask)),
new Tuple<string, object>("Id", id),
new Tuple<string, object>("Name", name),
new Tuple<string, object>("Salary", salary),
("__$start_lsn", BitConverter.GetBytes(startLsn).Reverse().ToArray()),
("__$seqval", BitConverter.GetBytes(seqVal).Reverse().ToArray()),
("__$operation", (int)operation),
("__$update_mask", Encoding.ASCII.GetBytes(updateMask)),
("Id", id),
("Name", name),
("Salary", salary),
};

var changeData = new ChangeData<dynamic>(
Expand Down Expand Up @@ -75,7 +75,7 @@ public void ConvertCdcColumn_ShouldThrowException_OnColumnFieldsBeingLessThanFou
var captureInstance = "dbo_Employee";

var columnFields = Enumerable.Range(0, columnFieldsCount)
.Select(x => new Tuple<string, object>(x.ToString(), x)).ToList();
.Select(x => (name: x.ToString(), value: (object)x)).ToList();

Invoking(() => DataConvert.ConvertCdcColumn(columnFields, captureInstance)).Should().Throw<Exception>();
}
Expand Down

0 comments on commit 616bfa8

Please sign in to comment.