Skip to content

Commit ee89f6e

Browse files
committed
Make an exception for COPY TO queries with NpgsqlDataReader more clear
1 parent bfbd903 commit ee89f6e

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/Npgsql/NpgsqlDataReader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ async Task<bool> NextResult(bool async, bool isConsuming = false, CancellationTo
491491
throw Connector.Break(new NotSupportedException(
492492
"COPY isn't supported in regular command execution - see https://www.npgsql.org/doc/copy.html for documentation on COPY with Npgsql. " +
493493
"If you are trying to execute a SQL script created by pg_dump, pass the '--inserts' switch to disable generating COPY statements."));
494+
case BackendMessageCode.CopyOutResponse:
495+
throw Connector.Break(new NotSupportedException(
496+
"COPY isn't supported in regular command execution - see https://www.npgsql.org/doc/copy.html for documentation on COPY with Npgsql."));
494497
default:
495498
throw Connector.UnexpectedMessageReceived(msg.Code);
496499
}

test/Npgsql.Tests/CopyTests.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,17 +1085,29 @@ public async Task Within_transaction()
10851085
}
10861086

10871087
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4199")]
1088-
public async Task Copy_is_not_supported_in_regular_command_execution()
1088+
public async Task Copy_from_is_not_supported_in_regular_command_execution()
10891089
{
10901090
// Run in a separate pool to protect other queries in multiplexing
10911091
// because we're going to break the connection on CopyInResponse
10921092
await using var dataSource = CreateDataSource();
1093-
using var conn = await dataSource.OpenConnectionAsync();
1093+
await using var conn = await dataSource.OpenConnectionAsync();
10941094
var table = await CreateTempTable(conn, "foo INT");
10951095

10961096
Assert.That(() => conn.ExecuteNonQuery($@"COPY {table} (foo) FROM stdin"), Throws.Exception.TypeOf<NotSupportedException>());
10971097
}
10981098

1099+
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4974")]
1100+
public async Task Copy_to_is_not_supported_in_regular_command_execution()
1101+
{
1102+
// Run in a separate pool to protect other queries in multiplexing
1103+
// because we're going to break the connection on CopyInResponse
1104+
await using var dataSource = CreateDataSource();
1105+
await using var conn = await dataSource.OpenConnectionAsync();
1106+
var table = await CreateTempTable(conn, "foo INT");
1107+
1108+
Assert.That(() => conn.ExecuteNonQuery($@"COPY {table} (foo) TO stdin"), Throws.Exception.TypeOf<NotSupportedException>());
1109+
}
1110+
10991111
#endregion
11001112

11011113
#region Utils

0 commit comments

Comments
 (0)