Beispiel #1
0
        /// <inheritdoc />
        protected override async Task WriteWithLength <TAny>(TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache?lengthCache, NpgsqlParameter?parameter, bool async, CancellationToken cancellationToken = default)
        {
            buf.WriteInt32(ValidateAndGetLength(value, ref lengthCache, parameter));

            if (_isJsonb)
            {
                if (buf.WriteSpaceLeft < 1)
                {
                    await buf.Flush(async, cancellationToken);
                }
                buf.WriteByte(JsonbProtocolVersion);
            }

            if (typeof(TAny) == typeof(string))
            {
                await _textHandler.Write((string)(object)value !, buf, lengthCache, parameter, async, cancellationToken);
            }
            else if (typeof(TAny) == typeof(char[]))
            {
                await _textHandler.Write((char[])(object)value !, buf, lengthCache, parameter, async, cancellationToken);
            }
            else if (typeof(TAny) == typeof(ArraySegment <char>))
            {
                await _textHandler.Write((ArraySegment <char>)(object) value !, buf, lengthCache, parameter, async, cancellationToken);
            }
            else if (typeof(TAny) == typeof(char))
            {
                await _textHandler.Write((char)(object)value !, buf, lengthCache, parameter, async, cancellationToken);
            }
            else if (typeof(TAny) == typeof(byte[]))
            {
                await _textHandler.Write((byte[])(object)value !, buf, lengthCache, parameter, async, cancellationToken);
            }
            else if (typeof(TAny) == typeof(JsonDocument))
            {
                var data = parameter?.ConvertedValue != null
                    ? (byte[])parameter.ConvertedValue
                    : SerializeJsonDocument((JsonDocument)(object)value !);
                await buf.WriteBytesRaw(data, async, cancellationToken);
            }
            else
            {
                // User POCO, read serialized representation from the validation phase
                var s = parameter?.ConvertedValue != null
                    ? (string)parameter.ConvertedValue
                    : JsonSerializer.Serialize(value !, value !.GetType(), _serializerOptions);

                await _textHandler.Write(s, buf, lengthCache, parameter, async, cancellationToken);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Writes the bytes to the buffer and flushes <b>only</b> when the buffer is full
        /// </summary>
        internal async Task WriteDataRowWithFlush(params byte[][] columnValues)
        {
            CheckDisposed();

            _writeBuffer.WriteByte((byte)BackendMessageCode.DataRow);
            _writeBuffer.WriteInt32(4 + 2 + columnValues.Sum(v => 4 + v.Length));
            _writeBuffer.WriteInt16(columnValues.Length);

            foreach (var field in columnValues)
            {
                _writeBuffer.WriteInt32(field.Length);
                await _writeBuffer.WriteBytesRaw(field, true);
            }
        }