Skip to content

Commit f675f79

Browse files
authored
Merge pull request #1194 from progressonderwijs/fons/json_rvs
Give the possibility to serialize rowversion columns to number properties in the JSON reader.
2 parents 1298fde + 5be59e6 commit f675f79

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Buffers;
2+
using System.Buffers.Binary;
23
using System.Data.Common;
34
using System.Text.Json;
45
using System.Text.Json.Serialization;
@@ -212,7 +213,7 @@ public readonly record struct JsonSqlCommand(ParameterizedSql Sql, CommandTimeou
212213
public JsonSqlCommand WithTimeout(CommandTimeout timeout)
213214
=> this with { CommandTimeout = timeout, };
214215

215-
public void Execute(SqlConnection conn, IBufferWriter<byte> buffer, JsonWriterOptions options, JsonIgnoreCondition defaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)
216+
public void Execute(SqlConnection conn, IBufferWriter<byte> buffer, JsonWriterOptions options, JsonIgnoreCondition defaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, bool rowVersionAsNumber = false)
216217
{
217218
if (defaultIgnoreCondition is JsonIgnoreCondition.Always) {
218219
throw new ArgumentException("The value cannot be 'Always'", nameof(defaultIgnoreCondition));
@@ -265,7 +266,12 @@ public void Execute(SqlConnection conn, IBufferWriter<byte> buffer, JsonWriterOp
265266
} else if (type == typeof(string)) {
266267
writer.WriteString(name, reader.GetString(i));
267268
} else if (type == typeof(byte[])) {
268-
writer.WriteBase64String(name, reader.GetFieldValue<byte[]>(i));
269+
var bytes = reader.GetFieldValue<byte[]>(i);
270+
if (sqlType is "rowversion" or "timestamp" && rowVersionAsNumber) {
271+
writer.WriteNumber(name, BinaryPrimitives.ReadUInt64BigEndian(bytes));
272+
} else {
273+
writer.WriteBase64String(name, bytes);
274+
}
269275
} else if (type == typeof(Guid)) {
270276
writer.WriteString(name, reader.GetGuid(i));
271277
} 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)