/// <summary> /// Compiles eSQL <paramref name="functionDefinition" /> and returns <see cref="DbLambda" />. /// Guarantees type match of lambda variables and <paramref name="functionParameters" />. /// Passes thru all excepions coming from <see cref="CqlQuery" />. /// </summary> internal static DbLambda CompileFunctionDefinition( string functionDefinition, IList <FunctionParameter> functionParameters, EdmItemCollection edmItemCollection) { DebugCheck.NotNull(functionParameters); DebugCheck.NotNull(edmItemCollection); var workspace = new MetadataWorkspace(); workspace.RegisterItemCollection(edmItemCollection); Perspective perspective = new ModelPerspective(workspace); // Since we compile lambda expression and generate variables from the function parameter definitions, // the returned DbLambda will contain variable types that match function parameter types. var functionBody = CqlQuery.CompileQueryCommandLambda( functionDefinition, perspective, null /* use default parser options */, null /* parameters */, functionParameters.Select(pInfo => pInfo.TypeUsage.Variable(pInfo.Name))); Debug.Assert(functionBody != null, "functionBody != null"); return(functionBody); }
internal static DbExpression CompileFunctionView( string viewDef, StorageMappingItemCollection mappingItemCollection, ParserOptions.CompilationMode compilationMode, IEnumerable <DbParameterReferenceExpression> parameters) { DebugCheck.NotEmpty(viewDef); DebugCheck.NotNull(mappingItemCollection); Perspective perspective = new TargetPerspective(mappingItemCollection.Workspace); var parserOptions = new ParserOptions(); parserOptions.ParserCompilationMode = compilationMode; // Parameters have to be accessible in the body as regular scope variables, not as command parameters. // Hence compile view as lambda with parameters as lambda vars, then invoke the lambda specifying // command parameters as values of the lambda vars. var functionBody = CqlQuery.CompileQueryCommandLambda( viewDef, perspective, parserOptions, null /* parameters */, parameters.Select(pInfo => pInfo.ResultType.Variable(pInfo.ParameterName))); Debug.Assert(functionBody != null, "functionBody != null"); DbExpression expr = functionBody.Invoke(parameters); return(expr); }
internal static DbLambda CompileFunctionDefinition( string functionDefinition, IList <FunctionParameter> functionParameters, EdmItemCollection edmItemCollection) { ModelPerspective modelPerspective = new ModelPerspective(new MetadataWorkspace((Func <EdmItemCollection>)(() => edmItemCollection), (Func <StoreItemCollection>)(() => (StoreItemCollection)null), (Func <StorageMappingItemCollection>)(() => (StorageMappingItemCollection)null))); return(CqlQuery.CompileQueryCommandLambda(functionDefinition, (Perspective)modelPerspective, (ParserOptions)null, (IEnumerable <DbParameterReferenceExpression>)null, functionParameters.Select <FunctionParameter, DbVariableReferenceExpression>((Func <FunctionParameter, DbVariableReferenceExpression>)(pInfo => pInfo.TypeUsage.Variable(pInfo.Name))))); }
internal static DbExpression CompileFunctionView( string viewDef, StorageMappingItemCollection mappingItemCollection, ParserOptions.CompilationMode compilationMode, IEnumerable <DbParameterReferenceExpression> parameters) { Perspective perspective = (Perspective) new TargetPerspective(mappingItemCollection.Workspace); return((DbExpression)CqlQuery.CompileQueryCommandLambda(viewDef, perspective, new ParserOptions() { ParserCompilationMode = compilationMode }, (IEnumerable <DbParameterReferenceExpression>)null, parameters.Select <DbParameterReferenceExpression, DbVariableReferenceExpression>((Func <DbParameterReferenceExpression, DbVariableReferenceExpression>)(pInfo => pInfo.ResultType.Variable(pInfo.ParameterName)))).Invoke((IEnumerable <DbExpression>)parameters)); }
internal DbExpression Parse() { if (_queryExpression != null) { return(_queryExpression); } List <DbParameterReferenceExpression> parameters = null; if (Parameters != null) { parameters = new List <DbParameterReferenceExpression>(Parameters.Count); foreach (var parameter in Parameters) { var typeUsage = parameter.TypeUsage; if (null == typeUsage) { // Since ObjectParameters do not allow users to specify 'facets', make // sure that the parameter TypeUsage is not populated with the provider // default facet values. ObjectContext.Perspective.TryGetTypeByName( parameter.MappableType.FullNameWithNesting(), false /* bIgnoreCase */, out typeUsage); } Debug.Assert(typeUsage != null, "typeUsage != null"); parameters.Add(typeUsage.Parameter(parameter.Name)); } } var lambda = CqlQuery.CompileQueryCommandLambda( _queryText, // Command Text ObjectContext.Perspective, // Perspective null, // Parser options - null indicates 'use default' parameters, // Parameters null // Variables ); Debug.Assert(lambda.Variables == null || lambda.Variables.Count == 0, "lambda.Variables must be empty"); return(lambda.Body); }
internal DbExpression Parse() { if (this._queryExpression != null) { return(this._queryExpression); } List <DbParameterReferenceExpression> referenceExpressionList = (List <DbParameterReferenceExpression>)null; if (this.Parameters != null) { referenceExpressionList = new List <DbParameterReferenceExpression>(this.Parameters.Count); foreach (ObjectParameter parameter in this.Parameters) { TypeUsage typeUsage = parameter.TypeUsage; if (typeUsage == null) { this.ObjectContext.Perspective.TryGetTypeByName(parameter.MappableType.FullNameWithNesting(), false, out typeUsage); } referenceExpressionList.Add(typeUsage.Parameter(parameter.Name)); } } return(CqlQuery.CompileQueryCommandLambda(this._queryText, (Perspective)this.ObjectContext.Perspective, (ParserOptions)null, (IEnumerable <DbParameterReferenceExpression>)referenceExpressionList, (IEnumerable <DbVariableReferenceExpression>)null).Body); }