private static Expression SelectResourceByKey(Expression queryExpression, ResourceType resourceType, KeyInstance key) { for (int i = 0; i < resourceType.KeyProperties.Count; i++) { Expression expression2; ResourceProperty property = resourceType.KeyProperties[i]; object obj2 = key.AreValuesNamed ? key.NamedValues[property.Name] : key.PositionalValues[i]; ParameterExpression expression = Expression.Parameter(queryExpression.ElementType(), "element"); if (property.CanReflectOnInstanceTypeProperty) { expression2 = Expression.Property(expression, property.Name); } else { expression2 = Expression.Convert(Expression.Call(null, DataServiceProviderMethods.GetValueMethodInfo, expression, Expression.Constant(property)), property.Type); } LambdaExpression predicate = Expression.Lambda(Expression.Equal(expression2, Expression.Constant(obj2)), new ParameterExpression[] { expression }); queryExpression = queryExpression.QueryableWhere(predicate); } return queryExpression; }
private static Expression SelectMultiple(Expression queryExpression, ResourceProperty property) { Type iEnumerableElement = BaseServiceProvider.GetIEnumerableElement(property.Type); ParameterExpression expression = Expression.Parameter(queryExpression.ElementType(), "element"); LambdaExpression selector = Expression.Lambda(Expression.ConvertChecked(Expression.Property(expression, property.Name), typeof(IEnumerable<>).MakeGenericType(new Type[] { iEnumerableElement })), new ParameterExpression[] { expression }); return queryExpression.QueryableSelectMany(selector); }
private static Expression SelectOpenProperty(Expression queryExpression, string propertyName) { ParameterExpression expression = Expression.Parameter(queryExpression.ElementType(), "element"); LambdaExpression selector = Expression.Lambda(Expression.Call(null, OpenTypeMethods.GetValueOpenPropertyMethodInfo, expression, Expression.Constant(propertyName)), new ParameterExpression[] { expression }); return queryExpression.QueryableSelect(selector); }
private static Expression SelectLateBoundProperty(Expression queryExpression, ResourceProperty property) { ParameterExpression expression = Expression.Parameter(queryExpression.ElementType(), "element"); LambdaExpression selector = Expression.Lambda(Expression.Convert(Expression.Call(null, DataServiceProviderMethods.GetValueMethodInfo, expression, Expression.Constant(property)), property.Type), new ParameterExpression[] { expression }); return queryExpression.QueryableSelect(selector); }
private static Expression SelectLateBoundPropertyMultiple(Expression queryExpression, ResourceProperty property) { Type iEnumerableElement = BaseServiceProvider.GetIEnumerableElement(property.Type); ParameterExpression expression = Expression.Parameter(queryExpression.ElementType(), "element"); MethodInfo method = DataServiceProviderMethods.GetSequenceValueMethodInfo.MakeGenericMethod(new Type[] { iEnumerableElement }); LambdaExpression selector = Expression.Lambda(Expression.Call(null, method, expression, Expression.Constant(property)), new ParameterExpression[] { expression }); return queryExpression.QueryableSelectMany(selector); }
private static Expression SelectDerivedResourceType(Expression queryExpression, ResourceType resourceType) { if (resourceType.CanReflectOnInstanceType) { return queryExpression.QueryableOfType(resourceType.InstanceType); } Type type = queryExpression.ElementType(); ConstantExpression expression = Expression.Constant(resourceType, typeof(ResourceType)); return Expression.Call(null, DataServiceExecutionProviderMethods.OfTypeMethodInfo.MakeGenericMethod(new Type[] { type, resourceType.InstanceType }), queryExpression, expression); }
private static Expression SelectElement(Expression queryExpression, ResourceProperty property) { ParameterExpression expression = Expression.Parameter(queryExpression.ElementType(), "element"); Expression expression2 = Expression.Property(expression, property.Name); if ((property.TypeKind == ResourceTypeKind.Collection) && (expression2.Type != property.Type)) { expression2 = Expression.Convert(expression2, property.Type); } LambdaExpression selector = Expression.Lambda(expression2, new ParameterExpression[] { expression }); return queryExpression.QueryableSelect(selector); }
/// <summary>Selects a single resource by key values.</summary> /// <param name="queryExpression">Base query for resources</param> /// <param name="resourceType">resource type whose keys are specified</param> /// <param name="key">Key values for the given resource type.</param> /// <returns>A new query that selects the single resource that matches the specified key values.</returns> private static Expression SelectResourceByKey(Expression queryExpression, ResourceType resourceType, KeySegment key) { Debug.Assert(queryExpression != null, "queryExpression != null"); Debug.Assert(key != null, "key != null"); List<KeyValuePair<string, object>> keyValues = key.Keys.ToList(); Debug.Assert(keyValues.Count != 0, "keyValues.Count != 0"); Debug.Assert(resourceType.KeyProperties.Count == keyValues.Count, "resourceType.KeyProperties.Count == keyValues.Count"); for (int i = 0; i < resourceType.KeyProperties.Count; i++) { ResourceProperty keyProperty = resourceType.KeyProperties[i]; Debug.Assert(keyProperty.IsOfKind(ResourcePropertyKind.Key), "keyProperty.IsOfKind(ResourcePropertyKind.Key)"); object keyValue; if (keyValues.Count == 0) { keyValue = keyValues[0].Value; } else { keyValue = keyValues.Single(v => v.Key == keyProperty.Name).Value; } var binaryValue = keyValue as byte[]; if (binaryValue != null && keyProperty.Type == typeof(System.Data.Linq.Binary)) { keyValue = new System.Data.Linq.Binary(binaryValue); } var stringValue = keyValue as string; if (stringValue != null && keyProperty.Type == typeof(XElement)) { keyValue = XElement.Parse(stringValue); } if (keyProperty.Type == typeof(DateTime)) { Debug.Assert(keyValue != null && keyValue is DateTimeOffset, "For DateTime properties, the value must be read as DateTimeOffset"); keyValue = WebUtil.ConvertDateTimeOffsetToDateTime((DateTimeOffset)keyValue); } ParameterExpression parameter = Expression.Parameter(queryExpression.ElementType(), "element"); Expression e; if (keyProperty.CanReflectOnInstanceTypeProperty) { e = Expression.Property(parameter, keyProperty.Name); } else { e = Expression.Call(null /*instance*/, DataServiceProviderMethods.GetValueMethodInfo, parameter, Expression.Constant(keyProperty)); e = Expression.Convert(e, keyProperty.Type); } BinaryExpression body = Expression.Equal(e, Expression.Constant(keyValue)); LambdaExpression predicate = Expression.Lambda(body, parameter); queryExpression = queryExpression.QueryableWhere(predicate); } return queryExpression; }
/// <summary>Filters a query like a SQL WHERE clause does.</summary> /// <param name="service">Service with data and configuration.</param> /// <param name="requestDescription">RequestDescription instance containing information about the current request being parsed.</param> /// <param name="source">Original source for query expression.</param> /// <returns>The composed query expression.</returns> internal static Expression Where(IDataService service, RequestDescription requestDescription, Expression source) { Debug.Assert(service != null, "service != null"); Debug.Assert(source != null, "source != null"); Debug.Assert(requestDescription != null, "requestDescription != null"); FilterClause filterClause = new RequestExpressionParser(service, requestDescription).ParseFilter(); if (filterClause == null) { return source; } bool filterQueryApplicable = requestDescription.TargetKind == RequestTargetKind.Resource || requestDescription.TargetKind == RequestTargetKind.OpenProperty || requestDescription.TargetKind == RequestTargetKind.ComplexObject || requestDescription.CountOption == RequestQueryCountOption.CountSegment; if (!filterQueryApplicable) { throw DataServiceException.CreateBadRequestError(Strings.RequestQueryProcessor_QueryFilterOptionNotApplicable); } Type queryElementType = source.ElementType(); Debug.Assert(queryElementType != null, "queryElementType != null"); ParameterExpression parameterForIt = Expression.Parameter(queryElementType, "it"); Debug.Assert( (requestDescription.TargetResourceSet == null && (requestDescription.TargetResourceType == null || requestDescription.TargetResourceType.ResourceTypeKind != ResourceTypeKind.EntityType)) || (requestDescription.TargetResourceType != null && requestDescription.TargetResourceType.ResourceTypeKind == ResourceTypeKind.EntityType), "setForIt cannot be null if typeForIt is an entity type."); Debug.Assert( requestDescription.TargetResourceType == null && parameterForIt.Type == typeof(object) || requestDescription.TargetResourceType != null && requestDescription.TargetResourceType.InstanceType == parameterForIt.Type, "non-open type expressions should have a typeForIt"); var translator = NodeToExpressionTranslator.Create(service, requestDescription, parameterForIt); LambdaExpression lambda = translator.TranslateFilterClause(filterClause); return source.QueryableWhere(lambda); }
private static Expression SelectLateBoundPropertyMultiple(Expression queryExpression, ResourceProperty property) { Debug.Assert(queryExpression != null, "queryExpression != null"); Debug.Assert(property != null && property.Kind == ResourcePropertyKind.ResourceSetReference && !property.CanReflectOnInstanceTypeProperty, "property != null && property.Kind == ResourcePropertyKind.ResourceSetReference && !property.CanReflectOnInstanceTypeProperty"); Type enumerableElement = BaseServiceProvider.GetIEnumerableElement(property.Type); ParameterExpression parameter = Expression.Parameter(queryExpression.ElementType(), "element"); MethodInfo getter = DataServiceProviderMethods.GetSequenceValueMethodInfo.MakeGenericMethod(enumerableElement); Expression body = Expression.Call(null /*instance*/, getter, parameter, Expression.Constant(property)); LambdaExpression selector = Expression.Lambda(body, parameter); return queryExpression.QueryableSelectMany(selector); }
private static Expression SelectLateBoundProperty(Expression queryExpression, ResourceProperty property) { Debug.Assert(queryExpression != null, "queryExpression != null"); Debug.Assert(property != null && !property.CanReflectOnInstanceTypeProperty, "property != null && !property.CanReflectOnInstanceTypeProperty"); ParameterExpression parameter = Expression.Parameter(queryExpression.ElementType(), "element"); Expression body = Expression.Call(null /*instance*/, DataServiceProviderMethods.GetValueMethodInfo, parameter, Expression.Constant(property)); body = Expression.Convert(body, property.Type); LambdaExpression selector = Expression.Lambda(body, parameter); return queryExpression.QueryableSelect(selector); }
private static Expression SelectOpenProperty(Expression queryExpression, string propertyName) { Debug.Assert(queryExpression != null, "queryExpression != null"); Debug.Assert(propertyName != null, "propertyName != null"); ParameterExpression parameter = Expression.Parameter(queryExpression.ElementType(), "element"); Expression body = Expression.Call(null /* instance */, OpenTypeMethods.GetValueOpenPropertyMethodInfo, parameter, Expression.Constant(propertyName)); LambdaExpression selector = Expression.Lambda(body, parameter); return queryExpression.QueryableSelect(selector); }
private static Expression SelectMultiple(Expression queryExpression, ResourceProperty property) { Debug.Assert(queryExpression != null, "queryExpression != null"); Debug.Assert(property.Kind == ResourcePropertyKind.ResourceSetReference, "property == ResourcePropertyKind.ResourceSetReference"); Type enumerableElement = BaseServiceProvider.GetIEnumerableElement(property.Type); Debug.Assert(enumerableElement != null, "Providers should never expose a property as a resource-set if it doesn't implement IEnumerable`1."); ParameterExpression parameter = Expression.Parameter(queryExpression.ElementType(), "element"); UnaryExpression body = Expression.ConvertChecked( Expression.Property(parameter, property.Name), typeof(IEnumerable<>).MakeGenericType(enumerableElement)); LambdaExpression selector = Expression.Lambda(body, parameter); return queryExpression.QueryableSelectMany(selector); }
private static Expression SelectElement(Expression queryExpression, ResourceProperty property) { Debug.Assert(queryExpression != null, "queryExpression != null"); Debug.Assert(property.Kind != ResourcePropertyKind.ResourceSetReference, "property != ResourcePropertyKind.ResourceSetReference"); ParameterExpression parameter = Expression.Parameter(queryExpression.ElementType(), "element"); Expression body = Expression.Property(parameter, property.Name); // If the property is a collection its instance type is always IEnumerable<T>, but the actual type of the property // might be type a type implementing the IEnumerable<T> (Like List<T>). We need to add an explicit cast here // so that our expression types match and we will be able to call the lambda later. if (property.TypeKind == ResourceTypeKind.Collection && body.Type != property.Type) { body = Expression.Convert(body, property.Type); } LambdaExpression selector = Expression.Lambda(body, parameter); return queryExpression.QueryableSelect(selector); }