Exemple #1
0
        public void Basic()
        {
            var attrib = new SqlFacetAttribute();

            attrib.IsFixedLength = true;
            attrib.IsNullable    = false;
            attrib.MaxSize       = 123;
            attrib.Precision     = 234;
            attrib.Scale         = 345;

            Assert.True(attrib.IsFixedLength);
            Assert.False(attrib.IsNullable);
            Assert.Equal(123, attrib.MaxSize);
            Assert.Equal(234, attrib.Precision);
            Assert.Equal(345, attrib.Scale);
        }
        public static string GetSqlType_Text(Type type, ICustomAttributeProvider provider, IDictionary <string, string> map)
        {
            StringBuilder sb = new StringBuilder();
            SqlTypeInfo   sqlTypeInfo;

            if (type.IsByRef)
            {
                type = type.GetElementType();
            }
            type = Nullable.GetUnderlyingType(type) ?? type;
            if (type.IsEnum)
            {
                type = Enum.GetUnderlyingType(type);
            }

            if (typesMap.TryGetValue(type, out sqlTypeInfo))
            {
                FunctionParameterAttribute paramAttr = provider != null ? (FunctionParameterAttribute)provider.GetCustomAttributes(typeof(FunctionParameterAttribute), false).FirstOrDefault() : null;
                SqlFacetAttribute          facetAttr = provider != null ? (SqlFacetAttribute)provider.GetCustomAttributes(typeof(SqlFacetAttribute), false).FirstOrDefault() : null;
                sb.Append(paramAttr != null && !string.IsNullOrEmpty(paramAttr.TypeName) ? paramAttr.TypeName : facetAttr != null && facetAttr.IsFixedLength ? sqlTypeInfo.TypeNameFixed : sqlTypeInfo.TypeName);
                //  Append type facets
                switch (sqlTypeInfo.Facet)
                {
                case SqlTypeFacet.Size:
                    if (facetAttr != null && (facetAttr.MaxSize > 0 || facetAttr.MaxSize == -1))
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "({0})", facetAttr.MaxSize == -1 ? (object)"max" : facetAttr.MaxSize);
                    }
                    else if (sqlTypeInfo.MaxSize.HasValue)
                    {
                        sb.AppendFormat(CultureInfo.InvariantCulture, "({0})", sqlTypeInfo.MaxSize == -1 ? (object)"max" : sqlTypeInfo.MaxSize);
                    }
                    break;

                case SqlTypeFacet.PrecisionScale:
                    if (facetAttr != null && facetAttr.Precision > 0)
                    {
                        if (facetAttr.Scale > 0)
                        {
                            sb.AppendFormat(CultureInfo.InvariantCulture, "({0}, {1})", facetAttr.Precision, facetAttr.Scale);
                        }
                        else
                        {
                            sb.AppendFormat(CultureInfo.InvariantCulture, "({0})", facetAttr.Precision);
                        }
                    }
                    else if (sqlTypeInfo.Precision.HasValue && sqlTypeInfo.Precision.Value > 0)
                    {
                        if (sqlTypeInfo.Scale.HasValue && sqlTypeInfo.Scale.Value > 0)
                        {
                            sb.AppendFormat(CultureInfo.InvariantCulture, "({0}, {1})", sqlTypeInfo.Precision.Value, sqlTypeInfo.Scale.Value);
                        }
                        else
                        {
                            sb.AppendFormat(CultureInfo.InvariantCulture, "({0})", sqlTypeInfo.Precision.Value);
                        }
                    }
                    break;
                }
            }
            else if (type.IsDefined(typeof(SqlUserDefinedTypeAttribute)))
            {
                SqlUserDefinedTypeAttribute udt = type.GetCustomAttribute <SqlUserDefinedTypeAttribute>();
                string schema;
                if (!map.TryGetValue(type.FullName, out schema))
                {
                    schema = "dbo";
                }
                sb.AppendFormat("[{0}].[{1}]", schema, !string.IsNullOrEmpty(udt.Name) ? udt.Name : type.Name);
            }
            else
            {
                throw new ArgumentException(string.Format("Framework type '{0}' is not mapped to sql type", type.FullName));
            }
            return(sb.ToString());
        }
Exemple #3
0
        bool GetFacet(ref string sParam, ICustomAttributeProvider ap, ref string prmName, ref object defVal)
        {
            //Debugger.Launch();
            bool isFacet         = false;
            bool SetFacet        = false;
            Type attr            = typeof(SqlFacetAttribute);
            Type attr2           = typeof(SqlParamFacetAttribute);
            SqlFacetAttribute fc = null;

            if (ap.IsDefined(attr2, false))
            {
                SqlParamFacetAttribute prmfcarr = ((SqlParamFacetAttribute[])ap.GetCustomAttributes(attr2, false))[0];
                if (prmfcarr.Name != null)
                {
                    prmName = prmfcarr.Name;
                }
                if (prmfcarr.DefaultValue != null)
                {
                    defVal = prmfcarr.DefaultValue;
                }
                isFacet = true;
                fc      = (SqlFacetAttribute)prmfcarr;
            }

            if (ap.IsDefined(attr, false) && !isFacet)
            {
                SqlFacetAttribute[] fcarr = (SqlFacetAttribute[])ap.GetCustomAttributes(attr, false);

                //I assume I can only have one sqlfacet per param
                fc      = (SqlFacetAttribute)fcarr[0];
                isFacet = true;
            }

            if (isFacet)
            {
                switch (sParam)
                {
                case "nvarchar":
                    if (fc.IsFixedLength)
                    {
                        sParam   = "nchar";
                        SetFacet = true;
                    }

                    if (fc.MaxSize > 0 && fc.MaxSize < 4001)
                    {
                        sParam   = sParam + "(" + fc.MaxSize.ToString() + ")";
                        SetFacet = true;
                    }
                    else if (fc.MaxSize == -1)
                    {
                        sParam   = sParam + "(max)";
                        SetFacet = true;
                    }
                    else
                    {
                        sParam   = sParam + "(50)";
                        SetFacet = true;
                    }

                    break;

                case "varbinary":
                    if (fc.MaxSize == -1)
                    {
                        sParam   = sParam + "(max)";
                        SetFacet = true;
                    }
                    else if (fc.MaxSize > 0)
                    {
                        sParam   = sParam + "(" + fc.MaxSize.ToString() + ")";
                        SetFacet = true;
                    }
                    else
                    {
                        sParam   = sParam + "(50)";
                        SetFacet = true;
                    }

                    break;

                case "decimal":
                    if (fc.Precision > 0 && fc.Scale > 0)
                    {
                        sParam   = sParam + "(" + fc.Precision.ToString() + ", " + fc.Scale.ToString() + ")";
                        SetFacet = true;
                    }

                    break;
                }
            }

            return(SetFacet);
        }