8b09606a6fe026dabec9fe316b953ae81fa8d3d9
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Metadata / Edm / EntitySetBase.cs
1 //---------------------------------------------------------------------
2 // <copyright file="EntitySetBase.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 using System;
11 using System.Collections.Generic;
12 using System.Data.Common;
13 using System.Diagnostics;
14 using System.Text;
15
16 namespace System.Data.Metadata.Edm
17 {
18     /// <summary>
19     /// Class for representing a entity set
20     /// </summary>
21     public abstract class EntitySetBase : MetadataItem
22     {
23         //----------------------------------------------------------------------------------------------
24         // Possible Future Enhancement: revisit factoring of EntitySetBase and delta between C constructs and S constructs
25         //
26         // Currently, we need to have a way to map an entityset or a relationship set in S space
27         // to the appropriate structures in the store. In order to address this we said we would
28         // add new ItemAttributes (tableName, schemaName and catalogName to the EntitySetBase)... 
29         // problem with this is that we are bleading a leaf-level, store specific set of constructs
30         // into the object model for things that may exist at either C or S. 
31         //
32         // We need to do this for now to push forward on enabling the conversion but we need to re-examine
33         // whether we should have separate C and S space constructs or some other mechanism for 
34         // maintaining this metadata.
35         //----------------------------------------------------------------------------------------------
36
37         #region Constructors
38         /// <summary>
39         /// The constructor for constructing the EntitySet with a given name and an entity type
40         /// </summary>
41         /// <param name="name">The name of the EntitySet</param>
42         /// <param name="schema">The db schema</param>
43         /// <param name="table">The db table</param>
44         /// <param name="definingQuery">The provider specific query that should be used to retrieve the EntitySet</param>
45         /// <param name="entityType">The entity type of the entities that this entity set type contains</param>        
46         /// <exception cref="System.ArgumentNullException">Thrown if the name or entityType argument is null</exception>
47         internal EntitySetBase(string name, string schema, string table, string definingQuery, EntityTypeBase entityType)
48         {
49             EntityUtil.GenericCheckArgumentNull(entityType, "entityType");
50             EntityUtil.CheckStringArgument(name, "name");
51             // SQLBU 480236: catalogName, schemaName & tableName are allowed to be null, empty & non-empty
52
53             _name = name;
54
55             //---- name of the 'schema'
56             //---- this is used by the SQL Gen utility to support generation of the correct name in the store
57             _schema = schema;
58
59             //---- name of the 'table'
60             //---- this is used by the SQL Gen utility to support generation of the correct name in the store
61             _table = table;
62
63             //---- the Provider specific query to use to retrieve the EntitySet data
64             _definingQuery = definingQuery;
65             
66             this.ElementType = entityType;
67         }
68         #endregion
69
70         #region Fields
71         private EntityContainer _entityContainer;
72         private string _name;
73         private EntityTypeBase _elementType;
74         private string _table;
75         private string _schema;
76         private string _definingQuery;
77         private string _cachedProviderSql;
78
79         #endregion
80
81         #region Properties
82         /// <summary>
83         /// Returns the kind of the type
84         /// </summary>
85         public override BuiltInTypeKind BuiltInTypeKind { get { return BuiltInTypeKind.EntitySetBase; } }
86
87
88         /// <summary>
89         /// Gets the identity for this item as a string
90         /// </summary>
91         internal override string Identity
92         {
93             get
94             {
95                 return this.Name;
96             }
97         }
98
99         /// <summary>
100         /// Gets or sets escaped SQL describing this entity set.
101         /// </summary>
102         [MetadataProperty(PrimitiveTypeKind.String, false)]
103         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] // referenced by System.Data.Entity.Design.dll
104         internal string DefiningQuery
105         {
106             get { return _definingQuery; }
107             set { _definingQuery = value; }
108         }
109
110         /// <summary>
111         /// Get and set by the provider only as a convientent place to 
112         /// store the created sql fragment that represetnts this entity set
113         /// </summary>
114         internal string CachedProviderSql
115         {
116             get { return _cachedProviderSql; }
117             set { _cachedProviderSql = value; }
118         }
119
120         /// <summary>
121         /// Gets/Sets the name of this entity set
122         /// </summary>
123         /// <exception cref="System.ArgumentNullException">Thrown if value passed into setter is null</exception>
124         /// <exception cref="System.InvalidOperationException">Thrown if the setter is called when EntitySetBase instance is in ReadOnly state</exception>
125         [MetadataProperty(PrimitiveTypeKind.String, false)]
126         public String Name
127         {
128             get
129             {
130                 return _name;
131             }
132         }
133
134         /// <summary>
135         /// Returns the entity container of the entity set
136         /// </summary>
137         /// <exception cref="System.ArgumentNullException">Thrown if value passed into setter is null</exception>
138         /// <exception cref="System.InvalidOperationException">Thrown if the setter is called when the EntitySetBase instance or the EntityContainer passed into the setter is in ReadOnly state</exception>
139         public EntityContainer EntityContainer
140         {
141             get
142             {
143                 return _entityContainer;
144             }
145         }
146
147         /// <summary>
148         /// Gets/Sets the entity type of this entity set
149         /// </summary>
150         /// <exception cref="System.ArgumentNullException">if value passed into setter is null</exception>
151         /// <exception cref="System.InvalidOperationException">Thrown if the setter is called when EntitySetBase instance is in ReadOnly state</exception>
152         [MetadataProperty(BuiltInTypeKind.EntityTypeBase, false)]
153         public EntityTypeBase ElementType
154         {
155             get
156             {
157                 return _elementType;
158             }
159             internal set
160             {
161                 EntityUtil.GenericCheckArgumentNull(value, "value");
162                 Util.ThrowIfReadOnly(this);
163                 _elementType = value;
164             }
165         }
166
167         [MetadataProperty(PrimitiveTypeKind.String, false)]
168         internal string Table
169         {
170             get
171             {
172                 return _table;
173             }
174         }
175
176         [MetadataProperty(PrimitiveTypeKind.String, false)]
177         internal string Schema
178         {
179             get
180             {
181                 return _schema;
182             }
183         }
184         #endregion
185
186         #region Methods
187         /// <summary>
188         /// Overriding System.Object.ToString to provide better String representation 
189         /// for this type.
190         /// </summary>
191         public override string ToString()
192         {
193             return Name;
194         }
195
196         /// <summary>
197         /// Sets this item to be readonly, once this is set, the item will never be writable again.
198         /// </summary>
199         internal override void SetReadOnly()
200         {
201             if (!this.IsReadOnly)
202             {
203                 base.SetReadOnly();
204
205                 EntityTypeBase elementType = ElementType;
206                 if (elementType != null)
207                 {
208                     elementType.SetReadOnly();
209                 }
210             }
211         }
212
213         /// <summary>
214         /// Change the entity container without doing fixup in the entity set collection
215         /// </summary>
216         internal void ChangeEntityContainerWithoutCollectionFixup(EntityContainer newEntityContainer)
217         {
218             _entityContainer = newEntityContainer;
219         }
220         #endregion
221     }
222 }