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 IntersectBuilder : MethodCallBuilder
|
|
10 {
|
|
11 protected override bool CanBuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
|
|
12 {
|
|
13 return methodCall.Arguments.Count == 2 && methodCall.IsQueryable("Except", "Intersect");
|
|
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 var query = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[1], new SqlQuery()));
|
|
20 var except = query.SqlQuery;
|
|
21
|
|
22 sequence = new SubQueryContext(sequence);
|
|
23
|
|
24 var sql = sequence.SqlQuery;
|
|
25
|
|
26 except.ParentSql = sql;
|
|
27
|
|
28 if (methodCall.Method.Name == "Except")
|
|
29 sql.Where.Not.Exists(except);
|
|
30 else
|
|
31 sql.Where.Exists(except);
|
|
32
|
|
33 var keys1 = sequence.ConvertToSql(null, 0, ConvertFlags.Key);
|
|
34 var keys2 = query. ConvertToSql(null, 0, ConvertFlags.Key);
|
|
35
|
|
36 if (keys1.Length != keys2.Length)
|
|
37 throw new InvalidOperationException();
|
|
38
|
|
39 for (var i = 0; i < keys1.Length; i++)
|
|
40 {
|
|
41 except.Where
|
|
42 .Expr(keys1[i].Sql)
|
|
43 .Equal
|
|
44 .Expr(keys2[i].Sql);
|
|
45 }
|
|
46
|
|
47 return sequence;
|
|
48 }
|
|
49
|
|
50 protected override SequenceConvertInfo Convert(
|
|
51 ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo, ParameterExpression param)
|
|
52 {
|
|
53 return null;
|
|
54 }
|
|
55 }
|
|
56 }
|