public static byte GetScale(this TypeUsage type) {
            byte value;

            if (type.TryGetScale(out value)) {
                return value;
            }

            throw new ApplicationException("Cannot get the scale.");
        }
Example #2
0
        private static byte GetScale(TypeUsage typeUsage)
        {
            byte scale;

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

            return(scale);
        }
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            Throw.IfNull(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));
            }
        }
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            Throw.IfNull(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));
            }
        }
        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));
            }
        }
        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>
 // Returns SqlParameter.Scale where the type facet exists. Otherwise,
 // returns null.
 // </summary>
 private static byte? GetScale(TypeUsage type)
 {
     byte scale;
     if (type.TryGetScale(out scale))
     {
         return scale;
     }
     else
     {
         return default(byte?);
     }
 }
        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));
            }
        }
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            ArgumentUtility.CheckNotNull("edmType", edmType);

            var primitiveType = edmType.EdmType as PrimitiveType;

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

            var facets = edmType.Facets;

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

            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["logical"]));

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

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

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["numeric"], 38, 0));

            case PrimitiveTypeKind.Decimal:
                byte precision;
                if (!edmType.TryGetPrecision(out precision))
                {
                    precision = 18;
                }

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

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

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

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

            case PrimitiveTypeKind.String:
                var isFixedLength = null != facets["FixedLength"].Value && (bool)facets["FixedLength"].Value;
                var f             = facets["MaxLength"];
                var isMaxLength   = f.IsUnbounded || null == f.Value || (int)f.Value > VfpMapping.MaximumCharacterFieldSize;

                if (isFixedLength)
                {
                    return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["char"], false, true, VfpMapping.MaximumCharacterFieldSize));
                }

                if (isMaxLength)
                {
                    return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["memo"], false, false));
                }

                return(TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], false, false, VfpMapping.MaximumCharacterFieldSize));

            case PrimitiveTypeKind.Time:
            case PrimitiveTypeKind.DateTime:
                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));
            }
        }
        /// <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));
            }
        }
        /// <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));
            }
        }
Example #12
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));
            }
        }
Example #13
0
        public override TypeUsage GetEdmType(TypeUsage storeType)
        {
            Check.NotNull <TypeUsage>(storeType, nameof(storeType));
            string lowerInvariant = storeType.EdmType.Name.ToLowerInvariant();

            if (!this.StoreTypeNameToEdmPrimitiveType.ContainsKey(lowerInvariant))
            {
                throw new ArgumentException(Strings.ProviderDoesNotSupportType((object)lowerInvariant));
            }
            PrimitiveType     primitiveType = this.StoreTypeNameToEdmPrimitiveType[lowerInvariant];
            int               maxLength     = 0;
            bool              isUnicode     = true;
            PrimitiveTypeKind primitiveTypeKind;
            bool              flag;
            bool              isFixedLength;

            switch (lowerInvariant)
            {
            case "tinyint":
            case "smallint":
            case "bigint":
            case "bit":
            case "uniqueidentifier":
            case "int":
            case "geography":
            case "geometry":
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)primitiveType));

            case "varchar":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode     = false;
                isFixedLength = false;
                break;

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

            case "nvarchar":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isUnicode     = true;
                isFixedLength = false;
                break;

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

            case "varchar(max)":
            case "text":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = true;
                isUnicode     = false;
                isFixedLength = false;
                break;

            case "nvarchar(max)":
            case "ntext":
            case "xml":
                primitiveTypeKind = PrimitiveTypeKind.String;
                flag          = true;
                isUnicode     = true;
                isFixedLength = false;
                break;

            case "binary":
                primitiveTypeKind = PrimitiveTypeKind.Binary;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLength = true;
                break;

            case "varbinary":
                primitiveTypeKind = PrimitiveTypeKind.Binary;
                flag          = !storeType.TryGetMaxLength(out maxLength);
                isFixedLength = false;
                break;

            case "varbinary(max)":
            case "image":
                primitiveTypeKind = PrimitiveTypeKind.Binary;
                flag          = true;
                isFixedLength = false;
                break;

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

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

            case "decimal":
            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 "money":
                return(TypeUsage.CreateDecimalTypeUsage(primitiveType, (byte)19, (byte)4));

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

            case "datetime":
            case "datetime2":
            case "smalldatetime":
                return(TypeUsage.CreateDateTimeTypeUsage(primitiveType, new byte?()));

            case "date":
                return(TypeUsage.CreateDefaultTypeUsage((EdmType)primitiveType));

            case "time":
                return(TypeUsage.CreateTimeTypeUsage(primitiveType, new byte?()));

            case "datetimeoffset":
                return(TypeUsage.CreateDateTimeOffsetTypeUsage(primitiveType, new byte?()));

            default:
                throw new NotSupportedException(Strings.ProviderDoesNotSupportType((object)lowerInvariant));
            }
            switch (primitiveTypeKind)
            {
            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(Strings.ProviderDoesNotSupportType((object)lowerInvariant));
            }
        }