示例#1
0
        public override TEnum Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
        {
            var str     = buf.ReadString(len);
            var success = _labelToEnum.TryGetValue(str, out var value);

            if (!success)
            {
                throw new InvalidCastException($"Received enum value '{str}' from database which wasn't found on enum {typeof(TEnum)}");
            }

            return(value);
        }
示例#2
0
            static async ValueTask <string> ReadLong(NpgsqlReadBuffer buf, int byteLen, bool async)
            {
                if (byteLen <= buf.Size)
                {
                    // The string's byte representation can fit in our read buffer, read it.
                    while (buf.ReadBytesLeft < byteLen)
                    {
                        await buf.ReadMore(async);
                    }
                    return(buf.ReadString(byteLen));
                }

                // Bad case: the string's byte representation doesn't fit in our buffer.
                // This is rare - will only happen in CommandBehavior.Sequential mode (otherwise the
                // entire row is in memory). Tweaking the buffer length via the connection string can
                // help avoid this.

                // Allocate a temporary byte buffer to hold the entire string and read it in chunks.
                var tempBuf = new byte[byteLen];
                var pos     = 0;

                while (true)
                {
                    var len = Math.Min(buf.ReadBytesLeft, byteLen - pos);
                    buf.ReadBytes(tempBuf, pos, len);
                    pos += len;
                    if (pos < byteLen)
                    {
                        await buf.ReadMore(async);

                        continue;
                    }
                    break;
                }
                return(buf.TextEncoding.GetString(tempBuf));
            }
示例#3
0
 /// <inheritdoc />
 public override ValueTask <string> Read(NpgsqlReadBuffer buf, int byteLen, bool async, FieldDescription?fieldDescription = null)
 {
     return(buf.ReadBytesLeft >= byteLen
         ? new ValueTask <string>(buf.ReadString(byteLen))
         : ReadLong(buf, byteLen, async));