Esempio n. 1
0
        public static void SerializeExplicit(SqlTypeCode typeCode)
        {
            var type   = new SqlDateTimeType(typeCode);
            var result = SqlTypeUtil.Serialize(type);

            Assert.Equal(type, result);
        }
Esempio n. 2
0
		public static void GetValidDateTimeType(SqlTypeCode typeCode) {
			var type = new SqlDateTimeType(typeCode);
			Assert.Equal(typeCode, type.TypeCode);
			Assert.True(type.IsIndexable);
			Assert.True(type.IsPrimitive);
			Assert.False(type.IsLargeObject);
			Assert.False(type.IsReference);
		}
Esempio n. 3
0
        public static void GetString(SqlTypeCode typeCode, string expected)
        {
            var type = new SqlDateTimeType(typeCode);

            var s = type.ToString();

            Assert.Equal(expected, s);
        }
Esempio n. 4
0
		private static void BinaryWithInterval(Func<SqlDateTimeType, Func<ISqlValue, ISqlValue, ISqlValue>> selector, string date, string offset, string expected) {
			var type = new SqlDateTimeType(SqlTypeCode.DateTime);
			var sqlDate = SqlDateTime.Parse(date);
			var dts = SqlDayToSecond.Parse(offset);

			var op = selector(type);
			var result = op(sqlDate, dts);

			var expectedResult = SqlDateTime.Parse(expected);

			Assert.Equal(expectedResult, result);
		}
Esempio n. 5
0
		private static void Binary(Func<SqlDateTimeType, Func<ISqlValue, ISqlValue, SqlBoolean>> selector, string date1, string date2, bool expected) {
			var type = new SqlDateTimeType(SqlTypeCode.DateTime);
			var sqlDate1 = SqlDateTime.Parse(date1);
			var sqlDate2 = SqlDateTime.Parse(date2);

			var op = selector(type);
			var result = op(sqlDate1, sqlDate2);

			var expectedResult = (SqlBoolean) expected;

			Assert.Equal(expectedResult, result);
		}
Esempio n. 6
0
		private static void BinaryWithInterval(Func<SqlDateTimeType, Func<ISqlValue, ISqlValue, ISqlValue>> selector, string date, int months, string expected)
		{
			var type = new SqlDateTimeType(SqlTypeCode.DateTime);
			var sqlDate = SqlDateTime.Parse(date);
			var ytm = new SqlYearToMonth(months);

			var op = selector(type);
			var result = op(sqlDate, ytm);

			var expectedResult = SqlDateTime.Parse(expected);

			Assert.Equal(expectedResult, result);
		}
Esempio n. 7
0
		public static void CastToString(string s, SqlTypeCode typeCode, SqlTypeCode destTypeCode, int maxSize, string expected) {
			var date = SqlDateTime.Parse(s);
			var type = new SqlDateTimeType(typeCode);

			var destType = new SqlCharacterType(destTypeCode, maxSize, null);
			Assert.True(type.CanCastTo(date, destType));

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

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

			Assert.Equal(expected, (SqlString) result);
		}
Esempio n. 8
0
		public static void CastToDateTime(string s, SqlTypeCode destTypeCode, string expected) {
			var date = SqlDateTime.Parse(s);
			var type = new SqlDateTimeType(SqlTypeCode.DateTime);

			var destType = new SqlDateTimeType(destTypeCode);
			Assert.True(type.CanCastTo(date, destType));

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

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

			var expectedResult = SqlDateTime.Parse(expected);
			Assert.Equal(expectedResult, result);
		}
Esempio n. 9
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);
		}
        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);
        }
Esempio 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");
        }
Esempio n. 12
0
 private ISqlValue ToDateTime(SqlDateTime date, SqlDateTimeType destType)
 {
     return(destType.NormalizeValue(date));
 }