0
|
1 using System;
|
|
2 using System.Data;
|
|
3 using System.Linq.Expressions;
|
|
4
|
|
5 namespace BLToolkit.Data.Linq.Builder
|
|
6 {
|
|
7 using Data.Sql;
|
|
8
|
|
9 public abstract class SequenceContextBase : IBuildContext
|
|
10 {
|
|
11 protected SequenceContextBase(IBuildContext parent, IBuildContext sequence, LambdaExpression lambda)
|
|
12 {
|
|
13 Parent = parent;
|
|
14 Sequence = sequence;
|
|
15 Builder = sequence.Builder;
|
|
16 Lambda = lambda;
|
|
17 SqlQuery = sequence.SqlQuery;
|
|
18
|
|
19 Sequence.Parent = this;
|
|
20
|
|
21 Builder.Contexts.Add(this);
|
|
22 }
|
|
23
|
|
24 #if DEBUG
|
|
25 [CLSCompliant(false)]
|
|
26 public string _sqlQueryText { get { return SqlQuery == null ? "" : SqlQuery.SqlText; } }
|
|
27 #endif
|
|
28
|
|
29 public IBuildContext Parent { get; set; }
|
|
30 public IBuildContext Sequence { get; set; }
|
|
31 public ExpressionBuilder Builder { get; set; }
|
|
32 public LambdaExpression Lambda { get; set; }
|
|
33 public SqlQuery SqlQuery { get; set; }
|
|
34
|
|
35 Expression IBuildContext.Expression { get { return Lambda; } }
|
|
36
|
|
37 public virtual void BuildQuery<T>(Query<T> query, ParameterExpression queryParameter)
|
|
38 {
|
|
39 var expr = BuildExpression(null, 0);
|
|
40 var mapper = Builder.BuildMapper<T>(expr);
|
|
41
|
|
42 query.SetQuery(mapper.Compile());
|
|
43 }
|
|
44
|
|
45 public abstract Expression BuildExpression(Expression expression, int level);
|
|
46 public abstract SqlInfo[] ConvertToSql (Expression expression, int level, ConvertFlags flags);
|
|
47 public abstract SqlInfo[] ConvertToIndex (Expression expression, int level, ConvertFlags flags);
|
|
48 public abstract IsExpressionResult IsExpression (Expression expression, int level, RequestFor requestFlag);
|
|
49 public abstract IBuildContext GetContext (Expression expression, int level, BuildInfo buildInfo);
|
|
50
|
|
51 public virtual int ConvertToParentIndex(int index, IBuildContext context)
|
|
52 {
|
|
53 return Parent == null ? index : Parent.ConvertToParentIndex(index, context);
|
|
54 }
|
|
55
|
|
56 public virtual void SetAlias(string alias)
|
|
57 {
|
|
58 }
|
|
59
|
|
60 public virtual ISqlExpression GetSubQuery(IBuildContext context)
|
|
61 {
|
|
62 return null;
|
|
63 }
|
|
64
|
|
65 protected bool IsSubQuery()
|
|
66 {
|
|
67 for (var p = Parent; p != null; p = p.Parent)
|
|
68 if (p.IsExpression(null, 0, RequestFor.SubQuery).Result)
|
|
69 return true;
|
|
70 return false;
|
|
71 }
|
|
72 }
|
|
73 }
|