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 DeleteBuilder : MethodCallBuilder
|
|
10 {
|
|
11 protected override bool CanBuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
|
|
12 {
|
|
13 return methodCall.IsQueryable("Delete");
|
|
14 }
|
|
15
|
|
16 protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
|
|
17 {
|
|
18 var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));
|
|
19
|
|
20 if (methodCall.Arguments.Count == 2)
|
|
21 sequence = builder.BuildWhere(buildInfo.Parent, sequence, (LambdaExpression)methodCall.Arguments[1].Unwrap(), false);
|
|
22
|
|
23 sequence.SqlQuery.QueryType = QueryType.Delete;
|
|
24
|
|
25 // Check association.
|
|
26 //
|
|
27 var ctx = sequence as SelectContext;
|
|
28
|
|
29 if (ctx != null && ctx.IsScalar)
|
|
30 {
|
|
31 var res = ctx.IsExpression(null, 0, RequestFor.Association);
|
|
32
|
|
33 if (res.Result && res.Context is TableBuilder.AssociatedTableContext)
|
|
34 {
|
|
35 var atc = (TableBuilder.AssociatedTableContext)res.Context;
|
|
36 sequence.SqlQuery.Delete.Table = atc.SqlTable;
|
|
37 }
|
|
38 else
|
|
39 {
|
|
40 res = ctx.IsExpression(null, 0, RequestFor.Table);
|
|
41
|
|
42 if (res.Result && res.Context is TableBuilder.TableContext)
|
|
43 {
|
|
44 var tc = (TableBuilder.TableContext)res.Context;
|
|
45
|
|
46 if (sequence.SqlQuery.From.Tables.Count == 0 || sequence.SqlQuery.From.Tables[0].Source != tc.SqlQuery)
|
|
47 sequence.SqlQuery.Delete.Table = tc.SqlTable;
|
|
48 }
|
|
49 }
|
|
50 }
|
|
51
|
|
52 return new DeleteContext(buildInfo.Parent, sequence);
|
|
53 }
|
|
54
|
|
55 protected override SequenceConvertInfo Convert(
|
|
56 ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo, ParameterExpression param)
|
|
57 {
|
|
58 return null;
|
|
59 }
|
|
60
|
|
61 class DeleteContext : SequenceContextBase
|
|
62 {
|
|
63 public DeleteContext(IBuildContext parent, IBuildContext sequence)
|
|
64 : base(parent, sequence, null)
|
|
65 {
|
|
66 }
|
|
67
|
|
68 public override void BuildQuery<T>(Query<T> query, ParameterExpression queryParameter)
|
|
69 {
|
|
70 query.SetNonQueryQuery();
|
|
71 }
|
|
72
|
|
73 public override Expression BuildExpression(Expression expression, int level)
|
|
74 {
|
|
75 throw new InvalidOperationException();
|
|
76 }
|
|
77
|
|
78 public override SqlInfo[] ConvertToSql(Expression expression, int level, ConvertFlags flags)
|
|
79 {
|
|
80 throw new InvalidOperationException();
|
|
81 }
|
|
82
|
|
83 public override SqlInfo[] ConvertToIndex(Expression expression, int level, ConvertFlags flags)
|
|
84 {
|
|
85 throw new InvalidOperationException();
|
|
86 }
|
|
87
|
|
88 public override IsExpressionResult IsExpression(Expression expression, int level, RequestFor requestFlag)
|
|
89 {
|
|
90 throw new InvalidOperationException();
|
|
91 }
|
|
92
|
|
93 public override IBuildContext GetContext(Expression expression, int level, BuildInfo buildInfo)
|
|
94 {
|
|
95 throw new InvalidOperationException();
|
|
96 }
|
|
97 }
|
|
98 }
|
|
99 }
|