コード例 #1
0
ファイル: DynamicViewCompiler.cs プロジェクト: ybdev/ravendb
        private void TransformQueryToClass()
        {
            CSharpSafeName = "Index_" + Regex.Replace(Name, @"[^\w\d]", "_");
            var type = new TypeDeclaration
            {
                Modifiers = Modifiers.Public,
                BaseTypes =
                {
                    new SimpleType(typeof(AbstractViewGenerator).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);

            HandleMapFunctions(ctor);

            HandleTransformResults(ctor);

            HandleReduceDefinition(ctor);

            AddAdditionalInformation(ctor);

            CompiledQueryText = QueryParsingUtils.GenerateText(type, extensions);
            var sb = new StringBuilder("@\"");

            foreach (var map in indexDefinition.Maps)
            {
                sb.AppendLine(map.Replace("\"", "\"\""));
            }

            if (indexDefinition.Reduce != null)
            {
                sb.AppendLine(indexDefinition.Reduce.Replace("\"", "\"\""));
            }

            if (indexDefinition.TransformResults != null)
            {
                sb.AppendLine(indexDefinition.TransformResults.Replace("\"", "\"\""));
            }
            sb.Length = sb.Length - 2;

            sb.Append("\"");
            CompiledQueryText = CompiledQueryText.Replace('"' + uniqueTextToken + '"', sb.ToString());
        }
コード例 #2
0
        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
            })));


            mapDefinition.Initializer.AcceptVisitor(captureSelectNewFieldNamesVisitor, null);

            mapDefinition.Initializer.AcceptVisitor(captureQueryParameterNamesVisitorForMap, null);

            HandleTransformResults(ctor);

            HandleReduceDefintion(ctor);

            AddAdditionalInformation(ctor);

            CompiledQueryText = QueryParsingUtils.GenerateText(type, extensions);
            var compiledQueryText = "@\"" + indexDefinition.Map.Replace("\"", "\"\"");

            if (indexDefinition.Reduce != null)
            {
                compiledQueryText += Environment.NewLine + indexDefinition.Reduce.Replace("\"", "\"\"");
            }

            if (indexDefinition.TransformResults != null)
            {
                compiledQueryText += Environment.NewLine + indexDefinition.TransformResults.Replace("\"", "\"\"");
            }

            compiledQueryText += "\"";
            CompiledQueryText  = CompiledQueryText.Replace("\"" + mapReduceTextToken + "\"",
                                                           compiledQueryText);
        }
コード例 #3
0
        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);

                    translatorDeclaration.AcceptVisitor(new TransformFromClauses(), null);
                }
                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);
            }
        }
コード例 #4
0
        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);
        }