CqlErrorHelper.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataEntity / System / Data / Common / EntitySql / CqlErrorHelper.cs / 2 / CqlErrorHelper.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner  [....]
// @backup [....] 
//--------------------------------------------------------------------- 

namespace System.Data.Common.EntitySql 
{
    using System;
    using System.Collections.Generic;
    using System.Globalization; 
    using System.Text;
    using System.Diagnostics; 
    using System.Diagnostics.CodeAnalysis; 

    using System.Data.Metadata.Edm; 
    using System.Data.Common.CommandTrees;

    /// 
    /// Error reporting Helper 
    /// 
    internal static class CqlErrorHelper 
    { 
        /// 
        /// Handles function resolution error 
        /// 
        /// 
        /// 
        ///  
        internal static void ReportFunctionOverloadError( MethodExpr functionExpr, EdmFunction functionType, List argTypes )
        { 
            string strDelim = ""; 
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(functionExpr.MethodPrefixExpr.FullName).Append("("); 
            for (int i = 0 ; i < argTypes.Count ; i++)
            {
                sb.Append(strDelim);
                sb.Append(argTypes[i].EdmType.NamespaceName); 
                sb.Append(".");
                sb.Append(argTypes[i].EdmType.Name); 
                strDelim = ", "; 
            }
            sb.Append(")"); 

            Func formatString;
            if (TypeSemantics.IsAggregateFunction(functionType))
            { 
                formatString = TypeHelpers.IsCanonicalFunction(functionType) ?
                                            (Func)System.Data.Entity.Strings.NoCanonicalAggrFunctionOverloadMatch : 
                                            (Func)System.Data.Entity.Strings.NoAggrFunctionOverloadMatch; 
            }
            else 
            {
                formatString = TypeHelpers.IsCanonicalFunction(functionType) ?
                                            (Func)System.Data.Entity.Strings.NoCanonicalFunctionOverloadMatch :
                                            (Func)System.Data.Entity.Strings.NoFunctionOverloadMatch; 
            }
 
            throw EntityUtil.EntitySqlError(functionExpr.ErrCtx.QueryText, 
                                 formatString(functionType.NamespaceName, functionType.Name, sb.ToString()),
                                 functionExpr.ErrCtx.InputPosition, 
                                 System.Data.Entity.Strings.CtxFunction(functionType.Name),
                                 false /* loadContextInfoFromResource */);
        }
 
        /// 
        /// Reports ambiguous function resolution 
        ///  
        /// 
        ///  
        internal static void ReportAmbiguousFunctionError(MethodExpr functionExpr, List foundNamespaces)
        {
            Debug.Assert(foundNamespaces.Count == 2,"foundNamespaces must have 2 entries only");
 
            bool hasCanonicalFunction = foundNamespaces[0].Equals(EdmConstants.CanonicalFunctionNamespace, StringComparison.OrdinalIgnoreCase) ||
                                        foundNamespaces[1].Equals(EdmConstants.CanonicalFunctionNamespace, StringComparison.OrdinalIgnoreCase); 
 
            if (hasCanonicalFunction)
            { 
                string nonCanonicalNamespace = foundNamespaces[0].Equals(EdmConstants.CanonicalFunctionNamespace, StringComparison.OrdinalIgnoreCase) ? foundNamespaces[1] : foundNamespaces[0];

                throw EntityUtil.EntitySqlError(functionExpr.ErrCtx,
                                            System.Data.Entity.Strings.AmbiguousCanonicalFunction( 
                                                                functionExpr.MethodPrefixExpr.FullName,
                                                                functionExpr.MethodName, 
                                                                nonCanonicalNamespace)); 
            }
            else 
            {
                throw EntityUtil.EntitySqlError(functionExpr.ErrCtx,
                                            System.Data.Entity.Strings.AmbiguousFunction(
                                                                functionExpr.MethodPrefixExpr.FullName, 
                                                                functionExpr.MethodName,
                                                                foundNamespaces[0], 
                                                                foundNamespaces[1])); 
            }
        } 

        /// 
        /// Handles Identifier resolution failure. The purpose is to provide better error feedback.
        ///  
        /// 
        ///  
        internal static void ReportIdentifierError(Expr expr, SemanticResolver sr) 
        {
            string varName = null; 
            string fullName = null;
            Identifier id = expr as Identifier;
            if (null != id)
            { 
                varName = fullName = id.Name;
            } 
 
            DotExpr dotExpr = expr as DotExpr;
            if (null != dotExpr) 
            {
                Debug.Assert(dotExpr.IsDottedIdentifier, "dotExpr.IsDottedIdentifier");
                varName = dotExpr.Names[0];
                fullName = dotExpr.FullName; 
            }
 
            if (null == dotExpr && null == id) 
            {
                EntityUtil.Argument("expr must be an identifier"); 
            }

            sr.SetScopeView(SemanticResolver.ScopeViewKind.CurrentContext);
            ScopeEntry scopeEntry; 
            int scopeIndex;
            if (sr.CurrentScopeRegionFlags.IsInGroupScope && sr.TryScopeLookup(varName, true /* ignoreGroupScope */, out scopeEntry, out scopeIndex)) 
            { 
                if (scopeEntry.VarKind == SourceVarKind.GroupInput)
                { 
                    throw EntityUtil.EntitySqlError(expr.ErrCtx, System.Data.Entity.Strings.InvalidGroupIdentifierReference(fullName));
                }
            }
            throw EntityUtil.EntitySqlError(expr.ErrCtx, System.Data.Entity.Strings.CouldNotResolveIdentifier(fullName)); 
        }
 
        ///  
        /// provides error feedback for aliases already used in a given context
        ///  
        /// 
        /// 
        /// 
        internal static void ReportAliasAlreadyUsedError( string aliasName, ErrorContext errCtx, string contextMessage ) 
        {
            throw EntityUtil.EntitySqlError(errCtx, String.Format(CultureInfo.InvariantCulture, "{0} {1}", System.Data.Entity.Strings.AliasNameAlreadyUsed(aliasName), contextMessage)); 
        } 

        ///  
        /// Reports incompatible type error
        /// 
        /// 
        ///  
        /// 
        internal static void ReportIncompatibleCommonType( ErrorContext errCtx, TypeUsage leftType, TypeUsage rightType ) 
        { 
            //
            // 'navigate' through the type structure in order to find where the incompability is 
            //
            ReportIncompatibleCommonType(errCtx, leftType, rightType, leftType, rightType);

            // 
            // if we hit this point, throw the generic incompatible type error message
            // 
            throw EntityUtil.EntitySqlError(errCtx, System.Data.Entity.Strings.ArgumentTypesAreIncompatible(leftType.Identity, rightType.Identity)); 
        }
 
        /// 
        /// navigates through the type structure to find where the incompatibility happens
        /// 
        ///  
        /// 
        ///  
        ///  
        /// 
        private static void ReportIncompatibleCommonType( ErrorContext errCtx, TypeUsage rootLeftType, TypeUsage rootRightType, TypeUsage leftType, TypeUsage rightType ) 
        {
            TypeUsage commonType = null;
            bool isRootType = (rootLeftType == leftType);
            string errorMessage = String.Empty; 

            if (leftType.EdmType.BuiltInTypeKind != rightType.EdmType.BuiltInTypeKind) 
            { 
                throw EntityUtil.EntitySqlError(errCtx,
                                     System.Data.Entity.Strings.TypeKindMismatch( 
                                                   GetReadableTypeKind(leftType),
                                                   GetReadableTypeName(leftType),
                                                   GetReadableTypeKind(rightType),
                                                   GetReadableTypeName(rightType))); 
            }
 
            switch( leftType.EdmType.BuiltInTypeKind ) 
            {
                case BuiltInTypeKind.RowType: 
                    RowType leftRow = (RowType)leftType.EdmType;
                    RowType rightRow = (RowType)rightType.EdmType;

                    if (leftRow.Members.Count != rightRow.Members.Count) 
                    {
                        if (isRootType) 
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidRootRowType(
                                                         GetReadableTypeName(leftRow), 
                                                         GetReadableTypeName(rightRow));
                        }
                        else
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidRowType(
                                                         GetReadableTypeName(leftRow), 
                                                         GetReadableTypeName(rootLeftType), 
                                                         GetReadableTypeName(rightRow),
                                                         GetReadableTypeName(rootRightType)); 
                        }

                        throw EntityUtil.EntitySqlError(errCtx, errorMessage);
                    } 

                    for (int i = 0 ; i < leftRow.Members.Count ; i++) 
                    { 
                        ReportIncompatibleCommonType(errCtx, rootLeftType, rootRightType, leftRow.Members[i].TypeUsage, rightRow.Members[i].TypeUsage);
                    } 
                break;

                case BuiltInTypeKind.CollectionType:
                case BuiltInTypeKind.RefType: 
                    ReportIncompatibleCommonType(errCtx,
                                               rootLeftType, 
                                               rootRightType, 
                                               TypeHelpers.GetElementTypeUsage(leftType),
                                               TypeHelpers.GetElementTypeUsage(rightType)); 
                    break;

                case BuiltInTypeKind.EntityType:
                    if (!TypeSemantics.TryGetCommonType(leftType, rightType, out commonType)) 
                    {
                        if (isRootType) 
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidEntityRootTypeArgument(
                                                         GetReadableTypeName(leftType), 
                                                         GetReadableTypeName(rightType));
                        }
                        else
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidEntityTypeArgument(
                                                         GetReadableTypeName(leftType), 
                                                         GetReadableTypeName(rootLeftType), 
                                                         GetReadableTypeName(rightType),
                                                         GetReadableTypeName(rootRightType)); 
                        }
                        throw EntityUtil.EntitySqlError(errCtx, errorMessage);
                    }
                    break; 

                case BuiltInTypeKind.ComplexType: 
                    ComplexType leftComplex = (ComplexType)leftType.EdmType; 
                    ComplexType rightComplex = (ComplexType)rightType.EdmType;
                    if (leftComplex.Members.Count != rightComplex.Members.Count) 
                    {
                        if (isRootType)
                        {
                            errorMessage = System.Data.Entity.Strings.InvalidRootComplexType( 
                                                         GetReadableTypeName(leftComplex),
                                                         GetReadableTypeName(rightComplex)); 
                        } 
                        else
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidComplexType(
                                                         GetReadableTypeName(leftComplex),
                                                         GetReadableTypeName(rootLeftType),
                                                         GetReadableTypeName(rightComplex), 
                                                         GetReadableTypeName(rootRightType));
                        } 
                        throw EntityUtil.EntitySqlError(errCtx, errorMessage); 
                    }
 
                    for (int i = 0 ; i < leftComplex.Members.Count ; i++)
                    {
                        ReportIncompatibleCommonType(errCtx,
                                                   rootLeftType, 
                                                   rootRightType,
                                                   leftComplex.Members[i].TypeUsage, 
                                                   rightComplex.Members[i].TypeUsage); 
                    }
                    break; 

                default:
                    if (!TypeSemantics.TryGetCommonType(leftType, rightType, out commonType))
                    { 
                        if (isRootType)
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidPlaceholderRootTypeArgument( 
                                                               GetReadableTypeKind(leftType),
                                                               GetReadableTypeName(leftType), 
                                                               GetReadableTypeKind(rightType),
                                                               GetReadableTypeName(rightType));
                        }
                        else 
                        {
                            errorMessage = System.Data.Entity.Strings.InvalidPlaceholderTypeArgument( 
                                                               GetReadableTypeKind(leftType), 
                                                               GetReadableTypeName(leftType),
                                                               GetReadableTypeName(rootLeftType), 
                                                               GetReadableTypeKind(rightType),
                                                               GetReadableTypeName(rightType),
                                                               GetReadableTypeName(rootRightType));
                        } 
                        throw EntityUtil.EntitySqlError(errCtx, errorMessage);
                    } 
                    break; 
            }
        } 

        /// 
        /// Reports Type resolution error
        ///  
        /// 
        ///  
        ///  
        internal static void ReportTypeResolutionError(string[] names, Expr astExpr, SemanticResolver sr)
        { 
            string typeNamespace = names.Length > 1 ? String.Join(".", names, 0, names.Length - 1) : String.Empty;
            string typeName = names[names.Length - 1];
            ErrorContext errCtx = astExpr.ErrCtx;
 
            //
            // Try to see if the type is in canonical namespace 
            // 
            if (typeNamespace.Equals(EdmConstants.EdmNamespace, StringComparison.OrdinalIgnoreCase))
            { 
                EdmType edmType;
                // check if the type exists in CSpace
                if (!sr.TypeResolver.Perspective.MetadataWorkspace.TryGetType(typeName, typeNamespace, true /*ignoreCase*/, DataSpace.CSpace, out edmType))
                { 
                    throw EntityUtil.EntitySqlError(errCtx,
                                                System.Data.Entity.Strings.CanonicalTypeNameNotFound( 
                                                                    typeName, 
                                                                    EdmConstants.EdmNamespace.ToUpperInvariant()));
                } 

                throw EntityUtil.EntitySqlError(errCtx,
                                            System.Data.Entity.Strings.CanonicalTypeCannotBeMapped(
                                                                TypeResolver.GetFullName(names))); 
            }
 
            // 
            // make sure ROW and MULTISET expressions/keywords are not being used as type identifiers. throw syntax error
            // 
            if (null != names && names.Length == 1)
            {
                Identifier idNode = astExpr as Identifier;
                MethodExpr methodNode = astExpr as MethodExpr; 
                bool isEscaped = (null != methodNode && methodNode.MethodIdentifier.IsEscaped) || (null != idNode && idNode.IsEscaped);
                if (!isEscaped) 
                { 
                    if (names[0].Equals("ROW", StringComparison.OrdinalIgnoreCase) ||
                        names[0].Equals("MULTISET", StringComparison.OrdinalIgnoreCase)) 
                    {
                        throw EntityUtil.EntitySqlError(errCtx, System.Data.Entity.Strings.GenericSyntaxError);
                    }
                } 
            }
 
            throw EntityUtil.EntitySqlError(errCtx, System.Data.Entity.Strings.TypeNameNotFound(TypeResolver.GetFullName(names))); 
        }
 

        internal static EntitySqlException ReportIdentifierElementError(ErrorContext errCtx, string name, TypeUsage definingType)
        {
            if (TypeSemantics.IsCollectionType(definingType)) 
            {
                return EntityUtil.EntitySqlError(errCtx, 
                                             System.Data.Entity.Strings.NotAMemberOfCollection( 
                                                                name,
                                                                definingType.EdmType.FullName)); 
            }
            else
            {
                return EntityUtil.EntitySqlError(errCtx, 
                                             System.Data.Entity.Strings.NotAMemberOfType(
                                                                name, 
                                                                definingType.EdmType.FullName)); 
            }
        } 

        #region Private Type Name Helpers
        private static string GetReadableTypeName( TypeUsage type )
        { 
            return GetReadableTypeName(type.EdmType);
        } 
 
        private static string GetReadableTypeName( EdmType type )
        { 
            if (type.BuiltInTypeKind == BuiltInTypeKind.RowType ||
                type.BuiltInTypeKind == BuiltInTypeKind.CollectionType ||
                type.BuiltInTypeKind == BuiltInTypeKind.RefType)
            { 
                return type.Name;
            } 
            return type.FullName; 
        }
 
        private static string GetReadableTypeKind( TypeUsage type )
        {
            return GetReadableTypeKind(type.EdmType);
        } 

        private static string GetReadableTypeKind( EdmType type ) 
        { 
            string typeKindName = String.Empty;
            switch( type.BuiltInTypeKind) 
            {
                case BuiltInTypeKind.RowType:
                    typeKindName = System.Data.Entity.Strings.LocalizedRow;
                    break; 
                case BuiltInTypeKind.CollectionType:
                    typeKindName = System.Data.Entity.Strings.LocalizedCollection; 
                    break; 
                case BuiltInTypeKind.RefType:
                    typeKindName = System.Data.Entity.Strings.LocalizedReference; 
                    break;
                case BuiltInTypeKind.EntityType:
                    typeKindName = System.Data.Entity.Strings.LocalizedEntity;
                    break; 
                case BuiltInTypeKind.ComplexType:
                    typeKindName = System.Data.Entity.Strings.LocalizedComplex; 
                    break; 
                case BuiltInTypeKind.PrimitiveType:
                    typeKindName = System.Data.Entity.Strings.LocalizedPrimitive; 
                    break;
                default:
                    typeKindName = type.BuiltInTypeKind.ToString();
                    break; 
            }
            return typeKindName + " " + System.Data.Entity.Strings.LocalizedType; 
        } 
        #endregion
    } 

}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner  [....]
// @backup [....] 
//--------------------------------------------------------------------- 

namespace System.Data.Common.EntitySql 
{
    using System;
    using System.Collections.Generic;
    using System.Globalization; 
    using System.Text;
    using System.Diagnostics; 
    using System.Diagnostics.CodeAnalysis; 

    using System.Data.Metadata.Edm; 
    using System.Data.Common.CommandTrees;

    /// 
    /// Error reporting Helper 
    /// 
    internal static class CqlErrorHelper 
    { 
        /// 
        /// Handles function resolution error 
        /// 
        /// 
        /// 
        ///  
        internal static void ReportFunctionOverloadError( MethodExpr functionExpr, EdmFunction functionType, List argTypes )
        { 
            string strDelim = ""; 
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(functionExpr.MethodPrefixExpr.FullName).Append("("); 
            for (int i = 0 ; i < argTypes.Count ; i++)
            {
                sb.Append(strDelim);
                sb.Append(argTypes[i].EdmType.NamespaceName); 
                sb.Append(".");
                sb.Append(argTypes[i].EdmType.Name); 
                strDelim = ", "; 
            }
            sb.Append(")"); 

            Func formatString;
            if (TypeSemantics.IsAggregateFunction(functionType))
            { 
                formatString = TypeHelpers.IsCanonicalFunction(functionType) ?
                                            (Func)System.Data.Entity.Strings.NoCanonicalAggrFunctionOverloadMatch : 
                                            (Func)System.Data.Entity.Strings.NoAggrFunctionOverloadMatch; 
            }
            else 
            {
                formatString = TypeHelpers.IsCanonicalFunction(functionType) ?
                                            (Func)System.Data.Entity.Strings.NoCanonicalFunctionOverloadMatch :
                                            (Func)System.Data.Entity.Strings.NoFunctionOverloadMatch; 
            }
 
            throw EntityUtil.EntitySqlError(functionExpr.ErrCtx.QueryText, 
                                 formatString(functionType.NamespaceName, functionType.Name, sb.ToString()),
                                 functionExpr.ErrCtx.InputPosition, 
                                 System.Data.Entity.Strings.CtxFunction(functionType.Name),
                                 false /* loadContextInfoFromResource */);
        }
 
        /// 
        /// Reports ambiguous function resolution 
        ///  
        /// 
        ///  
        internal static void ReportAmbiguousFunctionError(MethodExpr functionExpr, List foundNamespaces)
        {
            Debug.Assert(foundNamespaces.Count == 2,"foundNamespaces must have 2 entries only");
 
            bool hasCanonicalFunction = foundNamespaces[0].Equals(EdmConstants.CanonicalFunctionNamespace, StringComparison.OrdinalIgnoreCase) ||
                                        foundNamespaces[1].Equals(EdmConstants.CanonicalFunctionNamespace, StringComparison.OrdinalIgnoreCase); 
 
            if (hasCanonicalFunction)
            { 
                string nonCanonicalNamespace = foundNamespaces[0].Equals(EdmConstants.CanonicalFunctionNamespace, StringComparison.OrdinalIgnoreCase) ? foundNamespaces[1] : foundNamespaces[0];

                throw EntityUtil.EntitySqlError(functionExpr.ErrCtx,
                                            System.Data.Entity.Strings.AmbiguousCanonicalFunction( 
                                                                functionExpr.MethodPrefixExpr.FullName,
                                                                functionExpr.MethodName, 
                                                                nonCanonicalNamespace)); 
            }
            else 
            {
                throw EntityUtil.EntitySqlError(functionExpr.ErrCtx,
                                            System.Data.Entity.Strings.AmbiguousFunction(
                                                                functionExpr.MethodPrefixExpr.FullName, 
                                                                functionExpr.MethodName,
                                                                foundNamespaces[0], 
                                                                foundNamespaces[1])); 
            }
        } 

        /// 
        /// Handles Identifier resolution failure. The purpose is to provide better error feedback.
        ///  
        /// 
        ///  
        internal static void ReportIdentifierError(Expr expr, SemanticResolver sr) 
        {
            string varName = null; 
            string fullName = null;
            Identifier id = expr as Identifier;
            if (null != id)
            { 
                varName = fullName = id.Name;
            } 
 
            DotExpr dotExpr = expr as DotExpr;
            if (null != dotExpr) 
            {
                Debug.Assert(dotExpr.IsDottedIdentifier, "dotExpr.IsDottedIdentifier");
                varName = dotExpr.Names[0];
                fullName = dotExpr.FullName; 
            }
 
            if (null == dotExpr && null == id) 
            {
                EntityUtil.Argument("expr must be an identifier"); 
            }

            sr.SetScopeView(SemanticResolver.ScopeViewKind.CurrentContext);
            ScopeEntry scopeEntry; 
            int scopeIndex;
            if (sr.CurrentScopeRegionFlags.IsInGroupScope && sr.TryScopeLookup(varName, true /* ignoreGroupScope */, out scopeEntry, out scopeIndex)) 
            { 
                if (scopeEntry.VarKind == SourceVarKind.GroupInput)
                { 
                    throw EntityUtil.EntitySqlError(expr.ErrCtx, System.Data.Entity.Strings.InvalidGroupIdentifierReference(fullName));
                }
            }
            throw EntityUtil.EntitySqlError(expr.ErrCtx, System.Data.Entity.Strings.CouldNotResolveIdentifier(fullName)); 
        }
 
        ///  
        /// provides error feedback for aliases already used in a given context
        ///  
        /// 
        /// 
        /// 
        internal static void ReportAliasAlreadyUsedError( string aliasName, ErrorContext errCtx, string contextMessage ) 
        {
            throw EntityUtil.EntitySqlError(errCtx, String.Format(CultureInfo.InvariantCulture, "{0} {1}", System.Data.Entity.Strings.AliasNameAlreadyUsed(aliasName), contextMessage)); 
        } 

        ///  
        /// Reports incompatible type error
        /// 
        /// 
        ///  
        /// 
        internal static void ReportIncompatibleCommonType( ErrorContext errCtx, TypeUsage leftType, TypeUsage rightType ) 
        { 
            //
            // 'navigate' through the type structure in order to find where the incompability is 
            //
            ReportIncompatibleCommonType(errCtx, leftType, rightType, leftType, rightType);

            // 
            // if we hit this point, throw the generic incompatible type error message
            // 
            throw EntityUtil.EntitySqlError(errCtx, System.Data.Entity.Strings.ArgumentTypesAreIncompatible(leftType.Identity, rightType.Identity)); 
        }
 
        /// 
        /// navigates through the type structure to find where the incompatibility happens
        /// 
        ///  
        /// 
        ///  
        ///  
        /// 
        private static void ReportIncompatibleCommonType( ErrorContext errCtx, TypeUsage rootLeftType, TypeUsage rootRightType, TypeUsage leftType, TypeUsage rightType ) 
        {
            TypeUsage commonType = null;
            bool isRootType = (rootLeftType == leftType);
            string errorMessage = String.Empty; 

            if (leftType.EdmType.BuiltInTypeKind != rightType.EdmType.BuiltInTypeKind) 
            { 
                throw EntityUtil.EntitySqlError(errCtx,
                                     System.Data.Entity.Strings.TypeKindMismatch( 
                                                   GetReadableTypeKind(leftType),
                                                   GetReadableTypeName(leftType),
                                                   GetReadableTypeKind(rightType),
                                                   GetReadableTypeName(rightType))); 
            }
 
            switch( leftType.EdmType.BuiltInTypeKind ) 
            {
                case BuiltInTypeKind.RowType: 
                    RowType leftRow = (RowType)leftType.EdmType;
                    RowType rightRow = (RowType)rightType.EdmType;

                    if (leftRow.Members.Count != rightRow.Members.Count) 
                    {
                        if (isRootType) 
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidRootRowType(
                                                         GetReadableTypeName(leftRow), 
                                                         GetReadableTypeName(rightRow));
                        }
                        else
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidRowType(
                                                         GetReadableTypeName(leftRow), 
                                                         GetReadableTypeName(rootLeftType), 
                                                         GetReadableTypeName(rightRow),
                                                         GetReadableTypeName(rootRightType)); 
                        }

                        throw EntityUtil.EntitySqlError(errCtx, errorMessage);
                    } 

                    for (int i = 0 ; i < leftRow.Members.Count ; i++) 
                    { 
                        ReportIncompatibleCommonType(errCtx, rootLeftType, rootRightType, leftRow.Members[i].TypeUsage, rightRow.Members[i].TypeUsage);
                    } 
                break;

                case BuiltInTypeKind.CollectionType:
                case BuiltInTypeKind.RefType: 
                    ReportIncompatibleCommonType(errCtx,
                                               rootLeftType, 
                                               rootRightType, 
                                               TypeHelpers.GetElementTypeUsage(leftType),
                                               TypeHelpers.GetElementTypeUsage(rightType)); 
                    break;

                case BuiltInTypeKind.EntityType:
                    if (!TypeSemantics.TryGetCommonType(leftType, rightType, out commonType)) 
                    {
                        if (isRootType) 
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidEntityRootTypeArgument(
                                                         GetReadableTypeName(leftType), 
                                                         GetReadableTypeName(rightType));
                        }
                        else
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidEntityTypeArgument(
                                                         GetReadableTypeName(leftType), 
                                                         GetReadableTypeName(rootLeftType), 
                                                         GetReadableTypeName(rightType),
                                                         GetReadableTypeName(rootRightType)); 
                        }
                        throw EntityUtil.EntitySqlError(errCtx, errorMessage);
                    }
                    break; 

                case BuiltInTypeKind.ComplexType: 
                    ComplexType leftComplex = (ComplexType)leftType.EdmType; 
                    ComplexType rightComplex = (ComplexType)rightType.EdmType;
                    if (leftComplex.Members.Count != rightComplex.Members.Count) 
                    {
                        if (isRootType)
                        {
                            errorMessage = System.Data.Entity.Strings.InvalidRootComplexType( 
                                                         GetReadableTypeName(leftComplex),
                                                         GetReadableTypeName(rightComplex)); 
                        } 
                        else
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidComplexType(
                                                         GetReadableTypeName(leftComplex),
                                                         GetReadableTypeName(rootLeftType),
                                                         GetReadableTypeName(rightComplex), 
                                                         GetReadableTypeName(rootRightType));
                        } 
                        throw EntityUtil.EntitySqlError(errCtx, errorMessage); 
                    }
 
                    for (int i = 0 ; i < leftComplex.Members.Count ; i++)
                    {
                        ReportIncompatibleCommonType(errCtx,
                                                   rootLeftType, 
                                                   rootRightType,
                                                   leftComplex.Members[i].TypeUsage, 
                                                   rightComplex.Members[i].TypeUsage); 
                    }
                    break; 

                default:
                    if (!TypeSemantics.TryGetCommonType(leftType, rightType, out commonType))
                    { 
                        if (isRootType)
                        { 
                            errorMessage = System.Data.Entity.Strings.InvalidPlaceholderRootTypeArgument( 
                                                               GetReadableTypeKind(leftType),
                                                               GetReadableTypeName(leftType), 
                                                               GetReadableTypeKind(rightType),
                                                               GetReadableTypeName(rightType));
                        }
                        else 
                        {
                            errorMessage = System.Data.Entity.Strings.InvalidPlaceholderTypeArgument( 
                                                               GetReadableTypeKind(leftType), 
                                                               GetReadableTypeName(leftType),
                                                               GetReadableTypeName(rootLeftType), 
                                                               GetReadableTypeKind(rightType),
                                                               GetReadableTypeName(rightType),
                                                               GetReadableTypeName(rootRightType));
                        } 
                        throw EntityUtil.EntitySqlError(errCtx, errorMessage);
                    } 
                    break; 
            }
        } 

        /// 
        /// Reports Type resolution error
        ///  
        /// 
        ///  
        ///  
        internal static void ReportTypeResolutionError(string[] names, Expr astExpr, SemanticResolver sr)
        { 
            string typeNamespace = names.Length > 1 ? String.Join(".", names, 0, names.Length - 1) : String.Empty;
            string typeName = names[names.Length - 1];
            ErrorContext errCtx = astExpr.ErrCtx;
 
            //
            // Try to see if the type is in canonical namespace 
            // 
            if (typeNamespace.Equals(EdmConstants.EdmNamespace, StringComparison.OrdinalIgnoreCase))
            { 
                EdmType edmType;
                // check if the type exists in CSpace
                if (!sr.TypeResolver.Perspective.MetadataWorkspace.TryGetType(typeName, typeNamespace, true /*ignoreCase*/, DataSpace.CSpace, out edmType))
                { 
                    throw EntityUtil.EntitySqlError(errCtx,
                                                System.Data.Entity.Strings.CanonicalTypeNameNotFound( 
                                                                    typeName, 
                                                                    EdmConstants.EdmNamespace.ToUpperInvariant()));
                } 

                throw EntityUtil.EntitySqlError(errCtx,
                                            System.Data.Entity.Strings.CanonicalTypeCannotBeMapped(
                                                                TypeResolver.GetFullName(names))); 
            }
 
            // 
            // make sure ROW and MULTISET expressions/keywords are not being used as type identifiers. throw syntax error
            // 
            if (null != names && names.Length == 1)
            {
                Identifier idNode = astExpr as Identifier;
                MethodExpr methodNode = astExpr as MethodExpr; 
                bool isEscaped = (null != methodNode && methodNode.MethodIdentifier.IsEscaped) || (null != idNode && idNode.IsEscaped);
                if (!isEscaped) 
                { 
                    if (names[0].Equals("ROW", StringComparison.OrdinalIgnoreCase) ||
                        names[0].Equals("MULTISET", StringComparison.OrdinalIgnoreCase)) 
                    {
                        throw EntityUtil.EntitySqlError(errCtx, System.Data.Entity.Strings.GenericSyntaxError);
                    }
                } 
            }
 
            throw EntityUtil.EntitySqlError(errCtx, System.Data.Entity.Strings.TypeNameNotFound(TypeResolver.GetFullName(names))); 
        }
 

        internal static EntitySqlException ReportIdentifierElementError(ErrorContext errCtx, string name, TypeUsage definingType)
        {
            if (TypeSemantics.IsCollectionType(definingType)) 
            {
                return EntityUtil.EntitySqlError(errCtx, 
                                             System.Data.Entity.Strings.NotAMemberOfCollection( 
                                                                name,
                                                                definingType.EdmType.FullName)); 
            }
            else
            {
                return EntityUtil.EntitySqlError(errCtx, 
                                             System.Data.Entity.Strings.NotAMemberOfType(
                                                                name, 
                                                                definingType.EdmType.FullName)); 
            }
        } 

        #region Private Type Name Helpers
        private static string GetReadableTypeName( TypeUsage type )
        { 
            return GetReadableTypeName(type.EdmType);
        } 
 
        private static string GetReadableTypeName( EdmType type )
        { 
            if (type.BuiltInTypeKind == BuiltInTypeKind.RowType ||
                type.BuiltInTypeKind == BuiltInTypeKind.CollectionType ||
                type.BuiltInTypeKind == BuiltInTypeKind.RefType)
            { 
                return type.Name;
            } 
            return type.FullName; 
        }
 
        private static string GetReadableTypeKind( TypeUsage type )
        {
            return GetReadableTypeKind(type.EdmType);
        } 

        private static string GetReadableTypeKind( EdmType type ) 
        { 
            string typeKindName = String.Empty;
            switch( type.BuiltInTypeKind) 
            {
                case BuiltInTypeKind.RowType:
                    typeKindName = System.Data.Entity.Strings.LocalizedRow;
                    break; 
                case BuiltInTypeKind.CollectionType:
                    typeKindName = System.Data.Entity.Strings.LocalizedCollection; 
                    break; 
                case BuiltInTypeKind.RefType:
                    typeKindName = System.Data.Entity.Strings.LocalizedReference; 
                    break;
                case BuiltInTypeKind.EntityType:
                    typeKindName = System.Data.Entity.Strings.LocalizedEntity;
                    break; 
                case BuiltInTypeKind.ComplexType:
                    typeKindName = System.Data.Entity.Strings.LocalizedComplex; 
                    break; 
                case BuiltInTypeKind.PrimitiveType:
                    typeKindName = System.Data.Entity.Strings.LocalizedPrimitive; 
                    break;
                default:
                    typeKindName = type.BuiltInTypeKind.ToString();
                    break; 
            }
            return typeKindName + " " + System.Data.Entity.Strings.LocalizedType; 
        } 
        #endregion
    } 

}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK