PageRenderTime 36ms CodeModel.GetById 22ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Rhino.Etl.Dsl/Macros/OnMacro.cs

http://github.com/ayende/rhino-etl
C# | 70 lines | 49 code | 9 blank | 12 comment | 2 complexity | b1a5510e1929e17121c098cdc4384f93 MD5 | raw file
 1namespace Rhino.Etl.Dsl.Macros
 2{
 3    using System.Collections.Generic;
 4    using Boo.Lang.Compiler;
 5    using Boo.Lang.Compiler.Ast;
 6    using Boo.Lang.Compiler.TypeSystem;
 7    using Core;
 8
 9    /// <summary>
10    /// The on part of a join, split into two parts, the first is the condition, the second
11    /// is the merge
12    /// </summary>
13    public class OnMacro : AbstractChildMacro
14    {
15        /// <summary>
16        /// Initializes a new instance of the <see cref="OnMacro"/> class.
17        /// </summary>
18        public OnMacro()
19            : base("join")
20        {
21        }
22
23
24        /// <summary>
25        /// Perform the actual expansion of the macro
26        /// </summary>
27        /// <param name="macro">The macro.</param>
28        /// <returns></returns>
29        protected override Statement DoExpand(MacroStatement macro)
30        {
31            if (macro.Arguments.Count != 1)
32            {
33                Errors.Add(
34                    CompilerErrorFactory.CustomError(macro.LexicalInfo,
35                                                     "Only a single argument allowed for on statement"));
36                return null;
37            }
38
39            Method mergeRowsMethod = new Method("MergeRows");
40            mergeRowsMethod.Modifiers = TypeMemberModifiers.Override;
41            mergeRowsMethod.Parameters.Add(new ParameterDeclaration("left", new SimpleTypeReference(typeof(Row).FullName)));
42            mergeRowsMethod.Parameters.Add(new ParameterDeclaration("right", new SimpleTypeReference(typeof(Row).FullName)));
43            CodeBuilder.DeclareLocal(mergeRowsMethod, "row", TypeSystemServices.Map(typeof(Row)));
44            mergeRowsMethod.Body.Add(
45                new BinaryExpression(BinaryOperatorType.Assign,
46                                     new ReferenceExpression("row"),
47                                     new MethodInvocationExpression(
48                                         AstUtil.CreateReferenceExpression(typeof(Row).FullName))
49                    )
50                );
51            mergeRowsMethod.Body.Add(macro.Body);
52            mergeRowsMethod.Body.Add(new ReturnStatement(new ReferenceExpression("row")));
53
54            ParentMethods.Add(mergeRowsMethod);
55
56
57            Method matchJoinConditionMethod = new Method("MatchJoinCondition");
58            matchJoinConditionMethod.ReturnType = new SimpleTypeReference(typeof (bool).FullName);
59            matchJoinConditionMethod.Parameters.Add(new ParameterDeclaration("left", new SimpleTypeReference(typeof(Row).FullName)));
60            matchJoinConditionMethod.Parameters.Add(new ParameterDeclaration("right", new SimpleTypeReference(typeof(Row).FullName)));
61            matchJoinConditionMethod.Body.Add(
62                new ReturnStatement(macro.Arguments[0])
63                );
64            
65            ParentMethods.Add(matchJoinConditionMethod);
66
67            return null;
68        }
69    }
70}