Skip to content

Commit

Permalink
Better exception on COPY API confusion
Browse files Browse the repository at this point in the history
If a user calls one of the COPY APIs but supplies a server-side file
instead of STDIN, a proper exception is thrown.

Fixes #1673

(cherry picked from commit 2a5029e)
  • Loading branch information
roji committed Oct 8, 2017
1 parent 446d783 commit 37eb753
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/Npgsql/NpgsqlBinaryExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,24 @@ internal NpgsqlBinaryExporter(NpgsqlConnector connector, string copyToCommand)
{
_connector.SendQuery(copyToCommand);

// TODO: Failure will break the connection (e.g. if we get CopyOutResponse), handle more gracefully
var copyOutResponse = _connector.ReadExpecting<CopyOutResponseMessage>();
if (!copyOutResponse.IsBinary) {
throw new ArgumentException("copyToCommand triggered a text transfer, only binary is allowed", nameof(copyToCommand));
CopyOutResponseMessage copyOutResponse;
var msg = _connector.ReadMessage();
switch (msg.Code)
{
case BackendMessageCode.CopyOutResponse:
copyOutResponse = (CopyOutResponseMessage)msg;
if (!copyOutResponse.IsBinary)
throw new ArgumentException("copyToCommand triggered a text transfer, only binary is allowed", nameof(copyToCommand));
break;
case BackendMessageCode.CompletedResponse:
throw new InvalidOperationException(
"This API only supports import/export from the client, i.e. COPY commands containing TO/FROM STDIN. " +
"To import/export with files on your PostgreSQL machine, simply execute the command with ExecuteNonQuery. " +
"Note that your data has been successfully imported/exported.");
default:
throw _connector.UnexpectedMessageReceived(msg.Code);
}

NumColumns = copyOutResponse.NumColumns;
ReadHeader();
}
Expand Down
22 changes: 18 additions & 4 deletions src/Npgsql/NpgsqlBinaryImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,24 @@ internal NpgsqlBinaryImporter(NpgsqlConnector connector, string copyFromCommand)
{
_connector.SendQuery(copyFromCommand);

// TODO: Failure will break the connection (e.g. if we get CopyOutResponse), handle more gracefully
var copyInResponse = _connector.ReadExpecting<CopyInResponseMessage>();
if (!copyInResponse.IsBinary)
throw new ArgumentException("copyFromCommand triggered a text transfer, only binary is allowed", nameof(copyFromCommand));
CopyInResponseMessage copyInResponse;
var msg = _connector.ReadMessage();
switch (msg.Code)
{
case BackendMessageCode.CopyInResponse:
copyInResponse = (CopyInResponseMessage)msg;
if (!copyInResponse.IsBinary)
throw new ArgumentException("copyFromCommand triggered a text transfer, only binary is allowed", nameof(copyFromCommand));
break;
case BackendMessageCode.CompletedResponse:
throw new InvalidOperationException(
"This API only supports import/export from the client, i.e. COPY commands containing TO/FROM STDIN. " +
"To import/export with files on your PostgreSQL machine, simply execute the command with ExecuteNonQuery. " +
"Note that your data has been successfully imported/exported.");
default:
throw _connector.UnexpectedMessageReceived(msg.Code);
}

NumColumns = copyInResponse.NumColumns;
_buf.StartCopyMode();
WriteHeader();
Expand Down
5 changes: 5 additions & 0 deletions src/Npgsql/NpgsqlRawCopyStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ internal NpgsqlRawCopyStream(NpgsqlConnector connector, string copyCommand)
IsBinary = copyOutResponse.IsBinary;
_canRead = true;
break;
case BackendMessageCode.CompletedResponse:
throw new InvalidOperationException(
"This API only supports import/export from the client, i.e. COPY commands containing TO/FROM STDIN. " +
"To import/export with files on your PostgreSQL machine, simply execute the command with ExecuteNonQuery. " +
"Note that your data has been successfully imported/exported.");
default:
throw _connector.UnexpectedMessageReceived(msg.Code);
}
Expand Down

0 comments on commit 37eb753

Please sign in to comment.