Imported Upstream version 4.0.0~alpha1

Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
This commit is contained in:
Jo Shields
2015-04-07 09:35:12 +01:00
parent 283343f570
commit 3c1f479b9d
22469 changed files with 2931443 additions and 869343 deletions

View File

@@ -0,0 +1,179 @@
//------------------------------------------------------------------------------
// <copyright file="EDesignUtil.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// @owner [....]
// @backupOwner [....]
//------------------------------------------------------------------------------
namespace System.Data.Entity.Design.Common {
using System;
using System.Data;
using System.Data.Metadata.Edm;
internal static class EDesignUtil {
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//
// Helper Functions
//
internal static string GetMessagesFromEntireExceptionChain(Exception e)
{
// get the full error message list from the inner exceptions
string message = e.Message;
int count = 0;
for (Exception inner = e.InnerException; inner != null; inner = inner.InnerException)
{
count++;
string indent = string.Empty.PadLeft(count, '\t');
message += Environment.NewLine + indent;
message += inner.Message;
}
return message;
}
static internal T CheckArgumentNull<T>(T value, string parameterName) where T : class
{
if (null == value)
{
throw ArgumentNull(parameterName);
}
return value;
}
static internal void CheckStringArgument(string value, string parameterName)
{
// Throw ArgumentNullException when string is null
CheckArgumentNull(value, parameterName);
// Throw ArgumentException when string is empty
if (value.Length == 0)
{
throw InvalidStringArgument(parameterName);
}
}
static internal LanguageOption CheckLanguageOptionArgument(LanguageOption value, string paramName)
{
if (value == LanguageOption.GenerateCSharpCode ||
value == LanguageOption.GenerateVBCode)
{
return value;
}
throw ArgumentOutOfRange(paramName);
}
static internal ArgumentException SingleStoreEntityContainerExpected(string parameterName)
{
ArgumentException e = new ArgumentException(Strings.SingleStoreEntityContainerExpected, parameterName);
return e;
}
static internal ArgumentException InvalidStoreEntityContainer(string entityContainerName, string parameterName)
{
ArgumentException e = new ArgumentException(Strings.InvalidNonStoreEntityContainer(entityContainerName), parameterName);
return e;
}
static internal ArgumentException InvalidStringArgument(string parameterName) {
ArgumentException e = new ArgumentException(Strings.InvalidStringArgument(parameterName));
return e;
}
static internal ArgumentException EdmReservedNamespace(string namespaceName) {
ArgumentException e = new ArgumentException(Strings.ReservedNamespace(namespaceName));
return e;
}
static internal ArgumentNullException ArgumentNull(string parameter) {
ArgumentNullException e = new ArgumentNullException(parameter);
return e;
}
static internal ArgumentException Argument(string parameter)
{
ArgumentException e = new ArgumentException(parameter);
return e;
}
static internal ArgumentException Argument(string message, Exception inner)
{
ArgumentException e = new ArgumentException(message, inner);
return e;
}
static internal InvalidOperationException InvalidOperation(string error)
{
InvalidOperationException e = new InvalidOperationException(error);
return e;
}
// SSDL Generator
static internal StrongTypingException StrongTyping(string error, Exception innerException) {
StrongTypingException e = new StrongTypingException(error, innerException);
return e;
}
static internal StrongTypingException StonglyTypedAccessToNullValue(string columnName, string tableName, Exception innerException) {
return StrongTyping(Strings.StonglyTypedAccessToNullValue(columnName, tableName), innerException);
}
static internal InvalidOperationException EntityStoreGeneratorSchemaNotLoaded() {
return InvalidOperation(Strings.EntityStoreGeneratorSchemaNotLoaded);
}
static internal InvalidOperationException EntityModelGeneratorSchemaNotLoaded() {
return InvalidOperation(Strings.EntityModelGeneratorSchemaNotLoaded);
}
static internal InvalidOperationException NonSerializableType(BuiltInTypeKind kind)
{
return InvalidOperation(Strings.Serialization_UnknownGlobalItem(kind));
}
static internal InvalidOperationException MissingGenerationPatternForType(BuiltInTypeKind kind)
{
return InvalidOperation(Strings.ModelGeneration_UnGeneratableType(kind));
}
static internal ArgumentException InvalidNamespaceNameArgument(string namespaceName)
{
return new ArgumentException(Strings.InvalidNamespaceNameArgument(namespaceName));
}
static internal ArgumentException InvalidEntityContainerNameArgument(string entityContainerName)
{
return new ArgumentException(Strings.InvalidEntityContainerNameArgument(entityContainerName));
}
static internal ArgumentException DuplicateEntityContainerName(string newModelEntityContainerName, string storeEntityContainer)
{
return new ArgumentException(Strings.DuplicateEntityContainerName(newModelEntityContainerName, storeEntityContainer));
}
static internal ProviderIncompatibleException ProviderIncompatible(string message)
{
return new ProviderIncompatibleException(message);
}
static internal ProviderIncompatibleException ProviderIncompatible(string message, Exception inner)
{
return new ProviderIncompatibleException(message, inner);
}
static internal ArgumentOutOfRangeException ArgumentOutOfRange(string paramName)
{
return new ArgumentOutOfRangeException(paramName);
}
internal static void CheckTargetEntityFrameworkVersionArgument(Version targetEntityFrameworkVersion, string parameterName)
{
EDesignUtil.CheckArgumentNull(targetEntityFrameworkVersion, parameterName);
if (!EntityFrameworkVersions.IsValidVersion(targetEntityFrameworkVersion))
{
throw EDesignUtil.Argument(parameterName);
}
}
}
}

View File

@@ -0,0 +1,338 @@
//---------------------------------------------------------------------
// <copyright file="MetadataUtil.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data.Metadata.Edm;
using System.Xml;
using System.Data.Common;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Globalization;
using System.Linq;
namespace System.Data.Entity.Design.Common
{
internal static class MetadataUtil
{
private const string s_defaultDelimiter = ", ";
internal static bool IsStoreType(GlobalItem item)
{
return item.DataSpace == DataSpace.SSpace;
}
internal static DbProviderServices GetProviderServices(DbProviderFactory factory)
{
EDesignUtil.CheckArgumentNull(factory, "factory");
// Special case SQL client so that it will work with System.Data from .NET 4.0 even without
// a binding redirect.
if (factory is SqlClientFactory)
{
return SqlProviderServices.Instance;
}
IServiceProvider serviceProvider = factory as IServiceProvider;
if (serviceProvider == null)
{
throw MetadataUtil.ProviderIncompatible(System.Data.Entity.Design.Strings.EntityClient_DoesNotImplementIServiceProvider(
factory.GetType().ToString()));
}
DbProviderServices providerServices = serviceProvider.GetService(typeof(DbProviderServices)) as DbProviderServices;
if (providerServices == null)
{
throw MetadataUtil.ProviderIncompatible(
System.Data.Entity.Design.Strings.EntityClient_ReturnedNullOnProviderMethod(
"GetService",
factory.GetType().ToString()));
}
return providerServices;
}
static internal ProviderIncompatibleException ProviderIncompatible(string error)
{
ProviderIncompatibleException e = new ProviderIncompatibleException(error);
return e;
}
/// <summary>
/// Check if all the SchemaErrors have the serverity of SchemaErrorSeverity.Warning
/// </summary>
/// <param name="schemaErrors"></param>
/// <returns></returns>
internal static bool CheckIfAllErrorsAreWarnings(IList<EdmSchemaError> schemaErrors)
{
int length = schemaErrors.Count;
for (int i = 0; i < length; ++i)
{
EdmSchemaError error = schemaErrors[i];
if (error.Severity != EdmSchemaErrorSeverity.Warning)
{
return false;
}
}
return true;
}
/// <summary>
/// This private static method checks a string to make sure that it is not empty.
/// Comparing with String.Empty is not sufficient since a string with nothing
/// but white space isn't considered "empty" by that rationale.
/// </summary>
internal static bool IsNullOrEmptyOrWhiteSpace(string value)
{
return IsNullOrEmptyOrWhiteSpace(value, 0);
}
internal static bool IsNullOrEmptyOrWhiteSpace(string value, int offset)
{
// don't use Trim(), which will copy the string, which may be large, just to test for emptyness
//return String.IsNullOrEmpty(value) || String.IsNullOrEmpty(value.Trim());
if (null != value)
{
for (int i = offset; i < value.Length; ++i)
{
if (!Char.IsWhiteSpace(value[i]))
{
return false;
}
}
}
return true;
}
// separate implementation from IsNullOrEmptyOrWhiteSpace(string, int) because that one will
// pick up the jit optimization to avoid boundary checks and the this won't is unknown (most likely not)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] // referenced by System.Data.Entity.Design.dll
internal static bool IsNullOrEmptyOrWhiteSpace(string value, int offset, int length)
{
// don't use Trim(), which will copy the string, which may be large, just to test for emptyness
//return String.IsNullOrEmpty(value) || String.IsNullOrEmpty(value.Trim());
if (null != value)
{
length = Math.Min(value.Length, length);
for (int i = offset; i < length; ++i)
{
if (!Char.IsWhiteSpace(value[i]))
{
return false;
}
}
}
return true;
}
internal static string MembersToCommaSeparatedString(IEnumerable members)
{
StringBuilder builder = new StringBuilder();
builder.Append("{");
MetadataUtil.ToCommaSeparatedString(builder, members);
builder.Append("}");
return builder.ToString();
}
internal static void ToCommaSeparatedString(StringBuilder builder, IEnumerable list)
{
ToSeparatedStringPrivate(builder, list, s_defaultDelimiter, string.Empty, false);
}
// effects: Converts the list to a list of strings, sorts its (if
// toSort is true) and then converts to a string separated by
// "separator" with "nullValue" used for null values.
private static void ToSeparatedStringPrivate(StringBuilder stringBuilder, IEnumerable list, string separator,
string nullValue, bool toSort)
{
if (null == list)
{
return;
}
bool isFirst = true;
// Get the list of strings first
List<string> elementStrings = new List<string>();
foreach (object element in list)
{
string str;
// Get the element or its default null value
if (element == null)
{
str = nullValue;
}
else
{
str = FormatInvariant("{0}", element);
}
elementStrings.Add(str);
}
if (toSort == true)
{
// Sort the list
elementStrings.Sort(StringComparer.Ordinal);
}
// Now add the strings to the stringBuilder
foreach (string str in elementStrings)
{
if (false == isFirst)
{
stringBuilder.Append(separator);
}
stringBuilder.Append(str);
isFirst = false;
}
}
internal static string FormatInvariant(string format, params object[] args)
{
Debug.Assert(args.Length > 0, "Formatting utilities must be called with at least one argument");
return String.Format(CultureInfo.InvariantCulture, format, args);
}
/// <summary>
/// replace troublesome xml characters with equivalent entities
/// </summary>
/// <param name="text">text that make have characters troublesome in xml</param>
/// <returns>text with troublesome characters replaced with equivalent entities</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] // referenced by System.Data.Entity.Design.dll
internal static string Entityize(string text)
{
if (string.IsNullOrEmpty(text))
return "";
text = text.Replace("&", "&amp;");
text = text.Replace("<", "&lt;").Replace(">", "&gt;");
return text.Replace("\'", "&apos;").Replace("\"", "&quot;");
}
internal static bool TrySplitExtendedMetadataPropertyName(string name, out string xmlNamespaceUri, out string attributeName)
{
int pos = name.LastIndexOf(':');
if (pos < 0 || name.Length <= pos + 1)
{
Debug.Fail("the name is not in the form we expect");
xmlNamespaceUri = null;
attributeName = null;
return false;
}
xmlNamespaceUri = name.Substring(0, pos);
attributeName = name.Substring(pos + 1, (name.Length - 1) - pos);
return true;
}
static private readonly Type StackOverflowType = typeof(System.StackOverflowException);
static private readonly Type OutOfMemoryType = typeof(System.OutOfMemoryException);
static private readonly Type ThreadAbortType = typeof(System.Threading.ThreadAbortException);
static private readonly Type NullReferenceType = typeof(System.NullReferenceException);
static private readonly Type AccessViolationType = typeof(System.AccessViolationException);
static private readonly Type SecurityType = typeof(System.Security.SecurityException);
internal static bool IsCatchableExceptionType(Exception e)
{
// a 'catchable' exception is defined by what it is not.
Debug.Assert(e != null, "Unexpected null exception!");
Type type = e.GetType();
return ((type != StackOverflowType) &&
(type != OutOfMemoryType) &&
(type != ThreadAbortType) &&
(type != NullReferenceType) &&
(type != AccessViolationType) &&
!SecurityType.IsAssignableFrom(type));
}
/// <summary>
/// Returns the single error message from the list of errors
/// </summary>
/// <param name="errors"></param>
/// <returns></returns>
static internal string CombineErrorMessage(IEnumerable<System.Data.Metadata.Edm.EdmSchemaError> errors)
{
Debug.Assert(errors != null);
StringBuilder sb = new StringBuilder(System.Environment.NewLine);
int count = 0;
foreach (System.Data.Metadata.Edm.EdmSchemaError error in errors)
{
//Don't append a new line at the beginning of the messages
if ((count++) != 0)
{
sb.Append(System.Environment.NewLine);
}
sb.Append(error.ToString());
}
Debug.Assert(count != 0, "Empty Error List");
return sb.ToString();
}
internal static void DisposeXmlReaders(IEnumerable<XmlReader> xmlReaders)
{
Debug.Assert(xmlReaders != null);
foreach (XmlReader xmlReader in xmlReaders)
{
((IDisposable)xmlReader).Dispose();
}
}
internal static bool IsCollectionType(GlobalItem item)
{
return (BuiltInTypeKind.CollectionType == item.BuiltInTypeKind);
}
internal static bool IsComplexType(EdmType type)
{
return (BuiltInTypeKind.ComplexType == type.BuiltInTypeKind);
}
internal static bool IsPrimitiveType(EdmType type)
{
return (BuiltInTypeKind.PrimitiveType == type.BuiltInTypeKind);
}
internal static bool IsEntitySet(EntitySetBase entitySetBase)
{
return BuiltInTypeKind.EntitySet == entitySetBase.BuiltInTypeKind;
}
internal static bool IsValidKeyType(Version entityFrameworkVersion, EdmType type)
{
var primitiveType = type as PrimitiveType;
if (primitiveType == null)
{
return false;
}
if (EntityFrameworkVersions.Version1 == entityFrameworkVersion)
{
return primitiveType.PrimitiveTypeKind != PrimitiveTypeKind.Binary;
}
else
{
// From V2 onwards, Binary key properties are supported
return true;
}
}
/// <summary>
/// determines if type is of EnumerationType.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
internal static bool IsEnumerationType(EdmType type)
{
return (BuiltInTypeKind.EnumType == type.BuiltInTypeKind);
}
}
}

View File

@@ -0,0 +1,110 @@
//---------------------------------------------------------------------
// <copyright file="UniqueIdentifierService.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
namespace System.Data.Entity.Design.Common
{
/// <summary>
/// Service making names within a scope unique. Initialize a new instance
/// for every scope.
///
///
internal sealed class UniqueIdentifierService
{
internal UniqueIdentifierService(bool caseSensitive)
{
_knownIdentifiers = new Dictionary<string, bool>(caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);
_identifierToAdjustedIdentifier = new Dictionary<object, string>();
_transform = s => s;
}
internal UniqueIdentifierService(bool caseSensitive, Func<string, string> transform)
{
Debug.Assert(transform != null, "use the other constructor if you don't want any transform");
_knownIdentifiers = new Dictionary<string, bool>(caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);
_identifierToAdjustedIdentifier = new Dictionary<object, string>();
_transform = transform;
}
private readonly Dictionary<string, bool> _knownIdentifiers;
private readonly Dictionary<object, string> _identifierToAdjustedIdentifier;
private readonly Func<string, string> _transform;
/// <summary>
/// This method can be used in when you have an
/// identifier that you know can't be used, but you don't want
/// an adjusted version of it
/// </summary>
/// <param name="identifier"></param>
internal void RegisterUsedIdentifier(string identifier)
{
Debug.Assert(!_knownIdentifiers.ContainsKey(identifier), "don't register identifiers that already exist");
_knownIdentifiers.Add(identifier, true);
}
/// <summary>
/// Given an identifier, makes it unique within the scope by adding
/// a suffix (1, 2, 3, ...), and returns the adjusted identifier.
/// </summary>
/// <param name="identifier">Identifier. Must not be null or empty.</param>
/// <param name="value">Object associated with this identifier in case it is required to
/// retrieve the adjusted identifier. If not null, must not exist in the current scope already.</param>
/// <returns>Identifier adjusted to be unique within the scope.</returns>
internal string AdjustIdentifier(string identifier, object value)
{
Debug.Assert(!string.IsNullOrEmpty(identifier), "identifier is null or empty");
// find a unique name by adding suffix as necessary
int numberOfConflicts = 0;
string adjustedIdentifier = _transform(identifier);
while (_knownIdentifiers.ContainsKey(adjustedIdentifier))
{
++numberOfConflicts;
adjustedIdentifier = _transform(identifier) + numberOfConflicts.ToString(CultureInfo.InvariantCulture);
}
// remember the identifier in this scope
Debug.Assert(!_knownIdentifiers.ContainsKey(adjustedIdentifier), "we just made it unique");
_knownIdentifiers.Add(adjustedIdentifier, true);
if (null != value)
{
Debug.Assert(!_identifierToAdjustedIdentifier.ContainsKey(value), "should never register one value twice");
_identifierToAdjustedIdentifier.Add(value, adjustedIdentifier);
}
return adjustedIdentifier;
}
/// <summary>
/// Simple overload when you don't need to track back to an object
/// </summary>
/// <param name="identifier"></param>
/// <returns></returns>
internal string AdjustIdentifier(string identifier)
{
return AdjustIdentifier(identifier, null);
}
/// <summary>
/// Determines the adjusted name for an identifier if it has been registered in this scope.
/// </summary>
internal bool TryGetAdjustedName(object value, out string adjustedIdentifier)
{
return _identifierToAdjustedIdentifier.TryGetValue(value, out adjustedIdentifier);
}
}
}