Пример #1
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;
                WriteHeader();

                // We will be sending CopyData messages from now on, deduct the header from the buffer's usable size
                _buf.UsableSize -= 5;
            }
            catch
            {
                _connector.Break();
                throw;
            }
        }
Пример #2
0
        internal NpgsqlBinaryImporter(NpgsqlConnector connector, string copyFromCommand)
        {
            _connector   = connector;
            _buf         = connector.Buffer;
            _registry    = connector.TypeHandlerRegistry;
            _lengthCache = new LengthCache();
            _column      = -1;
            _dummyParam  = new NpgsqlParameter();

            try
            {
                _connector.SendSingleMessage(new QueryMessage(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", "copyFromCommand");
                }
                NumColumns = copyInResponse.NumColumns;
                WriteHeader();
            }
            catch
            {
                _connector.Break();
                throw;
            }
        }
Пример #3
0
 internal LengthCache GetOrCreateLengthCache(int capacity=0)
 {
     if (LengthCache == null) {
         LengthCache = capacity == 0 ? new LengthCache() : new LengthCache(capacity);
     }
     return LengthCache;
 }
Пример #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;
            }
        }
Пример #5
0
 internal abstract Task WriteWithLength([CanBeNull] object value, WriteBuffer buf, LengthCache lengthCache,
                                        NpgsqlParameter parameter,
                                        bool async, CancellationToken cancellationToken);
Пример #6
0
 public abstract int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null);
Пример #7
0
 protected abstract Task Write(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter,
                               bool async, CancellationToken cancellationToken);
Пример #8
0
        internal sealed override async Task WriteWithLength(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter,
                                                            bool async, CancellationToken cancellationToken)
        {
            if (buf.WriteSpaceLeft < 4)
            {
                await buf.Flush(async, cancellationToken);
            }

            if (value == null || value is DBNull)
            {
                buf.WriteInt32(-1);
                return;
            }

            buf.WriteInt32(ValidateAndGetLength(value, ref lengthCache, parameter));
            await Write(value, buf, lengthCache, parameter, async, cancellationToken);
        }
Пример #9
0
 public sealed override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null)
 => ValidateAndGetLength(value, parameter);
Пример #10
0
 public override void PrepareWrite(object value, WriteBuffer buf, LengthCache lengthCache, NpgsqlParameter parameter = null)
 {
     Contract.Requires(buf != null);
     Contract.Requires(value != null);
 }
Пример #11
0
 public override int ValidateAndGetLength(object value, ref LengthCache lengthCache, NpgsqlParameter parameter = null)
 {
     Contract.Requires(value != null);
     return(default(int));
 }
Пример #12
0
 /// <param name="value">the value to be written</param>
 /// <param name="buf"></param>
 /// <param name="lengthCache">a cache in which to store length(s) of values to be written</param>
 /// <param name="parameter">
 /// the <see cref="NpgsqlParameter"/> containing <paramref name="value"/>. Consulted for settings
 /// which impact how to send the parameter, e.g. <see cref="NpgsqlParameter.Size"/>. Can be null.
 /// <see cref="NpgsqlParameter.Size"/>.
 /// </param>
 public abstract void PrepareWrite(object value, WriteBuffer buf, LengthCache lengthCache, [CanBeNull] NpgsqlParameter parameter);
Пример #13
0
 /// <param name="value">the value to be examined</param>
 /// <param name="lengthCache">a cache in which to store length(s) of values to be written</param>
 /// <param name="parameter">
 /// the <see cref="NpgsqlParameter"/> containing <paramref name="value"/>. Consulted for settings
 /// which impact how to send the parameter, e.g. <see cref="NpgsqlParameter.Size"/>. Can be null.
 /// </param>
 public abstract int ValidateAndGetLength(object value, ref LengthCache lengthCache, [CanBeNull] NpgsqlParameter parameter);