Пример #1
0
        private ISqlString ToString(SqlDateTime date, SqlCharacterType destType)
        {
            var dateString = ToString(date);
            var s          = new SqlString(dateString);

            return((ISqlString)destType.NormalizeValue(s));
        }
        public static void SerializeExplicit(SqlTypeCode typeCode, int maxSize, string locale)
        {
            var culture = String.IsNullOrEmpty(locale) ? null : new CultureInfo(locale);
            var type    = new SqlCharacterType(typeCode, maxSize, culture);

            var result = SqlTypeUtil.Serialize(type);

            Assert.Equal(type, result);
        }
        public static void GetTypeString(SqlTypeCode typeCode, int maxSize, string locale, string expected)
        {
            var culture = String.IsNullOrEmpty(locale) ? null : new CultureInfo(locale);
            var type    = new SqlCharacterType(typeCode, maxSize, culture);

            var sql = type.ToString();

            Assert.Equal(expected, sql);
        }
        public static void NotEqual(string s1, string s2, string locale, bool expected)
        {
            var sqlString1 = new SqlString(s1);
            var sqlString2 = new SqlString(s2);

            var culture = String.IsNullOrEmpty(locale) ? null : new CultureInfo(locale);
            var type    = new SqlCharacterType(SqlTypeCode.String, -1, culture);

            Assert.Equal(expected, (bool)type.NotEqual(sqlString1, sqlString2));
        }
        private ISqlValue ToString(SqlNumber number, SqlCharacterType destType)
        {
            if (destType.HasMaxSize && number.Precision > destType.MaxSize)
            {
                return(SqlNull.Value);
            }

            var s = number.ToString();

            return(destType.NormalizeValue(new SqlString(s)));
        }
        private static void InvalidOp(Func <SqlType, Func <ISqlValue, ISqlValue> > selector)
        {
            var s1 = new SqlString("foo");

            var type   = new SqlCharacterType(SqlTypeCode.String, -1, null);
            var op     = selector(type);
            var result = op(s1);

            Assert.NotNull(result);
            Assert.IsType <SqlNull>(result);
        }
        private ISqlValue ToString(ISqlBinary binary, SqlCharacterType destType)
        {
            if (binary == null)
            {
                throw new InvalidCastException();
            }

            var bytes = binary.ToArray();
            var s     = new SqlString(bytes);

            return(destType.NormalizeValue(s));
        }
Пример #8
0
        public static void GetStringObject(SqlTypeCode code, int maxSize, string value, string expected)
        {
            var type = new SqlCharacterType(code, maxSize, null);
            var s    = new SqlString(value);

            var obj = new SqlObject(type, s);

            Assert.Equal(type, obj.Type);
            Assert.NotNull(obj.Value);
            Assert.Equal(expected, obj.Value.ToString());
            Assert.False(obj.IsNull);
        }
Пример #9
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);
		}
        public static void CreateStringType(SqlTypeCode typeCode, int maxSize, string locale)
        {
            var culture = String.IsNullOrEmpty(locale) ? null : new CultureInfo(locale);
            var type    = new SqlCharacterType(typeCode, maxSize, culture);

            Assert.Equal(typeCode, type.TypeCode);
            Assert.Equal(maxSize, type.MaxSize);
            Assert.Equal(maxSize > 0, type.HasMaxSize);
            Assert.Equal(locale, type.Locale == null ? null : type.Locale.Name);
            Assert.True(type.IsIndexable);
            Assert.False(type.IsReference);
            Assert.False(type.IsLargeObject);
            Assert.True(type.IsPrimitive);
        }
        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);
        }
Пример #12
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");
        }
Пример #13
0
        private ISqlString ToString(SqlBoolean value, SqlCharacterType destType)
        {
            var s = new SqlString(ToString(value));

            return((ISqlString)destType.NormalizeValue(s));
        }
        private ISqlValue ToString(SqlDayToSecond dts, SqlCharacterType destType)
        {
            var s = new SqlString(dts.ToString());

            return(destType.NormalizeValue(s));
        }