internal object EvaluateParameter(object[] arguments) { if ((object)this._cachedDelegate == null) { if (this._funcletizedExpression.NodeType == ExpressionType.Constant) { return(((ConstantExpression)this._funcletizedExpression).Value); } ConstantExpression constantExpression; if (QueryParameterExpression.TryEvaluatePath(this._funcletizedExpression, out constantExpression)) { return(constantExpression.Value); } } try { if ((object)this._cachedDelegate == null) { this._cachedDelegate = Expression.Lambda(TypeSystem.GetDelegateType(this._compiledQueryParameters.Select <ParameterExpression, Type>((Func <ParameterExpression, Type>)(p => p.Type)), this._type), this._funcletizedExpression, this._compiledQueryParameters).Compile(); } return(this._cachedDelegate.DynamicInvoke(arguments)); } catch (TargetInvocationException ex) { throw ex.InnerException; } }
private static bool TryEvaluatePath( Expression expression, out ConstantExpression constantExpression) { memberExpression = expression as MemberExpression; constantExpression = (ConstantExpression)null; if (memberExpression != null) { Stack <MemberExpression> memberExpressionStack = new Stack <MemberExpression>(); memberExpressionStack.Push(memberExpression); while (memberExpression.Expression is MemberExpression memberExpression) { memberExpressionStack.Push(memberExpression); } MemberExpression me1 = memberExpressionStack.Pop(); object memberValue; if (me1.Expression is ConstantExpression && QueryParameterExpression.TryGetFieldOrPropertyValue(me1, ((ConstantExpression)me1.Expression).Value, out memberValue)) { if (memberExpressionStack.Count > 0) { foreach (MemberExpression me2 in memberExpressionStack) { if (!QueryParameterExpression.TryGetFieldOrPropertyValue(me2, memberValue, out memberValue)) { return(false); } } } constantExpression = Expression.Constant(memberValue, expression.Type); return(true); } } return(false); }
internal override ObjectQueryExecutionPlan GetExecutionPlan( MergeOption?forMergeOption) { ObjectQueryExecutionPlan queryExecutionPlan1 = this._cachedPlan; if (queryExecutionPlan1 != null) { MergeOption?mergeOption = ObjectQueryState.GetMergeOption(forMergeOption, this.UserSpecifiedMergeOption); if (mergeOption.HasValue && mergeOption.Value != queryExecutionPlan1.MergeOption || (this._recompileRequired() || this.ObjectContext.ContextOptions.UseCSharpNullComparisonBehavior != this._useCSharpNullComparisonBehavior)) { queryExecutionPlan1 = (ObjectQueryExecutionPlan)null; } } if (queryExecutionPlan1 == null) { this._recompileRequired = (Func <bool>)null; this.ResetParameters(); ExpressionConverter expressionConverter = this.CreateExpressionConverter(); DbExpression dbExpression = expressionConverter.Convert(); this._recompileRequired = expressionConverter.RecompileRequired; MergeOption mergeOption = ObjectQueryState.EnsureMergeOption(forMergeOption, this.UserSpecifiedMergeOption, expressionConverter.PropagatedMergeOption); this._useCSharpNullComparisonBehavior = this.ObjectContext.ContextOptions.UseCSharpNullComparisonBehavior; this._linqParameters = expressionConverter.GetParameters(); if (this._linqParameters != null && this._linqParameters.Any <Tuple <ObjectParameter, QueryParameterExpression> >()) { ObjectParameterCollection parameterCollection = this.EnsureParameters(); parameterCollection.SetReadOnly(false); foreach (Tuple <ObjectParameter, QueryParameterExpression> linqParameter in this._linqParameters) { ObjectParameter objectParameter = linqParameter.Item1; parameterCollection.Add(objectParameter); } parameterCollection.SetReadOnly(true); } QueryCacheManager queryCacheManager = (QueryCacheManager)null; LinqQueryCacheKey key1 = (LinqQueryCacheKey)null; string key2; if (this.PlanCachingEnabled && !this._recompileRequired() && ExpressionKeyGen.TryGenerateKey(dbExpression, out key2)) { key1 = new LinqQueryCacheKey(key2, this.Parameters == null ? 0 : this.Parameters.Count, this.Parameters == null ? (string)null : this.Parameters.GetCacheKey(), expressionConverter.PropagatedSpan == null ? (string)null : expressionConverter.PropagatedSpan.GetCacheKey(), mergeOption, this.EffectiveStreamingBehavior, this._useCSharpNullComparisonBehavior, this.ElementType); queryCacheManager = this.ObjectContext.MetadataWorkspace.GetQueryCacheManager(); ObjectQueryExecutionPlan queryExecutionPlan2 = (ObjectQueryExecutionPlan)null; if (queryCacheManager.TryCacheLookup <LinqQueryCacheKey, ObjectQueryExecutionPlan>(key1, out queryExecutionPlan2)) { queryExecutionPlan1 = queryExecutionPlan2; } } if (queryExecutionPlan1 == null) { queryExecutionPlan1 = this._objectQueryExecutionPlanFactory.Prepare(this.ObjectContext, DbQueryCommandTree.FromValidExpression(this.ObjectContext.MetadataWorkspace, DataSpace.CSpace, dbExpression, !this._useCSharpNullComparisonBehavior), this.ElementType, mergeOption, this.EffectiveStreamingBehavior, expressionConverter.PropagatedSpan, (IEnumerable <Tuple <ObjectParameter, QueryParameterExpression> >)null, expressionConverter.AliasGenerator); if (key1 != null) { QueryCacheEntry inQueryCacheEntry = new QueryCacheEntry((QueryCacheKey)key1, (object)queryExecutionPlan1); QueryCacheEntry outQueryCacheEntry = (QueryCacheEntry)null; if (queryCacheManager.TryLookupAndAdd(inQueryCacheEntry, out outQueryCacheEntry)) { queryExecutionPlan1 = (ObjectQueryExecutionPlan)outQueryCacheEntry.GetTarget(); } } } this._cachedPlan = queryExecutionPlan1; } if (this._linqParameters != null) { foreach (Tuple <ObjectParameter, QueryParameterExpression> linqParameter in this._linqParameters) { ObjectParameter objectParameter = linqParameter.Item1; QueryParameterExpression parameterExpression = linqParameter.Item2; if (parameterExpression != null) { objectParameter.Value = parameterExpression.EvaluateParameter((object[])null); } } } return(queryExecutionPlan1); }
private void AddParameter(QueryParameterExpression queryParameter) { if (null == _parameters) { _parameters = new List<Tuple<ObjectParameter, QueryParameterExpression>>(); } if (!_parameters.Select(p => p.Item2).Contains(queryParameter)) { var parameter = new ObjectParameter(queryParameter.ParameterReference.ParameterName, queryParameter.Type); _parameters.Add(new Tuple<ObjectParameter, QueryParameterExpression>(parameter, queryParameter)); } }
internal override ObjectQueryExecutionPlan GetExecutionPlan( MergeOption?forMergeOption) { ObjectQueryExecutionPlan queryExecutionPlan = (ObjectQueryExecutionPlan)null; CompiledQueryCacheEntry compiledQueryCacheEntry = this._cacheEntry; bool comparisonBehavior = this.ObjectContext.ContextOptions.UseCSharpNullComparisonBehavior; if (compiledQueryCacheEntry != null) { MergeOption mergeOption = ObjectQueryState.EnsureMergeOption(forMergeOption, this.UserSpecifiedMergeOption, compiledQueryCacheEntry.PropagatedMergeOption); queryExecutionPlan = compiledQueryCacheEntry.GetExecutionPlan(mergeOption, comparisonBehavior); if (queryExecutionPlan == null) { ExpressionConverter expressionConverter = this.CreateExpressionConverter(); DbExpression query = expressionConverter.Convert(); IEnumerable <Tuple <ObjectParameter, QueryParameterExpression> > parameters = expressionConverter.GetParameters(); ObjectQueryExecutionPlan newPlan = this._objectQueryExecutionPlanFactory.Prepare(this.ObjectContext, DbQueryCommandTree.FromValidExpression(this.ObjectContext.MetadataWorkspace, DataSpace.CSpace, query, !comparisonBehavior), this.ElementType, mergeOption, this.EffectiveStreamingBehavior, expressionConverter.PropagatedSpan, parameters, expressionConverter.AliasGenerator); queryExecutionPlan = compiledQueryCacheEntry.SetExecutionPlan(newPlan, comparisonBehavior); } } else { QueryCacheManager queryCacheManager = this.ObjectContext.MetadataWorkspace.GetQueryCacheManager(); CompiledQueryCacheKey key = new CompiledQueryCacheKey(this._cacheToken); if (queryCacheManager.TryCacheLookup <CompiledQueryCacheKey, CompiledQueryCacheEntry>(key, out compiledQueryCacheEntry)) { this._cacheEntry = compiledQueryCacheEntry; MergeOption mergeOption = ObjectQueryState.EnsureMergeOption(forMergeOption, this.UserSpecifiedMergeOption, compiledQueryCacheEntry.PropagatedMergeOption); queryExecutionPlan = compiledQueryCacheEntry.GetExecutionPlan(mergeOption, comparisonBehavior); } if (queryExecutionPlan == null) { ExpressionConverter expressionConverter = this.CreateExpressionConverter(); DbExpression query = expressionConverter.Convert(); IEnumerable <Tuple <ObjectParameter, QueryParameterExpression> > parameters = expressionConverter.GetParameters(); DbQueryCommandTree tree = DbQueryCommandTree.FromValidExpression(this.ObjectContext.MetadataWorkspace, DataSpace.CSpace, query, !comparisonBehavior); if (compiledQueryCacheEntry == null) { compiledQueryCacheEntry = new CompiledQueryCacheEntry((QueryCacheKey)key, expressionConverter.PropagatedMergeOption); QueryCacheEntry outQueryCacheEntry; if (queryCacheManager.TryLookupAndAdd((QueryCacheEntry)compiledQueryCacheEntry, out outQueryCacheEntry)) { compiledQueryCacheEntry = (CompiledQueryCacheEntry)outQueryCacheEntry; } this._cacheEntry = compiledQueryCacheEntry; } MergeOption mergeOption = ObjectQueryState.EnsureMergeOption(forMergeOption, this.UserSpecifiedMergeOption, compiledQueryCacheEntry.PropagatedMergeOption); queryExecutionPlan = compiledQueryCacheEntry.GetExecutionPlan(mergeOption, comparisonBehavior); if (queryExecutionPlan == null) { ObjectQueryExecutionPlan newPlan = this._objectQueryExecutionPlanFactory.Prepare(this.ObjectContext, tree, this.ElementType, mergeOption, this.EffectiveStreamingBehavior, expressionConverter.PropagatedSpan, parameters, expressionConverter.AliasGenerator); queryExecutionPlan = compiledQueryCacheEntry.SetExecutionPlan(newPlan, comparisonBehavior); } } } ObjectParameterCollection parameterCollection = this.EnsureParameters(); if (queryExecutionPlan.CompiledQueryParameters != null && queryExecutionPlan.CompiledQueryParameters.Any <Tuple <ObjectParameter, QueryParameterExpression> >()) { parameterCollection.SetReadOnly(false); parameterCollection.Clear(); foreach (Tuple <ObjectParameter, QueryParameterExpression> compiledQueryParameter in queryExecutionPlan.CompiledQueryParameters) { ObjectParameter objectParameter = compiledQueryParameter.Item1.ShallowCopy(); QueryParameterExpression parameterExpression = compiledQueryParameter.Item2; parameterCollection.Add(objectParameter); if (parameterExpression != null) { objectParameter.Value = parameterExpression.EvaluateParameter(this._parameterValues); } } } parameterCollection.SetReadOnly(true); return(queryExecutionPlan); }