示例#1
0
        public static void SerializeExplicit(SqlTypeCode typeCode, int precision, int scale)
        {
            var type   = new SqlNumericType(typeCode, precision, scale);
            var result = SqlTypeUtil.Serialize(type);

            Assert.Equal(type, result);
        }
示例#2
0
        public static void GetNumericObject(SqlTypeCode code, int precision, int scale, double value)
        {
            var type   = new SqlNumericType(code, precision, scale);
            var number = (SqlNumber)value;

            var obj = new SqlObject(type, number);

            Assert.Equal(type, obj.Type);
            Assert.Equal(number, obj.Value);
            Assert.False(obj.IsNull);
        }
示例#3
0
        public static void CreateNumericType(SqlTypeCode typeCode, int precision, int scale)
        {
            var type = new SqlNumericType(typeCode, precision, scale);

            Assert.NotNull(type);
            Assert.Equal(typeCode, type.TypeCode);
            Assert.Equal(precision, type.Precision);
            Assert.Equal(scale, type.Scale);
            Assert.True(type.IsIndexable);
            Assert.True(type.IsPrimitive);
            Assert.False(type.IsLargeObject);
            Assert.False(type.IsReference);
        }
示例#4
0
		public static void CastToNumber(string s, SqlTypeCode typeCode, int precision, int scale) {
			var date = SqlDateTime.Parse(s);
			var type = new SqlDateTimeType(SqlTypeCode.DateTime);
			var destType = new SqlNumericType(typeCode, precision, scale);

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

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

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

			var value = (SqlNumber) result;

			var back = new SqlDateTime((long)value);

			Assert.Equal(date, back);
		}
        private ISqlValue ToNumber(ISqlBinary value, SqlNumericType destType)
        {
            if (value == null)
            {
                throw new InvalidCastException();
            }

            var precision = destType.Precision;
            var scale     = destType.Scale;

            if (precision <= 0)
            {
                throw new NotSupportedException();
            }
            if (scale <= 0)
            {
                scale = 1;
            }

            // TODO: handle the cases when precision and/or scale are not set
            return(new SqlNumber(value.ToArray(), scale, precision));
        }
 private ISqlValue ToNumeric(SqlNumber number, SqlNumericType destType)
 {
     // TODO: should we make some checks here?
     return(destType.NormalizeValue(number));
 }
示例#7
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");
        }