This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
MobileServiceTableQuery.cs
348 lines (319 loc) · 14.4 KB
/
MobileServiceTableQuery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices.Query;
namespace Microsoft.WindowsAzure.MobileServices.Query
{
/// <summary>
/// Represents a query that can be evaluated against a Mobile Services
/// table. MobileServiceTableQuery instances can be obtained via
/// MobileServiceClient.Query(of T)().
/// </summary>
/// <remarks>
/// Rather than implenting IQueryable directly, we've implemented the
/// portion of the LINQ query pattern we support on MobileServiceTableQuery
/// objects. MobileServiceTableQuery instances are used to build up
/// IQueryables from LINQ query operations.
/// </remarks>
internal class MobileServiceTableQuery<T> : IMobileServiceTableQuery<T>
{
/// <summary>
/// Initializes a new instance of the MobileServiceTableQuery class.
/// </summary>
/// <param name="table">
/// The table being queried.
/// </param>
/// <param name="queryProvider">
/// The <see cref="MobileServiceTableQueryProvider"/> associated with this
/// <see cref="T:MobileServiceTableQuery`1{T}"/>
/// </param>
/// <param name="query">
/// The encapsulated <see cref="IQueryable"/>.
/// </param>
/// <param name="parameters">
/// The optional user-defined query string parameters to include with the query.
/// </param>
/// <param name="includeTotalCount">
/// A value that if set will determine whether the query will request
/// the total count for all the records that would have been returned
/// ignoring any take paging/limit clause specified by client or
/// server.
/// </param>
internal MobileServiceTableQuery(IMobileServiceTable<T> table,
MobileServiceTableQueryProvider queryProvider,
IQueryable<T> query,
IDictionary<string, string> parameters,
bool includeTotalCount)
{
if (table == null)
{
throw new ArgumentNullException("table");
}
this.Table = table;
this.RequestTotalCount = includeTotalCount;
this.Parameters = parameters;
this.Query = query;
this.QueryProvider = queryProvider;
}
/// <summary>
/// Gets the MobileServiceTable being queried.
/// </summary>
public IMobileServiceTable<T> Table { get; private set; }
/// <summary>
/// Gets a value indicating whether the query will request the total
/// count for all the records that would have been returned ignoring
/// any take paging/limit clause specified by client or server.
/// </summary>
public bool RequestTotalCount { get; private set; }
/// <summary>
/// The user-defined query string parameters to include with the query.
/// </summary>
public IDictionary<string, string> Parameters { get; private set; }
/// <summary>
/// Gets the underlying IQueryable associated with this query.
/// </summary>
public IQueryable<T> Query { get; set; }
/// <summary>
/// Gets the associated Query Provider capable of executing a <see cref="T:MobileServiceTableQuery`1{T}"/>.
/// </summary>
public MobileServiceTableQueryProvider QueryProvider { get; set; }
/// <summary>
/// Applies the specified filter predicate to the source query.
/// </summary>
/// <param name="predicate">
/// The filter predicate.
/// </param>
/// <returns>
/// The composed query.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Part of the LINQ query pattern.")]
public IMobileServiceTableQuery<T> Where(Expression<Func<T, bool>> predicate)
{
if (predicate == null)
{
throw new ArgumentNullException("predicate");
}
return this.QueryProvider.Create<T>(this.Table, Queryable.Where(this.Query, predicate), this.Parameters, this.RequestTotalCount);
}
/// <summary>
/// Applies the specified selection to the source query.
/// </summary>
/// <typeparam name="U">
/// Type representing the projected result of the query.
/// </typeparam>
/// <param name="selector">
/// The selector function.
/// </param>
/// <returns>
/// The composed query.
/// </returns>
[SuppressMessage("Microsoft.Naming", "CA1715:IdentifiersShouldHaveCorrectPrefix", MessageId = "T", Justification = "Part of the LINQ query pattern.")]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "U", Justification = "Standard for LINQ")]
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Part of the LINQ query pattern.")]
public IMobileServiceTableQuery<U> Select<U>(Expression<Func<T, U>> selector)
{
if (selector == null)
{
throw new ArgumentNullException("selector");
}
// Create a new table with the same name/client but
// pretending to be of a different type since the query needs to
// have the same type as the table. This won't cause any issues
// since we're only going to use it to evaluate the query and it'll
// never leak to users.
MobileServiceTable<U> table = new MobileServiceTable<U>(
this.Table.TableName,
this.Table.MobileServiceClient);
return this.QueryProvider.Create(table,
Queryable.Select(this.Query, selector),
MobileServiceTable.AddSystemProperties(Table.SystemProperties, this.Parameters),
this.RequestTotalCount);
}
/// <summary>
/// Applies the specified ascending order clause to the source query.
/// </summary>
/// <typeparam name="TKey">
/// The type of the member being ordered by.
/// </typeparam>
/// <param name="keySelector">
/// The expression selecting the member to order by.
/// </param>
/// <returns>
/// The composed query.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Part of the LINQ query pattern.")]
public IMobileServiceTableQuery<T> OrderBy<TKey>(Expression<Func<T, TKey>> keySelector)
{
if (keySelector == null)
{
throw new ArgumentNullException("keySelector");
}
return this.QueryProvider.Create(this.Table, Queryable.OrderBy(this.Query, keySelector), this.Parameters, this.RequestTotalCount);
}
/// <summary>
/// Applies the specified descending order clause to the source query.
/// </summary>
/// <typeparam name="TKey">
/// The type of the member being ordered by.
/// </typeparam>
/// <param name="keySelector">
/// The expression selecting the member to order by.
/// </param>
/// <returns>
/// The composed query.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Part of the LINQ query pattern.")]
public IMobileServiceTableQuery<T> OrderByDescending<TKey>(Expression<Func<T, TKey>> keySelector)
{
if (keySelector == null)
{
throw new ArgumentNullException("keySelector");
}
return this.QueryProvider.Create(this.Table, Queryable.OrderByDescending(this.Query, keySelector), this.Parameters, this.RequestTotalCount);
}
/// <summary>
/// Applies the specified ascending order clause to the source query.
/// </summary>
/// <typeparam name="TKey">
/// The type of the member being ordered by.
/// </typeparam>
/// <param name="keySelector">
/// The expression selecting the member to order by.
/// </param>
/// <returns>
/// The composed query.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Part of the LINQ query pattern.")]
public IMobileServiceTableQuery<T> ThenBy<TKey>(Expression<Func<T, TKey>> keySelector)
{
if (keySelector == null)
{
throw new ArgumentNullException("keySelector");
}
return this.QueryProvider.Create(this.Table, Queryable.ThenBy((IOrderedQueryable<T>)this.Query, keySelector), this.Parameters, this.RequestTotalCount);
}
/// <summary>
/// Applies the specified descending order clause to the source query.
/// </summary>
/// <typeparam name="TKey">
/// The type of the member being ordered by.
/// </typeparam>
/// <param name="keySelector">
/// The expression selecting the member to order by.
/// </param>
/// <returns>
/// The composed query.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Part of the LINQ query pattern.")]
public IMobileServiceTableQuery<T> ThenByDescending<TKey>(Expression<Func<T, TKey>> keySelector)
{
if (keySelector == null)
{
throw new ArgumentNullException("keySelector");
}
return this.QueryProvider.Create(this.Table, Queryable.ThenByDescending((IOrderedQueryable<T>)this.Query, keySelector), this.Parameters, this.RequestTotalCount);
}
/// <summary>
/// Applies the specified skip clause to the source query.
/// </summary>
/// <param name="count">
/// The number to skip.
/// </param>
/// <returns>
/// The composed query.
/// </returns>
public IMobileServiceTableQuery<T> Skip(int count)
{
return this.QueryProvider.Create(this.Table, Queryable.Skip(this.Query, count), this.Parameters, this.RequestTotalCount);
}
/// <summary>
/// Applies the specified take clause to the source query.
/// </summary>
/// <param name="count">
/// The number to take.
/// </param>
/// <returns>
/// The composed query.
/// </returns>
public IMobileServiceTableQuery<T> Take(int count)
{
return this.QueryProvider.Create(this.Table, Queryable.Take(this.Query, count), this.Parameters, this.RequestTotalCount);
}
/// <summary>
/// Applies to the source query the specified string key-value
/// pairs to be used as user-defined parameters with the request URI
/// query string.
/// </summary>
/// <param name="parameters">
/// The parameters to apply.
/// </param>
/// <returns>
/// The composed query.
/// </returns>
public IMobileServiceTableQuery<T> WithParameters(IDictionary<string, string> parameters)
{
if (parameters != null)
{
// Make sure to replace any existing value for the key
foreach (KeyValuePair<string, string> pair in parameters)
{
this.Parameters[pair.Key] = pair.Value;
}
}
return this.QueryProvider.Create(this.Table, this.Query, this.Parameters, this.RequestTotalCount);
}
/// <summary>
/// Ensure the query will get the total count for all the records that
/// would have been returned ignoring any take paging/limit clause
/// specified by client or server.
/// </summary>
/// <returns>
/// The query object.
/// </returns>
public IMobileServiceTableQuery<T> IncludeTotalCount()
{
return this.QueryProvider.Create(this.Table, this.Query, this.Parameters, includeTotalCount: true);
}
/// <summary>
/// Ensure the query will get the deleted records. This requires the soft delete feature to be enabled on the Mobile Service. Visit <see href="http://go.microsoft.com/fwlink/?LinkId=507647">the link</see> for details.
/// </summary>
/// <returns>
/// The query object.
/// </returns>
public IMobileServiceTableQuery<T> IncludeDeleted()
{
return this.QueryProvider.Create(this.Table, this.Query, MobileServiceTable.IncludeDeleted(this.Parameters), includeTotalCount: true);
}
/// <summary>
/// Evalute the query asynchronously and return the results.
/// </summary>
/// <returns>
/// The evaluated query results.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Not nested when used via async pattern.")]
public Task<IEnumerable<T>> ToEnumerableAsync()
{
return this.QueryProvider.Execute(this);
}
/// <summary>
/// Evalute the query asynchronously and return the results in a new
/// List.
/// </summary>
/// <returns>
/// The evaluated query results as a List.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "Not nested when used via async pattern.")]
public async Task<List<T>> ToListAsync()
{
IEnumerable<T> items = await this.QueryProvider.Execute(this);
return new QueryResultList<T>(items);
}
}
}