generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDatabaseQuery.cs
More file actions
219 lines (191 loc) · 12 KB
/
DatabaseQuery.cs
File metadata and controls
219 lines (191 loc) · 12 KB
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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Entities;
using CoreEx.Results;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace CoreEx.Database.Extended
{
/// <summary>
/// Encapsulates a SQL query enabling select-like capabilities.
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
public class DatabaseQuery<T> : IDatabaseParameters<DatabaseQuery<T>> where T : class, new()
{
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseQuery{T}"/> class.
/// </summary>
/// <param name="command">The <see cref="DatabaseCommand"/>.</param>
/// <param name="args">The <see cref="DatabaseArgs"/>.</param>
/// <param name="queryParams">The query <see cref="DatabaseParameterCollection"/> action to enable additional filtering.</param>
internal DatabaseQuery(DatabaseCommand command, DatabaseArgs args, Action<DatabaseParameterCollection>? queryParams)
{
Command = command.ThrowIfNull(nameof(command));
Parameters = new DatabaseParameterCollection(Database);
QueryArgs = args;
Mapper = (IDatabaseMapper<T>)args.Mapper;
queryParams?.Invoke(Parameters);
}
/// <summary>
/// Gets the <see cref="DatabaseCommand"/>.
/// </summary>
public DatabaseCommand Command { get; }
/// <inheritdoc/>
public IDatabase Database => Command.Database;
/// <inheritdoc/>
public DatabaseParameterCollection Parameters { get; }
/// <summary>
/// Gets the <see cref="DatabaseArgs"/>.
/// </summary>
public DatabaseArgs QueryArgs { get; }
/// <summary>
/// Gets the <see cref="IDatabaseMapper{TSource}"/>.
/// </summary>
public IDatabaseMapper<T> Mapper { get; }
/// <summary>
/// Gets the <see cref="PagingResult"/>.
/// </summary>
public PagingResult? Paging { get; private set; }
/// <summary>
/// Adds <see cref="PagingArgs"/> to the query.
/// </summary>
/// <param name="paging">The <see cref="PagingArgs"/>.</param>
/// <returns>The <see cref="DatabaseQuery{T}"/> to suport fluent-style method-chaining.</returns>
public DatabaseQuery<T> WithPaging(PagingArgs? paging)
{
Paging = paging == null ? null : (paging is PagingResult pr ? pr : new PagingResult(paging));
return this;
}
/// <summary>
/// Adds <see cref="PagingArgs"/> to the query.
/// </summary>
/// <param name="skip">The specified number of elements in a sequence to bypass.</param>
/// <param name="take">The specified number of contiguous elements from the start of a sequence.</param>
/// <returns>The <see cref="DatabaseQuery{T}"/> to suport fluent-style method-chaining.</returns>
public DatabaseQuery<T> WithPaging(long skip, long? take = null) => WithPaging(PagingArgs.CreateSkipAndTake(skip, take));
/// <summary>
/// Selects a single item.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item.</returns>
public async Task<T> SelectSingleAsync(CancellationToken cancellationToken = default) => await SelectSingleWithResultAsync(cancellationToken).ConfigureAwait(false);
/// <summary>
/// Selects a single item with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item.</returns>
public Task<Result<T>> SelectSingleWithResultAsync(CancellationToken cancellationToken = default) => SelectWrapperWithResultAsync((cmd, ct) => cmd.SelectSingleWithResultAsync(Mapper, ct), cancellationToken);
/// <summary>
/// Selects a single item or default.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
public async Task<T?> SelectSingleOrDefaultAsync(CancellationToken cancellationToken = default) => await SelectSingleOrDefaultWithResultAsync(cancellationToken).ConfigureAwait(false);
/// <summary>
/// Selects a single item or default with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
public Task<Result<T?>> SelectSingleOrDefaultWithResultAsync(CancellationToken cancellationToken = default) => SelectWrapperWithResultAsync((cmd, ct) => cmd.SelectSingleOrDefaultWithResultAsync(Mapper, ct), cancellationToken);
/// <summary>
/// Selects first item.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The first item.</returns>
public async Task<T> SelectFirstAsync(CancellationToken cancellationToken = default) => await SelectFirstWithResultAsync(cancellationToken).ConfigureAwait(false);
/// <summary>
/// Selects first item with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The first item.</returns>
public Task<Result<T>> SelectFirstWithResultAsync(CancellationToken cancellationToken = default) => SelectWrapperWithResultAsync((cmd, ct) => cmd.SelectFirstWithResultAsync(Mapper, ct), cancellationToken);
/// <summary>
/// Selects first item or default.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
public async Task<T?> SelectFirstOrDefaultAsync(CancellationToken cancellationToken = default) => await SelectFirstOrDefaultWithResultAsync(cancellationToken).ConfigureAwait(false);
/// <summary>
/// Selects first item or default with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
public Task<Result<T?>> SelectFirstOrDefaultWithResultAsync(CancellationToken cancellationToken = default) => SelectWrapperWithResultAsync((cmd, ct) => cmd.SelectFirstOrDefaultWithResultAsync(Mapper, ct), cancellationToken);
/// <summary>
/// Executes the query command creating a <typeparamref name="TCollResult"/>.
/// </summary>
/// <typeparam name="TCollResult">The <see cref="ICollectionResult{TColl, TItem}"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TColl">The <see cref="ICollection{T}"/> <see cref="Type"/>.</typeparam>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The resulting <typeparamref name="TCollResult"/>.</returns>
public async Task<TCollResult> SelectResultAsync<TCollResult, TColl>(CancellationToken cancellationToken = default) where TCollResult : ICollectionResult<TColl, T>, new() where TColl : ICollection<T>, new()
=> (await SelectResultWithResultAsync<TCollResult, TColl>(cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Executes the query command creating a <typeparamref name="TCollResult"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="TCollResult">The <see cref="ICollectionResult{TColl, TItem}"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TColl">The <see cref="ICollection{T}"/> <see cref="Type"/>.</typeparam>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The resulting <typeparamref name="TCollResult"/>.</returns>
public async Task<Result<TCollResult>> SelectResultWithResultAsync<TCollResult, TColl>(CancellationToken cancellationToken = default) where TCollResult : ICollectionResult<TColl, T>, new() where TColl : ICollection<T>, new()
{
var result = await SelectQueryWithResultAsync<TColl>(cancellationToken).ConfigureAwait(false);
return result.ThenAs(items => new TCollResult { Items = items, Paging = Paging });
}
/// <summary>
/// Executes the query command creating a resultant collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A resultant collection.</returns>
public async Task<TColl> SelectQueryAsync<TColl>(CancellationToken cancellationToken = default) where TColl : ICollection<T>, new()
=> await SelectQueryWithResultAsync<TColl>(cancellationToken).ConfigureAwait(false);
/// <summary>
/// Executes the query command creating a resultant collection with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A resultant collection.</returns>
public async Task<Result<TColl>> SelectQueryWithResultAsync<TColl>(CancellationToken cancellationToken = default) where TColl : ICollection<T>, new()
{
var coll = new TColl();
var result = await SelectQueryWithResultAsync(coll, cancellationToken).ConfigureAwait(false);
return result.ThenAs(() => coll);
}
/// <summary>
/// Executes a query adding to the passed collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="coll">The collection to add items to.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public async Task SelectQueryAsync<TColl>(TColl coll, CancellationToken cancellationToken = default) where TColl : ICollection<T>
=> (await SelectQueryWithResultAsync(coll, cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Executes a query adding to the passed collection with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="coll">The collection to add items to.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public async Task<Result> SelectQueryWithResultAsync<TColl>(TColl coll, CancellationToken cancellationToken = default) where TColl : ICollection<T>
{
var result = await SelectWrapperWithResultAsync(async (cmd, ct) =>
{
var result = await cmd.SelectQueryWithResultAsync(coll, Mapper, ct).ConfigureAwait(false);
return result.ThenAs(() => coll);
}, cancellationToken).ConfigureAwait(false);
return result.Bind();
}
/// <summary>
/// Wraps the select query to perform standard logic.
/// </summary>
private async Task<Result<TResult>> SelectWrapperWithResultAsync<TResult>(Func<DatabaseCommand, CancellationToken, Task<Result<TResult>>> func, CancellationToken cancellationToken)
{
var rvp = Paging != null && Paging.IsGetCount ? Parameters.AddReturnValueParameter() : null;
var cmd = Command.Params(Parameters).PagingParams(Paging);
return await Result.GoAsync(func(cmd, cancellationToken))
.When(_ => rvp != null && rvp.Value != null, _ => { Paging!.TotalCount = (long)rvp!.Value!; })
.Then(res => QueryArgs.CleanUpResult ? Cleaner.Clean(res) : res);
}
}
}