private PostgresTypeCollection(List <PostgresType> types)
        {
            Types = types.ToArray();

            var arrayOids = new HashSet <int>(Types.Select(t => t.ArrayOid));

            _oidCodecLookup = new Dictionary <int, PostgresTypeConverter>(
                types.Count * 2);

            // Process entries with array values first, so that when the array
            // values themselves come up later they're skipped.
            foreach (var type in types.OrderByDescending(t => t.ArrayOid > 0))
            {
                if (_oidCodecLookup.ContainsKey(type.Oid))
                {
                    continue;
                }

                var converter = PostgresTypeConverter
                                .ConverterByName(
                    type.Name,
                    arrayOids.Contains(type.Oid));

                _oidCodecLookup[type.Oid] = converter;

                if (type.ArrayOid > 0 && converter.Codec != null)
                {
                    var arrayDecoder   = converter.Codec.CreateArrayDecoder();
                    var arrayConverter = new PostgresTypeConverter(
                        type.Name, arrayDecoder, true);

                    _oidCodecLookup[type.ArrayOid] = arrayConverter;
                }
            }
        }
        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 unsafe float DecodeBinary(DataRow row, PostgresClientState state)
        {
            PostgresTypeConverter.DemandDataLength(row, 4);

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

            return(*(float *)&asint);
        }
        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();
        }
Example #6
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 long DecodeBinary(DataRow row, PostgresClientState state)
        {
            PostgresTypeConverter.DemandDataLength(row, 8);

            return(BinaryBuffer.ReadLongNetworkUnsafe(row.Data, 0));
        }