Example #1
0
    async ValueTask <ulong> Complete(bool async, CancellationToken cancellationToken = default)
    {
        CheckReady();

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

        if (InMiddleOfRow)
        {
            await Cancel(async, cancellationToken);

            throw new InvalidOperationException("Binary importer closed in the middle of a row, cancelling import.");
        }

        try
        {
            await WriteTrailer(async, cancellationToken);

            await _buf.Flush(async, cancellationToken);

            _buf.EndCopyMode();
            await _connector.WriteCopyDone(async, cancellationToken);

            await _connector.Flush(async, cancellationToken);

            var cmdComplete = Expect <CommandCompleteMessage>(await _connector.ReadMessage(async), _connector);
            Expect <ReadyForQueryMessage>(await _connector.ReadMessage(async), _connector);
            _state = ImporterState.Committed;
            return(cmdComplete.Rows);
        }
        catch
        {
            Cleanup();
            throw;
        }
    }