Exemplo n.º 1
0
        public object Convert(
            int oid,
            DataRow row,
            PostgresFormatCode formatCode,
            PostgresClientState state)
        {
            if (!_oidCodecLookup.TryGetValue(oid, out var typeConverter))
            {
                // TODO
                throw new InvalidOperationException("Unknown oid " + oid);
            }

            var codec = typeConverter.Codec;

            if (codec == null)
            {
                throw new NotImplementedException(
                          $"Convertion of type '{typeConverter.Name}' ({oid}) is not supported.");
            }

            switch (formatCode)
            {
            case PostgresFormatCode.Text:
                return(codec.DecodeTextObject(row, state));

            case PostgresFormatCode.Binary:
                return(codec.DecodeBinaryObject(row, state));
            }

            throw new ArgumentOutOfRangeException(
                      nameof(formatCode), formatCode,
                      "Unknown format code.");
        }
        public override DateTime DecodeBinary(DataRow row, PostgresClientState state)
        {
            var date = GetInt4(row);

            return(PostgresDateTime.FromJulian(
                       date + PostgresDateTime.PostgresEpochJdate));
        }
        public void TestPasswordMessageCreateMd5()
        {
            var salt = new byte[] { 0x77, 0x16, 0x3e, 0xe3 };

            var authMessage = new AuthenticationMessage {
                AuthenticationMessageType = AuthenticationMessageType.MD5Password,
                DataBuffer = salt
            };

            var state = PostgresClientState.CreateDefault();

            var connectionString = new PostgresConnectionString(
                PostgresServerInformation.FakeConnectionString);

            var passwordMessage = PasswordMessage
                                  .CreateMd5(authMessage, state, connectionString);

            const string expectedHash = "md583ce447ce89d7e4e943205ff8f82f76a";

            try
            {
                var actualHash = Encoding.ASCII.GetString(
                    passwordMessage.Password, 0,
                    passwordMessage.PasswordLength);
                Assert.AreEqual(0x23, passwordMessage.PasswordLength);
                Assert.AreEqual(expectedHash, actualHash);
            }
            finally
            {
                authMessage.TryDispose();
                passwordMessage.TryDispose();
            }
        }
        public override unsafe IReadOnlyList <bool> DecodeBinaryArray(
            byte *data, int length, int count, PostgresClientState state)
        {
            if (length < count)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(count), count,
                          $"Element count is outside the data bounds of {length}");
            }

            var result = new bool[count];

            fixed(bool *resultPtr = result)
            {
                for (var i = 0; i < count; ++i)
                {
                    switch (data[i])
                    {
                    case 0: resultPtr[i] = false; break;

                    case 1: resultPtr[i] = true; break;

                    default: throw new ArgumentOutOfRangeException();
                    }
                }
            }

            return(result);
        }
        public override Guid DecodeText(DataRow row, PostgresClientState state)
        {
            // TODO: Get rid of the string allocation here.
            var guidString = PostgresStringCodec.Default
                             .DecodeText(row, state);

            return(new Guid(guidString));
        }
        public override unsafe float DecodeBinary(DataRow row, PostgresClientState state)
        {
            PostgresTypeConverter.DemandDataLength(row, 4);

            var asint = BinaryBuffer.ReadIntNetwork(row.Data, 0);

            return(*(float *)&asint);
        }
        public override unsafe double DecodeBinary(DataRow row, PostgresClientState state)
        {
            PostgresTypeConverter.DemandDataLength(row, 8);

            var asint = BinaryBuffer.ReadLongNetwork(row.Data, 0);

            return(*(double *)&asint);
        }
        public override string DecodeBinary(DataRow row, PostgresClientState state)
        {
            if (row.Length == 0)
            {
                return("");
            }

            return(state.ServerEncoding.GetString(row.Data, 0, row.Length));
        }
        public override unsafe Guid DecodeBinary(
            DataRow row, PostgresClientState state)
        {
            PostgresTypeConverter.DemandDataLength(row, 16);

            fixed(byte *data = row.Data)
            {
                return(ReadBinaryGuid(data));
            }
        }
        public override bool DecodeText(DataRow row, PostgresClientState state)
        {
            PostgresTypeConverter.DemandDataLength(row, 1);

            switch (row.Data[0])
            {
            case (byte)'f': return(false);

            case (byte)'t': return(true);
            }

            throw new ArgumentOutOfRangeException();
        }
Exemplo n.º 11
0
        private static void Test <T>(
            byte[] data,
            string postgresTypeName,
            T expected)
        {
            var row = new DataRow(data.Length, data);

            var actual = PostgresTypeConverter.Convert(
                postgresTypeName.GetHashCode(),
                row,
                PostgresFormatCode.Text,
                PostgresClientState.CreateDefault());

            Assert.IsTrue(actual is T, $"Expected {typeof(T)} but was {actual.GetType()}");
            Assert.AreEqual(expected, actual);
        }
        public override unsafe IReadOnlyList <Guid> DecodeBinaryArray(
            byte *data, int length, int count, PostgresClientState state)
        {
            const int dataLength = 16;

            var result      = new Guid[count];
            var resultIndex = 0;

            fixed(Guid *resultPtr = result)
            {
                for (var i = 0; i < length; i += dataLength)
                {
                    resultPtr[resultIndex++] = ReadBinaryGuid(&data[i]);
                }
            }

            return(result);
        }
        public T Decode(
            DataRow row,
            PostgresFormatCode formatCode,
            PostgresClientState state)
        {
            switch (formatCode)
            {
            case PostgresFormatCode.Text:
                return(DecodeText(row, state));

            case PostgresFormatCode.Binary:
                return(DecodeBinary(row, state));
            }

            throw new ArgumentOutOfRangeException(
                      nameof(formatCode), formatCode,
                      "Unknown format code.");
        }
        public override unsafe IReadOnlyList <long> DecodeBinaryArray(
            byte *data, int length, int count, PostgresClientState state)
        {
            const int dataLength = 8;

            var result      = new long[count];
            var resultIndex = 0;

            fixed(long *resultPtr = result)
            {
                for (var i = 0; i < length; i += dataLength)
                {
                    resultPtr[resultIndex++] = BinaryBuffer
                                               .ReadShortNetworkUnsafe(&data[i]);
                }
            }

            return(result);
        }
        public override unsafe IReadOnlyList <string> DecodeBinaryArray(
            byte *data, int length, int count, PostgresClientState state)
        {
            var results     = new string[count];
            var resultIndex = 0;

            for (var i = 0; i < length;)
            {
                var size = BinaryBuffer.ReadIntNetworkUnsafe(&data[i]);

                if (size + i > length)
                {
                    throw new ArgumentOutOfRangeException();
                }

                i += 4;

                var s = size == 0
                    ? ""
                    : state.ServerEncoding.GetString(&data[i], size);
                results[resultIndex] = s;
                ++resultIndex;

                if (resultIndex == count)
                {
                    break;
                }

                i += size;
            }

            if (resultIndex != count)
            {
                // TODO
                throw new Exception(
                          "Unexpected early termination of string array.");
            }

            return(results);
        }
 public abstract void EncodeText(MemoryStream ms, T value, PostgresClientState state);
 public override short DecodeText(DataRow row, PostgresClientState state)
 {
     return(NumericBuffer.AsciiToShort(row.Data, row.Length));
 }
 public override double DecodeText(DataRow row, PostgresClientState state)
 {
     return((double)NumericBuffer.AsciiToDecimal(row.Data, row.Length));
 }
 public override void EncodeText(
     MemoryStream ms, double value, PostgresClientState state)
 {
     throw new NotImplementedException();
 }
 public override unsafe IReadOnlyList <double> DecodeBinaryArray(
     byte *data, int length, int count, PostgresClientState state)
 {
     throw new NotImplementedException();
 }
 public object DecodeBinaryObject(
     DataRow row,
     PostgresClientState state)
 {
     return(DecodeBinary(row, state));
 }
        public override long DecodeBinary(DataRow row, PostgresClientState state)
        {
            PostgresTypeConverter.DemandDataLength(row, 8);

            return(BinaryBuffer.ReadLongNetworkUnsafe(row.Data, 0));
        }
 public override void EncodeBinary(
     MemoryStream ms, float value, PostgresClientState state)
 {
     throw new NotImplementedException();
 }
 public override decimal DecodeBinary(DataRow row, PostgresClientState state)
 {
     throw new NotImplementedException();
 }
 public override long DecodeText(DataRow row, PostgresClientState state)
 {
     return(NumericBuffer.AsciiToLong(row.Data, 0, row.Length));
 }
 public abstract T DecodeText(DataRow row, PostgresClientState state);
Exemplo n.º 27
0
 protected void TestInitialize()
 {
     Offset      = 0;
     ReadState   = new PostgresReadState();
     ClientState = PostgresClientState.CreateDefault();
 }
 public object DecodeTextObject(
     DataRow row,
     PostgresClientState state)
 {
     return(DecodeText(row, state));
 }
 public override DateTime DecodeText(DataRow row, PostgresClientState state)
 {
     throw new NotImplementedException();
 }
 public abstract T DecodeBinary(DataRow row, PostgresClientState state);