Exemple #1
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");
        }
        private static SqlType GetSqlType(ISqlValue value)
        {
            if (value == null ||
                SqlNull.Value == value)
            {
                throw new ArgumentException();
            }

            if (value is SqlNumber)
            {
                var number = (SqlNumber)value;

                if (number.CanBeInt32)
                {
                    return(PrimitiveTypes.Integer());
                }
                if (number.CanBeInt64)
                {
                    return(PrimitiveTypes.BigInt());
                }

                if (number.Precision == SqlNumericType.FloatPrecision)
                {
                    return(new SqlNumericType(SqlTypeCode.Float, number.Precision, number.Scale));
                }
                if (number.Precision == SqlNumericType.DoublePrecision)
                {
                    return(new SqlNumericType(SqlTypeCode.Double, number.Precision, number.Scale));
                }
                if (number.Precision == SqlNumericType.DecimalPrecision)
                {
                    return(new SqlNumericType(SqlTypeCode.Decimal, number.Precision, number.Scale));
                }

                return(PrimitiveTypes.Numeric(number.Precision, number.Scale));
            }

            if (value is ISqlString)
            {
                // TODO: support the long string
                var length = ((ISqlString)value).Length;

                return(PrimitiveTypes.VarChar((int)length));
            }

            if (value is SqlBinary)
            {
                var bin = (SqlBinary)value;

                return(PrimitiveTypes.VarBinary((int)bin.Length));
            }

            if (value is SqlDateTime)
            {
                return(PrimitiveTypes.TimeStamp());
            }

            if (value is SqlBoolean)
            {
                return(PrimitiveTypes.Boolean());
            }

            if (value is SqlYearToMonth)
            {
                return(PrimitiveTypes.YearToMonth());
            }
            if (value is SqlDayToSecond)
            {
                return(PrimitiveTypes.DayToSecond());
            }

            if (value is SqlArray)
            {
                return(PrimitiveTypes.Array(((SqlArray)value).Length));
            }

            throw new NotSupportedException();
        }