1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
//---------------------------------------------------------------------
// <copyright file="StorageModificationFunctionMapping.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Data.Metadata.Edm;
using System.Diagnostics;
using System.Globalization;
using System.Data.Common.Utils;
using System.Linq;
namespace System.Data.Mapping
{
/// <summary>
/// Describes modification function mappings for an association set.
/// </summary>
internal sealed class StorageAssociationSetModificationFunctionMapping
{
internal StorageAssociationSetModificationFunctionMapping(
AssociationSet associationSet,
StorageModificationFunctionMapping deleteFunctionMapping,
StorageModificationFunctionMapping insertFunctionMapping)
{
this.AssociationSet = EntityUtil.CheckArgumentNull(associationSet, "associationSet");
this.DeleteFunctionMapping = deleteFunctionMapping;
this.InsertFunctionMapping = insertFunctionMapping;
}
/// <summary>
/// Association set these functions handles.
/// </summary>
internal readonly AssociationSet AssociationSet;
/// <summary>
/// Delete function for this association set.
/// </summary>
internal readonly StorageModificationFunctionMapping DeleteFunctionMapping;
/// <summary>
/// Insert function for this association set.
/// </summary>
internal readonly StorageModificationFunctionMapping InsertFunctionMapping;
public override string ToString()
{
return String.Format(CultureInfo.InvariantCulture,
"AS{{{0}}}:{3}DFunc={{{1}}},{3}IFunc={{{2}}}", AssociationSet, DeleteFunctionMapping,
InsertFunctionMapping, Environment.NewLine + " ");
}
internal void Print(int index)
{
StorageEntityContainerMapping.GetPrettyPrintString(ref index);
StringBuilder sb = new StringBuilder();
sb.Append("Association Set Function Mapping");
sb.Append(" ");
sb.Append(this.ToString());
Console.WriteLine(sb.ToString());
}
}
/// <summary>
/// Describes modification function mappings for an entity type within an entity set.
/// </summary>
internal sealed class StorageEntityTypeModificationFunctionMapping
{
internal StorageEntityTypeModificationFunctionMapping(
EntityType entityType,
StorageModificationFunctionMapping deleteFunctionMapping,
StorageModificationFunctionMapping insertFunctionMapping,
StorageModificationFunctionMapping updateFunctionMapping)
{
this.EntityType = EntityUtil.CheckArgumentNull(entityType, "entityType");
this.DeleteFunctionMapping = deleteFunctionMapping;
this.InsertFunctionMapping = insertFunctionMapping;
this.UpdateFunctionMapping = updateFunctionMapping;
}
/// <summary>
/// Gets (specific) entity type these functions handle.
/// </summary>
internal readonly EntityType EntityType;
/// <summary>
/// Gets delete function for the current entity type.
/// </summary>
internal readonly StorageModificationFunctionMapping DeleteFunctionMapping;
/// <summary>
/// Gets insert function for the current entity type.
/// </summary>
internal readonly StorageModificationFunctionMapping InsertFunctionMapping;
/// <summary>
/// Gets update function for the current entity type.
/// </summary>
internal readonly StorageModificationFunctionMapping UpdateFunctionMapping;
public override string ToString()
{
return String.Format(CultureInfo.InvariantCulture,
"ET{{{0}}}:{4}DFunc={{{1}}},{4}IFunc={{{2}}},{4}UFunc={{{3}}}", EntityType, DeleteFunctionMapping,
InsertFunctionMapping, UpdateFunctionMapping, Environment.NewLine + " ");
}
internal void Print(int index)
{
StorageEntityContainerMapping.GetPrettyPrintString(ref index);
StringBuilder sb = new StringBuilder();
sb.Append("Entity Type Function Mapping");
sb.Append(" ");
sb.Append(this.ToString());
Console.WriteLine(sb.ToString());
}
}
/// <summary>
/// Describes modification function binding for change processing of entities or associations.
/// </summary>
internal sealed class StorageModificationFunctionMapping
{
internal StorageModificationFunctionMapping(
EntitySetBase entitySet,
EntityTypeBase entityType,
EdmFunction function,
IEnumerable<StorageModificationFunctionParameterBinding> parameterBindings,
FunctionParameter rowsAffectedParameter,
IEnumerable<StorageModificationFunctionResultBinding> resultBindings)
{
EntityUtil.CheckArgumentNull(entitySet, "entitySet");
this.Function = EntityUtil.CheckArgumentNull(function, "function");
this.RowsAffectedParameter = rowsAffectedParameter;
this.ParameterBindings = EntityUtil.CheckArgumentNull(parameterBindings, "parameterBindings")
.ToList().AsReadOnly();
if (null != resultBindings)
{
List<StorageModificationFunctionResultBinding> bindings = resultBindings.ToList();
if (0 < bindings.Count)
{
ResultBindings = bindings.AsReadOnly();
}
}
this.CollocatedAssociationSetEnds = GetReferencedAssociationSetEnds(entitySet as EntitySet, entityType as EntityType, parameterBindings)
.ToList()
.AsReadOnly();
}
/// <summary>
/// Gets output parameter producing number of rows affected. May be null.
/// </summary>
internal readonly FunctionParameter RowsAffectedParameter;
/// <summary>
/// Gets Metadata of function to which we should bind.
/// </summary>
internal readonly EdmFunction Function;
/// <summary>
/// Gets bindings for function parameters.
/// </summary>
internal readonly ReadOnlyCollection<StorageModificationFunctionParameterBinding> ParameterBindings;
/// <summary>
/// Gets all association set ends collocated in this mapping.
/// </summary>
internal readonly ReadOnlyCollection<AssociationSetEnd> CollocatedAssociationSetEnds;
/// <summary>
/// Gets bindings for the results of function evaluation.
/// </summary>
internal readonly ReadOnlyCollection<StorageModificationFunctionResultBinding> ResultBindings;
public override string ToString()
{
return String.Format(CultureInfo.InvariantCulture,
"Func{{{0}}}: Prm={{{1}}}, Result={{{2}}}", Function,
StringUtil.ToCommaSeparatedStringSorted(ParameterBindings),
StringUtil.ToCommaSeparatedStringSorted(ResultBindings));
}
// requires: entitySet must not be null
// Yields all referenced association set ends in this mapping.
private static IEnumerable<AssociationSetEnd> GetReferencedAssociationSetEnds(EntitySet entitySet, EntityType entityType, IEnumerable<StorageModificationFunctionParameterBinding> parameterBindings)
{
HashSet<AssociationSetEnd> ends = new HashSet<AssociationSetEnd>();
if (null != entitySet && null != entityType)
{
foreach (StorageModificationFunctionParameterBinding parameterBinding in parameterBindings)
{
AssociationSetEnd end = parameterBinding.MemberPath.AssociationSetEnd;
if (null != end)
{
ends.Add(end);
}
}
// If there is a referential constraint, it counts as an implicit mapping of
// the association set
foreach (AssociationSet assocationSet in MetadataHelper.GetAssociationsForEntitySet(entitySet))
{
ReadOnlyMetadataCollection<ReferentialConstraint> constraints = assocationSet.ElementType.ReferentialConstraints;
if (null != constraints)
{
foreach (ReferentialConstraint constraint in constraints)
{
if ((assocationSet.AssociationSetEnds[constraint.ToRole.Name].EntitySet == entitySet) &&
(constraint.ToRole.GetEntityType().IsAssignableFrom(entityType)))
{
ends.Add(assocationSet.AssociationSetEnds[constraint.FromRole.Name]);
}
}
}
}
}
return ends;
}
}
/// <summary>
/// Defines a binding from a named result set column to a member taking the value.
/// </summary>
internal sealed class StorageModificationFunctionResultBinding
{
internal StorageModificationFunctionResultBinding(string columnName, EdmProperty property)
{
this.ColumnName = EntityUtil.CheckArgumentNull(columnName, "columnName");
this.Property = EntityUtil.CheckArgumentNull(property, "property");
}
/// <summary>
/// Gets the name of the column to bind from the function result set. We use a string
/// value rather than EdmMember, since there is no metadata for function result sets.
/// </summary>
internal readonly string ColumnName;
/// <summary>
/// Gets the property to be set on the entity.
/// </summary>
internal readonly EdmProperty Property;
public override string ToString()
{
return String.Format(CultureInfo.InvariantCulture,
"{0}->{1}", ColumnName, Property);
}
}
/// <summary>
/// Binds a modification function parameter to a member of the entity or association being modified.
/// </summary>
internal sealed class StorageModificationFunctionParameterBinding
{
internal StorageModificationFunctionParameterBinding(FunctionParameter parameter, StorageModificationFunctionMemberPath memberPath, bool isCurrent)
{
this.Parameter = EntityUtil.CheckArgumentNull(parameter, "parameter");
this.MemberPath = EntityUtil.CheckArgumentNull(memberPath, "memberPath");
this.IsCurrent = isCurrent;
}
/// <summary>
/// Gets the parameter taking the value.
/// </summary>
internal readonly FunctionParameter Parameter;
/// <summary>
/// Gets the path to the entity or association member defining the value.
/// </summary>
internal readonly StorageModificationFunctionMemberPath MemberPath;
/// <summary>
/// Gets a value indicating whether the current or original
/// member value is being bound.
/// </summary>
internal readonly bool IsCurrent;
public override string ToString()
{
return String.Format(CultureInfo.InvariantCulture,
"@{0}->{1}{2}", Parameter, IsCurrent ? "+" : "-", MemberPath);
}
}
/// <summary>
/// Describes the location of a member within an entity or association type structure.
/// </summary>
internal sealed class StorageModificationFunctionMemberPath
{
internal StorageModificationFunctionMemberPath(IEnumerable<EdmMember> members, AssociationSet associationSetNavigation)
{
this.Members = new ReadOnlyCollection<EdmMember>(new List<EdmMember>(
EntityUtil.CheckArgumentNull(members, "members")));
if (null != associationSetNavigation)
{
Debug.Assert(2 == this.Members.Count, "Association bindings must always consist of the end and the key");
// find the association set end
this.AssociationSetEnd = associationSetNavigation.AssociationSetEnds[this.Members[1].Name];
}
}
/// <summary>
/// Gets the members in the path from the leaf (the member being bound)
/// to the Root of the structure.
/// </summary>
internal readonly ReadOnlyCollection<EdmMember> Members;
/// <summary>
/// Gets the association set to which we are navigating via this member. If the value
/// is null, this is not a navigation member path.
/// </summary>
internal readonly AssociationSetEnd AssociationSetEnd;
public override string ToString()
{
return String.Format(CultureInfo.InvariantCulture, "{0}{1}",
null == AssociationSetEnd ? String.Empty : "[" + AssociationSetEnd.ParentAssociationSet.ToString() + "]",
StringUtil.BuildDelimitedList(Members, null, "."));
}
}
}
|