comparison Source/Data/Linq/ExpressionQuery.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.Linq;
6 using System.Linq.Expressions;
7 using System.Reflection;
8
9 using JetBrains.Annotations;
10
11 namespace BLToolkit.Data.Linq
12 {
13 using Reflection;
14
15 public abstract class ExpressionQuery<T> : IOrderedQueryable<T>, IQueryProvider
16 {
17 #region Init
18
19 protected void Init(IDataContextInfo dataContextInfo, Expression expression)
20 {
21 #if SILVERLIGHT
22 if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
23
24 DataContextInfo = dataContextInfo;
25 #else
26 DataContextInfo = dataContextInfo ?? new DefaultDataContextInfo();
27 #endif
28 Expression = expression ?? Expression.Constant(this);
29 }
30
31 [NotNull] public Expression Expression { get; set; }
32 [NotNull] public IDataContextInfo DataContextInfo { get; set; }
33
34 internal Query<T> Info;
35 internal object[] Parameters;
36
37 #endregion
38
39 #region Public Members
40
41 [DebuggerBrowsable(DebuggerBrowsableState.Never)]
42 private string _sqlTextHolder;
43
44 // ReSharper disable InconsistentNaming
45 [UsedImplicitly]
46 private string _sqlText { get { return SqlText; }}
47 // ReSharper restore InconsistentNaming
48
49 public string SqlText
50 {
51 get
52 {
53 if (_sqlTextHolder == null)
54 {
55 var info = GetQuery(Expression, true);
56 _sqlTextHolder = info.GetSqlText(DataContextInfo.DataContext, Expression, Parameters, 0);
57 }
58
59 return _sqlTextHolder;
60 }
61 }
62
63 #endregion
64
65 #region Execute
66
67 IEnumerable<T> Execute(IDataContextInfo dataContextInfo, Expression expression)
68 {
69 return GetQuery(expression, true).GetIEnumerable(null, dataContextInfo, expression, Parameters);
70 }
71
72 Query<T> GetQuery(Expression expression, bool cache)
73 {
74 if (cache && Info != null)
75 return Info;
76
77 var info = Query<T>.GetQuery(DataContextInfo, expression);
78
79 if (cache)
80 Info = info;
81
82 return info;
83 }
84
85 #endregion
86
87 #region IQueryable Members
88
89 Type IQueryable.ElementType
90 {
91 get { return typeof(T); }
92 }
93
94 Expression IQueryable.Expression
95 {
96 get { return Expression; }
97 }
98
99 IQueryProvider IQueryable.Provider
100 {
101 get { return this; }
102 }
103
104 #endregion
105
106 #region IQueryProvider Members
107
108 IQueryable<TElement> IQueryProvider.CreateQuery<TElement>(Expression expression)
109 {
110 if (expression == null)
111 throw new ArgumentNullException("expression");
112
113 return new ExpressionQueryImpl<TElement>(DataContextInfo, expression);
114 }
115
116 IQueryable IQueryProvider.CreateQuery(Expression expression)
117 {
118 if (expression == null)
119 throw new ArgumentNullException("expression");
120
121 var elementType = TypeHelper.GetElementType(expression.Type) ?? expression.Type;
122
123 try
124 {
125 return (IQueryable)Activator.CreateInstance(typeof(ExpressionQueryImpl<>).MakeGenericType(elementType), new object[] { DataContextInfo, expression });
126 }
127 catch (TargetInvocationException ex)
128 {
129 throw ex.InnerException;
130 }
131 }
132
133 TResult IQueryProvider.Execute<TResult>(Expression expression)
134 {
135 return (TResult)GetQuery(expression, false).GetElement(null, DataContextInfo, expression, Parameters);
136 }
137
138 object IQueryProvider.Execute(Expression expression)
139 {
140 return GetQuery(expression, false).GetElement(null, DataContextInfo, expression, Parameters);
141 }
142
143 #endregion
144
145 #region IEnumerable Members
146
147 IEnumerator<T> IEnumerable<T>.GetEnumerator()
148 {
149 return Execute(DataContextInfo, Expression).GetEnumerator();
150 }
151
152 IEnumerator IEnumerable.GetEnumerator()
153 {
154 return Execute(DataContextInfo, Expression).GetEnumerator();
155 }
156
157 #endregion
158 }
159 }