0
|
1 using System;
|
|
2 using System.Linq.Expressions;
|
|
3
|
|
4 namespace BLToolkit.Data.Linq.Builder
|
|
5 {
|
|
6 using BLToolkit.Linq;
|
|
7 using Data.Sql;
|
|
8
|
|
9 public class ExpressionContext : SequenceContextBase
|
|
10 {
|
|
11 public ExpressionContext(IBuildContext parent, IBuildContext sequence, LambdaExpression lambda)
|
|
12 : base(parent, sequence, lambda)
|
|
13 {
|
|
14 }
|
|
15
|
|
16 public ExpressionContext(IBuildContext parent, IBuildContext sequence, LambdaExpression lambda, SqlQuery sqlQuery)
|
|
17 : base(parent, sequence, lambda)
|
|
18 {
|
|
19 SqlQuery = sqlQuery;
|
|
20 }
|
|
21
|
|
22 public override Expression BuildExpression(Expression expression, int level)
|
|
23 {
|
|
24 throw new InvalidOperationException();
|
|
25 }
|
|
26
|
|
27 public override SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
|
|
28 {
|
|
29 if (level == 0)
|
|
30 {
|
|
31 switch (flags)
|
|
32 {
|
|
33 case ConvertFlags.Field :
|
|
34 case ConvertFlags.Key :
|
|
35 case ConvertFlags.All :
|
|
36 {
|
|
37 var root = expression.GetRootObject();
|
|
38
|
|
39 if (root.NodeType == ExpressionType.Parameter)
|
|
40 {
|
|
41 var ctx = Builder.GetContext(this, root);
|
|
42
|
|
43 if (ctx != null)
|
|
44 {
|
|
45 if (ctx != this)
|
|
46 return ctx.ConvertToSql(expression, 0, flags);
|
|
47
|
|
48 return root == expression ?
|
|
49 Sequence.ConvertToSql(null, 0, flags) :
|
|
50 Sequence.ConvertToSql(expression, level + 1, flags);
|
|
51 }
|
|
52 }
|
|
53
|
|
54 break;
|
|
55 }
|
|
56 }
|
|
57
|
|
58 throw new InvalidOperationException();
|
|
59 }
|
|
60
|
|
61 throw new InvalidOperationException();
|
|
62 }
|
|
63
|
|
64 public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
|
|
65 {
|
|
66 throw new InvalidOperationException();
|
|
67 }
|
|
68
|
|
69 public override IsExpressionResult IsExpression(Expression expression, int level, RequestFor requestFlag)
|
|
70 {
|
|
71 switch (requestFlag)
|
|
72 {
|
|
73 case RequestFor.Root : return new IsExpressionResult(Lambda.Parameters.Count > 0 && expression == Lambda.Parameters[0]);
|
|
74
|
|
75 case RequestFor.Table :
|
|
76 case RequestFor.Association :
|
|
77 case RequestFor.Object :
|
|
78 case RequestFor.GroupJoin :
|
|
79 case RequestFor.Field :
|
|
80 case RequestFor.Expression :
|
|
81 {
|
|
82 var levelExpression = expression.GetLevelExpression(level);
|
|
83
|
|
84 return levelExpression == expression ?
|
|
85 Sequence.IsExpression(null, 0, requestFlag) :
|
|
86 Sequence.IsExpression(expression, level + 1, requestFlag);
|
|
87 }
|
|
88 }
|
|
89
|
|
90 return IsExpressionResult.False;
|
|
91 }
|
|
92
|
|
93 public override IBuildContext GetContext(Expression expression, int level, BuildInfo buildInfo)
|
|
94 {
|
|
95 if (expression == Lambda.Parameters[0])
|
|
96 return Sequence.GetContext(null, 0, buildInfo);
|
|
97
|
|
98 switch (expression.NodeType)
|
|
99 {
|
|
100 case ExpressionType.Constant :
|
|
101 case ExpressionType.New :
|
|
102 case ExpressionType.MemberInit : return null;
|
|
103 }
|
|
104
|
|
105 return Sequence.GetContext(expression, level + 1, buildInfo);
|
|
106 }
|
|
107 }
|
|
108 }
|