Ejemplo n.º 1
0
        /// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// in EDM.
        /// </summary>
        /// <param name="storeType">A TypeUsage encapsulating a store type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating an EDM type and a set of facets</returns>
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            if (storeType == null)
            {
                throw new ArgumentNullException("storeType");
            }

            var storeTypeName = storeType.EdmType.Name.ToLowerInvariant();

            if (!StoreTypeNameToEdmPrimitiveType.ContainsKey(storeTypeName))
            {
                throw new ArgumentException(string.Format("The underlying provider does not support the type '{0}'.", storeTypeName));
            }

            var edmPrimitiveType = base.StoreTypeNameToEdmPrimitiveType[storeTypeName];

            var maxLength   = 0;
            var isUnicode   = true;
            var isFixedLen  = false;
            var isUnbounded = true;

            PrimitiveTypeKind newPrimitiveTypeKind;

            switch (storeTypeName)
            {
            // for some types we just go with simple type usage with no facets
            case "smallint":
            case "int":
            case "bigint":
            case "smallint_bool":
            case "float":
            case "double":
            case "guid":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

            case "decimal":
            case "numeric":
                if (TypeHelpers.TryGetPrecision(storeType, out var precision) && TypeHelpers.TryGetScale(storeType, out var scale))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, precision, scale));
                }
                else
                {
                    return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType));
                }

            case "varchar":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !TypeHelpers.TryGetMaxLength(storeType, out maxLength);
                isUnicode            = true;              //hardcoded
                isFixedLen           = false;
                break;

            case "char":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !TypeHelpers.TryGetMaxLength(storeType, out maxLength);
                isUnicode            = true;              //hardcoded
                isFixedLen           = true;
                break;

            case "timestamp":
                return(TypeUsage.CreateDateTimeTypeUsage(edmPrimitiveType, null));

            case "date":
                return(TypeUsage.CreateDateTimeTypeUsage(edmPrimitiveType, null));

            case "time":
                return(TypeUsage.CreateTimeTypeUsage(edmPrimitiveType, null));

            case "blob":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = true;
                isFixedLen           = false;
                break;

            case "clob":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = true;
                isUnicode            = true;              //hardcoded
                isFixedLen           = false;
                break;

            default:
                throw new NotSupportedException(string.Format("The underlying provider does not support the type '{0}'.", storeTypeName));
            }

            Debug.Assert(newPrimitiveTypeKind == PrimitiveTypeKind.String || newPrimitiveTypeKind == PrimitiveTypeKind.Binary, "at this point only string and binary types should be present");

            switch (newPrimitiveTypeKind)
            {
            case PrimitiveTypeKind.String:
                if (!isUnbounded)
                {
                    return(TypeUsage.CreateStringTypeUsage(edmPrimitiveType, isUnicode, isFixedLen, maxLength));
                }
                else
                {
                    return(TypeUsage.CreateStringTypeUsage(edmPrimitiveType, isUnicode, isFixedLen));
                }

            case PrimitiveTypeKind.Binary:
                if (!isUnbounded)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, isFixedLen, maxLength));
                }
                else
                {
                    return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, isFixedLen));
                }

            default:
                throw new NotSupportedException(string.Format("The underlying provider does not support the type '{0}'.", storeTypeName));
            }
        }
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            ArgumentUtility.CheckNotNull("storeType", storeType);

            var storeTypeName = storeType.EdmType.Name.ToLowerInvariant();

            if (!StoreTypeNameToEdmPrimitiveType.ContainsKey(storeTypeName))
            {
                throw new ArgumentException(string.Format("The underlying provider does not support the type '{0}'.", storeTypeName));
            }

            var primitiveType = StoreTypeNameToEdmPrimitiveType[storeTypeName];
            var maxLength     = 0;
            var isFixedLen    = false;
            var isUnbounded   = true;

            PrimitiveTypeKind newPrimitiveTypeKind;

            switch (storeTypeName)
            {
            case "logical":
            case "int":
            case "integer":
            case "float":
            case "double":
                return(TypeUsage.CreateDefaultTypeUsage(primitiveType));

            case "date":
            case "datetime":
                return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, null));

            case "numeric":
                byte precision;
                byte scale;

                if (storeType.TryGetPrecision(out precision) && storeType.TryGetScale(out scale))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(primitiveType, precision, scale));
                }

                return(TypeUsage.CreateDecimalTypeUsage(primitiveType));

            case "currency":
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType, 19, 4));

            case "varchar":
            case "binaryvarchar":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = false;
                break;

            case "character":
            case "char":
            case "binarychar":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = true;
                break;

            case "guid":
                newPrimitiveTypeKind = PrimitiveTypeKind.Guid;
                isUnbounded          = false;
                isFixedLen           = true;
                break;

            case "memo":
            case "binarymemo":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = true;
                isFixedLen           = false;
                break;

            case "blob":
            case "general":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = true;
                isFixedLen           = false;
                break;

            default:
                throw new NotSupportedException(string.Format("The underlying provider does not support the type '{0}'.", storeTypeName));
            }

            switch (newPrimitiveTypeKind)
            {
            case PrimitiveTypeKind.String:
                if (!isUnbounded)
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, false, isFixedLen, maxLength));
                }

                return(TypeUsage.CreateStringTypeUsage(primitiveType, false, isFixedLen));

            case PrimitiveTypeKind.Binary:
                if (!isUnbounded)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(primitiveType, false, maxLength));
                }

                return(TypeUsage.CreateBinaryTypeUsage(primitiveType, isFixedLen));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(primitiveType));

            default:
                throw new NotSupportedException(string.Format("The underlying provider does not support the type '{0}'.", storeTypeName));
            }
        }