PageRenderTime 464ms CodeModel.GetById 181ms app.highlight 4ms RepoModel.GetById 278ms app.codeStats 0ms

/Rhino.Etl.Dsl/Macros/GroupByMacro.cs

http://github.com/ayende/rhino-etl
C# | 68 lines | 48 code | 8 blank | 12 comment | 2 complexity | 938f2ef7408d5f07fff22bd40de48479 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 Rhino.Etl.Core.Operations;
 7
 8    /// <summary>
 9    /// Creates the <see cref="AbstractAggregationOperation.GetColumnsToGroupBy"/> method from the reference
10    /// expressions
11    /// </summary>
12    public class GroupByMacro : AbstractChildMacro
13    {
14        /// <summary>
15        /// Initializes a new instance of the <see cref="GroupByMacro"/> class.
16        /// </summary>
17        public GroupByMacro() : base("aggregate")
18        {
19        }
20
21        /// <summary>
22        /// Perform the actual expansion of the macro
23        /// </summary>
24        /// <param name="macro">The macro.</param>
25        /// <returns></returns>
26        protected override Statement DoExpand(MacroStatement macro)
27        {
28            List<string> columns = new List<string>();
29
30            if(!macro.Body.IsEmpty)
31            {
32                Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "GroupBy cannot contain statements"));
33                return null;
34            }
35            foreach (Expression argument in macro.Arguments)
36            {
37                ReferenceExpression expr = argument as ReferenceExpression;
38                if(expr==null)
39                {
40                    Errors.Add(CompilerErrorFactory.CustomError(macro.LexicalInfo, "GroupBy arguments must be refernce expressions. Example: groupBy name, surname"));
41                    return null;
42                }
43                columns.Add(expr.Name);
44            }
45
46            Method method = CreateGetColumnsToGroupByMethod(macro, columns);
47
48            ParentMethods.Add(method);
49
50            return null;
51        }
52
53        private Method CreateGetColumnsToGroupByMethod(MacroStatement macro, IEnumerable<string> columns)
54        {
55            Method method = new Method("GetColumnsToGroupBy");
56            method.Modifiers = TypeMemberModifiers.Override;
57            ArrayLiteralExpression ale = new ArrayLiteralExpression(macro.LexicalInfo);
58            ale.Type = new ArrayTypeReference(CodeBuilder.CreateTypeReference(typeof(string)));
59            
60            foreach (string column in columns)
61            {
62                ale.Items.Add(new StringLiteralExpression(column));    
63            }
64            method.Body.Statements.Add(new ReturnStatement(ale));
65            return method;
66        }
67    }
68}