Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / EntityModel / SchemaObjectModel / ModelFunction.cs
1 //---------------------------------------------------------------------
2 // <copyright file="ModelFunction.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 namespace System.Data.EntityModel.SchemaObjectModel
11 {
12     using System.Data.Entity;
13     using System.Data.Metadata.Edm;
14     using System.Diagnostics;
15     using System.Xml;
16
17     /// <summary>
18     /// class representing the Schema element in the schema
19     /// </summary>
20     internal sealed class ModelFunction : Function
21     {
22         private TypeUsageBuilder _typeUsageBuilder;
23
24         #region Public Methods
25         /// <summary>
26         /// ctor for a schema function
27         /// </summary>
28         public ModelFunction(Schema parentElement)
29             :
30             base(parentElement)
31         {
32             _isComposable = true;
33             _typeUsageBuilder = new TypeUsageBuilder(this);
34         }
35
36         #endregion
37
38         public override SchemaType Type
39         {
40             get
41             {
42                 return this._type;
43             }
44         }
45
46         internal TypeUsage TypeUsage
47         {
48             get
49             {
50                 if (_typeUsageBuilder.TypeUsage == null)
51                 {
52                     return null;
53                 }
54                 else if (CollectionKind != CollectionKind.None)
55                 {
56                     return TypeUsage.Create(new CollectionType(_typeUsageBuilder.TypeUsage));
57                 }
58                 else
59                 {
60                     return _typeUsageBuilder.TypeUsage;
61                 }
62             }
63         }
64
65         internal void ValidateAndSetTypeUsage(ScalarType scalar)
66         {
67             _typeUsageBuilder.ValidateAndSetTypeUsage(scalar, false);
68         }
69
70         internal void ValidateAndSetTypeUsage(EdmType edmType)
71         {
72             _typeUsageBuilder.ValidateAndSetTypeUsage(edmType, false);
73         }
74
75         #region Protected Properties
76         protected override bool HandleElement(XmlReader reader)
77         {
78             if (base.HandleElement(reader))
79             {
80                 return true;
81             }
82             else if (CanHandleElement(reader, XmlConstants.DefiningExpression))
83             {
84                 HandleDefiningExpressionElment(reader);
85                 return true;
86             }
87             else if (CanHandleElement(reader, XmlConstants.Parameter))
88             {
89                 HandleParameterElement(reader);
90                 return true;
91             }
92
93             return false;
94         }
95
96         protected override void HandleReturnTypeAttribute(XmlReader reader)
97         {
98             base.HandleReturnTypeAttribute(reader);
99             _isComposable = true;
100         }
101
102         protected override bool HandleAttribute(XmlReader reader)
103         {
104             if (base.HandleAttribute(reader))
105             {
106                 return true;
107             }
108             else if (_typeUsageBuilder.HandleAttribute(reader))
109             {
110                 return true;
111             }
112
113             return false;
114         }
115
116         internal override void ResolveTopLevelNames()
117         {
118             if (null != UnresolvedReturnType)
119             {
120                 if (Schema.ResolveTypeName(this, UnresolvedReturnType, out _type))
121                 {
122                     if (_type is ScalarType)
123                     {
124                         _typeUsageBuilder.ValidateAndSetTypeUsage(_type as ScalarType, false);
125                     }
126                 }
127
128             }
129
130             foreach (Parameter parameter in this.Parameters)
131             {
132                 parameter.ResolveTopLevelNames();
133             }
134
135             if (ReturnTypeList != null)
136             {
137                 Debug.Assert(ReturnTypeList.Count == 1, "returnTypeList should always be non-empty.  Multiple ReturnTypes are only possible on FunctionImports.");
138                 ReturnTypeList[0].ResolveTopLevelNames();
139             }
140         }
141         #endregion
142
143         private void HandleDefiningExpressionElment(XmlReader reader)
144         {
145             Debug.Assert(reader != null);
146
147             FunctionCommandText commandText = new FunctionCommandText(this);
148             commandText.Parse(reader);
149             _commandText = commandText;
150         }
151
152         internal override void Validate()
153         {
154             base.Validate();
155
156             ValidationHelper.ValidateFacets(this, _type, _typeUsageBuilder);
157
158             if (_isRefType)
159             {
160                 ValidationHelper.ValidateRefType(this, _type);
161             }
162         }
163     }
164 }