Skip to content

Commit dbf664b

Browse files
authored
Merge pull request #30 from KSemenenko/main
Multi query
2 parents a09ecfb + 58d64e0 commit dbf664b

File tree

10 files changed

+274
-68
lines changed

10 files changed

+274
-68
lines changed

ManagedCode.Repository.AzureTable/AzureTableAdapter.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public async Task<int> ExecuteBatchAsync(IEnumerable<TableOperation> operations,
134134
}
135135

136136
public async IAsyncEnumerable<T> Query<T>(
137-
Expression whereExpression,
137+
Expression[] whereExpressions,
138138
Expression<Func<T, object>> orderByExpression = null,
139139
Order orderType = Order.By,
140140
Expression<Func<T, object>> thenByExpression = null,
@@ -147,11 +147,14 @@ public async IAsyncEnumerable<T> Query<T>(
147147
{
148148
string filterString = null;
149149

150-
if (whereExpression != null)
150+
var whereString = new List<string>();
151+
foreach (var expression in whereExpressions)
151152
{
152-
filterString = AzureTableQueryTranslator.TranslateExpression(whereExpression);
153+
whereString.Add(AzureTableQueryTranslator.TranslateExpression(expression));
153154
}
154155

156+
filterString = string.Join(" and ", whereString);
157+
155158
List<string> selectedProperties = null;
156159
if (selectExpression != null)
157160
{

ManagedCode.Repository.AzureTable/BaseAzureTableRepository.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected override async Task<int> DeleteAsyncInternal(Expression<Func<TItem, bo
210210
{
211211
token.ThrowIfCancellationRequested();
212212
var ids = await _tableAdapter
213-
.Query<DynamicTableEntity>(predicate, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
213+
.Query<DynamicTableEntity>(new[] {predicate}, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
214214
take: _tableAdapter.BatchSize)
215215
.ToListAsync();
216216

@@ -242,16 +242,20 @@ protected override Task<TItem> GetAsyncInternal(TId id, CancellationToken token
242242

243243
protected override async Task<TItem> GetAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
244244
{
245-
var item = await _tableAdapter.Query<TItem>(predicate, take: 1, cancellationToken: token).ToListAsync(token);
245+
var item = await _tableAdapter.Query<TItem>(new[] {predicate}, take: 1, cancellationToken: token).ToListAsync(token);
246246
return item.FirstOrDefault();
247247
}
248-
248+
249249
protected override IAsyncEnumerable<TItem> GetAllAsyncInternal(int? take = null, int skip = 0, CancellationToken token = default)
250250
{
251251
return _tableAdapter.Query<TItem>(null, take: take, skip: skip, cancellationToken: token);
252252
}
253253

254-
protected override IAsyncEnumerable<TItem> GetAllAsyncInternal(Expression<Func<TItem, object>> orderBy, Order orderType, int? take = null, int skip = 0, CancellationToken token = default)
254+
protected override IAsyncEnumerable<TItem> GetAllAsyncInternal(Expression<Func<TItem, object>> orderBy,
255+
Order orderType,
256+
int? take = null,
257+
int skip = 0,
258+
CancellationToken token = default)
255259
{
256260
return _tableAdapter.Query(null, orderBy, orderType, take: take, skip: skip, cancellationToken: token);
257261
}
@@ -260,25 +264,25 @@ protected override IAsyncEnumerable<TItem> GetAllAsyncInternal(Expression<Func<T
260264

261265
#region Find
262266

263-
protected override IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>> predicate,
267+
protected override IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>>[] predicates,
264268
int? take = null,
265269
int skip = 0,
266270
CancellationToken token = default)
267271
{
268-
return _tableAdapter.Query<TItem>(predicate, take: take, skip: skip, cancellationToken: token);
272+
return _tableAdapter.Query<TItem>(predicates, take: take, skip: skip, cancellationToken: token);
269273
}
270274

271-
protected override IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>> predicate,
275+
protected override IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>>[] predicates,
272276
Expression<Func<TItem, object>> orderBy,
273277
Order orderType,
274278
int? take = null,
275279
int skip = 0,
276280
CancellationToken token = default)
277281
{
278-
return _tableAdapter.Query(predicate, orderBy, orderType, take: take, skip: skip, cancellationToken: token);
282+
return _tableAdapter.Query(predicates, orderBy, orderType, take: take, skip: skip, cancellationToken: token);
279283
}
280284

281-
protected override IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>> predicate,
285+
protected override IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>>[] predicates,
282286
Expression<Func<TItem, object>> orderBy,
283287
Order orderType,
284288
Expression<Func<TItem, object>> thenBy,
@@ -287,7 +291,7 @@ protected override IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TIt
287291
int skip = 0,
288292
CancellationToken token = default)
289293
{
290-
return _tableAdapter.Query(predicate, orderBy, orderType, thenBy, thenType, take: take, skip: skip);
294+
return _tableAdapter.Query(predicates, orderBy, orderType, thenBy, thenType, take: take, skip: skip);
291295
}
292296

293297
#endregion
@@ -296,12 +300,12 @@ protected override IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TIt
296300

297301
protected override async Task<int> CountAsyncInternal(CancellationToken token = default)
298302
{
299-
int count = 0;
303+
var count = 0;
300304

301305
Expression<Func<TItem, bool>> predicate = item => true;
302306

303307
await foreach (var item in _tableAdapter
304-
.Query<DynamicTableEntity>(predicate, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
308+
.Query<DynamicTableEntity>(new[] {predicate}, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
305309
cancellationToken: token))
306310
{
307311
count++;
@@ -312,10 +316,10 @@ protected override async Task<int> CountAsyncInternal(CancellationToken token =
312316

313317
protected override async Task<int> CountAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default)
314318
{
315-
int count = 0;
319+
var count = 0;
316320

317321
await foreach (var item in _tableAdapter
318-
.Query<DynamicTableEntity>(predicate, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
322+
.Query<DynamicTableEntity>(new[] {predicate}, selectExpression: item => new DynamicTableEntity(item.PartitionKey, item.RowKey),
319323
cancellationToken: token))
320324
{
321325
count++;

ManagedCode.Repository.Core/BaseRepository.cs

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ public IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> order
172172
return GetAllAsyncInternal(orderBy, Order.By, take, skip, token);
173173
}
174174

175-
public IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> orderBy, Order orderType,
175+
public IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> orderBy,
176+
Order orderType,
176177
int? take = null,
177178
int skip = 0,
178179
CancellationToken token = default)
@@ -184,7 +185,7 @@ public IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> order
184185
protected abstract Task<TItem> GetAsyncInternal(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
185186

186187
protected abstract IAsyncEnumerable<TItem> GetAllAsyncInternal(int? take = null, int skip = 0, CancellationToken token = default);
187-
188+
188189
protected abstract IAsyncEnumerable<TItem> GetAllAsyncInternal(Expression<Func<TItem, object>> orderBy,
189190
Order orderType,
190191
int? take = null,
@@ -199,7 +200,7 @@ public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate
199200
{
200201
Contract.Requires(IsInitialized);
201202
Contract.Requires(predicate != null);
202-
return FindAsyncInternal(predicate, take, skip, token);
203+
return FindAsyncInternal(new[] {predicate}, take, skip, token);
203204
}
204205

205206
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
@@ -211,7 +212,7 @@ public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate
211212
Contract.Requires(IsInitialized);
212213
Contract.Requires(predicate != null);
213214
Contract.Requires(orderBy != null);
214-
return FindAsyncInternal(predicate, orderBy, Order.By, take, skip, token);
215+
return FindAsyncInternal(new[] {predicate}, orderBy, Order.By, take, skip, token);
215216
}
216217

217218
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
@@ -224,7 +225,7 @@ public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate
224225
Contract.Requires(IsInitialized);
225226
Contract.Requires(predicate != null);
226227
Contract.Requires(orderBy != null);
227-
return FindAsyncInternal(predicate, orderBy, orderType, take, skip, token);
228+
return FindAsyncInternal(new[] {predicate}, orderBy, orderType, take, skip, token);
228229
}
229230

230231
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
@@ -238,7 +239,7 @@ public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate
238239
Contract.Requires(predicate != null);
239240
Contract.Requires(orderBy != null);
240241
Contract.Requires(thenBy != null);
241-
return FindAsyncInternal(predicate, orderBy, Order.By, thenBy, Order.By, take, skip, token);
242+
return FindAsyncInternal(new[] {predicate}, orderBy, Order.By, thenBy, Order.By, take, skip, token);
242243
}
243244

244245
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
@@ -253,7 +254,7 @@ public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate
253254
Contract.Requires(predicate != null);
254255
Contract.Requires(orderBy != null);
255256
Contract.Requires(thenBy != null);
256-
return FindAsyncInternal(predicate, orderBy, orderType, thenBy, Order.By, take, skip, token);
257+
return FindAsyncInternal(new[] {predicate}, orderBy, orderType, thenBy, Order.By, take, skip, token);
257258
}
258259

259260
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
@@ -269,22 +270,99 @@ public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate
269270
Contract.Requires(predicate != null);
270271
Contract.Requires(orderBy != null);
271272
Contract.Requires(thenBy != null);
272-
return FindAsyncInternal(predicate, orderBy, orderType, thenBy, thenType, take, skip, token);
273+
return FindAsyncInternal(new[] {predicate}, orderBy, orderType, thenBy, thenType, take, skip, token);
274+
}
275+
276+
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates, int? take = null, int skip = 0, CancellationToken token = default)
277+
{
278+
Contract.Requires(IsInitialized);
279+
Contract.Requires(predicates != null);
280+
return FindAsyncInternal(predicates, take, skip, token);
281+
}
282+
283+
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
284+
Expression<Func<TItem, object>> orderBy,
285+
int? take = null,
286+
int skip = 0,
287+
CancellationToken token = default)
288+
{
289+
Contract.Requires(IsInitialized);
290+
Contract.Requires(predicates != null);
291+
Contract.Requires(orderBy != null);
292+
return FindAsyncInternal(predicates, orderBy, Order.By, take, skip, token);
293+
}
294+
295+
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
296+
Expression<Func<TItem, object>> orderBy,
297+
Order orderType,
298+
int? take = null,
299+
int skip = 0,
300+
CancellationToken token = default)
301+
{
302+
Contract.Requires(IsInitialized);
303+
Contract.Requires(predicates != null);
304+
Contract.Requires(orderBy != null);
305+
return FindAsyncInternal(predicates, orderBy, orderType, take, skip, token);
306+
}
307+
308+
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
309+
Expression<Func<TItem, object>> orderBy,
310+
Expression<Func<TItem, object>> thenBy,
311+
int? take = null,
312+
int skip = 0,
313+
CancellationToken token = default)
314+
{
315+
Contract.Requires(IsInitialized);
316+
Contract.Requires(predicates != null);
317+
Contract.Requires(orderBy != null);
318+
Contract.Requires(thenBy != null);
319+
return FindAsyncInternal(predicates, orderBy, Order.By, thenBy, Order.By, take, skip, token);
320+
}
321+
322+
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
323+
Expression<Func<TItem, object>> orderBy,
324+
Order orderType,
325+
Expression<Func<TItem, object>> thenBy,
326+
int? take = null,
327+
int skip = 0,
328+
CancellationToken token = default)
329+
{
330+
Contract.Requires(IsInitialized);
331+
Contract.Requires(predicates != null);
332+
Contract.Requires(orderBy != null);
333+
Contract.Requires(thenBy != null);
334+
return FindAsyncInternal(predicates, orderBy, orderType, thenBy, Order.By, take, skip, token);
335+
}
336+
337+
public IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
338+
Expression<Func<TItem, object>> orderBy,
339+
Order orderType,
340+
Expression<Func<TItem, object>> thenBy,
341+
Order thenType,
342+
int? take = null,
343+
int skip = 0,
344+
CancellationToken token = default)
345+
{
346+
Contract.Requires(IsInitialized);
347+
Contract.Requires(predicates != null);
348+
Contract.Requires(orderBy != null);
349+
Contract.Requires(thenBy != null);
350+
return FindAsyncInternal(predicates, orderBy, orderType, thenBy, thenType, take, skip, token);
273351
}
274352

275-
protected abstract IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>> predicate,
353+
protected abstract IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>>[] predicates,
276354
int? take = null,
277355
int skip = 0,
278356
CancellationToken token = default);
279357

280-
protected abstract IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>> predicate,
358+
protected abstract IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>>[] predicates,
281359
Expression<Func<TItem, object>> orderBy,
282360
Order orderType,
283361
int? take = null,
284362
int skip = 0,
285363
CancellationToken token = default);
286364

287-
protected abstract IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>> predicate,
365+
protected abstract IAsyncEnumerable<TItem> FindAsyncInternal(Expression<Func<TItem, bool>>[] predicates,
288366
Expression<Func<TItem, object>> orderBy,
289367
Order orderType,
290368
Expression<Func<TItem, object>> thenBy,

ManagedCode.Repository.Core/IRepository.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,45 +30,73 @@ public interface IRepository<in TId, TItem> where TItem : IItem<TId>
3030

3131
Task<TItem> GetAsync(TId id, CancellationToken token = default);
3232
Task<TItem> GetAsync(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
33-
33+
3434
IAsyncEnumerable<TItem> GetAllAsync(
3535
int? take = null,
3636
int skip = 0,
3737
CancellationToken token = default);
38-
38+
3939
IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> orderBy,
4040
int? take = null,
4141
int skip = 0,
4242
CancellationToken token = default);
43-
43+
4444
IAsyncEnumerable<TItem> GetAllAsync(Expression<Func<TItem, object>> orderBy,
4545
Order orderType,
4646
int? take = null,
4747
int skip = 0,
4848
CancellationToken token = default);
4949

50-
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate, int? take = null, int skip = 0, CancellationToken token = default);
50+
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
51+
int? take = null,
52+
int skip = 0,
53+
CancellationToken token = default);
54+
55+
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
56+
int? take = null,
57+
int skip = 0,
58+
CancellationToken token = default);
5159

5260
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
5361
Expression<Func<TItem, object>> orderBy,
5462
int? take = null,
5563
int skip = 0,
5664
CancellationToken token = default);
5765

66+
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
67+
Expression<Func<TItem, object>> orderBy,
68+
int? take = null,
69+
int skip = 0,
70+
CancellationToken token = default);
71+
5872
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
5973
Expression<Func<TItem, object>> orderBy,
6074
Order orderType,
6175
int? take = null,
6276
int skip = 0,
6377
CancellationToken token = default);
6478

79+
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
80+
Expression<Func<TItem, object>> orderBy,
81+
Order orderType,
82+
int? take = null,
83+
int skip = 0,
84+
CancellationToken token = default);
85+
6586
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
6687
Expression<Func<TItem, object>> orderBy,
6788
Expression<Func<TItem, object>> thenBy,
6889
int? take = null,
6990
int skip = 0,
7091
CancellationToken token = default);
7192

93+
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
94+
Expression<Func<TItem, object>> orderBy,
95+
Expression<Func<TItem, object>> thenBy,
96+
int? take = null,
97+
int skip = 0,
98+
CancellationToken token = default);
99+
72100
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
73101
Expression<Func<TItem, object>> orderBy,
74102
Order orderType,
@@ -86,6 +114,15 @@ IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>> predicate,
86114
int skip = 0,
87115
CancellationToken token = default);
88116

117+
IAsyncEnumerable<TItem> FindAsync(Expression<Func<TItem, bool>>[] predicates,
118+
Expression<Func<TItem, object>> orderBy,
119+
Order orderType,
120+
Expression<Func<TItem, object>> thenBy,
121+
Order thenType,
122+
int? take = null,
123+
int skip = 0,
124+
CancellationToken token = default);
125+
89126
Task<int> CountAsync(CancellationToken token = default);
90127
Task<int> CountAsync(Expression<Func<TItem, bool>> predicate, CancellationToken token = default);
91128
}

0 commit comments

Comments
 (0)