Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Query / InternalTrees / ColumnMapVisitor.cs
1 //---------------------------------------------------------------------
2 // <copyright file="ColumnMapVisitorWithResults.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner  [....]
7 // @backupOwner [....]
8 //---------------------------------------------------------------------
9
10 namespace System.Data.Query.InternalTrees
11 {
12     using System.Collections;
13     using System.Collections.Generic;
14     using System.Text;
15     using System.Data;
16     using System.Data.Common;
17     using System.Data.Common.CommandTrees;
18     using System.Data.Metadata.Edm;
19     using System.Data.Query.PlanCompiler;
20     using System.Diagnostics;
21     using System.IO;
22     using System.Threading;
23
24     /// <summary>
25     /// Basic Visitor Design Pattern support for ColumnMap hierarchy;
26     /// 
27     /// This visitor class will walk the entire hierarchy, but does not
28     /// return results; it's useful for operations such as printing and
29     /// searching.
30     /// </summary>
31     /// <typeparam name="TArgType"></typeparam>
32     internal abstract class ColumnMapVisitor<TArgType>
33     {
34         #region visitor helpers
35
36         /// <summary>
37         /// Common List(ColumnMap) code
38         /// </summary>
39         /// <param name="columnMaps"></param>
40         /// <param name="arg"></param>
41         protected void VisitList<TListType>(TListType[] columnMaps, TArgType arg)
42                    where TListType : ColumnMap
43         {
44             foreach (TListType columnMap in columnMaps)
45             {
46                 columnMap.Accept(this, arg);
47             }
48         }
49
50         #endregion
51
52         #region EntityIdentity handling
53
54         protected void VisitEntityIdentity(EntityIdentity entityIdentity, TArgType arg)
55         {
56             DiscriminatedEntityIdentity dei = entityIdentity as DiscriminatedEntityIdentity;
57             if (null != dei)
58             {
59                 VisitEntityIdentity(dei, arg);
60             }
61             else
62             {
63                 VisitEntityIdentity((SimpleEntityIdentity)entityIdentity, arg);
64             }
65         }
66
67         protected virtual void VisitEntityIdentity(DiscriminatedEntityIdentity entityIdentity, TArgType arg)
68         {
69             entityIdentity.EntitySetColumnMap.Accept(this, arg);
70             foreach (SimpleColumnMap columnMap in entityIdentity.Keys)
71             {
72                 columnMap.Accept(this, arg);
73             }
74         }
75
76         protected virtual void VisitEntityIdentity(SimpleEntityIdentity entityIdentity, TArgType arg)
77         {
78             foreach (SimpleColumnMap columnMap in entityIdentity.Keys)
79             {
80                 columnMap.Accept(this, arg);
81             }
82         }
83
84         #endregion
85
86         #region Visitor methods
87
88         internal virtual void Visit(ComplexTypeColumnMap columnMap, TArgType arg)
89         {
90             ColumnMap nullSentinel = columnMap.NullSentinel;
91             if (null != nullSentinel) 
92             {
93                 nullSentinel.Accept(this, arg);
94             }
95             foreach (ColumnMap p in columnMap.Properties)
96             {
97                 p.Accept(this, arg);
98             }
99         }
100
101         internal virtual void Visit(DiscriminatedCollectionColumnMap columnMap, TArgType arg)
102         {
103             columnMap.Discriminator.Accept(this, arg);
104             foreach (SimpleColumnMap fk in columnMap.ForeignKeys)
105             {
106                 fk.Accept(this, arg);
107             }
108             foreach (SimpleColumnMap k in columnMap.Keys)
109             {
110                 k.Accept(this, arg);
111             }
112             columnMap.Element.Accept(this, arg);
113         }
114
115         internal virtual void Visit(EntityColumnMap columnMap, TArgType arg)
116         {
117             VisitEntityIdentity(columnMap.EntityIdentity, arg);
118             foreach (ColumnMap p in columnMap.Properties)
119             {
120                 p.Accept(this, arg);
121             }
122         }
123
124         internal virtual void Visit(SimplePolymorphicColumnMap columnMap, TArgType arg)
125         {
126             columnMap.TypeDiscriminator.Accept(this, arg);
127             foreach (ColumnMap cm in columnMap.TypeChoices.Values)
128             {
129                 cm.Accept(this, arg);
130             }
131             foreach (ColumnMap p in columnMap.Properties)
132             {
133                 p.Accept(this, arg);
134             }
135         }
136
137         internal virtual void Visit(MultipleDiscriminatorPolymorphicColumnMap columnMap, TArgType arg)
138         {
139             foreach (var typeDiscriminator in columnMap.TypeDiscriminators)
140             {
141                 typeDiscriminator.Accept(this, arg);
142             }
143             foreach (var typeColumnMap in columnMap.TypeChoices.Values)
144             {
145                 typeColumnMap.Accept(this, arg);
146             }
147             foreach (var property in columnMap.Properties)
148             {
149                 property.Accept(this, arg);
150             }
151         }
152
153         internal virtual void Visit(RecordColumnMap columnMap, TArgType arg)
154         {
155             ColumnMap nullSentinel = columnMap.NullSentinel;
156             if (null != nullSentinel) 
157             {
158                 nullSentinel.Accept(this, arg);
159             }
160             foreach (ColumnMap p in columnMap.Properties)
161             {
162                 p.Accept(this, arg);
163             }
164         }
165
166         internal virtual void Visit(RefColumnMap columnMap, TArgType arg)
167         {
168             VisitEntityIdentity(columnMap.EntityIdentity, arg);
169         }
170
171         internal virtual void Visit(ScalarColumnMap columnMap, TArgType arg)
172         {
173         }
174
175         internal virtual void Visit(SimpleCollectionColumnMap columnMap, TArgType arg)
176         {
177             foreach (SimpleColumnMap fk in columnMap.ForeignKeys)
178             {
179                 fk.Accept(this, arg);
180             }
181             foreach (SimpleColumnMap k in columnMap.Keys)
182             {
183                 k.Accept(this, arg);
184             }
185             columnMap.Element.Accept(this, arg);
186         }
187
188         internal virtual void Visit(VarRefColumnMap columnMap, TArgType arg)
189         {
190         }
191
192         #endregion
193     }
194
195     /// <summary>
196     /// Basic Visitor Design Pattern support for ColumnMap hierarchy; 
197     /// 
198     /// This visitor class allows you to return results; it's useful for operations
199     /// that copy or manipulate the hierarchy.
200     /// </summary>
201     /// <typeparam name="TArgType"></typeparam>
202     /// <typeparam name="TResultType"></typeparam>
203     internal abstract class ColumnMapVisitorWithResults<TResultType, TArgType>
204     {
205
206         #region EntityIdentity handling
207
208         protected EntityIdentity VisitEntityIdentity(EntityIdentity entityIdentity, TArgType arg)
209         {
210             DiscriminatedEntityIdentity dei = entityIdentity as DiscriminatedEntityIdentity;
211             if (null != dei)
212             {
213                 return VisitEntityIdentity(dei, arg);
214             }
215             else
216             {
217                 return VisitEntityIdentity((SimpleEntityIdentity)entityIdentity, arg);
218             }
219         }
220
221         protected virtual EntityIdentity VisitEntityIdentity(DiscriminatedEntityIdentity entityIdentity, TArgType arg)
222         {
223             return entityIdentity;
224         }
225
226         protected virtual EntityIdentity VisitEntityIdentity(SimpleEntityIdentity entityIdentity, TArgType arg)
227         {
228             return entityIdentity;
229         }
230
231         #endregion
232
233         #region Visitor methods
234
235         internal abstract TResultType Visit(ComplexTypeColumnMap columnMap, TArgType arg);
236
237         internal abstract TResultType Visit(DiscriminatedCollectionColumnMap columnMap, TArgType arg);
238
239         internal abstract TResultType Visit(EntityColumnMap columnMap, TArgType arg);
240
241         internal abstract TResultType Visit(SimplePolymorphicColumnMap columnMap, TArgType arg);
242
243         internal abstract TResultType Visit(RecordColumnMap columnMap, TArgType arg);
244
245         internal abstract TResultType Visit(RefColumnMap columnMap, TArgType arg);
246
247         internal abstract TResultType Visit(ScalarColumnMap columnMap, TArgType arg);
248
249         internal abstract TResultType Visit(SimpleCollectionColumnMap columnMap, TArgType arg);
250
251         internal abstract TResultType Visit(VarRefColumnMap columnMap, TArgType arg);
252
253         internal abstract TResultType Visit(MultipleDiscriminatorPolymorphicColumnMap columnMap, TArgType arg);
254
255         #endregion
256     }
257
258 }