Esempio n. 1
0
        internal RowDescriptionMessage Load(NpgsqlReadBuffer buf, ConnectorTypeMapper typeMapper)
        {
            Fields.Clear();
            _nameIndex.Clear();
            _caseInsensitiveNameIndex.Clear();

            var numFields = buf.ReadInt16();

            for (var i = 0; i != numFields; ++i)
            {
                // TODO: Recycle
                var field = new FieldDescription();
                field.Populate(
                    typeMapper,
                    buf.ReadNullTerminatedString(),  // Name
                    buf.ReadUInt32(),                // TableOID
                    buf.ReadInt16(),                 // ColumnAttributeNumber
                    buf.ReadUInt32(),                // TypeOID
                    buf.ReadInt16(),                 // TypeSize
                    buf.ReadInt32(),                 // TypeModifier
                    (FormatCode)buf.ReadInt16()      // FormatCode
                    );

                Fields.Add(field);
                if (!_nameIndex.ContainsKey(field.Name))
                {
                    _nameIndex.Add(field.Name, i);
                    if (!_caseInsensitiveNameIndex.ContainsKey(field.Name))
                    {
                        _caseInsensitiveNameIndex.Add(field.Name, i);
                    }
                }
            }
            return(this);
        }
 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!");
     }
 }
Esempio n. 3
0
        internal async Task ExpectSimpleQuery(string expectedSql)
        {
            CheckDisposed();

            await _readBuffer.EnsureAsync(5);

            var actualCode = _readBuffer.ReadByte();

            Assert.That(actualCode, Is.EqualTo(FrontendMessageCode.Query), $"Expected message of type Query but got '{(char)actualCode}'");
            _ = _readBuffer.ReadInt32();
            var actualSql = _readBuffer.ReadNullTerminatedString();

            Assert.That(actualSql, Is.EqualTo(expectedSql));
        }
Esempio n. 4
0
        /// <inheritdoc />
        public override async ValueTask <NpgsqlTsVector> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription?fieldDescription = null)
        {
            await buf.Ensure(4, async);

            var numLexemes = buf.ReadInt32();

            len -= 4;

            var lexemes = new List <NpgsqlTsVector.Lexeme>();

            for (var lexemePos = 0; lexemePos < numLexemes; lexemePos++)
            {
                await buf.Ensure(Math.Min(len, MaxSingleLexemeBytes), async);

                var posBefore = buf.ReadPosition;

                List <NpgsqlTsVector.Lexeme.WordEntryPos>?positions = null;

                var lexemeString = buf.ReadNullTerminatedString();
                int numPositions = buf.ReadInt16();
                for (var i = 0; i < numPositions; i++)
                {
                    var wordEntryPos = buf.ReadInt16();
                    if (positions == null)
                    {
                        positions = new List <NpgsqlTsVector.Lexeme.WordEntryPos>();
                    }
                    positions.Add(new NpgsqlTsVector.Lexeme.WordEntryPos(wordEntryPos));
                }

                lexemes.Add(new NpgsqlTsVector.Lexeme(lexemeString, positions, true));

                len -= buf.ReadPosition - posBefore;
            }

            return(new NpgsqlTsVector(lexemes, true));
        }
Esempio n. 5
0
        internal RowDescriptionMessage Load(NpgsqlReadBuffer buf, ConnectorTypeMapper typeMapper)
        {
            _nameIndex.Clear();
            _insensitiveIndex?.Clear();

            var numFields = Count = buf.ReadInt16();

            if (_fields.Length < numFields)
            {
                var oldFields = _fields;
                _fields = new FieldDescription[numFields];
                Array.Copy(oldFields, _fields, oldFields.Length);
            }

            for (var i = 0; i < numFields; ++i)
            {
                var field = _fields[i] ??= new();

                field.Populate(
                    typeMapper,
                    buf.ReadNullTerminatedString(), // Name
                    buf.ReadUInt32(),               // TableOID
                    buf.ReadInt16(),                // ColumnAttributeNumber
                    buf.ReadUInt32(),               // TypeOID
                    buf.ReadInt16(),                // TypeSize
                    buf.ReadInt32(),                // TypeModifier
                    (FormatCode)buf.ReadInt16()     // FormatCode
                    );

                if (!_nameIndex.ContainsKey(field.Name))
                {
                    _nameIndex.Add(field.Name, i);
                }
            }

            return(this);
        }
Esempio n. 6
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 !);
Esempio n. 7
0
 internal NpgsqlNotificationEventArgs(NpgsqlReadBuffer buf)
 {
     PID     = buf.ReadInt32();
     Channel = buf.ReadNullTerminatedString();
     Payload = buf.ReadNullTerminatedString();
 }
Esempio n. 8
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;
                }
            }
        }
        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);
        }