Пример #1
0
        internal NpgsqlRawCopyStream(NpgsqlConnector connector, string copyCommand)
        {
            _connector = connector;
            _readBuf   = connector.ReadBuffer;
            _writeBuf  = connector.WriteBuffer;
            _connector.SendQuery(copyCommand);
            var msg = _connector.ReadMessage();

            switch (msg.Code)
            {
            case BackendMessageCode.CopyInResponse:
                var copyInResponse = (CopyInResponseMessage)msg;
                IsBinary  = copyInResponse.IsBinary;
                _canWrite = true;
                _writeBuf.StartCopyMode();
                break;

            case BackendMessageCode.CopyOutResponse:
                var copyOutResponse = (CopyOutResponseMessage)msg;
                IsBinary = copyOutResponse.IsBinary;
                _canRead = true;
                break;

            default:
                throw _connector.UnexpectedMessageReceived(msg.Code);
            }
        }
Пример #2
0
        internal NpgsqlBinaryImporter(NpgsqlConnector connector, string copyFromCommand)
        {
            _connector   = connector;
            _buf         = connector.WriteBuffer;
            _registry    = connector.TypeHandlerRegistry;
            _lengthCache = new LengthCache();
            _column      = -1;
            _dummyParam  = new NpgsqlParameter();

            try
            {
                _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));
                }
                NumColumns = copyInResponse.NumColumns;
                _buf.StartCopyMode();
                WriteHeader();
            }
            catch
            {
                _connector.Break();
                throw;
            }
        }
Пример #3
0
        internal NpgsqlRawCopyStream(NpgsqlConnector connector, string copyCommand)
        {
            _connector = connector;
            _readBuf   = connector.ReadBuffer;
            _writeBuf  = connector.WriteBuffer;
            _connector.SendQuery(copyCommand);
            var msg = _connector.ReadMessage();

            switch (msg.Code)
            {
            case BackendMessageCode.CopyInResponse:
                var copyInResponse = (CopyInResponseMessage)msg;
                IsBinary  = copyInResponse.IsBinary;
                _canWrite = true;
                _writeBuf.StartCopyMode();
                break;

            case BackendMessageCode.CopyOutResponse:
                var copyOutResponse = (CopyOutResponseMessage)msg;
                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);
            }
        }
Пример #4
0
        internal NpgsqlBinaryImporter(NpgsqlConnector connector, string copyFromCommand)
        {
            _connector   = connector;
            _buf         = connector.WriteBuffer;
            _registry    = connector.TypeHandlerRegistry;
            _lengthCache = new LengthCache();
            _column      = -1;
            _dummyParam  = new NpgsqlParameter();

            try
            {
                _connector.SendQuery(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();
            }
            catch
            {
                _connector.Break();
                throw;
            }
        }