Exemplo n.º 1
0
        /// <inheritdoc />
        public override async ValueTask <BitArray> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            await buf.Ensure(4, async);

            var numBits   = buf.ReadInt32();
            var result    = new BitArray(numBits);
            var bytesLeft = len - 4;  // Remove leading number of bits

            if (bytesLeft == 0)
            {
                return(result);
            }

            var bitNo = 0;

            while (true)
            {
                var iterationEndPos = bytesLeft > buf.ReadBytesLeft
                    ? bytesLeft - buf.ReadBytesLeft
                    : 1;

                for (; bytesLeft > iterationEndPos; bytesLeft--)
                {
                    // ReSharper disable ShiftExpressionRealShiftCountIsZero
                    var chunk = buf.ReadByte();
                    result[bitNo++] = (chunk & (1 << 7)) != 0;
                    result[bitNo++] = (chunk & (1 << 6)) != 0;
                    result[bitNo++] = (chunk & (1 << 5)) != 0;
                    result[bitNo++] = (chunk & (1 << 4)) != 0;
                    result[bitNo++] = (chunk & (1 << 3)) != 0;
                    result[bitNo++] = (chunk & (1 << 2)) != 0;
                    result[bitNo++] = (chunk & (1 << 1)) != 0;
                    result[bitNo++] = (chunk & (1 << 0)) != 0;
                }

                if (bytesLeft == 1)
                {
                    break;
                }

                Debug.Assert(buf.ReadBytesLeft == 0);
                await buf.Ensure(Math.Min(bytesLeft, buf.Size), async);
            }

            if (bitNo < result.Length)
            {
                var remainder = result.Length - bitNo;
                await buf.Ensure(1, async);

                var lastChunk = buf.ReadByte();
                for (var i = 7; i >= 8 - remainder; i--)
                {
                    result[bitNo++] = (lastChunk & (1 << i)) != 0;
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        internal async Task ExpectMessage(byte expectedCode)
        {
            CheckDisposed();

            await _readBuffer.EnsureAsync(5);

            var actualCode = _readBuffer.ReadByte();

            Assert.That(actualCode, Is.EqualTo(expectedCode),
                        $"Expected message of type '{(char)expectedCode}' but got '{(char)actualCode}'");
            var len = _readBuffer.ReadInt32();

            _readBuffer.Skip(len - 4);
        }
Exemplo n.º 3
0
        public override async ValueTask <NpgsqlPath> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)
        {
            await buf.Ensure(5, async);

            bool open;
            var  openByte = buf.ReadByte();

            switch (openByte)
            {
            case 1:
                open = false;
                break;

            case 0:
                open = true;
                break;

            default:
                throw new Exception("Error decoding binary geometric path: bad open byte");
            }

            var numPoints = buf.ReadInt32();
            var result    = new NpgsqlPath(numPoints, open);

            for (var i = 0; i < numPoints; i++)
            {
                await buf.Ensure(16, async);

                result.Add(new NpgsqlPoint(buf.ReadDouble(), buf.ReadDouble()));
            }
            return(result);
        }
Exemplo n.º 4
0
        private protected async ValueTask <NpgsqlRange <TAny> > DoRead <TAny>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription)
        {
            await buf.Ensure(1, async);

            var flags = (RangeFlags)buf.ReadByte();

            if ((flags & RangeFlags.Empty) != 0)
            {
                return(NpgsqlRange <TAny> .Empty);
            }

            var lowerBound = default(TAny);
            var upperBound = default(TAny);

            if ((flags & RangeFlags.LowerBoundInfinite) == 0)
            {
                lowerBound = await _subtypeHandler.ReadWithLength <TAny>(buf, async);
            }

            if ((flags & RangeFlags.UpperBoundInfinite) == 0)
            {
                upperBound = await _subtypeHandler.ReadWithLength <TAny>(buf, async);
            }

            return(new NpgsqlRange <TAny>(lowerBound, upperBound, flags));
        }
Exemplo n.º 5
0
#pragma warning disable CA1801 // Review unused parameters
        internal static NpgsqlInet DoRead(NpgsqlReadBuffer buf, [CanBeNull] FieldDescription fieldDescription, int len, bool isCidrHandler)
        {
            buf.ReadByte();  // addressFamily
            var mask   = buf.ReadByte();
            var isCidr = buf.ReadByte() == 1;

            Debug.Assert(isCidrHandler == isCidr);
            var numBytes = buf.ReadByte();
            var bytes    = new byte[numBytes];

            for (var i = 0; i < numBytes; i++)
            {
                bytes[i] = buf.ReadByte();
            }
            return(new NpgsqlInet(new IPAddress(bytes), mask));
        }
Exemplo n.º 6
0
        internal void Load(NpgsqlReadBuffer buf)
        {
            ColumnFormatCodes.Clear();

            var binaryIndicator = buf.ReadByte();

            switch (binaryIndicator)
            {
            case 0:
                IsBinary = false;
                break;

            case 1:
                IsBinary = true;
                break;

            default:
                throw new Exception("Invalid binary indicator in CopyInResponse message: " + binaryIndicator);
            }

            NumColumns = buf.ReadInt16();
            for (var i = 0; i < NumColumns; i++)
            {
                ColumnFormatCodes.Add((FormatCode)buf.ReadInt16());
            }
        }
Exemplo n.º 7
0
        public ValueTask <bool> MoveNextAsync()
        {
            if (_tupleEnumerable.State != RowState.Reading)
            {
                throw new ObjectDisposedException(null);
            }

            using (NoSynchronizationContextScope.Enter())
                return(MoveNextCore());

            async ValueTask <bool> MoveNextCore()
            {
                // Consume the previous column
                if (_pos != -1)
                {
                    await _value.Consume(_cancellationToken);
                }

                if (_pos + 1 == _numColumns)
                {
                    return(false);
                }
                _pos++;

                // Read the next column
                await _readBuffer.Ensure(1, async : true);

                var kind = (TupleDataKind)_readBuffer.ReadByte();
                int len;

                switch (kind)
                {
                case TupleDataKind.Null:
                case TupleDataKind.UnchangedToastedValue:
                    len = 0;
                    break;

                case TupleDataKind.TextValue:
                case TupleDataKind.BinaryValue:
                    if (_readBuffer.ReadBytesLeft < 4)
                    {
                        using var tokenRegistration = _readBuffer.Connector.StartNestedCancellableOperation(_cancellationToken);
                        await _readBuffer.Ensure(4, async : true);
                    }
                    len = _readBuffer.ReadInt32();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                _value.Reset(kind, len, _rowDescription[_pos]);

                return(true);
            }
        }
Exemplo n.º 8
0
 internal AuthenticationSASLMessage(NpgsqlReadBuffer buf)
 {
     while (buf.Buffer[buf.ReadPosition] != 0)
     {
         Mechanisms.Add(buf.ReadNullTerminatedString());
     }
     buf.ReadByte();
     if (Mechanisms.Count == 0)
     {
         throw new NpgsqlException("Received AuthenticationSASL message with 0 mechanisms!");
     }
 }
Exemplo n.º 9
0
        public override async ValueTask <BitArray> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)
        {
            await buf.Ensure(4, async);

            var numBits   = buf.ReadInt32();
            var result    = new BitArray(numBits);
            var bytesLeft = len - 4;  // Remove leading number of bits
            var bitNo     = 0;

            do
            {
                var iterationEndPos = bytesLeft - Math.Min(bytesLeft, buf.ReadBytesLeft) + 1;
                for (; bytesLeft > iterationEndPos; bytesLeft--)
                {
                    var chunk = buf.ReadByte();
                    result[bitNo++] = (chunk & (1 << 7)) != 0;
                    result[bitNo++] = (chunk & (1 << 6)) != 0;
                    result[bitNo++] = (chunk & (1 << 5)) != 0;
                    result[bitNo++] = (chunk & (1 << 4)) != 0;
                    result[bitNo++] = (chunk & (1 << 3)) != 0;
                    result[bitNo++] = (chunk & (1 << 2)) != 0;
                    result[bitNo++] = (chunk & (1 << 1)) != 0;
                    result[bitNo++] = (chunk & (1 << 0)) != 0;
                }
            } while (bytesLeft > 1);

            if (bitNo < result.Length)
            {
                var remainder = result.Length - bitNo;
                await buf.Ensure(1, async);

                var lastChunk = buf.ReadByte();
                for (var i = 7; i >= 8 - remainder; i--)
                {
                    result[bitNo++] = (lastChunk & (1 << i)) != 0;
                }
            }

            return(result);
        }
Exemplo n.º 10
0
    /// <inheritdoc />
    public override async ValueTask <string> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
    {
        await buf.Ensure(1, async);

        var version = buf.ReadByte();

        if (version != JsonPathVersion)
        {
            throw new NotSupportedException($"Don't know how to decode JSONPATH with wire format {version}, your connection is now broken");
        }

        return(await _textHandler.Read(buf, len - 1, async, fieldDescription));
    }
Exemplo n.º 11
0
        public override async ValueTask <string> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            await buf.Ensure(1, async);

            var version = buf.ReadByte();

            if (version != LTxtQueryProtocolVersion)
            {
                throw new NotSupportedException($"Don't know how to decode ltxtquery with wire format {version}, your connection is now broken");
            }

            return(await base.Read(buf, len - 1, async, fieldDescription));
        }
Exemplo n.º 12
0
        async ValueTask <bool> INpgsqlTypeHandler <bool> .Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription)
        {
            await buf.Ensure(5, async);

            var bitLen = buf.ReadInt32();

            if (bitLen != 1)
            {
                throw new InvalidCastException("Can't convert a BIT(N) type to bool, only BIT(1)");
            }
            var b = buf.ReadByte();

            return((b & 128) != 0);
        }
Exemplo n.º 13
0
        async ValueTask <bool> INpgsqlTypeHandler <bool> .Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription)
        {
            await buf.Ensure(5, async);

            var bitLen = buf.ReadInt32();

            if (bitLen != 1)
            {
                // This isn't a single bit - error.
                // Consume the rest of the value first so the connection is left in a good state.
                buf.Skip(len - 4);
                throw new NpgsqlSafeReadException(new InvalidCastException("Can't convert a BIT(N) type to bool, only BIT(1)"));
            }
            var b = buf.ReadByte();

            return((b & 128) != 0);
        }
Exemplo n.º 14
0
    internal void Load(NpgsqlReadBuffer buf)
    {
        ColumnFormatCodes.Clear();

        var binaryIndicator = buf.ReadByte();

        IsBinary = binaryIndicator switch
        {
            0 => false,
            1 => true,
            _ => throw new Exception("Invalid binary indicator in CopyInResponse message: " + binaryIndicator)
        };

        NumColumns = buf.ReadInt16();
        for (var i = 0; i < NumColumns; i++)
        {
            ColumnFormatCodes.Add((FormatCode)buf.ReadInt16());
        }
    }
}
Exemplo n.º 15
0
        public override async Task <PostgisGeometry> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)
        {
            await buf.Ensure(5, async);

            var bo = (ByteOrder)buf.ReadByte();
            var id = buf.ReadUInt32(bo);

            var srid = 0u;

            if ((id & (uint)EwkbModifiers.HasSRID) != 0)
            {
                await buf.Ensure(4, async);

                srid = buf.ReadUInt32(bo);
            }

            var geom = await DoRead(buf, (WkbIdentifier)(id & 7), bo, async);

            geom.SRID = srid;
            return(geom);
        }
Exemplo n.º 16
0
    async Task ReadHeader(bool async)
    {
        _leftToReadInDataMsg = Expect <CopyDataMessage>(await _connector.ReadMessage(async), _connector).Length;
        var headerLen = NpgsqlRawCopyStream.BinarySignature.Length + 4 + 4;
        await _buf.Ensure(headerLen, async);

        if (NpgsqlRawCopyStream.BinarySignature.Any(t => _buf.ReadByte() != t))
        {
            throw new NpgsqlException("Invalid COPY binary signature at beginning!");
        }

        var flags = _buf.ReadInt32();

        if (flags != 0)
        {
            throw new NotSupportedException("Unsupported flags in COPY operation (OID inclusion?)");
        }

        _buf.ReadInt32();   // Header extensions, currently unused
        _leftToReadInDataMsg -= headerLen;
    }
Exemplo n.º 17
0
        public override async ValueTask <PostgisGeometry> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null, CancellationToken cancellationToken = default)
        {
            await buf.Ensure(5, async, cancellationToken);

            var le = buf.ReadByte() != 0;
            var id = buf.ReadUInt32(le);

            var srid = 0u;

            if ((id & (uint)EwkbModifiers.HasSRID) != 0)
            {
                await buf.Ensure(4, async, cancellationToken);

                srid = buf.ReadUInt32(le);
            }

            var geom = await DoRead(buf, (WkbIdentifier)(id & 7), le, async, cancellationToken);

            geom.SRID = srid;
            return(geom);
        }
Exemplo n.º 18
0
        /// <inheritdoc />
        public override async ValueTask <NpgsqlPath> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            await buf.Ensure(5, async);

            var open = buf.ReadByte() switch
            {
                1 => false,
                0 => true,
                _ => throw new Exception("Error decoding binary geometric path: bad open byte")
            };

            var numPoints = buf.ReadInt32();
            var result    = new NpgsqlPath(numPoints, open);

            for (var i = 0; i < numPoints; i++)
            {
                await buf.Ensure(16, async);

                result.Add(new NpgsqlPoint(buf.ReadDouble(), buf.ReadDouble()));
            }
            return(result);
        }
Exemplo n.º 19
0
        public override async Task <NpgsqlRange <TElement> > Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)
        {
            await buf.Ensure(1, async);

            var flags = (RangeFlags)buf.ReadByte();

            if ((flags & RangeFlags.Empty) != 0)
            {
                return(NpgsqlRange <TElement> .Empty);
            }

            TElement lowerBound = default(TElement), upperBound = default(TElement);

            if ((flags & RangeFlags.LowerBoundInfinite) == 0)
            {
                lowerBound = await ElementHandler.ReadWithLength <TElement>(buf, async);
            }
            if ((flags & RangeFlags.UpperBoundInfinite) == 0)
            {
                upperBound = await ElementHandler.ReadWithLength <TElement>(buf, async);
            }
            return(new NpgsqlRange <TElement>(lowerBound, upperBound, flags));
        }
Exemplo n.º 20
0
 internal ReadyForQueryMessage Load(NpgsqlReadBuffer buf)
 {
     TransactionStatusIndicator = (TransactionStatus)buf.ReadByte();
     return(this);
 }
Exemplo n.º 21
0
 /// <inheritdoc />
 public override bool Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
 => buf.ReadByte() != 0;
Exemplo n.º 22
0
        // ReSharper disable once FunctionComplexityOverflow
        internal ErrorOrNoticeMessage(NpgsqlReadBuffer buf)
        {
            while (true)
            {
                var code = (ErrorFieldTypeCode)buf.ReadByte();
                switch (code)
                {
                case ErrorFieldTypeCode.Done:
                    // Null terminator; error message fully consumed.
                    return;

                case ErrorFieldTypeCode.Severity:
                    Severity = buf.ReadNullTerminatedString(PGUtil.RelaxedUTF8Encoding);
                    break;

                case ErrorFieldTypeCode.Code:
                    Code = buf.ReadNullTerminatedString();
                    break;

                case ErrorFieldTypeCode.Message:
                    Message = buf.ReadNullTerminatedString(PGUtil.RelaxedUTF8Encoding);
                    break;

                case ErrorFieldTypeCode.Detail:
                    Detail = buf.ReadNullTerminatedString(PGUtil.RelaxedUTF8Encoding);
                    break;

                case ErrorFieldTypeCode.Hint:
                    Hint = buf.ReadNullTerminatedString(PGUtil.RelaxedUTF8Encoding);
                    break;

                case ErrorFieldTypeCode.Position:
                    var positionStr = buf.ReadNullTerminatedString();
                    if (!int.TryParse(positionStr, out var position))
                    {
                        Log.Warn("Non-numeric position in ErrorResponse: " + positionStr);
                        continue;
                    }
                    Position = position;
                    break;

                case ErrorFieldTypeCode.InternalPosition:
                    var internalPositionStr = buf.ReadNullTerminatedString();
                    if (!Int32.TryParse(internalPositionStr, out var internalPosition))
                    {
                        Log.Warn("Non-numeric position in ErrorResponse: " + internalPositionStr);
                        continue;
                    }
                    InternalPosition = internalPosition;
                    break;

                case ErrorFieldTypeCode.InternalQuery:
                    InternalQuery = buf.ReadNullTerminatedString();
                    break;

                case ErrorFieldTypeCode.Where:
                    Where = buf.ReadNullTerminatedString();
                    break;

                case ErrorFieldTypeCode.File:
                    File = buf.ReadNullTerminatedString(PGUtil.RelaxedUTF8Encoding);
                    break;

                case ErrorFieldTypeCode.Line:
                    Line = buf.ReadNullTerminatedString();
                    break;

                case ErrorFieldTypeCode.Routine:
                    Routine = buf.ReadNullTerminatedString();
                    break;

                case ErrorFieldTypeCode.SchemaName:
                    SchemaName = buf.ReadNullTerminatedString();
                    break;

                case ErrorFieldTypeCode.TableName:
                    TableName = buf.ReadNullTerminatedString();
                    break;

                case ErrorFieldTypeCode.ColumnName:
                    ColumnName = buf.ReadNullTerminatedString();
                    break;

                case ErrorFieldTypeCode.DataTypeName:
                    DataTypeName = buf.ReadNullTerminatedString();
                    break;

                case ErrorFieldTypeCode.ConstraintName:
                    ConstraintName = buf.ReadNullTerminatedString();
                    break;

                default:
                    // Unknown error field; consume and discard.
                    buf.ReadNullTerminatedString();
                    break;
                }
            }
        }
Exemplo n.º 23
0
    // ReSharper disable once FunctionComplexityOverflow
    internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDetail)
    {
        (string?severity, string?invariantSeverity, string?code, string?message, string?detail, string?hint) = (null, null, null, null, null, null);
        var(position, internalPosition)      = (0, 0);
        (string?internalQuery, string?where) = (null, null);
        (string?schemaName, string?tableName, string?columnName, string?dataTypeName, string?constraintName) =
            (null, null, null, null, null);
        (string?file, string?line, string?routine) = (null, null, null);

        while (true)
        {
            var fieldCode = (ErrorFieldTypeCode)buf.ReadByte();
            switch (fieldCode)
            {
            case ErrorFieldTypeCode.Done:
                // Null terminator; error message fully consumed.
                goto End;

            case ErrorFieldTypeCode.Severity:
                severity = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.InvariantSeverity:
                invariantSeverity = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.Code:
                code = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.Message:
                message = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.Detail:
                detail = buf.ReadNullTerminatedStringRelaxed();
                if (!includeDetail && !string.IsNullOrEmpty(detail))
                {
                    detail = $"Detail redacted as it may contain sensitive data. Specify '{NpgsqlConnectionStringBuilder.IncludeExceptionDetailDisplayName}' in the connection string to include this information.";
                }
                break;

            case ErrorFieldTypeCode.Hint:
                hint = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.Position:
                var positionStr = buf.ReadNullTerminatedStringRelaxed();
                if (!int.TryParse(positionStr, out var tmpPosition))
                {
                    Logger.LogWarning("Non-numeric position in ErrorResponse: " + positionStr);
                    continue;
                }
                position = tmpPosition;
                break;

            case ErrorFieldTypeCode.InternalPosition:
                var internalPositionStr = buf.ReadNullTerminatedStringRelaxed();
                if (!int.TryParse(internalPositionStr, out var internalPositionTmp))
                {
                    Logger.LogWarning("Non-numeric position in ErrorResponse: " + internalPositionStr);
                    continue;
                }
                internalPosition = internalPositionTmp;
                break;

            case ErrorFieldTypeCode.InternalQuery:
                internalQuery = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.Where:
                where = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.File:
                file = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.Line:
                line = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.Routine:
                routine = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.SchemaName:
                schemaName = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.TableName:
                tableName = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.ColumnName:
                columnName = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.DataTypeName:
                dataTypeName = buf.ReadNullTerminatedStringRelaxed();
                break;

            case ErrorFieldTypeCode.ConstraintName:
                constraintName = buf.ReadNullTerminatedStringRelaxed();
                break;

            default:
                // Unknown error field; consume and discard.
                buf.ReadNullTerminatedStringRelaxed();
                break;
            }
        }

End:
        if (severity == null)
        {
            throw new NpgsqlException("Severity not received in server error message");
        }
        if (code == null)
        {
            throw new NpgsqlException("Code not received in server error message");
        }
        if (message == null)
        {
            throw new NpgsqlException("Message not received in server error message");
        }

        return(new ErrorOrNoticeMessage(
                   severity, invariantSeverity ?? severity, code, message,
                   detail, hint, position, internalPosition, internalQuery, where,
                   schemaName, tableName, columnName, dataTypeName, constraintName,
                   file, line, routine));
    }
Exemplo n.º 24
0
        public override async ValueTask <NpgsqlTsQuery> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null, CancellationToken cancellationToken = default)
        {
            await buf.Ensure(4, async, cancellationToken);

            var numTokens = buf.ReadInt32();

            if (numTokens == 0)
            {
                return(new NpgsqlTsQueryEmpty());
            }

            NpgsqlTsQuery?value = null;
            var           nodes = new Stack <Tuple <NpgsqlTsQuery, int> >();

            len -= 4;

            for (var tokenPos = 0; tokenPos < numTokens; tokenPos++)
            {
                await buf.Ensure(Math.Min(len, MaxSingleTokenBytes), async, cancellationToken);

                var readPos = buf.ReadPosition;

                var isOper = buf.ReadByte() == 2;
                if (isOper)
                {
                    var operKind = (NpgsqlTsQuery.NodeKind)buf.ReadByte();
                    if (operKind == NpgsqlTsQuery.NodeKind.Not)
                    {
                        var node = new NpgsqlTsQueryNot(null);
                        InsertInTree(node, nodes, ref value);
                        nodes.Push(new Tuple <NpgsqlTsQuery, int>(node, 0));
                    }
                    else
                    {
                        var node = operKind switch
                        {
                            NpgsqlTsQuery.NodeKind.And => (NpgsqlTsQuery) new NpgsqlTsQueryAnd(null, null),
                            NpgsqlTsQuery.NodeKind.Or => new NpgsqlTsQueryOr(null, null),
                            NpgsqlTsQuery.NodeKind.Phrase => new NpgsqlTsQueryFollowedBy(null, buf.ReadInt16(), null),
                            _ => throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {operKind} of enum {nameof(NpgsqlTsQuery.NodeKind)}. Please file a bug.")
                        };

                        InsertInTree(node, nodes, ref value);

                        nodes.Push(new Tuple <NpgsqlTsQuery, int>(node, 2));
                        nodes.Push(new Tuple <NpgsqlTsQuery, int>(node, 1));
                    }
                }
                else
                {
                    var weight = (NpgsqlTsQueryLexeme.Weight)buf.ReadByte();
                    var prefix = buf.ReadByte() != 0;
                    var str    = buf.ReadNullTerminatedString();
                    InsertInTree(new NpgsqlTsQueryLexeme(str, weight, prefix), nodes, ref value);
                }

                len -= buf.ReadPosition - readPos;
            }

            if (nodes.Count != 0)
            {
                throw new InvalidOperationException("Internal Npgsql bug, please report.");
            }

            return(value !);
Exemplo n.º 25
0
 int INpgsqlSimpleTypeHandler <int> .Read(NpgsqlReadBuffer buf, int len, [CanBeNull] FieldDescription fieldDescription)
 => buf.ReadByte();
Exemplo n.º 26
0
        async Task <PostgisGeometry> DoRead(NpgsqlReadBuffer buf, WkbIdentifier id, ByteOrder bo, bool async)
        {
            switch (id)
            {
            case WkbIdentifier.Point:
                await buf.Ensure(16, async);

                return(new PostgisPoint(buf.ReadDouble(bo), buf.ReadDouble(bo)));

            case WkbIdentifier.LineString:
            {
                await buf.Ensure(4, async);

                var points = new Coordinate2D[buf.ReadInt32(bo)];
                for (var ipts = 0; ipts < points.Length; ipts++)
                {
                    await buf.Ensure(16, async);

                    points[ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                }
                return(new PostgisLineString(points));
            }

            case WkbIdentifier.Polygon:
            {
                await buf.Ensure(4, async);

                var rings = new Coordinate2D[buf.ReadInt32(bo)][];

                for (var irng = 0; irng < rings.Length; irng++)
                {
                    await buf.Ensure(4, async);

                    rings[irng] = new Coordinate2D[buf.ReadInt32(bo)];
                    for (var ipts = 0; ipts < rings[irng].Length; ipts++)
                    {
                        await buf.Ensure(16, async);

                        rings[irng][ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                    }
                }
                return(new PostgisPolygon(rings));
            }

            case WkbIdentifier.MultiPoint:
            {
                await buf.Ensure(4, async);

                var points = new Coordinate2D[buf.ReadInt32(bo)];
                for (var ipts = 0; ipts < points.Length; ipts++)
                {
                    await buf.Ensure(21, async);

                    await buf.Skip(5, async);

                    points[ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                }
                return(new PostgisMultiPoint(points));
            }

            case WkbIdentifier.MultiLineString:
            {
                await buf.Ensure(4, async);

                var rings = new Coordinate2D[buf.ReadInt32(bo)][];

                for (var irng = 0; irng < rings.Length; irng++)
                {
                    await buf.Ensure(9, async);

                    await buf.Skip(5, async);

                    rings[irng] = new Coordinate2D[buf.ReadInt32(bo)];
                    for (var ipts = 0; ipts < rings[irng].Length; ipts++)
                    {
                        await buf.Ensure(16, async);

                        rings[irng][ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                    }
                }
                return(new PostgisMultiLineString(rings));
            }

            case WkbIdentifier.MultiPolygon:
            {
                await buf.Ensure(4, async);

                var pols = new Coordinate2D[buf.ReadInt32(bo)][][];

                for (var ipol = 0; ipol < pols.Length; ipol++)
                {
                    await buf.Ensure(9, async);

                    await buf.Skip(5, async);

                    pols[ipol] = new Coordinate2D[buf.ReadInt32(bo)][];
                    for (var irng = 0; irng < pols[ipol].Length; irng++)
                    {
                        await buf.Ensure(4, async);

                        pols[ipol][irng] = new Coordinate2D[buf.ReadInt32(bo)];
                        for (var ipts = 0; ipts < pols[ipol][irng].Length; ipts++)
                        {
                            await buf.Ensure(16, async);

                            pols[ipol][irng][ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo));
                        }
                    }
                }
                return(new PostgisMultiPolygon(pols));
            }

            case WkbIdentifier.GeometryCollection:
            {
                await buf.Ensure(4, async);

                var g = new PostgisGeometry[buf.ReadInt32(bo)];

                for (var i = 0; i < g.Length; i++)
                {
                    await buf.Ensure(5, async);

                    var elemBo = (ByteOrder)buf.ReadByte();
                    var elemId = (WkbIdentifier)(buf.ReadUInt32(bo) & 7);

                    g[i] = await DoRead(buf, elemId, elemBo, async);
                }
                return(new PostgisGeometryCollection(g));
            }

            default:
                throw new InvalidOperationException("Unknown Postgis identifier.");
            }
        }
Exemplo n.º 27
0
        async ValueTask <GeoJSONObject> ReadGeometryCore(NpgsqlReadBuffer buf, bool async, BoundingBoxBuilder?boundingBox)
        {
            await buf.Ensure(SizeOfHeader, async);

            var littleEndian = buf.ReadByte() > 0;
            var type         = (EwkbGeometryType)buf.ReadUInt32(littleEndian);

            GeoJSONObject geometry;
            NamedCRS?     crs = null;

            if (HasSrid(type))
            {
                await buf.Ensure(4, async);

                crs = GetCrs(buf.ReadInt32(littleEndian));
            }

            switch (type & EwkbGeometryType.BaseType)
            {
            case EwkbGeometryType.Point:
            {
                await buf.Ensure(SizeOfPoint(type), async);

                var position = ReadPosition(buf, type, littleEndian);
                boundingBox?.Accumulate(position);
                geometry = new Point(position);
                break;
            }

            case EwkbGeometryType.LineString:
            {
                await buf.Ensure(SizeOfLength, async);

                var coordinates = new Position[buf.ReadInt32(littleEndian)];
                for (var i = 0; i < coordinates.Length; ++i)
                {
                    await buf.Ensure(SizeOfPoint(type), async);

                    var position = ReadPosition(buf, type, littleEndian);
                    boundingBox?.Accumulate(position);
                    coordinates[i] = position;
                }
                geometry = new LineString(coordinates);
                break;
            }

            case EwkbGeometryType.Polygon:
            {
                await buf.Ensure(SizeOfLength, async);

                var lines = new LineString[buf.ReadInt32(littleEndian)];
                for (var i = 0; i < lines.Length; ++i)
                {
                    var coordinates = new Position[buf.ReadInt32(littleEndian)];
                    for (var j = 0; j < coordinates.Length; ++j)
                    {
                        await buf.Ensure(SizeOfPoint(type), async);

                        var position = ReadPosition(buf, type, littleEndian);
                        boundingBox?.Accumulate(position);
                        coordinates[j] = position;
                    }
                    lines[i] = new LineString(coordinates);
                }
                geometry = new Polygon(lines);
                break;
            }

            case EwkbGeometryType.MultiPoint:
            {
                await buf.Ensure(SizeOfLength, async);

                var points = new Point[buf.ReadInt32(littleEndian)];
                for (var i = 0; i < points.Length; ++i)
                {
                    await buf.Ensure(SizeOfHeader + SizeOfPoint(type), async);

                    await buf.Skip(SizeOfHeader, async);

                    var position = ReadPosition(buf, type, littleEndian);
                    boundingBox?.Accumulate(position);
                    points[i] = new Point(position);
                }
                geometry = new MultiPoint(points);
                break;
            }

            case EwkbGeometryType.MultiLineString:
            {
                await buf.Ensure(SizeOfLength, async);

                var lines = new LineString[buf.ReadInt32(littleEndian)];
                for (var i = 0; i < lines.Length; ++i)
                {
                    await buf.Ensure(SizeOfHeaderWithLength, async);

                    await buf.Skip(SizeOfHeader, async);

                    var coordinates = new Position[buf.ReadInt32(littleEndian)];
                    for (var j = 0; j < coordinates.Length; ++j)
                    {
                        await buf.Ensure(SizeOfPoint(type), async);

                        var position = ReadPosition(buf, type, littleEndian);
                        boundingBox?.Accumulate(position);
                        coordinates[j] = position;
                    }
                    lines[i] = new LineString(coordinates);
                }
                geometry = new MultiLineString(lines);
                break;
            }

            case EwkbGeometryType.MultiPolygon:
            {
                await buf.Ensure(SizeOfLength, async);

                var polygons = new Polygon[buf.ReadInt32(littleEndian)];
                for (var i = 0; i < polygons.Length; ++i)
                {
                    await buf.Ensure(SizeOfHeaderWithLength, async);

                    await buf.Skip(SizeOfHeader, async);

                    var lines = new LineString[buf.ReadInt32(littleEndian)];
                    for (var j = 0; j < lines.Length; ++j)
                    {
                        var coordinates = new Position[buf.ReadInt32(littleEndian)];
                        for (var k = 0; k < coordinates.Length; ++k)
                        {
                            await buf.Ensure(SizeOfPoint(type), async);

                            var position = ReadPosition(buf, type, littleEndian);
                            boundingBox?.Accumulate(position);
                            coordinates[k] = position;
                        }
                        lines[j] = new LineString(coordinates);
                    }
                    polygons[i] = new Polygon(lines);
                }
                geometry = new MultiPolygon(polygons);
                break;
            }

            case EwkbGeometryType.GeometryCollection:
            {
                await buf.Ensure(SizeOfLength, async);

                var elements = new IGeometryObject[buf.ReadInt32(littleEndian)];
                for (var i = 0; i < elements.Length; ++i)
                {
                    elements[i] = (IGeometryObject) await ReadGeometryCore(buf, async, boundingBox);
                }
                geometry = new GeometryCollection(elements);
                break;
            }

            default:
                throw UnknownPostGisType();
            }

            geometry.CRS = crs;
            return(geometry);
        }
 long INpgsqlSimpleTypeHandler <long> .Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription)
 => buf.ReadByte();
 /// <inheritdoc />
 public override char Read(NpgsqlReadBuffer buf, int len, FieldDescription?fieldDescription = null)
 => (char)buf.ReadByte();
Exemplo n.º 30
0
        public override async Task <NpgsqlTsQuery> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)
        {
            await buf.Ensure(4, async);

            var numTokens = buf.ReadInt32();

            if (numTokens == 0)
            {
                return(new NpgsqlTsQueryEmpty());
            }

            _nodes = new Stack <Tuple <NpgsqlTsQuery, int> >();
            len   -= 4;

            for (var tokenPos = 0; tokenPos < numTokens; tokenPos++)
            {
                await buf.Ensure(Math.Min(len, MaxSingleTokenBytes), async);

                var readPos = buf.ReadPosition;

                var isOper = buf.ReadByte() == 2;
                if (isOper)
                {
                    var operKind = (NpgsqlTsQuery.NodeKind)buf.ReadByte();
                    if (operKind == NpgsqlTsQuery.NodeKind.Not)
                    {
                        var node = new NpgsqlTsQueryNot(null);
                        InsertInTree(node);
                        _nodes.Push(new Tuple <NpgsqlTsQuery, int>(node, 0));
                    }
                    else
                    {
                        NpgsqlTsQuery node;

                        switch (operKind)
                        {
                        case NpgsqlTsQuery.NodeKind.And:
                            node = new NpgsqlTsQueryAnd(null, null);
                            break;

                        case NpgsqlTsQuery.NodeKind.Or:
                            node = new NpgsqlTsQueryOr(null, null);
                            break;

                        default:
                            throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {operKind} of enum {nameof(NpgsqlTsQuery.NodeKind)}. Please file a bug.");
                        }

                        InsertInTree(node);

                        _nodes.Push(new Tuple <NpgsqlTsQuery, int>(node, 2));
                        _nodes.Push(new Tuple <NpgsqlTsQuery, int>(node, 1));
                    }
                }
                else
                {
                    var weight = (NpgsqlTsQueryLexeme.Weight)buf.ReadByte();
                    var prefix = buf.ReadByte() != 0;
                    var str    = buf.ReadNullTerminatedString();
                    InsertInTree(new NpgsqlTsQueryLexeme(str, weight, prefix));
                }

                len -= buf.ReadPosition - readPos;
            }

            if (_nodes.Count != 0)
            {
                throw new InvalidOperationException("Internal Npgsql bug, please report.");
            }

            return(_value);
        }