Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Validator.cs « Internal « CommandTrees « Common « Data « System « System.Data.Entity « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75ddb26261836b352166235f279e60873aa1b37b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//---------------------------------------------------------------------
// <copyright file="Validator.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner  [....]
// @backupOwner [....]
//---------------------------------------------------------------------

namespace System.Data.Common.CommandTrees.Internal
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Metadata.Edm;
    using System.Diagnostics;
    using System.Linq;

    internal sealed class DbExpressionValidator : DbExpressionRebinder
    {
        private readonly DataSpace requiredSpace;
        private readonly DataSpace[] allowedMetadataSpaces;
        private readonly DataSpace[] allowedFunctionSpaces;
        private readonly Dictionary<string, DbParameterReferenceExpression> paramMappings = new Dictionary<string, DbParameterReferenceExpression>();
        private readonly Stack<Dictionary<string, TypeUsage>> variableScopes = new Stack<Dictionary<string, TypeUsage>>();

        private string expressionArgumentName;

        internal DbExpressionValidator(MetadataWorkspace metadata, DataSpace expectedDataSpace)
            : base(metadata)
        {
            this.requiredSpace = expectedDataSpace;
            this.allowedFunctionSpaces = new[] { DataSpace.CSpace, DataSpace.SSpace };
            if (expectedDataSpace == DataSpace.SSpace)
            {
                this.allowedMetadataSpaces = new[] { DataSpace.SSpace, DataSpace.CSpace };
            }
            else
            {
                this.allowedMetadataSpaces = new[] { DataSpace.CSpace };   
            }
        }

        internal Dictionary<string, DbParameterReferenceExpression> Parameters { get { return this.paramMappings; } }

        internal void ValidateExpression(DbExpression expression, string argumentName)
        {
            Debug.Assert(expression != null, "Ensure expression is non-null before calling ValidateExpression");
            this.expressionArgumentName = argumentName;
            this.VisitExpression(expression);
            this.expressionArgumentName = null;
            Debug.Assert(this.variableScopes.Count == 0, "Variable scope stack left in inconsistent state");
        }

        protected override EntitySetBase VisitEntitySet(EntitySetBase entitySet)
        {
            return ValidateMetadata(entitySet, base.VisitEntitySet, es => es.EntityContainer.DataSpace, this.allowedMetadataSpaces);
        }

        protected override EdmFunction VisitFunction(EdmFunction function)
        {
            // Functions from the current space and S-Space are allowed
            return ValidateMetadata(function, base.VisitFunction, func => func.DataSpace, this.allowedFunctionSpaces);
        }

        protected override EdmType VisitType(EdmType type)
        {
            return ValidateMetadata(type, base.VisitType, et => et.DataSpace, this.allowedMetadataSpaces);
        }

        protected override TypeUsage VisitTypeUsage(TypeUsage type)
        {
            return ValidateMetadata(type, base.VisitTypeUsage, tu => tu.EdmType.DataSpace, this.allowedMetadataSpaces);
        }

        protected override void OnEnterScope(IEnumerable<DbVariableReferenceExpression> scopeVariables)
        {
            var newScope = scopeVariables.ToDictionary(var => var.VariableName, var => var.ResultType, StringComparer.Ordinal);
            this.variableScopes.Push(newScope);
        }

        protected override void OnExitScope()
        {
            this.variableScopes.Pop();
        }

        public override DbExpression Visit(DbVariableReferenceExpression expression)
        {
            DbExpression result = base.Visit(expression);
            if(result.ExpressionKind == DbExpressionKind.VariableReference)
            {
                DbVariableReferenceExpression varRef = (DbVariableReferenceExpression)result;
                TypeUsage foundType = null;
                foreach(Dictionary<string, TypeUsage> scope in this.variableScopes)
                {
                    if(scope.TryGetValue(varRef.VariableName, out foundType))
                    {
                        break;
                    }
                }
                
                if(foundType == null)
                {
                    ThrowInvalid(System.Data.Entity.Strings.Cqt_Validator_VarRefInvalid(varRef.VariableName));
                }
                                
                // SQLBUDT#545720: Equivalence is not a sufficient check (consider row types) - equality is required.
                if (!TypeSemantics.IsEqual(varRef.ResultType, foundType))
                {
                    ThrowInvalid(System.Data.Entity.Strings.Cqt_Validator_VarRefTypeMismatch(varRef.VariableName));
                }
            }

            return result;
        }

        public override DbExpression Visit(DbParameterReferenceExpression expression)
        {
            DbExpression result = base.Visit(expression);
            if (result.ExpressionKind == DbExpressionKind.ParameterReference)
            {
                DbParameterReferenceExpression paramRef = result as DbParameterReferenceExpression;

                DbParameterReferenceExpression foundParam;
                if (this.paramMappings.TryGetValue(paramRef.ParameterName, out foundParam))
                {
                    // SQLBUDT#545720: Equivalence is not a sufficient check (consider row types for TVPs) - equality is required.
                    if (!TypeSemantics.IsEqual(paramRef.ResultType, foundParam.ResultType))
                    {
                        ThrowInvalid(Strings.Cqt_Validator_InvalidIncompatibleParameterReferences(paramRef.ParameterName));
                    }
                }
                else
                {
                    this.paramMappings.Add(paramRef.ParameterName, paramRef);
                }
            }
            return result;
        }

        private TMetadata ValidateMetadata<TMetadata>(TMetadata metadata, Func<TMetadata, TMetadata> map, Func<TMetadata, DataSpace> getDataSpace, DataSpace[] allowedSpaces)
        {
            TMetadata result = map(metadata);
            if (!object.ReferenceEquals(metadata, result))
            {
                ThrowInvalidMetadata(metadata);
            }

            DataSpace resultSpace = getDataSpace(result);
            if (!allowedSpaces.Any(ds => ds == resultSpace))
            {
                ThrowInvalidSpace(metadata);
            }
            return result;
        }
                
        private void ThrowInvalidMetadata<TMetadata>(TMetadata invalid)
        {
            ThrowInvalid(Strings.Cqt_Validator_InvalidOtherWorkspaceMetadata(typeof(TMetadata).Name));
        }

        private void ThrowInvalidSpace<TMetadata>(TMetadata invalid)
        {
            ThrowInvalid(Strings.Cqt_Validator_InvalidIncorrectDataSpaceMetadata(typeof(TMetadata).Name, Enum.GetName(typeof(DataSpace), this.requiredSpace)));
        }

        private void ThrowInvalid(string message)
        {
            throw EntityUtil.Argument(message, this.expressionArgumentName);
        }
    }
}