internal static bool TryGetMaxLength(this TypeUsage type, out int maxLength)
 {
     if (type.IsPrimitiveType(PrimitiveTypeKind.String) || type.IsPrimitiveType(PrimitiveTypeKind.Binary))
     {
         return(type.TryGetFacetValue <int>("MaxLength", out maxLength));
     }
     maxLength = 0;
     return(false);
 }
        internal static bool TryGetIsFixedLength(this TypeUsage type, out bool isFixedLength) {
            isFixedLength = false;

            if (!type.IsPrimitiveType(PrimitiveTypeKind.String) && !type.IsPrimitiveType(PrimitiveTypeKind.Binary)) {
                return false;
            }

            var temp = type.GetFacetValueOrDefault<bool?>(FacetInfo.FixedLengthFacetName, null);

            if (temp.HasValue) {
                isFixedLength = temp.Value;
            }

            return temp.HasValue;
        }
 internal static bool IsPrimitiveType(this TypeUsage type, PrimitiveTypeKind primitiveTypeKind)
 {
     if (type.IsPrimitiveType())
     {
         return(((PrimitiveType)type.EdmType).PrimitiveTypeKind == primitiveTypeKind);
     }
     return(false);
 }
 internal static bool TryGetScale(this TypeUsage type, out byte scale)
 {
     if (type.IsPrimitiveType(PrimitiveTypeKind.Decimal))
     {
         return(type.TryGetFacetValue <byte>("Scale", out scale));
     }
     scale = (byte)0;
     return(false);
 }
 internal static bool TryGetPrecision(this TypeUsage type, out byte precision)
 {
     if (type.IsPrimitiveType(PrimitiveTypeKind.Decimal))
     {
         return(type.TryGetFacetValue <byte>("Precision", out precision));
     }
     precision = (byte)0;
     return(false);
 }
 internal static bool TryGetIsUnicode(this TypeUsage type, out bool isUnicode)
 {
     if (type.IsPrimitiveType(PrimitiveTypeKind.String))
     {
         return(type.TryGetFacetValue <bool>("Unicode", out isUnicode));
     }
     isUnicode = false;
     return(false);
 }
Exemplo n.º 7
0
        internal static bool IsPrimitiveType(this TypeUsage type, PrimitiveTypeKind primitiveTypeKind)
        {
            // Null type okay--returns false

            return(type.IsPrimitiveType() && ((PrimitiveType)type.EdmType).PrimitiveTypeKind == primitiveTypeKind);
        }
        /// <summary>
        /// Sets the parameter value and appropriate facets for the given <see cref="TypeUsage"/>.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        /// <param name="parameterType">The type of the parameter.</param>
        /// <param name="value">The value of the parameter.</param>
        protected override void SetDbParameterValue(DbParameter parameter, TypeUsage parameterType, object value)
        {
            Check.NotNull(parameter, "parameter");
            Check.NotNull(parameterType, "parameterType");

            // Ensure a value that can be used with SqlParameter
            value = EnsureSqlParameterValue(value);

            if (parameterType.IsPrimitiveType(PrimitiveTypeKind.String)
                || parameterType.IsPrimitiveType(PrimitiveTypeKind.Binary))
            {
                var size = GetParameterSize(parameterType, ((parameter.Direction & ParameterDirection.Output) == ParameterDirection.Output));
                if (!size.HasValue)
                {
                    // Remember the current Size
                    var previousSize = parameter.Size;

                    // Infer the Size from the value
                    parameter.Size = 0;
                    parameter.Value = value;

                    if (previousSize > -1)
                    {
                        // The 'max' length was chosen as a specific value for the parameter's Size property on Sql8 (4000 or 8000)
                        // because no MaxLength was specified in the TypeUsage and the provider is Sql8. 
                        // If the value's length is less than or equal to this preset size, then the Size value can be retained, 
                        // otherwise this preset size must be removed in favor of the Size inferred from the value itself.

                        // If the inferred Size is less than the preset 'max' size, restore that preset size
                        if (parameter.Size < previousSize)
                        {
                            parameter.Size = previousSize;
                        }
                    }
                    else
                    {
                        // -1 was chosen as the parameter's size because no MaxLength was specified in the TypeUsage and the 
                        // provider is more recent than Sql8. However, it is more optimal to specify a non-max (-1) value for
                        // the size where possible, since 'max' parameters may prevent, for example, filter pushdown.
                        // (see Dev10#617447 for more details)
                        var suggestedLength = GetNonMaxLength(((SqlParameter)parameter).SqlDbType);
                        if (parameter.Size < suggestedLength)
                        {
                            parameter.Size = suggestedLength;
                        }
                        else if (parameter.Size > suggestedLength)
                        {
                            // The parameter size is greater than the suggested length, so the suggested length cannot be used.
                            // Since the provider is Sql9 or newer, set the size to max (-1) instead of the inferred size for better plan reuse.
                            parameter.Size = -1;
                        }
                    }
                }
                else
                {
                    // Just set the value
                    parameter.Value = value;
                }
            }
            else
            {
                // Not a string or binary parameter - just set the value
                parameter.Value = value;
            }
        }