1 #region MIT license
2 //
3 // MIT license
4 //
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 // THE SOFTWARE.
24 //
25 #endregion
26 
27 using System;
28 using System.Diagnostics;
29 using System.Linq.Expressions;
30 
31 using DbLinq.Data.Linq.Sugar.Expressions;
32 
33 namespace DbLinq.Data.Linq.Sugar.Expressions
34 {
35     /// <summary>
36     /// A table is a default table, or a joined table
37     /// Different joins specify different tables
38     /// </summary>
39     [DebuggerDisplay("TableExpression {Name} (as {Alias})")]
40 #if !MONO_STRICT
41     public
42 #endif
43     class TableExpression : MutableExpression
44     {
45         public const ExpressionType ExpressionType = (ExpressionType)CustomExpressionType.Table;
46 
47         // Table idenfitication
48         public string Name { get; private set; }
49 
50         // Join: if this table is related to another, the following informations are filled
51         public Expression JoinExpression { get; private set; } // full information is here, ReferencedTable and Join could be (in theory) extracted from this
52         public TableJoinType JoinType { get; private set; }
53         public string JoinID { get; private set; }
54         public TableExpression JoinedTable { get; private set; }
55 
56         public string Alias { get; set; }
57 
58         /// <summary>
59         /// Set table join
60         /// </summary>
61         /// <param name="joinType"></param>
62         /// <param name="joinedTable"></param>
63         /// <param name="joinExpression"></param>
Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression)64         public void Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression)
65         {
66             JoinExpression = joinExpression;
67             JoinType = joinType;
68             JoinedTable = joinedTable;
69         }
70 
71         /// <summary>
72         /// Set table join
73         /// </summary>
74         /// <param name="joinType"></param>
75         /// <param name="joinedTable"></param>
76         /// <param name="joinExpression"></param>
77         /// <param name="joinID"></param>
Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression, string joinID)78         public void Join(TableJoinType joinType, TableExpression joinedTable, Expression joinExpression, string joinID)
79         {
80             Join(joinType, joinedTable, joinExpression);
81             JoinID = joinID;
82         }
83 
84         /// <summary>
85         /// Set the table outer join, depending on the current table location
86         /// Result can set the table to be left outer join or right outer join
87         /// </summary>
SetOuterJoin()88         public void SetOuterJoin()
89         {
90             // JoinExpression is non-null for associated table
91             if (JoinExpression != null)
92                 JoinType |= TableJoinType.LeftOuter;
93             else
94                 JoinType |= TableJoinType.RightOuter;
95         }
96 
97         /// <summary>
98         /// Ctor for associated table
99         /// </summary>
100         /// <param name="type">.NET type</param>
101         /// <param name="name">Table base name</param>
102         /// <param name="joinID"></param>
TableExpression(Type type, string name, string joinID)103         public TableExpression(Type type, string name, string joinID)
104             : base(ExpressionType, type)
105         {
106             Name = name;
107             JoinID = joinID;
108         }
109 
110         /// <summary>
111         /// Ctor for default table
112         /// </summary>
113         /// <param name="type">.NET type</param>
114         /// <param name="name">Table base name</param>
TableExpression(Type type, string name)115         public TableExpression(Type type, string name)
116             : this(type, name, null)
117         {
118         }
119 
TableExpression(ExpressionType expressionType, TableExpression tableExpression)120         protected TableExpression(ExpressionType expressionType, TableExpression tableExpression)
121             : base(expressionType, tableExpression.Type)
122         {
123             Name = tableExpression.Name;
124         }
125 
IsEqualTo(TableExpression expression)126         public virtual bool IsEqualTo(TableExpression expression)
127         {
128             return Name == expression.Name && JoinID == expression.JoinID;
129         }
130     }
131 }