-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathBulkExtensions.cs
181 lines (167 loc) · 8.98 KB
/
BulkExtensions.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
// Copyright (C) 2019-2020 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
using System.Diagnostics.Contracts;
using System.Linq.Expressions;
namespace Xtensive.Orm.BulkOperations
{
/// <summary>
/// Extension methods for bulk operation.
/// </summary>
public static class BulkExtensions
{
/// <summary>
/// Executes bulk delete of entities specified by the query.
/// </summary>
/// <typeparam name="T">Type of the entity.</typeparam>
/// <param name="query">The query.</param>
/// <returns>Number of the deleted entities.</returns>
public static int Delete<T>(this IQueryable<T> query) where T : class, IEntity =>
new BulkDeleteOperation<T>(query).Execute();
/// <summary>
/// Asynchronously executes bulk delete of entities specified by the query.
/// </summary>
/// <remarks>Multiple active operations in the same session instance are not supported. Use
/// <see langword="await"/> to ensure that all asynchronous operations have completed before calling
/// another method in this session.</remarks>
/// <typeparam name="T">Type of the entity.</typeparam>
/// <param name="query">The query.</param>
/// <param name="token">The cancellation token to terminate execution if needed.</param>
/// <returns>Number of the deleted entities.</returns>
public static Task<int> DeleteAsync<T>(this IQueryable<T> query, CancellationToken token = default)
where T : class, IEntity =>
new BulkDeleteOperation<T>(query).ExecuteAsync(token);
/// <summary>
/// Executes bulk update of entities specified by the query.
/// </summary>
/// <typeparam name="T">The type of the entity.</typeparam>
/// <typeparam name="TResult">The type of the field.</typeparam>
/// <param name="query">The query.</param>
/// <param name="field">The expression, that specify the field.</param>
/// <param name="update">The expression, that specify new value of the field.</param>
/// <returns>Instance of <see cref=" IUpdatable<T>"/>.</returns>
[Pure]
public static IUpdatable<T> Set<T, TResult>(this IQueryable<T> query, Expression<Func<T, TResult>> field,
Expression<Func<T, TResult>> update) where T: IEntity =>
new Updatable<T>(query, field, update);
/// <summary>
/// Executes bulk update of entities specified by the query.
/// </summary>
/// <typeparam name="T">The type of the entity.</typeparam>
/// <typeparam name="TResult">The type of the field.</typeparam>
/// <param name="query"><see cref="IUpdatable{T}"/>, that describes UPDATE operation.</param>
/// <param name="field">The expression, that specify the field.</param>
/// <param name="update">The expression, that specify new value of the field.</param>
/// <returns>Instance of <see cref=" IUpdatable<T>"/>.</returns>
[Pure]
public static IUpdatable<T> Set<T, TResult>(this IUpdatable<T> query, Expression<Func<T, TResult>> field,
Expression<Func<T, TResult>> update) where T: IEntity =>
new Updatable<T>((Updatable<T>) query, field, update);
/// <summary>
/// Executes bulk update of entities specified by the query.
/// </summary>
/// <typeparam name="T">The type of the entity.</typeparam>
/// <typeparam name="TResult">The type of the field.</typeparam>
/// <param name="query">The query.</param>
/// <param name="field">The expression, that specify the field.</param>
/// <param name="value">New value of the field.</param>
/// <returns>Instance of <see cref=" IUpdatable<T>"/>.</returns>
[Pure]
public static IUpdatable<T> Set<T, TResult>(this IQueryable<T> query, Expression<Func<T, TResult>> field,
TResult value) where T: IEntity
{
// Manually constructed expression is simpler than `a => value`
var valueFunc = Expression.Lambda<Func<T, TResult>>(Expression.Constant(value, typeof(TResult)),
Expression.Parameter(typeof(T), "a"));
return Set(query, field, valueFunc);
}
/// <summary>
/// Executes bulk update of entities specified by the query.
/// </summary>
/// <typeparam name="T">The type of the entity.</typeparam>
/// <typeparam name="TResult">The type of the field.</typeparam>
/// <param name="query"><see cref="IUpdatable{T}"/>, that describes UPDATE operation.</param>
/// <param name="field">The expression, that specify the field.</param>
/// <param name="value">New value of the field.</param>
/// <returns>Instance of <see cref=" IUpdatable<T>"/>.</returns>
[Pure]
public static IUpdatable<T> Set<T, TResult>(this IUpdatable<T> query, Expression<Func<T, TResult>> field,
TResult value) where T: IEntity =>
Set(query, field, a => value);
/// <summary>
/// Executes the UPDATE operation.
/// </summary>
/// <typeparam name="T">Type of the entity.</typeparam>
/// <param name="query">The query.</param>
/// <param name="evaluator">The expression, that specify new values. Constructor parameters are ignored.</param>
/// <returns>Number of updated entities.</returns>
public static int Update<T>(this IQueryable<T> query, Expression<Func<T, T>> evaluator) where T : class, IEntity =>
new BulkUpdateOperation<T>(query, evaluator).Execute();
/// <summary>
/// Asynchronously executes the UPDATE operation.
/// </summary>
/// <remarks>Multiple active operations in the same session instance are not supported. Use
/// <see langword="await"/> to ensure that all asynchronous operations have completed before calling
/// another method in this session.</remarks>
/// <typeparam name="T">Type of the entity.</typeparam>
/// <param name="query">The query.</param>
/// <param name="evaluator">The expression, that specify new values. Constructor parameters are ignored.</param>
/// <param name="token">The cancellation token to terminate execution if necessary.</param>
/// <returns>Number of updated entities.</returns>
public static Task<int> UpdateAsync<T>(this IQueryable<T> query, Expression<Func<T, T>> evaluator,
CancellationToken token = default) where T : class, IEntity =>
new BulkUpdateOperation<T>(query, evaluator).ExecuteAsync(token);
/// <summary>
/// Executes the UPDATE operation.
/// </summary>
/// <typeparam name="T">Type of the entity.</typeparam>
/// <param name="query">The query.</param>
/// <returns>Number of updated entities.</returns>
public static int Update<T>(this IUpdatable<T> query) where T : class, IEntity =>
new BulkUpdateOperation<T>(query).Execute();
/// <summary>
/// Asynchronously executes the UPDATE operation.
/// </summary>
/// <remarks>Multiple active operations in the same session instance are not supported. Use
/// <see langword="await"/> to ensure that all asynchronous operations have completed before calling
/// another method in this session.</remarks>
/// <typeparam name="T">Type of the entity.</typeparam>
/// <param name="query">The query.</param>
/// <param name="token">The cancellation token to terminate execution if necessary.</param>
/// <returns>Number of updated entities.</returns>
public static Task<int> UpdateAsync<T>(this IUpdatable<T> query, CancellationToken token = default)
where T : class, IEntity =>
new BulkUpdateOperation<T>(query).ExecuteAsync(token);
/// <summary>
/// Executes INSERT operation.
/// </summary>
/// <typeparam name="T">Type of the entity.</typeparam>
/// <param name="queryEndpoint">The query endpoint.</param>
/// <param name="evaluator">The expression, tha specify new values.</param>
/// <returns>Key of the created entity.</returns>
public static Key Insert<T>(this QueryEndpoint queryEndpoint, Expression<Func<T>> evaluator) where T : Entity
{
var operation = new InsertOperation<T>(queryEndpoint.Provider, evaluator);
operation.Execute();
return operation.Key;
}
/// <summary>
/// Asynchronously executes INSERT operation.
/// </summary>
/// <remarks>Multiple active operations in the same session instance are not supported. Use
/// <see langword="await"/> to ensure that all asynchronous operations have completed before calling
/// another method in this session.</remarks>
/// <typeparam name="T">Type of the entity.</typeparam>
/// <param name="queryEndpoint">The query endpoint.</param>
/// <param name="evaluator">The expression, tha specify new values.</param>
/// <param name="token">The cancellation token to terminate execution if necessary.</param>
/// <returns>Key of the created entity.</returns>
public static async Task<Key> InsertAsync<T>(this QueryEndpoint queryEndpoint, Expression<Func<T>> evaluator,
CancellationToken token = default) where T : Entity
{
var operation = new InsertOperation<T>(queryEndpoint.Provider, evaluator);
await operation.ExecuteAsync(token).ConfigureAwait(false);
return operation.Key;
}
}
}