public static void BinaryTypeNotEqualToOtherType()
        {
            var type1 = new SqlBinaryType(SqlTypeCode.VarBinary);
            var type2 = new SqlBooleanType(SqlTypeCode.Bit);

            Assert.False(type1.Equals(type2));
        }
        public static void BinaryTypesEqual(SqlTypeCode typeCode1, int size1, SqlTypeCode typeCode2, int size2, bool expected)
        {
            var type1 = new SqlBinaryType(typeCode1, size1);
            var type2 = new SqlBinaryType(typeCode2, size2);

            Assert.Equal(expected, type1.Equals(type2));
        }
        public static void Serialize(SqlTypeCode typeCode, int size)
        {
            var type   = new SqlBinaryType(typeCode, size);
            var result = BinarySerializeUtil.Serialize(type);

            Assert.Equal(type, result);
        }
        private ISqlValue ToBinary(SqlDayToSecond dts, SqlBinaryType destType)
        {
            var bytes  = dts.ToByArray();
            var binary = new SqlBinary(bytes);

            return(destType.NormalizeValue(binary));
        }
        public static void GetString(SqlTypeCode typeCode, int size, string expected)
        {
            var type = new SqlBinaryType(typeCode, size);

            var s = type.ToString();

            Assert.Equal(expected, s);
        }
        private ISqlValue ToBinary(SqlNumber number, SqlBinaryType destType)
        {
            var bytes = number.ToByteArray();

            if (bytes.Length > destType.MaxSize)
            {
                return(SqlNull.Value);
            }

            return(destType.NormalizeValue(new SqlBinary(bytes)));
        }
        public static void CastToBoolean(byte singleByte, bool expected)
        {
            var type = new SqlBinaryType(SqlTypeCode.Binary);

            var value = new SqlBinary(new[] { singleByte });

            Assert.True(type.CanCastTo(value, PrimitiveTypes.Bit()));

            var result = type.Cast(value, PrimitiveTypes.Bit());

            Assert.NotNull(result);
            Assert.IsType <SqlBoolean>(result);

            Assert.Equal(expected, (bool?)((SqlBoolean)result));
        }
        public static void CastToDate(string s, SqlTypeCode typeCode)
        {
            var type     = new SqlBinaryType(SqlTypeCode.Binary);
            var date     = SqlDateTime.Parse(s);
            var destType = new SqlDateTimeType(typeCode);

            var bytes  = date.ToByteArray();
            var binary = new SqlBinary(bytes);

            Assert.True(type.CanCastTo(binary, destType));

            var result = type.Cast(binary, destType);

            Assert.NotNull(result);
            Assert.IsType <SqlDateTime>(result);

            Assert.Equal(date, (SqlDateTime)result);
        }
        public static void CastToString(string s, SqlTypeCode typeCode, int maxSize, string expected)
        {
            var type     = new SqlBinaryType(SqlTypeCode.Binary);
            var input    = (SqlString)s;
            var destType = new SqlCharacterType(typeCode, maxSize, null);

            var bytes  = input.ToByteArray();
            var binary = new SqlBinary(bytes);

            Assert.True(type.CanCastTo(binary, destType));

            var result = type.Cast(binary, destType);

            Assert.NotNull(result);
            Assert.IsType <SqlString>(result);

            Assert.Equal(expected, (SqlString)result);
        }
        public static void CastToNumber()
        {
            var type = new SqlBinaryType(SqlTypeCode.Binary);

            var value = new SqlBinary(new byte[] { 44, 95, 122, 0 });

            Assert.True(type.CanCastTo(value, PrimitiveTypes.Numeric(22, 4)));

            var result = type.Cast(value, PrimitiveTypes.Numeric(22, 4));

            Assert.NotNull(result);
            Assert.IsType <SqlNumber>(result);

            var number = (SqlNumber)result;

            Assert.NotNull(number);
            Assert.False(number.CanBeInt32);
            Assert.Equal(74445.4656, (double)number);
        }
Ejemplo n.º 11
0
        public static SqlType Deserialize(BinaryReader reader, ISqlTypeResolver typeResolver)
        {
            var typeCode = (SqlTypeCode)reader.ReadByte();

            if (SqlBooleanType.IsBooleanType(typeCode))
            {
                return(PrimitiveTypes.Boolean(typeCode));
            }
            if (SqlDateTimeType.IsDateType(typeCode))
            {
                return(PrimitiveTypes.DateTime(typeCode));
            }

            if (typeCode == SqlTypeCode.DayToSecond)
            {
                return(PrimitiveTypes.DayToSecond());
            }
            if (typeCode == SqlTypeCode.YearToMonth)
            {
                return(PrimitiveTypes.YearToMonth());
            }

            if (SqlCharacterType.IsStringType(typeCode))
            {
                var maxSize = reader.ReadInt32();

                CultureInfo locale    = null;
                var         hasLocale = reader.ReadByte() == 1;
                if (hasLocale)
                {
                    var name = reader.ReadString();
                    locale = new CultureInfo(name);
                }

                return(PrimitiveTypes.String(typeCode, maxSize, locale));
            }

            if (SqlNumericType.IsNumericType(typeCode))
            {
                var size  = reader.ReadInt32();
                var scale = reader.ReadInt32();

                return(PrimitiveTypes.Numeric(typeCode, size, scale));
            }

            if (SqlBinaryType.IsBinaryType(typeCode))
            {
                var size = reader.ReadInt32();
                return(PrimitiveTypes.Binary(typeCode, size));
            }

            if (typeCode == SqlTypeCode.Type)
            {
                if (typeResolver == null)
                {
                    throw new NotSupportedException("User-Defined types require a resolver context.");
                }

                // TODO: support type arguments
                var typeName = reader.ReadString();
                return(typeResolver.Resolve(new SqlTypeResolveInfo(typeName)));
            }

            /*
             * TODO:
             * if (typeCode == SqlTypeCode.QueryPlan)
             *      return new SqlQueryType();
             */

            if (typeCode == SqlTypeCode.Array)
            {
                var size = reader.ReadInt32();
                return(new SqlArrayType(size));
            }

            throw new NotSupportedException($"The type code '{typeCode}' does not support deserialization");
        }