Skip to content

Commit

Permalink
Fix NpgsqlBinaryImporter when value type is changed (#3655)
Browse files Browse the repository at this point in the history
Fixes #3649

(cherry picked from commit b585861)
  • Loading branch information
manandre authored and roji committed Apr 15, 2021
1 parent d1fd635 commit ea77317
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Npgsql/NpgsqlBinaryImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,11 @@ async Task Write<T>([AllowNull] T value, NpgsqlParameter param, bool async, Canc
}
else
{
if (!(param is NpgsqlParameter<T> typedParam))
if (param is not NpgsqlParameter<T> typedParam)
{
_params[_column] = typedParam = new NpgsqlParameter<T>();
typedParam.NpgsqlDbType = param.NpgsqlDbType;
param = typedParam;
}
typedParam.TypedValue = value;
}
Expand Down
29 changes: 29 additions & 0 deletions test/Npgsql.Tests/BugTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,5 +1351,34 @@ public async Task Bug3373()
await using var reader = await cmd.ExecuteReaderAsync();
Assert.DoesNotThrowAsync(async () => await reader.NextResultAsync());
}

[Test, IssueLink("https://github.com/npgsql/npgsql/issues/3649")]
public async Task Bug3649()
{
await using var conn = await OpenConnectionAsync();
await using var _ = await CreateTempTable(conn, "value integer", out var table);

using (var importer = conn.BeginBinaryImport($"COPY {table} (value) FROM STDIN (FORMAT binary)"))
{
await importer.StartRowAsync();
await importer.WriteAsync(DBNull.Value, NpgsqlDbType.Integer);
await importer.StartRowAsync();
await importer.WriteAsync(1, NpgsqlDbType.Integer);
await importer.StartRowAsync();
await importer.WriteAsync(2, NpgsqlDbType.Integer);
await importer.CompleteAsync();
}

using (var exporter = conn.BeginBinaryExport($"COPY {table} (value) TO STDIN (FORMAT binary)"))
{
await exporter.StartRowAsync();
Assert.IsTrue(exporter.IsNull);
await exporter.SkipAsync();
await exporter.StartRowAsync();
Assert.AreEqual(1, await exporter.ReadAsync<int?>());
await exporter.StartRowAsync();
Assert.AreEqual(2, await exporter.ReadAsync<int?>());
}
}
}
}

0 comments on commit ea77317

Please sign in to comment.