Пример #1
0
    internal async Task Init(string copyFromCommand, bool async, CancellationToken cancellationToken = default)
    {
        await _connector.WriteQuery(copyFromCommand, async, cancellationToken);

        await _connector.Flush(async, cancellationToken);

        using var registration = _connector.StartNestedCancellableOperation(cancellationToken, attemptPgCancellation: false);

        CopyInResponseMessage copyInResponse;
        var msg = await _connector.ReadMessage(async);

        switch (msg.Code)
        {
        case BackendMessageCode.CopyInResponse:
            copyInResponse = (CopyInResponseMessage)msg;
            if (!copyInResponse.IsBinary)
            {
                throw _connector.Break(
                          new ArgumentException("copyFromCommand triggered a text transfer, only binary is allowed",
                                                nameof(copyFromCommand)));
            }
            break;

        case BackendMessageCode.CommandComplete:
            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;
        _params       = new NpgsqlParameter[NumColumns];
        _rowsImported = 0;
        _buf.StartCopyMode();
        WriteHeader();
    }