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 class ScalarSelectBuilder : ISequenceBuilder
|
|
10 {
|
|
11 public int BuildCounter { get; set; }
|
|
12
|
|
13 public bool CanBuild(ExpressionBuilder builder, BuildInfo buildInfo)
|
|
14 {
|
|
15 return
|
|
16 buildInfo.Expression.NodeType == ExpressionType.Lambda &&
|
|
17 ((LambdaExpression)buildInfo.Expression).Parameters.Count == 0;
|
|
18 }
|
|
19
|
|
20 public IBuildContext BuildSequence(ExpressionBuilder builder, BuildInfo buildInfo)
|
|
21 {
|
|
22 return new ScalarSelectContext(builder)
|
|
23 {
|
|
24 Parent = buildInfo.Parent,
|
|
25 Expression = buildInfo.Expression,
|
|
26 SqlQuery = buildInfo.SqlQuery
|
|
27 };
|
|
28 }
|
|
29
|
|
30 public SequenceConvertInfo Convert(ExpressionBuilder builder, BuildInfo buildInfo, ParameterExpression param)
|
|
31 {
|
|
32 return null;
|
|
33 }
|
|
34
|
|
35 public bool IsSequence(ExpressionBuilder builder, BuildInfo buildInfo)
|
|
36 {
|
|
37 return true;
|
|
38 }
|
|
39
|
|
40 class ScalarSelectContext : IBuildContext
|
|
41 {
|
|
42 public ScalarSelectContext(ExpressionBuilder builder)
|
|
43 {
|
|
44 Builder = builder;
|
|
45
|
|
46 builder.Contexts.Add(this);
|
|
47 }
|
|
48
|
|
49 #if DEBUG
|
|
50 public string _sqlQueryText { get { return SqlQuery == null ? "" : SqlQuery.SqlText; } }
|
|
51 #endif
|
|
52
|
|
53 public ExpressionBuilder Builder { get; set; }
|
|
54 public Expression Expression { get; set; }
|
|
55 public SqlQuery SqlQuery { get; set; }
|
|
56 public IBuildContext Parent { get; set; }
|
|
57
|
|
58 public void BuildQuery<T>(Query<T> query, ParameterExpression queryParameter)
|
|
59 {
|
|
60 var expr = BuildExpression(null, 0);
|
|
61 var mapper = Builder.BuildMapper<T>(expr);
|
|
62
|
|
63 query.SetQuery(mapper.Compile());
|
|
64 }
|
|
65
|
|
66 public Expression BuildExpression(Expression expression, int level)
|
|
67 {
|
|
68 if (expression == null)
|
|
69 expression = ((LambdaExpression)Expression).Body.Unwrap();
|
|
70
|
|
71 switch (expression.NodeType)
|
|
72 {
|
|
73 case ExpressionType.New:
|
|
74 case ExpressionType.MemberInit:
|
|
75 {
|
|
76 var expr = Builder.BuildExpression(this, expression);
|
|
77
|
|
78 if (SqlQuery.Select.Columns.Count == 0)
|
|
79 SqlQuery.Select.Expr(new SqlValue(1));
|
|
80
|
|
81 return expr;
|
|
82 }
|
|
83
|
|
84 default :
|
|
85 {
|
|
86 var expr = Builder.ConvertToSql(this, expression, false);
|
|
87 var idx = SqlQuery.Select.Add(expr);
|
|
88
|
|
89 return Builder.BuildSql(expression.Type, idx);
|
|
90 }
|
|
91 }
|
|
92
|
|
93 }
|
|
94
|
|
95 public SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
|
|
96 {
|
|
97 throw new NotImplementedException();
|
|
98 }
|
|
99
|
|
100 public SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
|
|
101 {
|
|
102 throw new NotImplementedException();
|
|
103 }
|
|
104
|
|
105 public IsExpressionResult IsExpression(Expression expression, int level, RequestFor requestFlag)
|
|
106 {
|
|
107 switch (requestFlag)
|
|
108 {
|
|
109 case RequestFor.Expression : return IsExpressionResult.True;
|
|
110 default : return IsExpressionResult.False;
|
|
111 }
|
|
112 }
|
|
113
|
|
114 public IBuildContext GetContext(Expression expression, int level, BuildInfo buildInfo)
|
|
115 {
|
|
116 throw new NotImplementedException();
|
|
117 }
|
|
118
|
|
119 public int ConvertToParentIndex(int index, IBuildContext context)
|
|
120 {
|
|
121 throw new NotImplementedException();
|
|
122 }
|
|
123
|
|
124 public void SetAlias(string alias)
|
|
125 {
|
|
126 }
|
|
127
|
|
128 public ISqlExpression GetSubQuery(IBuildContext context)
|
|
129 {
|
|
130 return null;
|
|
131 }
|
|
132 }
|
|
133 }
|
|
134 }
|