示例#1
0
        private static TypeUsage GetTypeUsage(EdmType edmType, SimpleTypeDescriptor simpleTypeDescriptor)
        {
            if (edmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType)
            {
                PrimitiveType primitiveType = (PrimitiveType)edmType;
                switch (primitiveType.PrimitiveTypeKind)
                {
                case PrimitiveTypeKind.Binary:
                    return(simpleTypeDescriptor.Length.HasValue && simpleTypeDescriptor.Length.Value >= 0 ?
                           TypeUsage.CreateBinaryTypeUsage(primitiveType, simpleTypeDescriptor.IsFixedLength.HasValue && simpleTypeDescriptor.IsFixedLength.Value, simpleTypeDescriptor.Length.Value) :
                           simpleTypeDescriptor.IsFixedLength.HasValue?TypeUsage.CreateBinaryTypeUsage(primitiveType, simpleTypeDescriptor.IsFixedLength.Value) : TypeUsage.CreateDefaultTypeUsage(primitiveType));

                case PrimitiveTypeKind.String:
                    return(simpleTypeDescriptor.Length.HasValue && simpleTypeDescriptor.Length.Value >= 0 ?
                           TypeUsage.CreateStringTypeUsage(primitiveType, true, simpleTypeDescriptor.IsFixedLength.HasValue && simpleTypeDescriptor.IsFixedLength.Value, simpleTypeDescriptor.Length.Value) :
                           simpleTypeDescriptor.IsFixedLength.HasValue?TypeUsage.CreateStringTypeUsage(primitiveType, true, simpleTypeDescriptor.IsFixedLength.Value) : TypeUsage.CreateDefaultTypeUsage(primitiveType));

                case PrimitiveTypeKind.Decimal:
                    return(simpleTypeDescriptor.Precision.HasValue && simpleTypeDescriptor.Scale.HasValue ?
                           TypeUsage.CreateDecimalTypeUsage(primitiveType, simpleTypeDescriptor.Precision.Value, simpleTypeDescriptor.Scale.Value) : TypeUsage.CreateDefaultTypeUsage(primitiveType));

                case PrimitiveTypeKind.DateTimeOffset:
                    return(TypeUsage.CreateDateTimeOffsetTypeUsage(primitiveType, simpleTypeDescriptor.Precision));

                case PrimitiveTypeKind.Time:
                    return(TypeUsage.CreateTimeTypeUsage(primitiveType, simpleTypeDescriptor.Precision));
                }
            }
            return(TypeUsage.CreateDefaultTypeUsage(edmType));
        }
 private TypeUsage CreateBinaryTypeUsage(PrimitiveType edmPrimitiveType, bool isFixedLen, int?maxLength = null)
 {
     if (maxLength == null)
     {
         return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, isFixedLen));
     }
     return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, isFixedLen, maxLength.Value));
 }
示例#3
0
        public EdmProperty BuildBinary(string name, bool isFixedLength)
        {
            var typeUsage = TypeUsage.CreateBinaryTypeUsage(
                PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Binary),
                isFixedLength);

            var property = EdmProperty.Create(
                name,
                typeUsage);

            return(property);
        }
示例#4
0
        public void GetStoreType_returns_correct_type_usages_for_specific_Binary_type_usages()
        {
            TypeUsageVerificationHelper.VerifyTypeUsagesEquivalent(
                LegacyProviderManifest.GetStoreType(
                    LegacyMetadata.TypeUsage.CreateBinaryTypeUsage(LegacyEdmPrimitiveTypes["Binary"], isFixedLength: false)),
                ProviderManifestWrapper.GetStoreType(
                    TypeUsage.CreateBinaryTypeUsage(EdmPrimitiveTypes["Binary"], isFixedLength: false)));

            TypeUsageVerificationHelper.VerifyTypeUsagesEquivalent(
                LegacyProviderManifest.GetStoreType(
                    LegacyMetadata.TypeUsage.CreateBinaryTypeUsage(LegacyEdmPrimitiveTypes["Binary"], isFixedLength: true)),
                ProviderManifestWrapper.GetStoreType(
                    TypeUsage.CreateBinaryTypeUsage(EdmPrimitiveTypes["Binary"], isFixedLength: true)));
        }
        public static void VisitDbIsNullExpression_variable_size_binary_parameter_with_max_length_is_not_cast()
        {
            var primitiveType    = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Binary);
            var typeUsage        = TypeUsage.CreateBinaryTypeUsage(primitiveType, isFixedLength: false, maxLength: 100);
            var isNullExpression = typeUsage.Parameter("parameterName").IsNull();

            var sqlGenerator = new SqlGenerator();
            var sqlFragment  = sqlGenerator.Visit(isNullExpression);

            var builder = new StringBuilder();

            using (var writer = new SqlWriter(builder))
            {
                sqlFragment.WriteSql(writer, sqlGenerator);
            }

            Assert.Equal("@parameterName IS NULL", builder.ToString());
        }
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            if (storeType == null)
            {
                throw new ArgumentNullException("storeType");
            }

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

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

            PrimitiveType edmPrimitiveType = base.StoreTypeNameToEdmPrimitiveType[storeTypeName];

            if (edmPrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.Binary)
            {
                return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, false));
            }

            if (edmPrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.String)
            {
                Facet facet;
                if (storeType.Facets.TryGetValue("MaxLength", false, out facet) && !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateStringTypeUsage(edmPrimitiveType, false, false, (int)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateStringTypeUsage(edmPrimitiveType, false, false));
                }
            }

            return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));
        }
        /// <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");
            }

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

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

            PrimitiveType edmPrimitiveType = base.StoreTypeNameToEdmPrimitiveType[storeTypeName];

            int  maxLength   = 0;
            bool isUnicode   = true;
            bool isFixedLen  = false;
            bool isUnbounded = true;

            PrimitiveTypeKind newPrimitiveTypeKind;

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

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

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

            case "nvarchar(max)":
            case "varchar(max)":
            case "ntext":
            case "text":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = true;
                isFixedLen           = false;
                break;

            case "binary":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = true;
                break;

            case "varbinary":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = false;
                break;

            case "varbinary(max)":
            case "image":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = true;
                isFixedLen           = false;
                break;

            case "float":
            case "real":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

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

            case "money":
                return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, 19, 4));

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

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

            default:
                throw new NotSupportedException(String.Format("Jet 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("Jet does not support the type '{0}'.", storeTypeName));
            }
        }
示例#8
0
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            if (storeType == null)
            {
                throw new ArgumentNullException("storeType");
            }

            string        storeTypeName = storeType.EdmType.Name;
            PrimitiveType primitiveType = StoreTypeNameToEdmPrimitiveType[storeTypeName];
            // TODO: come up with way to determin if unicode is used
            bool  isUnicode = true;
            Facet facet;

            switch (storeTypeName)
            {
            case "bool":
            case "int2":
            case "int4":
            case "int8":
            case "float4":
            case "float8":
            case "uuid":
                return(TypeUsage.CreateDefaultTypeUsage(primitiveType));

            case "numeric":
            {
                byte scale;
                byte precision;
                if (storeType.Facets.TryGetValue(ScaleFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    scale = (byte)facet.Value;
                    if (storeType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                        !facet.IsUnbounded && facet.Value != null)
                    {
                        precision = (byte)facet.Value;
                        return(TypeUsage.CreateDecimalTypeUsage(primitiveType, precision, scale));
                    }
                }
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType));
            }

            case "bpchar":
                if (storeType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, true, (int)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, true));
                }

            case "varchar":
                if (storeType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, false, (int)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, false));
                }

            case "text":
            case "xml":
                return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, false));

            case "timestamp":
                // TODO: make sure the arguments are correct here
                if (storeType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, null));
                }

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

            case "timestamptz":
                if (storeType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateDateTimeOffsetTypeUsage(primitiveType, (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateDateTimeOffsetTypeUsage(primitiveType, null));
                }

            case "time":
            case "interval":
                if (storeType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateTimeTypeUsage(primitiveType, (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateTimeTypeUsage(primitiveType, null));
                }

            case "bytea":
            {
                if (storeType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(primitiveType, false, (int)facet.Value));
                }
                return(TypeUsage.CreateBinaryTypeUsage(primitiveType, false));
            }

            case "rowversion":
            {
                return(TypeUsage.CreateBinaryTypeUsage(primitiveType, true, 8));
            }
                //TypeUsage.CreateBinaryTypeUsage
                //TypeUsage.CreateDateTimeTypeUsage
                //TypeUsage.CreateDecimalTypeUsage
                //TypeUsage.CreateStringTypeUsage
            }
            throw new NotSupportedException("Not supported store type: " + storeTypeName);
        }
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            Check.NotNull(storeType, "storeType");

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

            if (!base.StoreTypeNameToEdmPrimitiveType.ContainsKey(storeTypeName))
            {
                throw new ArgumentException(Strings.ProviderDoesNotSupportType(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 "tinyint":
            case "smallint":
            case "bigint":
            case "bit":
            case "uniqueidentifier":
            case "int":
            case "geography":
            case "geometry":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

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

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

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

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

            case "varchar(max)":
            case "text":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = true;
                isUnicode            = false;
                isFixedLen           = false;
                break;

            case "nvarchar(max)":
            case "ntext":
            case "xml":
                newPrimitiveTypeKind = PrimitiveTypeKind.String;
                isUnbounded          = true;
                isUnicode            = true;
                isFixedLen           = false;
                break;

            case "binary":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = true;
                break;

            case "varbinary":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLen           = false;
                break;

            case "varbinary(max)":
            case "image":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = true;
                isFixedLen           = false;
                break;

            case "timestamp":
            case "rowversion":
                return(TypeUsage.CreateBinaryTypeUsage(edmPrimitiveType, true, 8));

            case "float":
            case "real":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

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

            case "money":
                return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, 19, 4));

            case "smallmoney":
                return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType, 10, 4));

            case "datetime":
            case "datetime2":
            case "smalldatetime":
                return(TypeUsage.CreateDateTimeTypeUsage(edmPrimitiveType, null));

            case "date":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

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

            case "datetimeoffset":
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(edmPrimitiveType, null));

            default:
                throw new NotSupportedException(Strings.ProviderDoesNotSupportType(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(Strings.ProviderDoesNotSupportType(storeTypeName));
            }
        }
        /// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// </summary>
        /// <param name="edmType">A TypeUsage encapsulating an EDM type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating a store type and a set of facets</returns>
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format("SQLite does not support the type '{0}'.", edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["integer"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["uniqueidentifier"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Decimal: // decimal, numeric, smallmoney, money
            {
                byte precision;
                if (!TypeHelpers.TryGetPrecision(edmType, out precision))
                {
                    precision = 18;
                }

                byte scale;
                if (!TypeHelpers.TryGetScale(edmType, out scale))
                {
                    scale = 0;
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.Binary: // binary, varbinary, varbinary(max), image, timestamp, rowversion
            {
                bool  isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                Facet f             = facets["MaxLength"];

                bool isMaxLength = f.IsUnbounded || null == f.Value || (int)f.Value > Int32.MaxValue;
                int  maxLength   = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;
                if (isFixedLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["blob"], true, maxLength);
                }
                else
                {
                    if (isMaxLength)
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["blob"], false);
                    }
                    else
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["blob"], false, maxLength);
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.String: // char, nchar, varchar, nvarchar, varchar(max), nvarchar(max), ntext, text
            {
                bool  isUnicode     = null == facets["Unicode"].Value || (bool)facets["Unicode"].Value;
                bool  isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                Facet f             = facets["MaxLength"];
                // maxlen is true if facet value is unbounded, the value is bigger than the limited string sizes *or* the facet
                // value is null. this is needed since functions still have maxlength facet value as null
                bool isMaxLength = f.IsUnbounded || null == f.Value || (int)f.Value > (isUnicode ? Int32.MaxValue : Int32.MaxValue);
                int  maxLength   = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;

                if (isUnicode)
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["nchar"], true, true, maxLength);
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["nvarchar"], true, false);
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, maxLength);
                        }
                    }
                }
                else
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["char"], false, true, maxLength);
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], false, false);
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength);
                        }
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.DateTime: // datetime, smalldatetime
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            default:
                throw new NotSupportedException(String.Format("There is no store type corresponding to the EDM type '{0}' of primitive type '{1}'.", edmType, primitiveType.PrimitiveTypeKind));
            }
        }
示例#11
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 GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format(EntityFramework.Properties.Resources.TypeNotSupported, edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bool"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["utinyint"]));

            case PrimitiveTypeKind.SByte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["guid"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["double"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Decimal:
            {
                byte  precision = DEFAULT_DECIMAL_PRECISION;
                byte  scale     = DEFAULT_DECIMAL_SCALE;
                Facet facet;

                if (edmType.Facets.TryGetValue("Precision", false, out facet))
                {
                    if (!facet.IsUnbounded && facet.Value != null)
                    {
                        precision = (byte)facet.Value;
                    }
                }

                if (edmType.Facets.TryGetValue("Scale", false, out facet))
                {
                    if (!facet.IsUnbounded && facet.Value != null)
                    {
                        scale = (byte)facet.Value;
                    }
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.Binary:
            {
                bool  isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                Facet f             = facets["MaxLength"];
                bool  isMaxLength   = f.IsUnbounded || null == f.Value || (int)f.Value > MEDIUMBLOB_MAXLEN;
                int   maxLength     = !isMaxLength ? (int)f.Value : LONGBLOB_MAXLEN;

                string typeName = String.Empty;

                // now this applies for both isFixedLength and !isFixedLength
                if (maxLength < CHAR_MAXLEN)
                {
                    typeName = "tinyblob";
                }
                else if (maxLength < MEDIUMBLOB_MAXLEN)
                {
                    typeName = "blob";
                }
                else if (maxLength < LONGTEXT_MAXLEN)
                {
                    typeName = "mediumblob";
                }
                else
                {
                    typeName = "longblob";
                }

                return(TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isFixedLength, maxLength));
            }

            case PrimitiveTypeKind.String:
            {
                string typeName      = String.Empty;
                bool   isUnicode     = null != facets["Unicode"].Value && (bool)facets["Unicode"].Value;
                bool   isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                int    maxLenghtValue;

                Facet maxLengthFacet = facets["MaxLength"];
                if (isFixedLength)
                {
                    typeName = isUnicode ? "nchar" : "char";
                    if (maxLengthFacet.Value != null && Int32.TryParse(maxLengthFacet.Value.ToString(), out maxLenghtValue) && maxLenghtValue <= CHAR_MAXLEN)
                    {
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isUnicode, isFixedLength, (int)maxLengthFacet.Value));
                    }
                    else if (maxLengthFacet.Value != null && maxLengthFacet.Value.ToString() == "Max")
                    {
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isUnicode, isFixedLength, CHAR_MAXLEN));
                    }
                    else
                    {
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isUnicode, isFixedLength));
                    }
                }
                else
                {
                    typeName = isUnicode ? "nvarchar" : "varchar";
                    if (maxLengthFacet.Value != null && Int32.TryParse(maxLengthFacet.Value.ToString(), out maxLenghtValue))
                    {
                        if (maxLenghtValue > VARCHAR_MAXLEN && maxLenghtValue <= MEDIUMTEXT_MAXLEN)
                        {
                            typeName = "mediumtext";
                        }
                        else if ((int)maxLengthFacet.Value > MEDIUMTEXT_MAXLEN)
                        {
                            typeName = "longtext";
                        }
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isUnicode, isFixedLength, (int)maxLengthFacet.Value));
                    }
                    else
                    {
                        return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["longtext"], isUnicode, isFixedLength, LONGBLOB_MAXLEN));
                    }
                }
            }

            case PrimitiveTypeKind.DateTimeOffset:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["timestamp"]));

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.Time:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["time"]));

            case PrimitiveTypeKind.Geometry:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["geometry"]));

            default:
                throw new NotSupportedException(String.Format(EntityFramework.Properties.Resources.NoStoreTypeForEdmType, edmType, primitiveType.PrimitiveTypeKind));
            }
        }
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            bool bUse32DataTypes = ODTSettings.m_bUse32DataTypes;

            if (EFProviderSettings.s_tracingEnabled)
            {
                EFProviderSettings.Instance.Trace(EFProviderSettings.EFTraceLevel.Entry, " (ENTRY) EFOracleProviderManifest::GetStoreType() \n");
            }
            EntityUtils.CheckArgumentNull <TypeUsage>(edmType, nameof(edmType));
            PrimitiveType edmType1 = edmType.EdmType as PrimitiveType;

            if (edmType1 == null)
            {
                throw new ArgumentException(EFProviderSettings.Instance.GetErrorMessage(-1703, "Oracle Data Provider for .NET", edmType.EdmType.FullName));
            }
            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            if (EFProviderSettings.s_tracingEnabled)
            {
                EFProviderSettings.Instance.Trace(EFProviderSettings.EFTraceLevel.Entry, " (EXIT) EFOracleProviderManifest::GetStoreType() \n");
            }
            switch (edmType1.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Binary:
                bool  flag1      = facets["FixedLength"].Value != null && (bool)facets["FixedLength"].Value;
                Facet facet1     = facets["MaxLength"];
                bool  flag2      = !bUse32DataTypes ? facet1.IsUnbounded || facet1.Value == null || (int)facet1.Value > 2000 : facet1.IsUnbounded || facet1.Value == null || (int)facet1.Value > this.BinaryMaxSize_12c;
                int   maxLength1 = !flag2 ? (int)facet1.Value : int.MinValue;
                return(!flag1 ? (!flag2 ? TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["raw"], false, maxLength1) : TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["blob"], false)) : (!bUse32DataTypes ? TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["raw"], true, flag2 ? 2000 : maxLength1) : TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["raw"], true, flag2 ? this.BinaryMaxSize_12c : maxLength1)));

            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["number"], (byte)EFOracleProviderManifest.m_edmMappingMaxBOOL, (byte)0));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["number"], (byte)EFOracleProviderManifest.m_edmMappingMaxBYTE, (byte)0));

            case PrimitiveTypeKind.DateTime:
                if (facets == null || facets["Precision"].Value == null)
                {
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["date"]));
                }
                if ((int)(byte)facets["Precision"].Value > 9)
                {
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["timestamp with local time zone"]));
                }
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["timestamp"]));

            case PrimitiveTypeKind.Decimal:
                byte precision;
                byte scale;
                if (EF6MetadataHelpers.TryGetPrecision(edmType, out precision) && EF6MetadataHelpers.TryGetScale(edmType, out scale))
                {
                    if ((int)precision == 250 && (int)scale == 0)
                    {
                        return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["interval year to month"], (byte)9, (byte)0));
                    }
                    if ((int)precision == 251 && (int)scale == 0)
                    {
                        return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["interval day to second"], (byte)9, (byte)0));
                    }
                    return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["number"], precision, scale));
                }
                if (EF6MetadataHelpers.TryGetPrecision(edmType, out precision))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["number"], precision, (byte)0));
                }
                if (EF6MetadataHelpers.TryGetScale(edmType, out scale))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["number"], (byte)38, scale));
                }
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["number"]));

            case PrimitiveTypeKind.Double:
                if (this._version < EFOracleVersion.Oracle10gR1)
                {
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["number"]));
                }
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["binary_double"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["guid raw"]));

            case PrimitiveTypeKind.Single:
                if (this._version < EFOracleVersion.Oracle10gR1)
                {
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["number"]));
                }
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["binary_float"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["number"], (byte)EFOracleProviderManifest.m_edmMappingMaxINT16, (byte)0));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["number"], (byte)EFOracleProviderManifest.m_edmMappingMaxINT32, (byte)0));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["number"], (byte)EFOracleProviderManifest.m_edmMappingMaxINT64, (byte)0));

            case PrimitiveTypeKind.String:
                bool  flag3      = facets["Unicode"].Value == null || (bool)facets["Unicode"].Value;
                bool  flag4      = facets["FixedLength"].Value != null && (bool)facets["FixedLength"].Value;
                Facet facet2     = facets["MaxLength"];
                bool  flag5      = !bUse32DataTypes ? facet2.IsUnbounded || facet2.Value == null || (int)facet2.Value > (flag3 ? 4000 : 4000) : facet2.IsUnbounded || facet2.Value == null || (int)facet2.Value > (flag3 ? this.Nvarchar2MaxSize_12c : this.Varchar2MaxSize_12c);
                int   maxLength2 = !flag5 ? (int)facet2.Value : int.MinValue;
                return(!flag3 ? (!flag4 ? (!flag5 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["varchar2"], false, false, maxLength2) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["clob"], false, false)) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["char"], false, true, flag5 ? 2000 : maxLength2)) : (!flag4 ? (!flag5 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nvarchar2"], true, false, maxLength2) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nclob"], true, false)) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nchar"], true, true, flag5 ? 2000 : maxLength2)));

            case PrimitiveTypeKind.DateTimeOffset:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["timestamp with time zone"]));

            default:
                throw new NotSupportedException(EFProviderSettings.Instance.GetErrorMessage(-1703, "Oracle Data Provider for .NET", edmType1.PrimitiveTypeKind.ToString()));
            }
        }
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            if (EFProviderSettings.s_tracingEnabled)
            {
                EFProviderSettings.Instance.Trace(EFProviderSettings.EFTraceLevel.Entry, " (ENTRY) EFOracleProviderManifest::GetEdmType()\n");
            }
            EntityUtils.CheckArgumentNull <TypeUsage>(storeType, nameof(storeType));
            string lowerInvariant = storeType.EdmType.Name.ToLowerInvariant();

            if (!this.StoreTypeNameToEdmPrimitiveType.ContainsKey(lowerInvariant))
            {
                throw new ArgumentException(EFProviderSettings.Instance.GetErrorMessage(-1703, "Oracle Data Provider for .NET", lowerInvariant));
            }
            PrimitiveType primitiveType = this.StoreTypeNameToEdmPrimitiveType[lowerInvariant];
            int           maxLength     = 0;
            bool          isUnicode     = true;

            if (EFProviderSettings.s_tracingEnabled)
            {
                EFProviderSettings.Instance.Trace(EFProviderSettings.EFTraceLevel.Entry, " (EXIT) EFOracleProviderManifest::GetEdmType()\n");
            }
            PrimitiveTypeKind primitiveTypeKind1;
            bool flag;
            bool isFixedLength;

            switch (lowerInvariant)
            {
            case "int":
            case "smallint":
            case "binary_integer":
            case "pl/sql boolean":
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)primitiveType));

            case "mlslabel":
                return(TypeUsage.CreateBinaryTypeUsage(primitiveType, true, 12345));

            case "varchar2":
                primitiveTypeKind1 = PrimitiveTypeKind.String;
                flag          = !EF6MetadataHelpers.TryGetMaxLength(storeType, out maxLength);
                isUnicode     = false;
                isFixedLength = false;
                break;

            case "char":
                primitiveTypeKind1 = PrimitiveTypeKind.String;
                flag          = !EF6MetadataHelpers.TryGetMaxLength(storeType, out maxLength);
                isUnicode     = false;
                isFixedLength = true;
                break;

            case "nvarchar2":
                primitiveTypeKind1 = PrimitiveTypeKind.String;
                flag          = !EF6MetadataHelpers.TryGetMaxLength(storeType, out maxLength);
                isUnicode     = true;
                isFixedLength = false;
                break;

            case "nchar":
                primitiveTypeKind1 = PrimitiveTypeKind.String;
                flag          = !EF6MetadataHelpers.TryGetMaxLength(storeType, out maxLength);
                isUnicode     = true;
                isFixedLength = true;
                break;

            case "clob":
            case "long":
                primitiveTypeKind1 = PrimitiveTypeKind.String;
                flag          = true;
                isUnicode     = false;
                isFixedLength = false;
                break;

            case "xmltype":
            case "nclob":
                primitiveTypeKind1 = PrimitiveTypeKind.String;
                flag          = true;
                isUnicode     = true;
                isFixedLength = false;
                break;

            case "blob":
            case "bfile":
                primitiveTypeKind1 = PrimitiveTypeKind.Binary;
                flag          = true;
                isFixedLength = false;
                break;

            case "raw":
                primitiveTypeKind1 = PrimitiveTypeKind.Binary;
                flag          = !EF6MetadataHelpers.TryGetMaxLength(storeType, out maxLength);
                isFixedLength = false;
                if (maxLength == 16)
                {
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Guid)));
                }
                break;

            case "long raw":
                primitiveTypeKind1 = PrimitiveTypeKind.Binary;
                flag          = !EF6MetadataHelpers.TryGetMaxLength(storeType, out maxLength);
                isFixedLength = false;
                break;

            case "guid raw":
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Guid)));

            case "guid":
            case "binary_float":
            case "binary_double":
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)primitiveType));

            case "rowid":
            case "urowid":
                primitiveTypeKind1 = PrimitiveTypeKind.String;
                flag          = !EF6MetadataHelpers.TryGetMaxLength(storeType, out maxLength);
                isUnicode     = false;
                isFixedLength = false;
                break;

            case "float":
                byte precision1;
                byte scale1;
                if (!EF6MetadataHelpers.TryGetPrecision(storeType, out precision1) || !EF6MetadataHelpers.TryGetScale(storeType, out scale1))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(primitiveType));
                }
                byte precision2 = byte.Parse(((int)((double)Convert.ToInt32(precision1) * 0.30103 + 1.0)).ToString());
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType, precision2, scale1));

            case "odp_internal_use_type":
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Boolean)));

            case "number":
                byte precision3;
                byte scale2;
                if (!EF6MetadataHelpers.TryGetPrecision(storeType, out precision3) || !EF6MetadataHelpers.TryGetScale(storeType, out scale2))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(primitiveType));
                }
                if (!ConfigBaseClass.s_bLegacyEdmMappingPresent && (int)scale2 == 0)
                {
                    if ((int)precision3 < 0 || (int)precision3 > 38)
                    {
                        precision3 = (byte)0;
                    }
                    DbType key = ConfigBaseClass.s_edmPrecisonMapping[(int)precision3];
                    if (EFOracleProviderManifest.s_DbTypeToPrimitiveTypeKind.ContainsKey(key))
                    {
                        PrimitiveTypeKind primitiveTypeKind2;
                        EFOracleProviderManifest.s_DbTypeToPrimitiveTypeKind.TryGetValue(key, out primitiveTypeKind2);
                        if (primitiveTypeKind2 == PrimitiveTypeKind.Boolean || primitiveTypeKind2 == PrimitiveTypeKind.Byte || (primitiveTypeKind2 == PrimitiveTypeKind.Int16 || primitiveTypeKind2 == PrimitiveTypeKind.Int32) || primitiveTypeKind2 == PrimitiveTypeKind.Int64)
                        {
                            return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(primitiveTypeKind2)));
                        }
                        if (primitiveTypeKind2 == PrimitiveTypeKind.Decimal)
                        {
                            return(TypeUsage.CreateDecimalTypeUsage(primitiveType, precision3, scale2));
                        }
                    }
                }
                if ((int)precision3 == 1 && (int)scale2 == 0)
                {
                    if (EFOracleProviderManifest.m_bMapNumberToBoolean && (int)precision3 <= (int)(byte)EFOracleProviderManifest.m_edmMappingMaxBOOL)
                    {
                        return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Boolean)));
                    }
                    if (EFOracleProviderManifest.m_bMapNumberToByte && (int)precision3 <= (int)(byte)EFOracleProviderManifest.m_edmMappingMaxBYTE)
                    {
                        return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Byte)));
                    }
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int16)));
                }
                if (EFOracleProviderManifest.m_bMapNumberToByte && (int)scale2 == 0 && (int)precision3 <= (int)(byte)EFOracleProviderManifest.m_edmMappingMaxBYTE)
                {
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Byte)));
                }
                if ((int)scale2 == 0 && (int)precision3 <= (int)(byte)EFOracleProviderManifest.m_edmMappingMaxINT16)
                {
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int16)));
                }
                if ((int)scale2 == 0 && (int)precision3 <= (int)(byte)EFOracleProviderManifest.m_edmMappingMaxINT32)
                {
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)));
                }
                if ((int)scale2 == 0 && (int)precision3 <= (int)(byte)EFOracleProviderManifest.m_edmMappingMaxINT64)
                {
                    return(TypeUsage.CreateDefaultTypeUsage((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64)));
                }
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType, precision3, scale2));

            case "date":
                return(TypeUsage.CreateDateTimeTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.DateTime), new byte?()));

            case "timestamp":
                byte byteValue;
                if (EF6MetadataHelpers.TryGetByteFacetValue(storeType, "Precision", out byteValue))
                {
                    return(TypeUsage.CreateDateTimeTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.DateTime), new byte?(byteValue)));
                }
                return(TypeUsage.CreateDateTimeTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.DateTime), new byte?((byte)9)));

            case "timestamp with time zone":
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.DateTimeOffset), new byte?((byte)9)));

            case "timestamp with local time zone":
                return(TypeUsage.CreateDateTimeTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.DateTime), new byte?(byte.MaxValue)));

            case "interval year to month":
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType, (byte)250, (byte)0));

            case "interval day to second":
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType, (byte)251, (byte)0));

            default:
                throw new NotSupportedException(EFProviderSettings.Instance.GetErrorMessage(-1703, "Oracle Data Provider for .NET", lowerInvariant));
            }
            switch (primitiveTypeKind1)
            {
            case PrimitiveTypeKind.Binary:
                if (!flag)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(primitiveType, isFixedLength, maxLength));
                }
                return(TypeUsage.CreateBinaryTypeUsage(primitiveType, isFixedLength));

            case PrimitiveTypeKind.String:
                if (!flag)
                {
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, isFixedLength, maxLength));
                }
                return(TypeUsage.CreateStringTypeUsage(primitiveType, isUnicode, isFixedLength));

            default:
                throw new NotSupportedException(EFProviderSettings.Instance.GetErrorMessage(-1703, "Oracle Data Provider for .NET", lowerInvariant));
            }
        }
        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));
            }
        }
示例#16
0
        /// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// in SQL Server, taking the store version into consideration.
        /// </summary>
        /// <param name="storeType">A TypeUsage encapsulating an EDM type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating a store type and a set of facets</returns>
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }
            System.Diagnostics.Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format("The underlying provider does not support the type '{0}'.", edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["uniqueidentifier"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Decimal:     // decimal, numeric, smallmoney, money
            {
                byte precision;
                if (!TypeHelpers.TryGetPrecision(edmType, out precision))
                {
                    precision = 18;
                }

                byte scale;
                if (!TypeHelpers.TryGetScale(edmType, out scale))
                {
                    scale = 0;
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.Binary:     // binary, varbinary, varbinary(max), image, timestamp, rowversion
            {
                bool  isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                Facet f             = facets["MaxLength"];

                bool isMaxLength = f.IsUnbounded || null == f.Value || (int)f.Value > binaryMaxSize;
                int  maxLength   = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;
                if (isFixedLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["binary"], true, maxLength);
                }
                else
                {
                    if (isMaxLength)
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary(max)"], false);
                        System.Diagnostics.Debug.Assert(tu.Facets["MaxLength"].Description.IsConstant, "varbinary(max) is not constant!");
                    }
                    else
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary"], false, maxLength);
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.String:
                // char, nchar, varchar, nvarchar, varchar(max), nvarchar(max), ntext, text, xml
            {
                bool  isUnicode     = null == facets["Unicode"].Value || (bool)facets["Unicode"].Value;
                bool  isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                Facet f             = facets["MaxLength"];
                // maxlen is true if facet value is unbounded, the value is bigger than the limited string sizes *or* the facet
                // value is null. this is needed since functions still have maxlength facet value as null
                bool isMaxLength = f.IsUnbounded || null == f.Value || (int)f.Value > (isUnicode ? nvarcharMaxSize : varcharMaxSize);
                int  maxLength   = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;

                if (isUnicode)
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["nchar"], true, true, maxLength);
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["nvarchar(max)"], true, false);
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, maxLength);
                        }
                    }
                }
                else
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["char"], false, true, maxLength);
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar(max)"], false, false);
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength);
                        }
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.DateTime:     // datetime, smalldatetime

                Facet preserveSecondsFacet;
                bool  preserveSeconds;
                if (edmType.Facets.TryGetValue("PreserveSeconds", true, out preserveSecondsFacet) && null != preserveSecondsFacet.Value)
                {
                    preserveSeconds = (bool)preserveSecondsFacet.Value;
                }
                else
                {
                    preserveSeconds = true;
                }

                return(TypeUsage.CreateDefaultTypeUsage(preserveSeconds ? StoreTypeNameToStorePrimitiveType["datetime"] : StoreTypeNameToStorePrimitiveType["smalldatetime"]));

            case PrimitiveTypeKind.Geography:
            case PrimitiveTypeKind.GeographyPoint:
            case PrimitiveTypeKind.GeographyLineString:
            case PrimitiveTypeKind.GeographyPolygon:
            case PrimitiveTypeKind.GeographyMultiPoint:
            case PrimitiveTypeKind.GeographyMultiLineString:
            case PrimitiveTypeKind.GeographyMultiPolygon:
            case PrimitiveTypeKind.GeographyCollection:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["geography"]));

            case PrimitiveTypeKind.Geometry:
            case PrimitiveTypeKind.GeometryPoint:
            case PrimitiveTypeKind.GeometryLineString:
            case PrimitiveTypeKind.GeometryPolygon:
            case PrimitiveTypeKind.GeometryMultiPoint:
            case PrimitiveTypeKind.GeometryMultiLineString:
            case PrimitiveTypeKind.GeometryMultiPolygon:
            case PrimitiveTypeKind.GeometryCollection:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["geometry"]));

            default:
                throw new NotSupportedException(String.Format("There is no store type corresponding to the EDM type '{0}' of primitive type '{1}'.", edmType, primitiveType.PrimitiveTypeKind));
            }
        }
示例#17
0
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            System.Diagnostics.Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format("The underlying provider does not support the type '{0}'.", edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;
            Facet f;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["boolean"]));

            case PrimitiveTypeKind.Byte:
            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["integer"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["double"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Decimal:     // decimal, numeric
            {
                byte precision = 9;
                if (facets.TryGetValue("Precision", false, out f) && !f.IsUnbounded && f.Value != null)
                {
                    precision = (byte)f.Value;
                }
                byte scale = 0;
                if (facets.TryGetValue("Scale", false, out f) && !f.IsUnbounded && f.Value != null)
                {
                    scale = (byte)f.Value;
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.String:     // char, varchar, text blob
            {
                bool isUnicode = true;
                if (facets.TryGetValue("Unicode", false, out f) && !f.IsUnbounded && f.Value != null)
                {
                    isUnicode = (bool)f.Value;
                }
                bool isFixedLength = true;
                if (facets.TryGetValue("FixedLength", false, out f) && !f.IsUnbounded && f.Value != null)
                {
                    isFixedLength = (bool)f.Value;
                }
                int maxLength = Int32.MinValue;
                if (facets.TryGetValue("MaxLength", false, out f) && !f.IsUnbounded && f.Value != null)
                {
                    maxLength = (int)f.Value;
                }

                PrimitiveType storePrimitiveType = StoreTypeNameToStorePrimitiveType[isFixedLength ? "char" : "varchar"];
                if (maxLength != Int32.MinValue)
                {
                    return(TypeUsage.CreateStringTypeUsage(storePrimitiveType, isUnicode, isFixedLength, maxLength));
                }

                return(TypeUsage.CreateStringTypeUsage(storePrimitiveType, isUnicode, isFixedLength));
            }

            case PrimitiveTypeKind.DateTime:     // datetime, date
            {
                byte precision = 4;
                if (facets.TryGetValue("Precision", false, out f) && !f.IsUnbounded && f.Value != null)
                {
                    precision = (byte)f.Value;
                }

                bool useTimestamp = (precision != 0);

                return(TypeUsage.CreateDefaultTypeUsage(useTimestamp ? StoreTypeNameToStorePrimitiveType["datetime"] : StoreTypeNameToStorePrimitiveType["date"]));
            }

            case PrimitiveTypeKind.Time:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["time"]));

            case PrimitiveTypeKind.Binary:     // blob
            {
                bool isFixedLength = true;
                if (facets.TryGetValue("FixedLength", false, out f) && !f.IsUnbounded && f.Value != null)
                {
                    isFixedLength = (bool)f.Value;
                }
                int maxLength = Int32.MinValue;
                if (facets.TryGetValue("MaxLength", false, out f) && !f.IsUnbounded && f.Value != null)
                {
                    maxLength = (int)f.Value;
                }

                PrimitiveType storePrimitiveType = StoreTypeNameToStorePrimitiveType["binary"];
                if (maxLength != Int32.MinValue)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(storePrimitiveType, isFixedLength, maxLength));
                }

                return(TypeUsage.CreateBinaryTypeUsage(storePrimitiveType, isFixedLength));
            }

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["guid_char"]));

            default:
                throw new NotSupportedException(string.Format("There is no store type corresponding to the EDM type '{0}' of primitive type '{1}'.", edmType, primitiveType.PrimitiveTypeKind));
            }
        }
        /// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// in Jet
        /// </summary>
        /// <param name="storeType">A TypeUsage encapsulating an EDM type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating a store type and a set of facets</returns>
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            System.Diagnostics.Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format("The underlying provider does not support the type '{0}'.", edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["guid"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Decimal:     // decimal, numeric, smallmoney, money
            {
                byte precision;
                if (!edmType.TryGetPrecision(out precision))
                {
                    precision = 18;
                }

                byte scale;
                if (!edmType.TryGetScale(out scale))
                {
                    scale = 0;
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.Binary:     // binary, varbinary, image
            {
                bool isFixedLength = edmType.GetIsFixedLength();
                bool isMaxLength   = edmType.GetMaxLength() > BINARY_MAXSIZE;
                int  maxLength     = edmType.GetMaxLength();

                TypeUsage tu;
                if (isFixedLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["binary"], true, maxLength);
                }
                else if (isMaxLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["image"], false);
                    System.Diagnostics.Debug.Assert(tu.Facets["MaxLength"].Description.IsConstant, "varbinary(max) is not constant!");
                }
                else
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary"], false, maxLength);
                }

                return(tu);
            }

            case PrimitiveTypeKind.String:
                // char, varchar, text
            {
                bool isUnicode     = edmType.GetIsUnicode();     // We do not handle unicode (everything's unicode in Jet)
                bool isFixedLength = edmType.GetIsFixedLength();
                bool isMaxLength   = edmType.GetMaxLength() > VARCHAR_MAXSIZE;
                int  maxLength     = edmType.GetMaxLength();

                TypeUsage tu;

                if (isFixedLength)
                {
                    tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["char"], false, true, maxLength);
                }
                else if (isMaxLength)
                {
                    tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["text"], false, false);
                }
                else
                {
                    tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength);
                }

                return(tu);
            }

            case PrimitiveTypeKind.DateTime:     // datetime
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.Time:     // time
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["time"]));

            default:
                throw new NotSupportedException(String.Format("There is no store type corresponding to the EDM type '{0}' of primitive type '{1}'.", edmType, primitiveType.PrimitiveTypeKind));
            }
        }
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            Check.NotNull(edmType, "edmType");

            Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            var primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw ADP1.Argument(EntityRes.GetString(EntityRes.ProviderDoesNotSupportType, edmType.EdmType));
            }

            var facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["uniqueidentifier"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Decimal:     // decimal, numeric, money
            {
                byte precision;
                if (!TypeHelpers.TryGetPrecision(edmType, out precision))
                {
                    precision = 18;
                }

                byte scale;
                if (!TypeHelpers.TryGetScale(edmType, out scale))
                {
                    scale = 0;
                }
                var tu = TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale);
                return(tu);
            }

            case PrimitiveTypeKind.Binary:     // binary, varbinary, image, timestamp, rowversion
            {
                var isFixedLength = null != facets[ProviderManifest.FixedLengthFacetName].Value &&
                                    (bool)facets[ProviderManifest.FixedLengthFacetName].Value;
                var f           = facets[ProviderManifest.MaxLengthFacetName];
                var isMaxLength = Helper.IsUnboundedFacetValue(f) || null == f.Value || (int)f.Value > binaryMaxSize;
                var maxLength   = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;
                if (isFixedLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(
                        StoreTypeNameToStorePrimitiveType["binary"], true, (isMaxLength ? binaryMaxSize : maxLength));
                }
                else
                {
                    if (null == f.Value)
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary"], false, binaryMaxSize);
                    }
                    else if (Helper.IsUnboundedFacetValue(f) ||
                             edmType.EdmType.Name == "image")
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["image"], false);
                    }
                    else if ((int)f.Value > binaryMaxSize)
                    {
                        throw ADP1.ColumnGreaterThanMaxLengthNotSupported(edmType.EdmType.Name, binaryMaxSize);
                    }
                    else
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary"], false, maxLength);
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.String:
                //char, nchar, varchar, nvarchar, ntext, text, xml
            {
                var isFixedLength = null != facets[ProviderManifest.FixedLengthFacetName].Value &&
                                    (bool)facets[ProviderManifest.FixedLengthFacetName].Value;
                var f = facets[ProviderManifest.MaxLengthFacetName];
                // maxlen is true if facet value is unbounded, the value is bigger than the limited string sizes *or* the facet
                // value is null. this is needed since functions still have maxlength facet value as null
                var isMaxLength = Helper.IsUnboundedFacetValue(f) || null == f.Value || (int)f.Value > (nvarcharMaxSize);
                var maxLength   = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;

                if (isFixedLength)
                {
                    tu = TypeUsage.CreateStringTypeUsage(
                        StoreTypeNameToStorePrimitiveType["nchar"], true, true, (isMaxLength ? nvarcharMaxSize : maxLength));
                }
                else
                {
                    if (null == f.Value)
                    {
                        // if it is unknown, fallback to nvarchar[4000] instead of ntext since it has limited store semantics
                        tu = TypeUsage.CreateStringTypeUsage(
                            StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, nvarcharMaxSize);
                    }
                    else if (Helper.IsUnboundedFacetValue(f) ||
                             edmType.EdmType.Name == "ntext")
                    {
                        tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["ntext"], true, false);
                    }
                    else if ((int)f.Value > nvarcharMaxSize)
                    {
                        throw ADP1.ColumnGreaterThanMaxLengthNotSupported(edmType.EdmType.Name, nvarcharMaxSize);
                    }
                    else
                    {
                        tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, maxLength);
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            default:
                throw ADP1.NotSupported(
                          EntityRes.GetString(
                              EntityRes.NoStoreTypeForEdmType, TypeHelpers.GetIdentity(edmType), primitiveType.PrimitiveTypeKind));
            }
        }
        private static TypeUsage ConvertFromLegacyEdmTypeUsage(LegacyMetadata.TypeUsage legacyTypeUsage)
        {
            Debug.Assert(legacyTypeUsage != null, "legacyTypeUsage != null");
            Debug.Assert(legacyTypeUsage.EdmType is LegacyMetadata.PrimitiveType, "primitive type expected.");
            Debug.Assert(
                (LegacyMetadata.DataSpace) typeof(LegacyMetadata.EdmType)
                .GetProperty("DataSpace", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(legacyTypeUsage.EdmType) == LegacyMetadata.DataSpace.CSpace,
                "Expected CSpace type.");

            var legacyPrimitiveEdmType = (LegacyMetadata.PrimitiveType)legacyTypeUsage.EdmType;
            var primitiveEdmType       = FromLegacyPrimitiveType(legacyPrimitiveEdmType);

            switch (legacyPrimitiveEdmType.PrimitiveTypeKind)
            {
            case LegacyMetadata.PrimitiveTypeKind.Boolean:
            case LegacyMetadata.PrimitiveTypeKind.Byte:
            case LegacyMetadata.PrimitiveTypeKind.SByte:
            case LegacyMetadata.PrimitiveTypeKind.Int16:
            case LegacyMetadata.PrimitiveTypeKind.Int32:
            case LegacyMetadata.PrimitiveTypeKind.Int64:
            case LegacyMetadata.PrimitiveTypeKind.Guid:
            case LegacyMetadata.PrimitiveTypeKind.Double:
            case LegacyMetadata.PrimitiveTypeKind.Single:
            case LegacyMetadata.PrimitiveTypeKind.Geography:
            case LegacyMetadata.PrimitiveTypeKind.GeographyPoint:
            case LegacyMetadata.PrimitiveTypeKind.GeographyLineString:
            case LegacyMetadata.PrimitiveTypeKind.GeographyPolygon:
            case LegacyMetadata.PrimitiveTypeKind.GeographyMultiPoint:
            case LegacyMetadata.PrimitiveTypeKind.GeographyMultiLineString:
            case LegacyMetadata.PrimitiveTypeKind.GeographyMultiPolygon:
            case LegacyMetadata.PrimitiveTypeKind.GeographyCollection:
            case LegacyMetadata.PrimitiveTypeKind.Geometry:
            case LegacyMetadata.PrimitiveTypeKind.GeometryPoint:
            case LegacyMetadata.PrimitiveTypeKind.GeometryLineString:
            case LegacyMetadata.PrimitiveTypeKind.GeometryPolygon:
            case LegacyMetadata.PrimitiveTypeKind.GeometryMultiPoint:
            case LegacyMetadata.PrimitiveTypeKind.GeometryMultiLineString:
            case LegacyMetadata.PrimitiveTypeKind.GeometryMultiPolygon:
            case LegacyMetadata.PrimitiveTypeKind.GeometryCollection:
                return(TypeUsage.CreateDefaultTypeUsage(primitiveEdmType));

            case LegacyMetadata.PrimitiveTypeKind.Decimal:
                var precisionFacetValue = legacyTypeUsage.Facets[PrecisionFacetName].Value;

                if (precisionFacetValue == null ||
                    precisionFacetValue == TypeUsageHelper.LegacyUnboundedValue)
                {
                    Debug.Assert(
                        legacyTypeUsage.Facets[ScaleFacetName].Value == precisionFacetValue,
                        "Precision and Scale facets are expected to be both unbounded (Max) or null");

                    return(TypeUsage.CreateDecimalTypeUsage(primitiveEdmType));
                }
                else
                {
                    var scaleFacetValue = legacyTypeUsage.Facets[ScaleFacetName].Value;

                    Debug.Assert(
                        precisionFacetValue is byte && scaleFacetValue is byte,
                        "Precision and Scale facets are expected to be both unbounded (Max) or both of byte type");

                    return
                        (TypeUsage.CreateDecimalTypeUsage(
                             primitiveEdmType,
                             (byte)precisionFacetValue,
                             (byte)scaleFacetValue));
                }

            case LegacyMetadata.PrimitiveTypeKind.Binary:
                Debug.Assert(
                    !(legacyTypeUsage.Facets[FixedLengthFacetName].Value == null
                      ^ legacyTypeUsage.Facets[MaxLengthFacetName].Value == null),
                    "Both Fixed Length and Max Length facet values should be null or none should be null");

                var fixedLengthFacetValue = legacyTypeUsage.Facets[FixedLengthFacetName].Value;
                if (fixedLengthFacetValue == null)
                {
                    return(TypeUsage.CreateDefaultTypeUsage(primitiveEdmType));
                }
                else
                {
                    var maxLengthBinaryFacetValue = legacyTypeUsage.Facets[MaxLengthFacetName].Value;

                    return
                        (maxLengthBinaryFacetValue == TypeUsageHelper.LegacyUnboundedValue
                                ? TypeUsage.CreateBinaryTypeUsage(
                             primitiveEdmType,
                             (bool)fixedLengthFacetValue)
                                : TypeUsage.CreateBinaryTypeUsage(
                             primitiveEdmType,
                             (bool)fixedLengthFacetValue,
                             (int)maxLengthBinaryFacetValue));
                }

            case LegacyMetadata.PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDateTimeTypeUsage(
                           primitiveEdmType,
                           (byte?)legacyTypeUsage.Facets[PrecisionFacetName].Value));

            case LegacyMetadata.PrimitiveTypeKind.DateTimeOffset:
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(
                           primitiveEdmType,
                           (byte?)legacyTypeUsage.Facets[PrecisionFacetName].Value));

            case LegacyMetadata.PrimitiveTypeKind.Time:
                return(TypeUsage.CreateTimeTypeUsage(
                           primitiveEdmType,
                           (byte?)legacyTypeUsage.Facets[PrecisionFacetName].Value));

            case LegacyMetadata.PrimitiveTypeKind.String:
                var maxLengthStringFacetValue = legacyTypeUsage.Facets[MaxLengthFacetName].Value;
                if (maxLengthStringFacetValue == null)
                {
                    return(TypeUsage.CreateDefaultTypeUsage(primitiveEdmType));
                }
                else
                {
                    return(maxLengthStringFacetValue == TypeUsageHelper.LegacyUnboundedValue
                                   ? TypeUsage.CreateStringTypeUsage(
                               primitiveEdmType,
                               (bool)legacyTypeUsage.Facets[UnicodeFacetName].Value,
                               (bool)legacyTypeUsage.Facets[FixedLengthFacetName].Value)
                                   : TypeUsage.CreateStringTypeUsage(
                               primitiveEdmType,
                               (bool)legacyTypeUsage.Facets[UnicodeFacetName].Value,
                               (bool)legacyTypeUsage.Facets[FixedLengthFacetName].Value,
                               (int)maxLengthStringFacetValue));
                }
            }

            Debug.Fail("Unknown primitive type kind.");

            throw new NotSupportedException();
        }
示例#21
0
        /// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// in SQL Server, taking the store version into consideration.
        /// </summary>
        /// <param name="storeType">A TypeUsage encapsulating an EDM type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating a store type and a set of facets</returns>
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }
            Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            if (!(edmType.EdmType is PrimitiveType primitiveType))
            {
                throw new ArgumentException(string.Format("The underlying provider does not support the type '{0}'.", edmType));
            }

            var facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint_bool"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["double"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Decimal:                     // decimal, numeric
            {
                if (!TypeHelpers.TryGetPrecision(edmType, out var precision))
                {
                    precision = 9;
                }

                if (!TypeHelpers.TryGetScale(edmType, out var scale))
                {
                    scale = 0;
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.Binary:                     // blob
            {
                var isFixedLength = null != facets[MetadataHelpers.FixedLengthFacetName].Value && (bool)facets[MetadataHelpers.FixedLengthFacetName].Value;
                var f             = facets[MetadataHelpers.MaxLengthFacetName];

                var isMaxLength = f.IsUnbounded || null == f.Value || (int)f.Value > BinaryMaxSize;
                var maxLength   = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;
                if (isFixedLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["blob"], true, maxLength);
                }
                else
                {
                    if (isMaxLength)
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["blob"], false);
                        Debug.Assert(tu.Facets["MaxLength"].Description.IsConstant, "blob is not constant!");
                    }
                    else
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["blob"], false, maxLength);
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.String:                     // char, varchar, text blob
            {
                var isUnicode     = null == facets[MetadataHelpers.UnicodeFacetName].Value || (bool)facets[MetadataHelpers.UnicodeFacetName].Value;
                var isFixedLength = null != facets[MetadataHelpers.FixedLengthFacetName].Value && (bool)facets[MetadataHelpers.FixedLengthFacetName].Value;
                var f             = facets[MetadataHelpers.MaxLengthFacetName];
                // maxlen is true if facet value is unbounded, the value is bigger than the limited string sizes *or* the facet
                // value is null. this is needed since functions still have maxlength facet value as null
                var isMaxLength = f.IsUnbounded || null == f.Value || (int)f.Value > (isUnicode ? UnicodeVarcharMaxSize : AsciiVarcharMaxSize);
                var maxLength   = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;

                if (isUnicode)
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["char"], true, true, maxLength);
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["clob"], true, false);
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], true, false, maxLength);
                        }
                    }
                }
                else
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["char"], false, true, maxLength);
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["clob"], false, false);
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength);
                        }
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.DateTime:                     // datetime, date
            {
                bool useTimestamp;
                if (TypeHelpers.TryGetPrecision(edmType, out var precision))
                {
                    if (precision == 0)
                    {
                        useTimestamp = false;
                    }
                    else
                    {
                        useTimestamp = true;
                    }
                }
                else
                {
                    useTimestamp = true;
                }

                return(TypeUsage.CreateDefaultTypeUsage(useTimestamp ? StoreTypeNameToStorePrimitiveType["timestamp"] : StoreTypeNameToStorePrimitiveType["date"]));
            }

            case PrimitiveTypeKind.Time:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["time"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["guid"]));

            default:
                throw new NotSupportedException(string.Format("There is no store type corresponding to the EDM type '{0}' of primitive type '{1}'.", edmType, primitiveType.PrimitiveTypeKind));
            }
        }
        private TypeUsage ConvertFromLegacyStoreTypeUsage(LegacyMetadata.TypeUsage legacyStoreTypeUsage)
        {
            Debug.Assert(legacyStoreTypeUsage != null, "legacyStoreTypeUsage != null");
            Debug.Assert(legacyStoreTypeUsage.EdmType is LegacyMetadata.PrimitiveType, "primitive type expected");
            Debug.Assert(
                (LegacyMetadata.DataSpace) typeof(LegacyMetadata.EdmType)
                .GetProperty("DataSpace", BindingFlags.Instance | BindingFlags.NonPublic)
                .GetValue(legacyStoreTypeUsage.EdmType) == LegacyMetadata.DataSpace.SSpace,
                "Expected SSpace type.");

            var legacyStorePrimitiveType = (LegacyMetadata.PrimitiveType)legacyStoreTypeUsage.EdmType;
            var storePrimitiveType       = _storeTypes.Single(t => t.FullName == legacyStorePrimitiveType.FullName);

            switch (storePrimitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
            case PrimitiveTypeKind.Byte:
            case PrimitiveTypeKind.SByte:
            case PrimitiveTypeKind.Int16:
            case PrimitiveTypeKind.Int32:
            case PrimitiveTypeKind.Int64:
            case PrimitiveTypeKind.Guid:
            case PrimitiveTypeKind.Double:
            case PrimitiveTypeKind.Single:
            case PrimitiveTypeKind.HierarchyId:
            case PrimitiveTypeKind.Geography:
            case PrimitiveTypeKind.GeographyPoint:
            case PrimitiveTypeKind.GeographyLineString:
            case PrimitiveTypeKind.GeographyPolygon:
            case PrimitiveTypeKind.GeographyMultiPoint:
            case PrimitiveTypeKind.GeographyMultiLineString:
            case PrimitiveTypeKind.GeographyMultiPolygon:
            case PrimitiveTypeKind.GeographyCollection:
            case PrimitiveTypeKind.Geometry:
            case PrimitiveTypeKind.GeometryPoint:
            case PrimitiveTypeKind.GeometryLineString:
            case PrimitiveTypeKind.GeometryPolygon:
            case PrimitiveTypeKind.GeometryMultiPoint:
            case PrimitiveTypeKind.GeometryMultiLineString:
            case PrimitiveTypeKind.GeometryMultiPolygon:
            case PrimitiveTypeKind.GeometryCollection:
                return(TypeUsage.CreateDefaultTypeUsage(storePrimitiveType));

            case PrimitiveTypeKind.Decimal:
                return(TypeUsage.CreateDecimalTypeUsage(
                           storePrimitiveType,
                           (byte)legacyStoreTypeUsage.Facets[PrecisionFacetName].Value,
                           (byte)legacyStoreTypeUsage.Facets[ScaleFacetName].Value));

            case PrimitiveTypeKind.Binary:
                return(TypeUsage.CreateBinaryTypeUsage(
                           storePrimitiveType,
                           (bool)legacyStoreTypeUsage.Facets[FixedLengthFacetName].Value,
                           (int)legacyStoreTypeUsage.Facets[MaxLengthFacetName].Value));

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDateTimeTypeUsage(
                           storePrimitiveType,
                           (byte?)legacyStoreTypeUsage.Facets[PrecisionFacetName].Value));

            case PrimitiveTypeKind.DateTimeOffset:
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(
                           storePrimitiveType,
                           (byte?)legacyStoreTypeUsage.Facets[PrecisionFacetName].Value));

            case PrimitiveTypeKind.Time:
                return(TypeUsage.CreateTimeTypeUsage(
                           storePrimitiveType,
                           (byte?)legacyStoreTypeUsage.Facets[PrecisionFacetName].Value));

            case PrimitiveTypeKind.String:
                return(TypeUsage.CreateStringTypeUsage(
                           storePrimitiveType,
                           (bool)legacyStoreTypeUsage.Facets[UnicodeFacetName].Value,
                           (bool)legacyStoreTypeUsage.Facets[FixedLengthFacetName].Value,
                           (int)legacyStoreTypeUsage.Facets[MaxLengthFacetName].Value));
            }

            Debug.Fail("Unknown primitive type kind.");

            throw new NotSupportedException();
        }
        /// <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");
            }

            string storeTypeName = storeType.EdmType.Name.ToLowerInvariant();
            //if (!base.StoreTypeNameToEdmPrimitiveType.ContainsKey(storeTypeName))
            //{
            //  switch (storeTypeName)
            //  {
            //    case "integer":
            //      return TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int64));
            //    default:
            //      throw new ArgumentException(String.Format("SQLite does not support the type '{0}'.", storeTypeName));
            //  }
            //}

            PrimitiveType edmPrimitiveType;

            if (base.StoreTypeNameToEdmPrimitiveType.TryGetValue(storeTypeName, out edmPrimitiveType) == false)
            {
                throw new ArgumentException(String.Format("SQLite does not support the type '{0}'.", storeTypeName));
            }

            int  maxLength   = 0;
            bool isUnicode   = true;
            bool isFixedLen  = false;
            bool isUnbounded = true;

            PrimitiveTypeKind newPrimitiveTypeKind;

            switch (storeTypeName)
            {
            case "tinyint":
            case "smallint":
            case "integer":
            case "bit":
            case "uniqueidentifier":
            case "int":
            case "float":
            case "real":
                return(TypeUsage.CreateDefaultTypeUsage(edmPrimitiveType));

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

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

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

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

            case "blob":
                newPrimitiveTypeKind = PrimitiveTypeKind.Binary;
                isUnbounded          = !TypeHelpers.TryGetMaxLength(storeType, out maxLength);
                isFixedLen           = false;
                break;

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

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

            default:
                throw new NotSupportedException(String.Format("SQLite does not support the type '{0}'.", storeTypeName));
            }

            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("SQLite does not support the type '{0}'.", storeTypeName));
            }
        }
示例#24
0
        private TypeUsage BuildTypeUsage()
        {
            PrimitiveType edmPrimitiveType = PrimitiveType.GetEdmPrimitiveType(this.Type);

            if (this.Type == PrimitiveTypeKind.Binary)
            {
                if (this.MaxLength.HasValue)
                {
                    PrimitiveType primitiveType = edmPrimitiveType;
                    bool?         isFixedLength = this.IsFixedLength;
                    int           num           = isFixedLength.HasValue ? (isFixedLength.GetValueOrDefault() ? 1 : 0) : 0;
                    int           maxLength     = this.MaxLength.Value;
                    return(TypeUsage.CreateBinaryTypeUsage(primitiveType, num != 0, maxLength));
                }
                PrimitiveType primitiveType1 = edmPrimitiveType;
                bool?         isFixedLength1 = this.IsFixedLength;
                int           num1           = isFixedLength1.HasValue ? (isFixedLength1.GetValueOrDefault() ? 1 : 0) : 0;
                return(TypeUsage.CreateBinaryTypeUsage(primitiveType1, num1 != 0));
            }
            if (this.Type == PrimitiveTypeKind.String)
            {
                if (this.MaxLength.HasValue)
                {
                    PrimitiveType primitiveType = edmPrimitiveType;
                    bool?         isUnicode     = this.IsUnicode;
                    int           num1          = isUnicode.HasValue ? (isUnicode.GetValueOrDefault() ? 1 : 0) : 1;
                    bool?         isFixedLength = this.IsFixedLength;
                    int           num2          = isFixedLength.HasValue ? (isFixedLength.GetValueOrDefault() ? 1 : 0) : 0;
                    int           maxLength     = this.MaxLength.Value;
                    return(TypeUsage.CreateStringTypeUsage(primitiveType, num1 != 0, num2 != 0, maxLength));
                }
                PrimitiveType primitiveType1 = edmPrimitiveType;
                bool?         isUnicode1     = this.IsUnicode;
                int           num3           = isUnicode1.HasValue ? (isUnicode1.GetValueOrDefault() ? 1 : 0) : 1;
                bool?         isFixedLength1 = this.IsFixedLength;
                int           num4           = isFixedLength1.HasValue ? (isFixedLength1.GetValueOrDefault() ? 1 : 0) : 0;
                return(TypeUsage.CreateStringTypeUsage(primitiveType1, num3 != 0, num4 != 0));
            }
            if (this.Type == PrimitiveTypeKind.DateTime)
            {
                return(TypeUsage.CreateDateTimeTypeUsage(edmPrimitiveType, this.Precision));
            }
            if (this.Type == PrimitiveTypeKind.DateTimeOffset)
            {
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(edmPrimitiveType, this.Precision));
            }
            if (this.Type == PrimitiveTypeKind.Decimal)
            {
                byte?precision1 = this.Precision;
                if (!(precision1.HasValue ? new int?((int)precision1.GetValueOrDefault()) : new int?()).HasValue)
                {
                    byte?scale = this.Scale;
                    if (!(scale.HasValue ? new int?((int)scale.GetValueOrDefault()) : new int?()).HasValue)
                    {
                        return(TypeUsage.CreateDecimalTypeUsage(edmPrimitiveType));
                    }
                }
                PrimitiveType primitiveType = edmPrimitiveType;
                byte?         precision2    = this.Precision;
                int           num1          = precision2.HasValue ? (int)precision2.GetValueOrDefault() : 18;
                byte?         scale1        = this.Scale;
                int           num2          = scale1.HasValue ? (int)scale1.GetValueOrDefault() : 0;
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType, (byte)num1, (byte)num2));
            }
            if (this.Type != PrimitiveTypeKind.Time)
            {
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)edmPrimitiveType));
            }
            return(TypeUsage.CreateTimeTypeUsage(edmPrimitiveType, this.Precision));
        }
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            Check.NotNull(edmType, "edmType");
            Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            var primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(Strings.ProviderDoesNotSupportType(edmType.EdmType.Name));
            }

            var facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Geography:
            case PrimitiveTypeKind.GeographyPoint:
            case PrimitiveTypeKind.GeographyLineString:
            case PrimitiveTypeKind.GeographyPolygon:
            case PrimitiveTypeKind.GeographyMultiPoint:
            case PrimitiveTypeKind.GeographyMultiLineString:
            case PrimitiveTypeKind.GeographyMultiPolygon:
            case PrimitiveTypeKind.GeographyCollection:
                return(GetStorePrimitiveTypeIfPostSql9("geography", edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));

            case PrimitiveTypeKind.Geometry:
            case PrimitiveTypeKind.GeometryPoint:
            case PrimitiveTypeKind.GeometryLineString:
            case PrimitiveTypeKind.GeometryPolygon:
            case PrimitiveTypeKind.GeometryMultiPoint:
            case PrimitiveTypeKind.GeometryMultiLineString:
            case PrimitiveTypeKind.GeometryMultiPolygon:
            case PrimitiveTypeKind.GeometryCollection:
                return(GetStorePrimitiveTypeIfPostSql9("geometry", edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["uniqueidentifier"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Decimal:     // decimal, numeric, smallmoney, money
            {
                byte precision;
                if (!edmType.TryGetPrecision(out precision))
                {
                    precision = 18;
                }

                byte scale;
                if (!edmType.TryGetScale(out scale))
                {
                    scale = 0;
                }
                var tu = TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale);
                return(tu);
            }

            case PrimitiveTypeKind.Binary:     // binary, varbinary, varbinary(max), image, timestamp, rowversion
            {
                var isFixedLength = null != facets[FixedLengthFacetName].Value && (bool)facets[FixedLengthFacetName].Value;
                var f             = facets[MaxLengthFacetName];
                var isMaxLength   = f.IsUnbounded || null == f.Value || (int)f.Value > binaryMaxSize;
                var maxLength     = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;
                if (isFixedLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(
                        StoreTypeNameToStorePrimitiveType["binary"], true, (isMaxLength ? binaryMaxSize : maxLength));
                }
                else
                {
                    if (isMaxLength)
                    {
                        if (_version != SqlVersion.Sql8)
                        {
                            tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary(max)"], false);
                            Debug.Assert(tu.Facets[MaxLengthFacetName].Description.IsConstant, "varbinary(max) is not constant!");
                        }
                        else
                        {
                            tu = TypeUsage.CreateBinaryTypeUsage(
                                StoreTypeNameToStorePrimitiveType["varbinary"], false, binaryMaxSize);
                        }
                    }
                    else
                    {
                        tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary"], false, maxLength);
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.String:
                // char, nchar, varchar, nvarchar, varchar(max), nvarchar(max), ntext, text, xml
            {
                var isUnicode     = null == facets[UnicodeFacetName].Value || (bool)facets[UnicodeFacetName].Value;
                var isFixedLength = null != facets[FixedLengthFacetName].Value && (bool)facets[FixedLengthFacetName].Value;
                var f             = facets[MaxLengthFacetName];
                // maxlen is true if facet value is unbounded, the value is bigger than the limited string sizes *or* the facet
                // value is null. this is needed since functions still have maxlength facet value as null
                var isMaxLength = f.IsUnbounded || null == f.Value ||
                                  (int)f.Value > (isUnicode ? nvarcharMaxSize : varcharMaxSize);
                var maxLength = !isMaxLength ? (int)f.Value : Int32.MinValue;

                TypeUsage tu;

                if (isUnicode)
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(
                            StoreTypeNameToStorePrimitiveType["nchar"], true, true, (isMaxLength ? nvarcharMaxSize : maxLength));
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            // nvarchar(max) (SQL 9) or ntext (SQL 8)
                            if (_version != SqlVersion.Sql8)
                            {
                                tu = TypeUsage.CreateStringTypeUsage(
                                    StoreTypeNameToStorePrimitiveType["nvarchar(max)"], true, false);
                                Debug.Assert(tu.Facets[MaxLengthFacetName].Description.IsConstant, "NVarchar(max) is not constant!");
                            }
                            else
                            {
                                // if it is unknown, fallback to nvarchar[4000] instead of ntext since it has limited store semantics
                                tu = TypeUsage.CreateStringTypeUsage(
                                    StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, nvarcharMaxSize);
                            }
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(
                                StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, maxLength);
                        }
                    }
                }
                else         // !isUnicode
                {
                    if (isFixedLength)
                    {
                        tu = TypeUsage.CreateStringTypeUsage(
                            StoreTypeNameToStorePrimitiveType["char"], false, true,
                            (isMaxLength ? varcharMaxSize : maxLength));
                    }
                    else
                    {
                        if (isMaxLength)
                        {
                            // nvarchar(max) (SQL 9) or ntext (SQL 8)
                            if (_version != SqlVersion.Sql8)
                            {
                                tu = TypeUsage.CreateStringTypeUsage(
                                    StoreTypeNameToStorePrimitiveType["varchar(max)"], false, false);
                                Debug.Assert(tu.Facets[MaxLengthFacetName].Description.IsConstant, "varchar(max) is not constant!");
                            }
                            else
                            {
                                // if it is unknown, fallback to varchar[8000] instead of text since it has limited store semantics
                                tu = TypeUsage.CreateStringTypeUsage(
                                    StoreTypeNameToStorePrimitiveType["varchar"], false, false, varcharMaxSize);
                            }
                        }
                        else
                        {
                            tu = TypeUsage.CreateStringTypeUsage(
                                StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength);
                        }
                    }
                }
                return(tu);
            }

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.DateTimeOffset:
                return(GetStorePrimitiveTypeIfPostSql9("datetimeoffset", edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));

            case PrimitiveTypeKind.Time:
                return(GetStorePrimitiveTypeIfPostSql9("time", edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));

            default:
                throw new NotSupportedException(Strings.NoStoreTypeForEdmType(edmType.EdmType.Name, primitiveType.PrimitiveTypeKind));
            }
        }
示例#26
0
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format(Resources.TypeNotSupported, edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bool"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["guid"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["double"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Decimal:
            {
                byte  precision = 10;
                byte  scale     = 0;
                Facet facet;

                if (edmType.Facets.TryGetValue("Precision", false, out facet))
                {
                    if (!facet.IsUnbounded && facet.Value != null)
                    {
                        precision = (byte)facet.Value;
                    }
                }

                if (edmType.Facets.TryGetValue("Scale", false, out facet))
                {
                    if (!facet.IsUnbounded && facet.Value != null)
                    {
                        scale = (byte)facet.Value;
                    }
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.Binary:
            {
                bool  isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                Facet f             = facets["MaxLength"];
                bool  isMaxLength   = f.IsUnbounded || null == f.Value || (int)f.Value > MEDIUMBLOB_MAXLEN;
                int   maxLength     = !isMaxLength ? (int)f.Value : LONGBLOB_MAXLEN;

                string typeName = String.Empty;
                if (isFixedLength)
                {
                    if (maxLength < CHAR_MAXLEN)
                    {
                        typeName = "tinyblob";
                    }
                    else if (maxLength < MEDIUMBLOB_MAXLEN)
                    {
                        typeName = "blob";
                    }
                    else if (maxLength < LONGTEXT_MAXLEN)
                    {
                        typeName = "mediumblob";
                    }
                    else
                    {
                        typeName = "longblob";
                    }
                }
                else
                {
                    typeName  = isMaxLength || maxLength > BINARY_MAXLEN ? "varbinary" : "binary";
                    maxLength = isMaxLength ? VARBINARY_MAXLEN : maxLength;
                }

                return(TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isFixedLength, maxLength));
            }

            case PrimitiveTypeKind.String:
            {
                bool  isUnicode     = null == facets["Unicode"].Value || (bool)facets["Unicode"].Value;
                bool  isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                Facet f             = facets["MaxLength"];
                // maxlen is true if facet value is unbounded, the value is bigger than the limited string sizes *or* the facet
                // value is null. this is needed since functions still have maxlength facet value as null
                bool isMaxLength = f.IsUnbounded || null == f.Value || (int)f.Value > MEDIUMTEXT_MAXLEN;
                int  maxLength   = !isMaxLength ? (int)f.Value : LONGTEXT_MAXLEN;

                string typeName = String.Empty;
                if (isFixedLength)
                {
                    if (maxLength < CHAR_MAXLEN)
                    {
                        typeName = "char";
                    }
                    else if (maxLength < LONGTEXT_MAXLEN)
                    {
                        typeName = "mediumtext";
                    }
                    else
                    {
                        typeName = "longtext";
                    }
                }
                else
                {
                    typeName  = isMaxLength || maxLength > CHAR_MAXLEN ? "varchar" : "char";
                    maxLength = isMaxLength ? VARCHAR_MAXLEN : maxLength;
                }
                if (typeName.EndsWith("char") && isUnicode)
                {
                    typeName = "n" + typeName;
                }

                return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType[typeName], isUnicode, isFixedLength, maxLength));
            }

            case PrimitiveTypeKind.DateTimeOffset:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["timestamp"]));

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.Time:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["time"]));

            default:
                throw new NotSupportedException(String.Format(Resources.NoStoreTypeForEdmType, edmType, primitiveType.PrimitiveTypeKind));
            }
        }
示例#27
0
        private TypeUsage BuildTypeUsage()
        {
            var primitiveType = PrimitiveType.GetEdmPrimitiveType(Type);

            if (Type == PrimitiveTypeKind.Binary)
            {
                if (MaxLength != null)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(
                               primitiveType,
                               IsFixedLength ?? false,
                               MaxLength.Value));
                }

                return(TypeUsage.CreateBinaryTypeUsage(
                           primitiveType,
                           IsFixedLength ?? false));
            }

            if (Type == PrimitiveTypeKind.String)
            {
                if (MaxLength != null)
                {
                    return(TypeUsage.CreateStringTypeUsage(
                               primitiveType,
                               IsUnicode ?? true,
                               IsFixedLength ?? false,
                               MaxLength.Value));
                }

                return(TypeUsage.CreateStringTypeUsage(
                           primitiveType,
                           IsUnicode ?? true,
                           IsFixedLength ?? false));
            }

            if (Type == PrimitiveTypeKind.DateTime)
            {
                return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, Precision));
            }

            if (Type == PrimitiveTypeKind.DateTimeOffset)
            {
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(primitiveType, Precision));
            }

            if (Type == PrimitiveTypeKind.Decimal)
            {
                if ((Precision != null) ||
                    (Scale != null))
                {
                    return(TypeUsage.CreateDecimalTypeUsage(
                               primitiveType,
                               Precision ?? 18,
                               Scale ?? 0));
                }

                return(TypeUsage.CreateDecimalTypeUsage(primitiveType));
            }

            return((Type == PrimitiveTypeKind.Time)
                       ? TypeUsage.CreateTimeTypeUsage(primitiveType, Precision)
                       : TypeUsage.CreateDefaultTypeUsage(primitiveType));
        }
示例#28
0
        private static TypeUsage ConvertTypeUsage(TypeUsage original, PrimitiveType goal)
        {
            byte precision;
            byte scale;
            bool isUnicode;
            bool isFixed;
            int  maxLength;

            switch (goal.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.DateTime:

                if (!TypeUsageHelper.TryGetPrecision(original, out precision))
                {
                    precision = 7;
                }

                return(TypeUsage.CreateDateTimeTypeUsage(goal, precision));

            case PrimitiveTypeKind.DateTimeOffset:

                if (!TypeUsageHelper.TryGetPrecision(original, out precision))
                {
                    precision = 7;
                }

                return(TypeUsage.CreateDateTimeOffsetTypeUsage(goal, precision));

            case PrimitiveTypeKind.Time:

                if (!TypeUsageHelper.TryGetPrecision(original, out precision))
                {
                    precision = 7;
                }

                return(TypeUsage.CreateTimeTypeUsage(goal, precision));

            case PrimitiveTypeKind.Decimal:

                if (!TypeUsageHelper.TryGetPrecision(original, out precision))
                {
                    precision = 18;
                }

                if (!TypeUsageHelper.TryGetScale(original, out scale))
                {
                    scale = 0;
                }

                return(TypeUsage.CreateDecimalTypeUsage(goal, precision, scale));

            case PrimitiveTypeKind.Binary:

                if (!TypeUsageHelper.TryGetIsFixedLength(original, out isFixed))
                {
                    isFixed = false;
                }

                if (TypeUsageHelper.TryGetMaxLength(original, out maxLength))
                {
                    return(TypeUsage.CreateBinaryTypeUsage(goal, isFixed, maxLength));
                }
                else
                {
                    return(TypeUsage.CreateBinaryTypeUsage(goal, isFixed));
                }

            case PrimitiveTypeKind.String:

                if (!TypeUsageHelper.TryGetIsFixedLength(original, out isFixed))
                {
                    isFixed = false;
                }

                if (!TypeUsageHelper.TryGetIsUnicode(original, out isUnicode))
                {
                    isUnicode = true;
                }

                if (TypeUsageHelper.TryGetMaxLength(original, out maxLength))
                {
                    return
                        (TypeUsage.CreateStringTypeUsage(
                             goal,
                             isUnicode,
                             isFixed,
                             maxLength));
                }
                else
                {
                    return
                        (TypeUsage.CreateStringTypeUsage(
                             goal,
                             isUnicode,
                             isFixed));
                }
            }

            return(TypeUsage.CreateDefaultTypeUsage(goal));
        }
示例#29
0
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException("Store does not support specified edm type");
            }

            // TODO: come up with way to determin if unicode is used
            bool  isUnicode = true;
            Facet facet;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bool"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int2"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int4"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int8"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float4"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float8"]));

            case PrimitiveTypeKind.Decimal:
            {
                byte scale;
                byte precision;
                if (edmType.Facets.TryGetValue(ScaleFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    scale = (byte)facet.Value;
                    if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                        !facet.IsUnbounded && facet.Value != null)
                    {
                        precision = (byte)facet.Value;
                        return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["numeric"], precision, scale));
                    }
                }
                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["numeric"]));
            }

            case PrimitiveTypeKind.String:
            {
                // TODO: could get character, character varying, text
                if (edmType.Facets.TryGetValue(FixedLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null && (bool)facet.Value)
                {
                    PrimitiveType characterPrimitive = StoreTypeNameToStorePrimitiveType["bpchar"];
                    if (edmType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                        !facet.IsUnbounded && facet.Value != null)
                    {
                        return(TypeUsage.CreateStringTypeUsage(characterPrimitive, isUnicode, true, (int)facet.Value));
                    }
                    // this may not work well
                    return(TypeUsage.CreateStringTypeUsage(characterPrimitive, isUnicode, true));
                }
                if (edmType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], isUnicode, false, (int)facet.Value));
                }
                // assume text since it is not fixed length and has no max length
                return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["text"], isUnicode, false));
            }

            case PrimitiveTypeKind.DateTime:
                if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateDateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["timestamp"], (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateDateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["timestamp"], null));
                }

            case PrimitiveTypeKind.DateTimeOffset:
                if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateDateTimeOffsetTypeUsage(StoreTypeNameToStorePrimitiveType["timestamptz"], (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateDateTimeOffsetTypeUsage(StoreTypeNameToStorePrimitiveType["timestamptz"], null));
                }

            case PrimitiveTypeKind.Time:
                if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["interval"], (byte)facet.Value));
                }
                else
                {
                    return(TypeUsage.CreateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["interval"], null));
                }

            case PrimitiveTypeKind.Binary:
            {
                if (edmType.Facets.TryGetValue(MaxLengthFacet, false, out facet) &&
                    !facet.IsUnbounded && facet.Value != null)
                {
                    return(TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["bytea"], false, (int)facet.Value));
                }
                return(TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["bytea"], false));
            }

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["uuid"]));

            case PrimitiveTypeKind.Byte:
            case PrimitiveTypeKind.SByte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int2"]));
            }

            throw new NotSupportedException("Not supported edm type: " + edmType);
        }
示例#30
0
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            Check.NotNull <TypeUsage>(edmType, nameof(edmType));
            PrimitiveType edmType1 = edmType.EdmType as PrimitiveType;

            if (edmType1 == null)
            {
                throw new ArgumentException(Strings.ProviderDoesNotSupportType((object)edmType.EdmType.Name));
            }
            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (edmType1.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Binary:
                bool  flag1      = facets["FixedLength"].Value != null && (bool)facets["FixedLength"].Value;
                Facet facet1     = facets["MaxLength"];
                bool  flag2      = facet1.IsUnbounded || facet1.Value == null || (int)facet1.Value > 8000;
                int   maxLength1 = !flag2 ? (int)facet1.Value : int.MinValue;
                return(!flag1 ? (!flag2 ? TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["varbinary"], false, maxLength1) : (this._version == SqlVersion.Sql8 ? TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["varbinary"], false, 8000) : TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["varbinary(max)"], false))) : TypeUsage.CreateBinaryTypeUsage(this.StoreTypeNameToStorePrimitiveType["binary"], true, flag2 ? 8000 : maxLength1));

            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.DateTime:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.Decimal:
                byte precision;
                if (!edmType.TryGetPrecision(out precision))
                {
                    precision = (byte)18;
                }
                byte scale;
                if (!edmType.TryGetScale(out scale))
                {
                    scale = (byte)0;
                }
                return(TypeUsage.CreateDecimalTypeUsage(this.StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["uniqueidentifier"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)this.StoreTypeNameToStorePrimitiveType["bigint"]));

            case PrimitiveTypeKind.String:
                bool  flag3      = facets["Unicode"].Value == null || (bool)facets["Unicode"].Value;
                bool  flag4      = facets["FixedLength"].Value != null && (bool)facets["FixedLength"].Value;
                Facet facet2     = facets["MaxLength"];
                bool  flag5      = facet2.IsUnbounded || facet2.Value == null || (int)facet2.Value > (flag3 ? 4000 : 8000);
                int   maxLength2 = !flag5 ? (int)facet2.Value : int.MinValue;
                return(!flag3 ? (!flag4 ? (!flag5 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength2) : (this._version == SqlVersion.Sql8 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["varchar"], false, false, 8000) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["varchar(max)"], false, false))) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["char"], false, true, flag5 ? 8000 : maxLength2)) : (!flag4 ? (!flag5 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, maxLength2) : (this._version == SqlVersion.Sql8 ? TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nvarchar"], true, false, 4000) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nvarchar(max)"], true, false))) : TypeUsage.CreateStringTypeUsage(this.StoreTypeNameToStorePrimitiveType["nchar"], true, true, flag5 ? 4000 : maxLength2)));

            case PrimitiveTypeKind.Time:
                return(this.GetStorePrimitiveTypeIfPostSql9("time", edmType.EdmType.Name, edmType1.PrimitiveTypeKind));

            case PrimitiveTypeKind.DateTimeOffset:
                return(this.GetStorePrimitiveTypeIfPostSql9("datetimeoffset", edmType.EdmType.Name, edmType1.PrimitiveTypeKind));

            case PrimitiveTypeKind.Geometry:
            case PrimitiveTypeKind.GeometryPoint:
            case PrimitiveTypeKind.GeometryLineString:
            case PrimitiveTypeKind.GeometryPolygon:
            case PrimitiveTypeKind.GeometryMultiPoint:
            case PrimitiveTypeKind.GeometryMultiLineString:
            case PrimitiveTypeKind.GeometryMultiPolygon:
            case PrimitiveTypeKind.GeometryCollection:
                return(this.GetStorePrimitiveTypeIfPostSql9("geometry", edmType.EdmType.Name, edmType1.PrimitiveTypeKind));

            case PrimitiveTypeKind.Geography:
            case PrimitiveTypeKind.GeographyPoint:
            case PrimitiveTypeKind.GeographyLineString:
            case PrimitiveTypeKind.GeographyPolygon:
            case PrimitiveTypeKind.GeographyMultiPoint:
            case PrimitiveTypeKind.GeographyMultiLineString:
            case PrimitiveTypeKind.GeographyMultiPolygon:
            case PrimitiveTypeKind.GeographyCollection:
                return(this.GetStorePrimitiveTypeIfPostSql9("geography", edmType.EdmType.Name, edmType1.PrimitiveTypeKind));

            default:
                throw new NotSupportedException(Strings.NoStoreTypeForEdmType((object)edmType.EdmType.Name, (object)edmType1.PrimitiveTypeKind));
            }
        }