Skip to content

Commit f2daff4

Browse files
committed
Give the possibility to serialize rowversion columns to number properties in the JSON reader.
1 parent 1298fde commit f2daff4

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

src/ProgressOnderwijsUtils/Data/ParameterizedSqlObjectMapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public static void ExecuteNonQuery(this ParameterizedSql sql, SqlConnection sqlC
7070
where T : IWrittenImplicitly
7171
=> q.OfPocos<T>().Execute(sqlConn);
7272

73-
public static void ReadJson(this ParameterizedSql q, SqlConnection sqlConn, IBufferWriter<byte> buffer, JsonWriterOptions options, JsonIgnoreCondition defaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)
74-
=> q.OfJson().Execute(sqlConn, buffer, options, defaultIgnoreCondition);
73+
public static void ReadJson(this ParameterizedSql q, SqlConnection sqlConn, IBufferWriter<byte> buffer, JsonWriterOptions options, JsonIgnoreCondition defaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, bool rowVersionAsNumber = false)
74+
=> q.OfJson().Execute(sqlConn, buffer, options, defaultIgnoreCondition, rowVersionAsNumber);
7575

7676
/// <summary>
7777
/// Reads all records of the given query from the database, unpacking into a C# array of tuples in field order

src/ProgressOnderwijsUtils/Data/SqlBatch.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public readonly record struct JsonSqlCommand(ParameterizedSql Sql, CommandTimeou
212212
public JsonSqlCommand WithTimeout(CommandTimeout timeout)
213213
=> this with { CommandTimeout = timeout, };
214214

215-
public void Execute(SqlConnection conn, IBufferWriter<byte> buffer, JsonWriterOptions options, JsonIgnoreCondition defaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)
215+
public void Execute(SqlConnection conn, IBufferWriter<byte> buffer, JsonWriterOptions options, JsonIgnoreCondition defaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, bool rowVersionAsNumber = false)
216216
{
217217
if (defaultIgnoreCondition is JsonIgnoreCondition.Always) {
218218
throw new ArgumentException("The value cannot be 'Always'", nameof(defaultIgnoreCondition));
@@ -265,7 +265,15 @@ public void Execute(SqlConnection conn, IBufferWriter<byte> buffer, JsonWriterOp
265265
} else if (type == typeof(string)) {
266266
writer.WriteString(name, reader.GetString(i));
267267
} else if (type == typeof(byte[])) {
268-
writer.WriteBase64String(name, reader.GetFieldValue<byte[]>(i));
268+
var bytes = reader.GetFieldValue<byte[]>(i);
269+
if (sqlType is "rowversion" or "timestamp" && rowVersionAsNumber) {
270+
if (BitConverter.IsLittleEndian) {
271+
Array.Reverse(bytes);
272+
}
273+
writer.WriteNumber(name, BitConverter.ToUInt64(bytes, 0));
274+
} else {
275+
writer.WriteBase64String(name, bytes);
276+
}
269277
} else if (type == typeof(Guid)) {
270278
writer.WriteString(name, reader.GetGuid(i));
271279
} else {

src/ProgressOnderwijsUtils/ProgressOnderwijsUtils.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="..\NugetPackagesCommon.props" />
33
<PropertyGroup Label="Configuration">
4-
<Version>110.1.0</Version>
5-
<PackageReleaseNotes>Added standaard configuration parm for configuring the serialization of null-properties when reading JSON from SQL query.</PackageReleaseNotes>
4+
<Version>110.2.0</Version>
5+
<PackageReleaseNotes>Give the possibility to serialize rowversion columns to number properties in the JSON reader.</PackageReleaseNotes>
66
<Title>ProgressOnderwijsUtils</Title>
77
<Description>Collection of utilities developed by ProgressOnderwijs</Description>
88
<PackageTags>ProgressOnderwijs</PackageTags>

test/ProgressOnderwijsUtils.Tests/Data/ReadJsonTest.ReadJson_can_read_all_known_used_column_types_from_the_db.approved.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
"UniqueIdentifierColumn": "82dbee37-3af8-46f2-a403-ae0a1950bc6e"
1717
},
1818
{
19-
"ReadJsonTestId": 2
19+
"ReadJsonTestId": 2,
20+
"BitColumn": null,
21+
"IntColumn": null,
22+
"BigIntColumn": null,
23+
"DecimalColumn": null,
24+
"FloatColumn": null,
25+
"DateColumn": null,
26+
"DateTimeOffsetColumn": null,
27+
"CharColumn": null,
28+
"VarCharColumn": null,
29+
"NCharColumn": null,
30+
"NVarCharColumn": null,
31+
"BinaryColumn": null,
32+
"UniqueIdentifierColumn": null
2033
}
2134
]

test/ProgressOnderwijsUtils.Tests/Data/ReadJsonTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ ReadJsonTestId int not null
100100
).ExecuteNonQuery(Connection);
101101

102102
var pipe = new Pipe();
103-
SQL($"select t.* from #ReadJsonTest t order by t.ReadJsonTestId").ReadJson(Connection, pipe.Writer, new() { Indented = true, });
103+
SQL($"select t.* from #ReadJsonTest t order by t.ReadJsonTestId").ReadJson(Connection, pipe.Writer, new() { Indented = true, }, JsonIgnoreCondition.Never);
104104
pipe.Writer.Complete();
105105

106106
ApprovalTest.CreateHere().AssertUnchangedAndSave(Encoding.UTF8.GetString(pipe.Reader.ReadAsync().GetAwaiter().GetResult().Buffer));
@@ -156,6 +156,7 @@ sealed record ReadJsonPocoTest : IWrittenImplicitly
156156
public string? StringColumn { get; init; }
157157
public DateTime? DateTimeColumn { get; init; }
158158
public byte[]? BinaryColumn { get; init; }
159+
public ulong RijRevisie { get; init; }
159160
}
160161

161162
[Fact]
@@ -173,6 +174,7 @@ ReadJsonPocoTestId int not null
173174
, StringColumn nvarchar(32)
174175
, DateTimeColumn datetime2
175176
, BinaryColumn varbinary(32)
177+
, RijRevisie rowversion
176178
);
177179
"""
178180
).ExecuteNonQuery(Connection);
@@ -199,7 +201,7 @@ ReadJsonPocoTestId int not null
199201
var pocos = query.ReadPocos<ReadJsonPocoTest>(Connection);
200202

201203
var pipe = new Pipe();
202-
query.ReadJson(Connection, pipe.Writer, new() { Indented = true, });
204+
query.ReadJson(Connection, pipe.Writer, new() { Indented = true, }, JsonIgnoreCondition.WhenWritingNull, true);
203205
pipe.Writer.Complete();
204206
var json = Encoding.UTF8.GetString(pipe.Reader.ReadAsync().GetAwaiter().GetResult().Buffer);
205207
var jsonPocos = JsonSerializer.Deserialize<ReadJsonPocoTest[]>(json).AssertNotNull();

0 commit comments

Comments
 (0)