1
//---------------------------------------------------------------------
2 // <copyright file="StoreItemCollection.Loader.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9 namespace System
.Data
.Metadata
.Edm
11 using System
.Collections
.Generic
;
12 using System
.Data
.Common
;
13 using System
.Data
.Common
.Utils
;
14 using System
.Data
.Entity
;
15 using System
.Data
.EntityModel
.SchemaObjectModel
;
16 using System
.Diagnostics
;
20 public sealed partial class StoreItemCollection
24 private string _provider
= null;
25 private string _providerManifestToken
= null;
26 private DbProviderManifest _providerManifest
;
27 private DbProviderFactory _providerFactory
;
28 IList
<EdmSchemaError
> _errors
;
29 IList
<Schema
> _schemas
;
32 public Loader(IEnumerable
<XmlReader
> xmlReaders
, IEnumerable
<string> sourceFilePaths
, bool throwOnError
)
34 _throwOnError
= throwOnError
;
35 LoadItems(xmlReaders
, sourceFilePaths
);
40 public IList
<EdmSchemaError
> Errors
42 get { return _errors; }
45 public IList
<Schema
> Schemas
47 get { return _schemas; }
50 public DbProviderManifest ProviderManifest
52 get { return _providerManifest; }
55 public DbProviderFactory ProviderFactory
57 get { return _providerFactory; }
60 public string ProviderManifestToken
62 get { return _providerManifestToken; }
65 public bool HasNonWarningErrors
69 return !MetadataHelper
.CheckIfAllErrorsAreWarnings(_errors
);
73 private void LoadItems(IEnumerable
<XmlReader
> xmlReaders
, IEnumerable
<string> sourceFilePaths
)
76 Debug
.Assert(_errors
== null, "we are expecting this to be the location that sets _errors for the first time");
77 _errors
= SchemaManager
.ParseAndValidate(xmlReaders
,
79 SchemaDataModelOption
.ProviderDataModel
,
80 OnProviderNotification
,
81 OnProviderManifestTokenNotification
,
82 OnProviderManifestNeeded
,
87 ThrowOnNonWarningErrors();
91 internal void ThrowOnNonWarningErrors()
93 if (!MetadataHelper
.CheckIfAllErrorsAreWarnings(_errors
))
95 //Future Enhancement: if there is an error, we throw exception with error and warnings.
96 //Otherwise the user has no clue to know about warnings.
97 throw EntityUtil
.InvalidSchemaEncountered(Helper
.CombineErrorMessage(_errors
));
101 private void OnProviderNotification(string provider
, Action
<string, ErrorCode
, EdmSchemaErrorSeverity
> addError
)
103 string expected
= _provider
;
104 if (_provider
== null)
106 // Even if the Provider is only now being discovered from the first SSDL file,
107 // it must still match the 'implicit' provider that is implied by the DbConnection
108 // or DbProviderFactory that was used to construct this StoreItemCollection.
109 _provider
= provider
;
110 InitializeProviderManifest(addError
);
115 // The provider was previously discovered from a preceeding SSDL file; it is an error
116 // if the 'Provider' attributes in all SSDL files are not identical.
117 if (_provider
== provider
)
123 Debug
.Assert(expected
!= null, "Expected provider name not initialized from _provider or _providerFactory?");
125 addError(Strings
.AllArtifactsMustTargetSameProvider_InvariantName(expected
, _provider
),
126 ErrorCode
.InconsistentProvider
,
127 EdmSchemaErrorSeverity
.Error
);
130 private void InitializeProviderManifest(Action
<string, ErrorCode
, EdmSchemaErrorSeverity
> addError
)
133 if (_providerManifest
== null && (_providerManifestToken
!= null && _provider
!= null))
135 DbProviderFactory factory
= null;
138 factory
= DbProviderServices
.GetProviderFactory(_provider
);
140 catch (ArgumentException e
)
142 addError(e
.Message
, ErrorCode
.InvalidProvider
, EdmSchemaErrorSeverity
.Error
);
148 DbProviderServices services
= DbProviderServices
.GetProviderServices(factory
);
149 _providerManifest
= services
.GetProviderManifest(_providerManifestToken
);
150 _providerFactory
= factory
;
151 if (_providerManifest
is EdmProviderManifest
)
155 throw EntityUtil
.NotSupported(Strings
.OnlyStoreConnectionsSupported
);
159 addError(Strings
.OnlyStoreConnectionsSupported
, ErrorCode
.InvalidProvider
, EdmSchemaErrorSeverity
.Error
);
164 catch (ProviderIncompatibleException e
)
168 // we want to surface these as ProviderIncompatibleExceptions if we are "allowed" to.
172 AddProviderIncompatibleError(e
, addError
);
177 private void OnProviderManifestTokenNotification(string token
, Action
<string, ErrorCode
, EdmSchemaErrorSeverity
> addError
)
179 if (_providerManifestToken
== null)
181 _providerManifestToken
= token
;
182 InitializeProviderManifest(addError
);
186 if (_providerManifestToken
!= token
)
188 addError(Strings
.AllArtifactsMustTargetSameProvider_ManifestToken(token
, _providerManifestToken
),
189 ErrorCode
.ProviderManifestTokenMismatch
,
190 EdmSchemaErrorSeverity
.Error
);
194 private DbProviderManifest
OnProviderManifestNeeded(Action
<string, ErrorCode
, EdmSchemaErrorSeverity
> addError
)
196 if (_providerManifest
== null)
198 addError(Strings
.ProviderManifestTokenNotFound
,
199 ErrorCode
.ProviderManifestTokenNotFound
,
200 EdmSchemaErrorSeverity
.Error
);
202 return _providerManifest
;
205 private void AddProviderIncompatibleError(ProviderIncompatibleException provEx
, Action
<string, ErrorCode
, EdmSchemaErrorSeverity
> addError
)
207 Debug
.Assert(provEx
!= null);
208 Debug
.Assert(addError
!= null);
210 StringBuilder message
= new StringBuilder(provEx
.Message
);
211 if (provEx
.InnerException
!= null && !string.IsNullOrEmpty(provEx
.InnerException
.Message
))
213 message
.AppendFormat(" {0}", provEx
.InnerException
.Message
);
216 addError(message
.ToString(),
217 ErrorCode
.FailedToRetrieveProviderManifest
,
218 EdmSchemaErrorSeverity
.Error
);