WriteUInt16() public method

public WriteUInt16 ( int i ) : void
i int
return void
示例#1
0
        internal async Task WriteParse(string sql, string statementName, List <NpgsqlParameter> inputParameters, bool async)
        {
            Debug.Assert(statementName.All(c => c < 128));

            int queryByteLen;

            try
            {
                queryByteLen = TextEncoding.GetByteCount(sql);
            }
            catch (Exception e)
            {
                Break(e);
                throw;
            }

            if (WriteBuffer.WriteSpaceLeft < 1 + 4 + statementName.Length + 1)
            {
                await Flush(async);
            }

            var messageLength =
                sizeof(byte) +                        // Message code
                sizeof(int) +                         // Length
                statementName.Length +                // Statement name
                sizeof(byte) +                        // Null terminator for the statement name
                queryByteLen + sizeof(byte) +         // SQL query length plus null terminator
                sizeof(ushort) +                      // Number of parameters
                inputParameters.Count * sizeof(int);  // Parameter OIDs

            WriteBuffer.WriteByte(FrontendMessageCode.Parse);
            WriteBuffer.WriteInt32(messageLength - 1);
            WriteBuffer.WriteNullTerminatedString(statementName);

            await WriteBuffer.WriteString(sql, queryByteLen, async);

            if (WriteBuffer.WriteSpaceLeft < 1 + 2)
            {
                await Flush(async);
            }
            WriteBuffer.WriteByte(0); // Null terminator for the query
            WriteBuffer.WriteUInt16((ushort)inputParameters.Count);

            foreach (var p in inputParameters)
            {
                if (WriteBuffer.WriteSpaceLeft < 4)
                {
                    await Flush(async);
                }

                WriteBuffer.WriteInt32((int)p.Handler !.PostgresType.OID);
            }
        }
示例#2
0
        internal async Task WriteBind(
            List <NpgsqlParameter> inputParameters,
            string portal,
            string statement,
            bool allResultTypesAreUnknown,
            bool[]?unknownResultTypeList,
            bool async)
        {
            Debug.Assert(statement.All(c => c < 128));
            Debug.Assert(portal.All(c => c < 128));

            var headerLength =
                sizeof(byte) +                        // Message code
                sizeof(int) +                         // Message length
                sizeof(byte) +                        // Portal is always empty (only a null terminator)
                statement.Length + sizeof(byte) +     // Statement name plus null terminator
                sizeof(ushort);                       // Number of parameter format codes that follow

            if (WriteBuffer.WriteSpaceLeft < headerLength)
            {
                Debug.Assert(WriteBuffer.Size >= headerLength, "Write buffer too small for Bind header");
                await Flush(async);
            }

            var formatCodesSum = 0;
            var paramsLength   = 0;

            foreach (var p in inputParameters)
            {
                formatCodesSum += (int)p.FormatCode;
                p.LengthCache?.Rewind();
                paramsLength += p.ValidateAndGetLength();
            }

            var formatCodeListLength = formatCodesSum == 0 ? 0 : formatCodesSum == inputParameters.Count ? 1 : inputParameters.Count;

            var messageLength = headerLength +
                                sizeof(short) * formatCodeListLength +                // List of format codes
                                sizeof(short) +                                       // Number of parameters
                                sizeof(int) * inputParameters.Count +                 // Parameter lengths
                                paramsLength +                                        // Parameter values
                                sizeof(short) +                                       // Number of result format codes
                                sizeof(short) * (unknownResultTypeList?.Length ?? 1); // Result format codes

            WriteBuffer.WriteByte(FrontendMessageCode.Bind);
            WriteBuffer.WriteInt32(messageLength - 1);
            Debug.Assert(portal == string.Empty);
            WriteBuffer.WriteByte(0);  // Portal is always empty

            WriteBuffer.WriteNullTerminatedString(statement);
            WriteBuffer.WriteInt16(formatCodeListLength);

            // 0 length implicitly means all-text, 1 means all-binary, >1 means mix-and-match
            if (formatCodeListLength == 1)
            {
                if (WriteBuffer.WriteSpaceLeft < 2)
                {
                    await Flush(async);
                }
                WriteBuffer.WriteInt16((short)FormatCode.Binary);
            }
            else if (formatCodeListLength > 1)
            {
                foreach (var p in inputParameters)
                {
                    if (WriteBuffer.WriteSpaceLeft < 2)
                    {
                        await Flush(async);
                    }
                    WriteBuffer.WriteInt16((short)p.FormatCode);
                }
            }

            if (WriteBuffer.WriteSpaceLeft < 2)
            {
                await Flush(async);
            }

            WriteBuffer.WriteUInt16((ushort)inputParameters.Count);

            foreach (var param in inputParameters)
            {
                param.LengthCache?.Rewind();
                await param.WriteWithLength(WriteBuffer, async);
            }

            if (unknownResultTypeList != null)
            {
                if (WriteBuffer.WriteSpaceLeft < 2 + unknownResultTypeList.Length * 2)
                {
                    await Flush(async);
                }
                WriteBuffer.WriteInt16(unknownResultTypeList.Length);
                foreach (var t in unknownResultTypeList)
                {
                    WriteBuffer.WriteInt16(t ? 0 : 1);
                }
            }
            else
            {
                if (WriteBuffer.WriteSpaceLeft < 4)
                {
                    await Flush(async);
                }
                WriteBuffer.WriteInt16(1);
                WriteBuffer.WriteInt16(allResultTypesAreUnknown ? 0 : 1);
            }
        }