public static GetVariableDeclarationForLinqMethods ( string query, bool requiresSelectNewAnonymousType ) : |
||
query | string | |
requiresSelectNewAnonymousType | bool | |
return |
private void HandleTransformResults(ConstructorDeclaration ctor) { if (indexDefinition.TransformResults == null) { return; } VariableDeclaration translatorDeclaration; if (indexDefinition.TransformResults.Trim().StartsWith("from")) { translatorDeclaration = QueryParsingUtils.GetVariableDeclarationForLinqQuery(indexDefinition.TransformResults, requiresSelectNewAnonymousType: false); } else { translatorDeclaration = QueryParsingUtils.GetVariableDeclarationForLinqMethods(indexDefinition.TransformResults); } // this.Translator = (Database,results) => from doc in results ...; ctor.Body.AddChild(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "TransformResultsDefinition"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclarationExpression(null, "Database"), new ParameterDeclarationExpression(null, "results") }, ExpressionBody = translatorDeclaration.Initializer }))); }
private VariableInitializer TransformMapDefinitionFromLinqMethodSyntax(string query, out string entityName) { var variableDeclaration = QueryParsingUtils.GetVariableDeclarationForLinqMethods(query, RequiresSelectNewAnonymousType); AddEntityNameFilteringIfNeeded(variableDeclaration, out entityName); variableDeclaration.AcceptVisitor(new AddDocumentIdToLambdas(), null); return(variableDeclaration); }
private VariableDeclaration TransformMapDefinitionFromLinqMethodSyntax(out string entityName) { var variableDeclaration = QueryParsingUtils.GetVariableDeclarationForLinqMethods(indexDefinition.Map); AddEntityNameFilteringIfNeeded(variableDeclaration, out entityName); variableDeclaration.AcceptVisitor(new AddDocumentIdToLambdas(), null); return(variableDeclaration); }
private void HandleTransformResults(ConstructorDeclaration ctor) { try { if (string.IsNullOrEmpty(indexDefinition.TransformResults)) { return; } VariableInitializer translatorDeclaration; if (indexDefinition.TransformResults.Trim().StartsWith("from")) { translatorDeclaration = QueryParsingUtils.GetVariableDeclarationForLinqQuery(indexDefinition.TransformResults, requiresSelectNewAnonymousType: false); } else { translatorDeclaration = QueryParsingUtils.GetVariableDeclarationForLinqMethods(indexDefinition.TransformResults, requiresSelectNewAnonymousType: false); } translatorDeclaration.AcceptVisitor(new ThrowOnInvalidMethodCallsForTransformResults(), null); // this.Translator = (Database,results) => from doc in results ...; ctor.Body.Statements.Add(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "TransformResultsDefinition"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclaration(null, "Database"), new ParameterDeclaration(null, "results") }, Body = translatorDeclaration.Initializer.Clone() }))); } catch (InvalidOperationException ex) { throw new IndexCompilationException(ex.Message, ex) { IndexDefinitionProperty = "TransformResults", ProblematicText = indexDefinition.TransformResults }; } }
private VariableDeclaration TransformMapDefinitionFromLinqMethodSyntax(out string entityName) { var variableDeclaration = QueryParsingUtils.GetVariableDeclarationForLinqMethods(indexDefinition.Map); AddEntityNameFilteringIfNeeded(variableDeclaration, out entityName); var invocationExpression = ((InvocationExpression)variableDeclaration.Initializer); var targetExpression = ((MemberReferenceExpression)invocationExpression.TargetObject); do { AddDocumentIdFieldToLambdaIfCreatingNewObject((LambdaExpression)invocationExpression.Arguments.Last()); invocationExpression = (InvocationExpression)targetExpression.TargetObject; targetExpression = (MemberReferenceExpression)invocationExpression.TargetObject; } while (targetExpression.TargetObject is InvocationExpression); return(variableDeclaration); }
private void HandleReduceDefinition(ConstructorDeclaration ctor) { try { if (!indexDefinition.IsMapReduce) { return; } VariableInitializer reduceDefinition; AstNode groupBySource; string groupByParameter; string groupByIdentifier; if (indexDefinition.Reduce.Trim().StartsWith("from")) { reduceDefinition = QueryParsingUtils.GetVariableDeclarationForLinqQuery(indexDefinition.Reduce, RequiresSelectNewAnonymousType); var queryExpression = ((QueryExpression)reduceDefinition.Initializer); var queryContinuationClause = queryExpression.Clauses.OfType <QueryContinuationClause>().FirstOrDefault(); if (queryContinuationClause == null) { throw new IndexCompilationException("Reduce query must contain a 'group ... into ...' clause") { ProblematicText = indexDefinition.Reduce, IndexDefinitionProperty = "Reduce", }; } var queryGroupClause = queryContinuationClause.PrecedingQuery.Clauses.OfType <QueryGroupClause>().FirstOrDefault(); if (queryGroupClause == null) { throw new IndexCompilationException("Reduce query must contain a 'group ... into ...' clause") { ProblematicText = indexDefinition.Reduce, IndexDefinitionProperty = "Reduce", }; } groupByIdentifier = queryContinuationClause.Identifier; groupBySource = queryGroupClause.Key; groupByParameter = queryContinuationClause.PrecedingQuery.Clauses.OfType <QueryFromClause>().First().Identifier; } else { reduceDefinition = QueryParsingUtils.GetVariableDeclarationForLinqMethods(indexDefinition.Reduce, RequiresSelectNewAnonymousType); var initialInvocation = ((InvocationExpression)reduceDefinition.Initializer); var invocation = initialInvocation; var target = (MemberReferenceExpression)invocation.Target; while (target.MemberName != "GroupBy") { if (!(target.Target is InvocationExpression)) { // we've reached the initial results variable without encountering a GroupBy call throw new IndexCompilationException("Reduce expression must contain a call to GroupBy") { ProblematicText = indexDefinition.Reduce, IndexDefinitionProperty = "Reduce", }; } invocation = (InvocationExpression)target.Target; target = (MemberReferenceExpression)invocation.Target; } var lambdaExpression = GetLambdaExpression(invocation); groupByParameter = lambdaExpression.Parameters.First().Name; groupBySource = lambdaExpression.Body; groupByIdentifier = null; } var mapFields = captureSelectNewFieldNamesVisitor.FieldNames.ToList(); captureSelectNewFieldNamesVisitor.Clear(); // reduce override the map fields reduceDefinition.Initializer.AcceptVisitor(captureSelectNewFieldNamesVisitor, null); reduceDefinition.Initializer.AcceptVisitor(captureQueryParameterNamesVisitorForReduce, null); reduceDefinition.Initializer.AcceptVisitor(new ThrowOnInvalidMethodCalls(groupByIdentifier), null); ValidateMapReduceFields(mapFields); // this.ReduceDefinition = from result in results...; ctor.Body.Statements.Add(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "ReduceDefinition"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclaration(null, "results") }, Body = reduceDefinition.Initializer.Clone() }))); ctor.Body.Statements.Add(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "GroupByExtraction"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclaration(null, groupByParameter) }, Body = groupBySource.Clone() }))); } catch (InvalidOperationException ex) { throw new IndexCompilationException(ex.Message) { ProblematicText = indexDefinition.Reduce, IndexDefinitionProperty = "Reduce", }; } }
private void HandleReduceDefintion(ConstructorDeclaration ctor) { if (!indexDefinition.IsMapReduce) { return; } VariableDeclaration reduceDefiniton; Expression groupBySource; string groupByParamter; if (indexDefinition.Reduce.Trim().StartsWith("from")) { reduceDefiniton = QueryParsingUtils.GetVariableDeclarationForLinqQuery(indexDefinition.Reduce, RequiresSelectNewAnonymousType); var sourceSelect = (QueryExpression)((QueryExpression)reduceDefiniton.Initializer).FromClause.InExpression; groupBySource = ((QueryExpressionGroupClause)sourceSelect.SelectOrGroupClause).GroupBy; groupByParamter = sourceSelect.FromClause.Identifier; } else { reduceDefiniton = QueryParsingUtils.GetVariableDeclarationForLinqMethods(indexDefinition.Reduce, RequiresSelectNewAnonymousType); var invocation = ((InvocationExpression)reduceDefiniton.Initializer); var target = (MemberReferenceExpression)invocation.TargetObject; while (target.MemberName != "GroupBy") { invocation = (InvocationExpression)target.TargetObject; target = (MemberReferenceExpression)invocation.TargetObject; } var lambdaExpression = GetLambdaExpression(invocation); groupByParamter = lambdaExpression.Parameters[0].ParameterName; groupBySource = lambdaExpression.ExpressionBody; } var mapFields = captureSelectNewFieldNamesVisitor.FieldNames.ToList(); captureSelectNewFieldNamesVisitor.Clear(); // reduce override the map fields reduceDefiniton.Initializer.AcceptVisitor(captureSelectNewFieldNamesVisitor, null); reduceDefiniton.Initializer.AcceptChildren(captureQueryParameterNamesVisitorForReduce, null); reduceDefiniton.Initializer.AcceptVisitor(new ThrowOnInvalidMethodCalls(), null); ValidateMapReduceFields(mapFields); // this.ReduceDefinition = from result in results...; ctor.Body.AddChild(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "ReduceDefinition"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclarationExpression(null, "results") }, ExpressionBody = reduceDefiniton.Initializer }))); ctor.Body.AddChild(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "GroupByExtraction"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclarationExpression(null, groupByParamter) }, ExpressionBody = groupBySource }))); }
private void TransformToClass() { if (transformerDefinition.TransformResults == null) { throw new TransformCompilationException("Cannot compile a transformer without a transformer function"); } try { CSharpSafeName = "Transformer_" + Regex.Replace(Name, @"[^\w\d]", "_"); var type = new TypeDeclaration { Modifiers = Modifiers.Public, BaseTypes = { new SimpleType(typeof(AbstractTransformer).FullName) }, Name = CSharpSafeName, ClassType = ClassType.Class }; var body = new BlockStatement(); // this.ViewText = "96E65595-1C9E-4BFB-A0E5-80BF2D6FC185"; // Will be replaced later var viewText = new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "ViewText"), AssignmentOperatorType.Assign, new StringLiteralExpression(uniqueTextToken))); body.Statements.Add(viewText); var ctor = new ConstructorDeclaration { Name = CSharpSafeName, Modifiers = Modifiers.Public, Body = body }; type.Members.Add(ctor); VariableInitializer translatorDeclaration; if (transformerDefinition.TransformResults.Trim().StartsWith("from")) { translatorDeclaration = QueryParsingUtils.GetVariableDeclarationForLinqQuery(transformerDefinition.TransformResults, requiresSelectNewAnonymousType: false); } else { translatorDeclaration = QueryParsingUtils.GetVariableDeclarationForLinqMethods(transformerDefinition.TransformResults, requiresSelectNewAnonymousType: false); } translatorDeclaration.AcceptVisitor(new ThrowOnInvalidMethodCallsForTransformResults(), null); // this.Translator = (results) => from doc in results ...; ctor.Body.Statements.Add(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "TransformResultsDefinition"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclaration(null, "results") }, Body = translatorDeclaration.Initializer.Clone() }))); CompiledQueryText = QueryParsingUtils.GenerateText(type, extensions); var sb = new StringBuilder("@\""); sb.AppendLine(transformerDefinition.TransformResults.Replace("\"", "\"\"")); sb.Append("\""); CompiledQueryText = CompiledQueryText.Replace('"' + uniqueTextToken + '"', sb.ToString()); } catch (Exception ex) { throw new TransformCompilationException(ex.Message, ex); } }
private void TransformQueryToClass() { string entityName; var mapDefinition = TransformMapDefinition(out entityName); CSharpSafeName = "Index_" + Regex.Replace(Name, @"[^\w\d]", "_"); var type = new TypeDeclaration(Modifiers.Public, new List <AttributeSection>()) { BaseTypes = { new TypeReference("AbstractViewGenerator") }, Name = CSharpSafeName, Type = ClassType.Class }; var ctor = new ConstructorDeclaration(CSharpSafeName, Modifiers.Public, new List <ParameterDeclarationExpression>(), null); type.Children.Add(ctor); ctor.Body = new BlockStatement(); //this.ForEntityName = entityName; ctor.Body.AddChild(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "ForEntityName"), AssignmentOperatorType.Assign, new PrimitiveExpression(entityName, entityName)))); // this.ViewText = "96E65595-1C9E-4BFB-A0E5-80BF2D6FC185"; // Will be replaced later ctor.Body.AddChild(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "ViewText"), AssignmentOperatorType.Assign, new PrimitiveExpression(mapReduceTextToken, mapReduceTextToken)))); // this.MapDefinition = from doc in docs ...; ctor.Body.AddChild(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "MapDefinition"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclarationExpression(null, "docs") }, ExpressionBody = mapDefinition.Initializer }))); if (indexDefinition.IsMapReduce) { VariableDeclaration reduceDefiniton; Expression groupBySource; string groupByParamter; if (indexDefinition.Reduce.Trim().StartsWith("from")) { reduceDefiniton = QueryParsingUtils.GetVariableDeclarationForLinqQuery(indexDefinition.Reduce); var sourceSelect = (QueryExpression)((QueryExpression)reduceDefiniton.Initializer).FromClause.InExpression; groupBySource = ((QueryExpressionGroupClause)sourceSelect.SelectOrGroupClause).GroupBy; groupByParamter = sourceSelect.FromClause.Identifier; } else { reduceDefiniton = QueryParsingUtils.GetVariableDeclarationForLinqMethods(indexDefinition.Reduce); var invocation = ((InvocationExpression)reduceDefiniton.Initializer); var target = (MemberReferenceExpression)invocation.TargetObject; while (target.MemberName != "GroupBy") { invocation = (InvocationExpression)target.TargetObject; target = (MemberReferenceExpression)invocation.TargetObject; } var lambdaExpression = ((LambdaExpression)invocation.Arguments[0]); groupByParamter = lambdaExpression.Parameters[0].ParameterName; groupBySource = lambdaExpression.ExpressionBody; } // this.ReduceDefinition = from result in results...; ctor.Body.AddChild(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "ReduceDefinition"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclarationExpression(null, "results") }, ExpressionBody = reduceDefiniton.Initializer }))); ctor.Body.AddChild(new ExpressionStatement( new AssignmentExpression( new MemberReferenceExpression(new ThisReferenceExpression(), "GroupByExtraction"), AssignmentOperatorType.Assign, new LambdaExpression { Parameters = { new ParameterDeclarationExpression(null, groupByParamter) }, ExpressionBody = groupBySource }))); } CompiledQueryText = QueryParsingUtils.GenerateText(type, extensions); var compiledQueryText = "@\"" + indexDefinition.Map.Replace("\"", "\"\""); if (indexDefinition.Reduce != null) { compiledQueryText += Environment.NewLine + indexDefinition.Reduce.Replace("\"", "\"\""); } compiledQueryText += "\""; CompiledQueryText = CompiledQueryText.Replace("\"" + mapReduceTextToken + "\"", compiledQueryText); }