2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Data / Linq / Sugar / Expressions / SelectExpression.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne\r
6 // \r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
8 // of this software and associated documentation files (the "Software"), to deal\r
9 // in the Software without restriction, including without limitation the rights\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
11 // copies of the Software, and to permit persons to whom the Software is\r
12 // furnished to do so, subject to the following conditions:\r
13 // \r
14 // The above copyright notice and this permission notice shall be included in\r
15 // all copies or substantial portions of the Software.\r
16 // \r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 \r
27 using System;\r
28 using System.Collections.Generic;\r
29 using System.Linq.Expressions;\r
30 \r
31 using DbLinq.Data.Linq.Sugar.Expressions;\r
32 \r
33 namespace DbLinq.Data.Linq.Sugar.Expressions\r
34 {\r
35     /// <summary>\r
36     /// ScopeExpression describes a selection.\r
37     /// It can be present at top-level or as subexpressions\r
38     /// </summary>\r
39 #if !MONO_STRICT\r
40     public\r
41 #endif\r
42     class SelectExpression : OperandsMutableExpression\r
43     {\r
44         public const ExpressionType ExpressionType = (ExpressionType)CustomExpressionType.Scope;\r
45 \r
46         // Involved entities\r
47         public IList<TableExpression> Tables { get; private set; }\r
48         public IList<ColumnExpression> Columns { get; private set; }\r
49 \r
50         // Clauses\r
51         public string ExecuteMethodName { get; set; } // for Execute<> calls, this member is filled with the method name\r
52         public LambdaExpression Reader { get; set; } // Func<IDataRecord,DataMapper,T> --> creates an object from data record\r
53         public IList<Expression> Where { get; private set; }\r
54         public IList<OrderByExpression> OrderBy { get; private set; }\r
55         public IList<GroupExpression> Group { get; private set; }\r
56 \r
57         public Expression Offset { get; set; }\r
58         public Expression Limit { get; set; }\r
59         public Expression OffsetAndLimit { get; set; }\r
60 \r
61         // the following two clauses are used by expressions of same level, linked by a special operation (like "union")\r
62         public SelectExpression NextSelectExpression;\r
63         public SelectOperatorType NextSelectExpressionOperator;\r
64 \r
65         // Parent scope: we will climb up to find if we don't find the request table in the current scope\r
66         public SelectExpression Parent { get; set; }\r
67 \r
68         public SelectExpression()\r
69             : base(ExpressionType, null, null)\r
70         {\r
71             Tables = new List<TableExpression>();\r
72             Columns = new List<ColumnExpression>();\r
73             // Local clauses\r
74             Where = new List<Expression>();\r
75             OrderBy = new List<OrderByExpression>();\r
76             Group = new List<GroupExpression>();\r
77         }\r
78 \r
79         public SelectExpression(SelectExpression parentSelectExpression)\r
80             : base(ExpressionType, null, null)\r
81         {\r
82             Parent = parentSelectExpression;\r
83             // Tables and columns are empty, since the table/column lookup recurses to parentScopePiece\r
84             Tables = new List<TableExpression>();\r
85             Columns = new List<ColumnExpression>();\r
86             // Local clauses\r
87             Where = new List<Expression>();\r
88             OrderBy = new List<OrderByExpression>();\r
89             Group = new List<GroupExpression>();\r
90         }\r
91 \r
92         private SelectExpression(Type type, IList<Expression> operands)\r
93             : base(ExpressionType, type, operands)\r
94         {\r
95         }\r
96 \r
97         protected override Expression Mutate2(IList<Expression> newOperands)\r
98         {\r
99             Type type;\r
100             if (newOperands.Count > 0)\r
101                 type = newOperands[0].Type;\r
102             else\r
103                 type = Type;\r
104             var scopeExpression = new SelectExpression(type, newOperands);\r
105             scopeExpression.Tables = Tables;\r
106             scopeExpression.Columns = Columns;\r
107             scopeExpression.Where = Where;\r
108             scopeExpression.OrderBy = OrderBy;\r
109             scopeExpression.Group = Group;\r
110             scopeExpression.Parent = Parent;\r
111             scopeExpression.ExecuteMethodName = ExecuteMethodName;\r
112             scopeExpression.Reader = Reader;\r
113             scopeExpression.Limit = Limit;\r
114             scopeExpression.Offset = Offset;\r
115             scopeExpression.OffsetAndLimit = OffsetAndLimit;\r
116             scopeExpression.NextSelectExpression = NextSelectExpression;\r
117             scopeExpression.NextSelectExpressionOperator = NextSelectExpressionOperator;\r
118             return scopeExpression;\r
119         }\r
120     }\r
121 }