Beispiel #1
0
        internal static SqlUdtInfo GetFromType(Type target)
        {
            SqlUdtInfo udtAttr = TryGetFromType(target);

            if (udtAttr == null)
            {
                throw InvalidUdtException.Create(target, Strings.SqlUdtReason_NoUdtAttribute);
            }
            return(udtAttr);
        }
Beispiel #2
0
        internal static SqlUdtInfo GetFromType(Type target)
        {
            SqlUdtInfo udtAttr = TryGetFromType(target);

            if (udtAttr == null)
            {
                Type       myType    = typeof(InvalidUdtException);
                var        arguments = new Type[] { typeof(Type), typeof(String) };
                MethodInfo Create    = myType.GetMethod("Create", arguments);
                Create.Invoke(null, new object[] { Strings.SqlUdtReason_NoUdtAttribute });
            }
            return(udtAttr);
        }
Beispiel #3
0
        internal static SqlUdtInfo TryGetFromType(Type target)
        {
            if (s_types2UdtInfo == null)
            {
                s_types2UdtInfo = new Dictionary <Type, SqlUdtInfo>();
            }

            if (!s_types2UdtInfo.TryGetValue(target, out SqlUdtInfo udtAttr))
            {
                // query SqlUserDefinedTypeAttribute first time and cache the result
                object[] attr = target.GetCustomAttributes(typeof(SqlUserDefinedTypeAttribute), false);
                if (attr != null && attr.Length == 1)
                {
                    udtAttr = new SqlUdtInfo((SqlUserDefinedTypeAttribute)attr[0]);
                }
                s_types2UdtInfo.Add(target, udtAttr);
            }
            return(udtAttr);
        }
Beispiel #4
0
        //The attribute we are looking for is now moved to an external dll that server provides. If the name is changed.
        //then we we have to make corresponding changes here.
        //please also change sqludcdatetime.cs, sqltime.cs and sqldate.cs

        internal static SqlUdtInfo GetInfoFromType(Type t)
        {
            Debug.Assert(t != null, "Type object cant be NULL");

            Type orig = t;

            do
            {
                SqlUdtInfo attr = SqlUdtInfo.TryGetFromType(t);

                if (attr != null)
                {
                    return(attr);
                }
                else
                {
                    t = t.BaseType;
                }
            }while (t != null);

            throw SQL.UDTInvalidSqlType(orig.AssemblyQualifiedName);
        }
Beispiel #5
0
        private static MetaType GetMetaTypeFromValue(Type dataType, object value, bool inferLen, bool streamAllowed)
        {
            switch (Type.GetTypeCode(dataType))
            {
            case TypeCode.Empty:
                throw ADP.InvalidDataType(TypeCode.Empty);

            case TypeCode.Object:
                if (dataType == typeof(byte[]))
                {
                    // Must not default to image if inferLen is false
                    if (!inferLen || ((byte[])value).Length <= TdsEnums.TYPE_SIZE_LIMIT)
                    {
                        return(MetaVarBinary);
                    }
                    else
                    {
                        return(MetaImage);
                    }
                }
                else if (dataType == typeof(System.Guid))
                {
                    return(s_metaUniqueId);
                }
                else if (dataType == typeof(object))
                {
                    return(s_metaVariant);
                }     // check sql types now
                else if (dataType == typeof(SqlBinary))
                {
                    return(MetaVarBinary);
                }
                else if (dataType == typeof(SqlBoolean))
                {
                    return(s_metaBit);
                }
                else if (dataType == typeof(SqlByte))
                {
                    return(s_metaTinyInt);
                }
                else if (dataType == typeof(SqlBytes))
                {
                    return(MetaVarBinary);
                }
                else if (dataType == typeof(SqlChars))
                {
                    return(MetaNVarChar);
                }
                else if (dataType == typeof(SqlDateTime))
                {
                    return(s_metaDateTime);
                }
                else if (dataType == typeof(SqlDouble))
                {
                    return(s_metaFloat);
                }
                else if (dataType == typeof(SqlGuid))
                {
                    return(s_metaUniqueId);
                }
                else if (dataType == typeof(SqlInt16))
                {
                    return(s_metaSmallInt);
                }
                else if (dataType == typeof(SqlInt32))
                {
                    return(s_metaInt);
                }
                else if (dataType == typeof(SqlInt64))
                {
                    return(s_metaBigInt);
                }
                else if (dataType == typeof(SqlMoney))
                {
                    return(s_metaMoney);
                }
                else if (dataType == typeof(SqlDecimal))
                {
                    return(MetaDecimal);
                }
                else if (dataType == typeof(SqlSingle))
                {
                    return(s_metaReal);
                }
                else if (dataType == typeof(SqlXml))
                {
                    return(MetaXml);
                }
                else if (dataType == typeof(SqlString))
                {
                    return((inferLen && !((SqlString)value).IsNull)
                            ? PromoteStringType(((SqlString)value).Value)
                            : MetaNVarChar);
                }
                else if (dataType == typeof(IEnumerable <DbDataRecord>) || dataType == typeof(DataTable))
                {
                    return(s_metaTable);
                }
                else if (dataType == typeof(TimeSpan))
                {
                    return(MetaTime);
                }
                else if (dataType == typeof(DateTimeOffset))
                {
                    return(MetaDateTimeOffset);
                }
                else
                {
                    // UDT ?
                    SqlUdtInfo attribs = SqlUdtInfo.TryGetFromType(dataType);
                    if (attribs != null)
                    {
                        return(MetaUdt);
                    }
                    if (streamAllowed)
                    {
                        // Derived from Stream ?
                        if (typeof(Stream).IsAssignableFrom(dataType))
                        {
                            return(MetaVarBinary);
                        }
                        // Derived from TextReader ?
                        else if (typeof(TextReader).IsAssignableFrom(dataType))
                        {
                            return(MetaNVarChar);
                        }
                        // Derived from XmlReader ?
                        else if (typeof(System.Xml.XmlReader).IsAssignableFrom(dataType))
                        {
                            return(MetaXml);
                        }
                    }
                }
                throw ADP.UnknownDataType(dataType);

            case TypeCode.DBNull:
                throw ADP.InvalidDataType(TypeCode.DBNull);

            case TypeCode.Boolean:
                return(s_metaBit);

            case TypeCode.Char:
                throw ADP.InvalidDataType(TypeCode.Char);

            case TypeCode.SByte:
                throw ADP.InvalidDataType(TypeCode.SByte);

            case TypeCode.Byte:
                return(s_metaTinyInt);

            case TypeCode.Int16:
                return(s_metaSmallInt);

            case TypeCode.UInt16:
                throw ADP.InvalidDataType(TypeCode.UInt16);

            case TypeCode.Int32:
                return(s_metaInt);

            case TypeCode.UInt32:
                throw ADP.InvalidDataType(TypeCode.UInt32);

            case TypeCode.Int64:
                return(s_metaBigInt);

            case TypeCode.UInt64:
                throw ADP.InvalidDataType(TypeCode.UInt64);

            case TypeCode.Single:
                return(s_metaReal);

            case TypeCode.Double:
                return(s_metaFloat);

            case TypeCode.Decimal:
                return(MetaDecimal);

            case TypeCode.DateTime:
                return(s_metaDateTime);

            case TypeCode.String:
                return(inferLen ? PromoteStringType((string)value) : MetaNVarChar);

            default:
                throw ADP.UnknownDataTypeCode(dataType, Type.GetTypeCode(dataType));
            }
        }